Skip to content

Commit

Permalink
add some needed local declarations to macros (JuliaLang#37219)
Browse files Browse the repository at this point in the history
to avoid leaking globals
  • Loading branch information
JeffBezanson authored and simeonschaub committed Aug 29, 2020
1 parent b2c010b commit 0b5556c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ function ccall_macro_lower(convention, func, rettype, types, args, nreq)
sym = Symbol(string("arg", i, "root"))
sym2 = Symbol(string("arg", i, ))
earg, etype = esc(arg), esc(type)
push!(lowering, :($sym = Base.cconvert($etype, $earg)))
push!(lowering, :($sym2 = Base.unsafe_convert($etype, $sym)))
push!(lowering, :(local $sym = $(GlobalRef(Base, :cconvert))($etype, $earg)))
push!(lowering, :(local $sym2 = $(GlobalRef(Base, :unsafe_convert))($etype, $sym)))
push!(realargs, sym2)
push!(gcroots, sym)
end
Expand Down
2 changes: 2 additions & 0 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ default_group(file) = Symbol(splitext(basename(file))[1])
function logmsg_code(_module, file, line, level, message, exs...)
log_data = process_logmsg_exs(_module, file, line, level, message, exs...)
quote
let
level = $level
std_level = convert(LogLevel, level)
if std_level >= getindex(_min_enabled_level)
Expand Down Expand Up @@ -305,6 +306,7 @@ function logmsg_code(_module, file, line, level, message, exs...)
end
nothing
end
end
end

function process_logmsg_exs(_orig_module, _file, _line, level, message, exs...)
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ macro show(exs...)
blk = Expr(:block)
for ex in exs
push!(blk.args, :(println($(sprint(show_unquoted,ex)*" = "),
repr(begin value=$(esc(ex)) end))))
repr(begin local value = $(esc(ex)) end))))
end
isempty(exs) || push!(blk.args, :value)
return blk
Expand Down
4 changes: 2 additions & 2 deletions stdlib/InteractiveUtils/src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ end
macro code_typed(ex0...)
thecall = gen_call_with_extracted_types_and_kwargs(__module__, :code_typed, ex0)
quote
results = $thecall
local results = $thecall
length(results) == 1 ? results[1] : results
end
end

macro code_lowered(ex0...)
thecall = gen_call_with_extracted_types_and_kwargs(__module__, :code_lowered, ex0)
quote
results = $thecall
local results = $thecall
length(results) == 1 ? results[1] : results
end
end
Expand Down
16 changes: 8 additions & 8 deletions test/ccall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1605,12 +1605,12 @@ end
)::Cstring))...)
@test call == Base.remove_linenums!(
quote
arg1root = Base.cconvert($(Expr(:escape, :Cstring)), $(Expr(:escape, :str)))
arg1 = Base.unsafe_convert($(Expr(:escape, :Cstring)), arg1root)
arg2root = Base.cconvert($(Expr(:escape, :Cint)), $(Expr(:escape, :num1)))
arg2 = Base.unsafe_convert($(Expr(:escape, :Cint)), arg2root)
arg3root = Base.cconvert($(Expr(:escape, :Cint)), $(Expr(:escape, :num2)))
arg3 = Base.unsafe_convert($(Expr(:escape, :Cint)), arg3root)
local arg1root = $(GlobalRef(Base, :cconvert))($(Expr(:escape, :Cstring)), $(Expr(:escape, :str)))
local arg1 = $(GlobalRef(Base, :unsafe_convert))($(Expr(:escape, :Cstring)), arg1root)
local arg2root = $(GlobalRef(Base, :cconvert))($(Expr(:escape, :Cint)), $(Expr(:escape, :num1)))
local arg2 = $(GlobalRef(Base, :unsafe_convert))($(Expr(:escape, :Cint)), arg2root)
local arg3root = $(GlobalRef(Base, :cconvert))($(Expr(:escape, :Cint)), $(Expr(:escape, :num2)))
local arg3 = $(GlobalRef(Base, :unsafe_convert))($(Expr(:escape, :Cint)), arg3root)
$(Expr(:foreigncall,
:($(Expr(:escape, :((:func, libstring))))),
:($(Expr(:escape, :Cstring))),
Expand All @@ -1631,8 +1631,8 @@ end
throw(ArgumentError("interpolated function `$(name)` was not a Ptr{Cvoid}, but $(typeof(func))"))
end
end
arg1root = Base.cconvert($(Expr(:escape, :Cstring)), $(Expr(:escape, "bar")))
arg1 = Base.unsafe_convert($(Expr(:escape, :Cstring)), arg1root)
local arg1root = $(GlobalRef(Base, :cconvert))($(Expr(:escape, :Cstring)), $(Expr(:escape, "bar")))
local arg1 = $(GlobalRef(Base, :unsafe_convert))($(Expr(:escape, :Cstring)), arg1root)
$(Expr(:foreigncall, :func, :($(Expr(:escape, :Cvoid))), :($(Expr(:escape, :(($(Expr(:core, :svec)))(Cstring))))), 0, :(:ccall), :arg1, :arg1root))
end)

Expand Down

0 comments on commit 0b5556c

Please sign in to comment.