Skip to content

Commit

Permalink
InteractiveUtils: escape macro keywords (#41040)
Browse files Browse the repository at this point in the history
Previously `@code_typed` and its family can't accept variables as
keyword options since they're not escaped:
```julia
julia> let
           opt = false
           @code_typed optimize=opt sum(1:10)
       end
ERROR: UndefVarError: opt not defined
Stacktrace:
 [1] macro expansion
   @ ~/julia/julia/usr/share/julia/stdlib/v1.7/InteractiveUtils/src/macros.jl:222 [inlined]
 [2] top-level scope
   @ REPL[1]:3
```
  • Loading branch information
aviatesk authored Jun 1, 2021
1 parent 27aaadb commit d1c2e09
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stdlib/InteractiveUtils/src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function gen_call_with_extracted_types_and_kwargs(__module__, fcn, ex0)
if length(x.args) != 2
return Expr(:call, :error, "Invalid keyword argument: $x")
end
push!(kws, Expr(:kw, x.args[1], x.args[2]))
push!(kws, Expr(:kw, esc(x.args[1]), esc(x.args[2])))
else
return Expr(:call, :error, "@$fcn expects only one non-keyword argument")
end
Expand Down
6 changes: 6 additions & 0 deletions stdlib/InteractiveUtils/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,9 @@ end
ms = methodswith(A41010, @__MODULE__) |> collect
@test ms[1].name == :B41010
end

# macro options should accept both literals and variables
let
opt = false
@test !(first(@code_typed optimize=opt sum(1:10)).inferred)
end

0 comments on commit d1c2e09

Please sign in to comment.