Skip to content

Commit 38afa5d

Browse files
authored
fix #37540, interpolation in quoted exprs returned from macros (#37573)
1 parent 8407c59 commit 38afa5d

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ New language features
3232
Language changes
3333
----------------
3434

35+
* Macros that return `:quote` expressions (e.g. via `Expr(:quote, ...)`) were previously
36+
able to work without escaping (`esc(...)`) their output when needed. This has been
37+
corrected, and now `esc` must be used in these macros as it is in other macros ([#37540]).
3538
* The `-->` operator now lowers to a `:call` expression, so it can be defined as
3639
a function like other operators. The dotted version `.-->` is now parsed as well.
3740
For backwards compatibility, `-->` still parses using its own expression head

base/essentials.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ Evaluate an expression with values interpolated into it using `eval`.
216216
If two arguments are provided, the first is the module to evaluate in.
217217
"""
218218
macro eval(ex)
219-
:(Core.eval($__module__, $(Expr(:quote,ex))))
219+
return Expr(:escape, Expr(:call, GlobalRef(Core, :eval), __module__, Expr(:quote, ex)))
220220
end
221221
macro eval(mod, ex)
222-
:(Core.eval($(esc(mod)), $(Expr(:quote,ex))))
222+
return Expr(:escape, Expr(:call, GlobalRef(Core, :eval), mod, Expr(:quote, ex)))
223223
end
224224

225225
argtail(x, rest...) = rest

src/ast.c

-6
Original file line numberDiff line numberDiff line change
@@ -1028,12 +1028,6 @@ static jl_value_t *jl_expand_macros(jl_value_t *expr, jl_module_t *inmodule, str
10281028
if (e->head == quote_sym && jl_expr_nargs(e) == 1) {
10291029
expr = jl_call_scm_on_ast("julia-bq-macro", jl_exprarg(e, 0), inmodule);
10301030
JL_GC_PUSH1(&expr);
1031-
if (macroctx) {
1032-
// in a macro, `quote` also implies `escape`
1033-
jl_expr_t *e2 = jl_exprn(escape_sym, 1);
1034-
jl_array_ptr_set(e2->args, 0, expr);
1035-
expr = (jl_value_t*)e2;
1036-
}
10371031
expr = jl_expand_macros(expr, inmodule, macroctx, onelevel, world);
10381032
JL_GC_POP();
10391033
return expr;

stdlib/Distributed/src/macros.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ macro everywhere(procs, ex)
202202
imps = extract_imports(ex)
203203
return quote
204204
$(isempty(imps) ? nothing : Expr(:toplevel, imps...)) # run imports locally first
205-
let ex = Expr(:toplevel, :(task_local_storage()[:SOURCE_PATH] = $(get(task_local_storage(), :SOURCE_PATH, nothing))), $(Expr(:quote, ex))),
205+
let ex = Expr(:toplevel, :(task_local_storage()[:SOURCE_PATH] = $(get(task_local_storage(), :SOURCE_PATH, nothing))), $(esc(Expr(:quote, ex)))),
206206
procs = $(esc(procs))
207207
remotecall_eval(Main, procs, ex)
208208
end

test/syntax.jl

+9
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,15 @@ end
23652365
# issue #37656
23662366
@test :(if true 'a' else 1 end) == Expr(:if, true, quote 'a' end, quote 1 end)
23672367

2368+
# issue #37540
2369+
macro m37540()
2370+
quote
2371+
x = 1
2372+
:($x)
2373+
end
2374+
end
2375+
@test @m37540() == 1
2376+
23682377
# issue #37890
23692378
struct A37890{A, B}
23702379
a

0 commit comments

Comments
 (0)