Skip to content

Commit 0ed8343

Browse files
authored
Pass expr_compat_mode into the new @eval (JuliaLang/JuliaLowering.jl#113)
1 parent 3dff0a0 commit 0ed8343

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

JuliaLowering/src/macro_expansion.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct MacroContext <: AbstractLoweringContext
7575
graph::SyntaxGraph
7676
macrocall::Union{SyntaxTree,LineNumberNode,SourceRef}
7777
scope_layer::ScopeLayer
78+
expr_compat_mode::Bool
7879
end
7980

8081
function adopt_scope(ex, ctx::MacroContext)
@@ -257,7 +258,7 @@ function expand_macro(ctx, ex)
257258
@assert kind(ex) == K"macrocall"
258259

259260
macname = ex[1]
260-
mctx = MacroContext(ctx.graph, ex, current_layer(ctx))
261+
mctx = MacroContext(ctx.graph, ex, current_layer(ctx), ctx.expr_compat_mode)
261262
macfunc = eval_macro_name(ctx, mctx, macname)
262263
raw_args = ex[2:end]
263264
macro_loc = let loc = source_location(LineNumberNode, ex)

JuliaLowering/src/runtime.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ function (g::GeneratedFunctionStub)(world::UInt, source::Method, @nospecialize a
330330

331331
# Run code generator - this acts like a macro expander and like a macro
332332
# expander it gets a MacroContext.
333-
mctx = MacroContext(syntax_graph(ctx1), g.srcref, layer)
333+
mctx = MacroContext(syntax_graph(ctx1), g.srcref, layer, false)
334334
ex0 = g.gen(mctx, args...)
335335
if ex0 isa SyntaxTree
336336
if !is_compatible_graph(ctx1, ex0)

JuliaLowering/src/syntax_macros.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ function _at_eval_code(ctx, srcref, mod, ex)
282282
JuliaLowering.eval::K"Value"
283283
mod
284284
[K"quote" ex]
285+
[K"parameters"
286+
[K"="
287+
"expr_compat_mode"::K"Identifier"
288+
ctx.expr_compat_mode::K"Bool"
289+
]
290+
]
285291
]
286292
]
287293
]

JuliaLowering/test/misc.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,15 @@ end
202202

203203
end
204204

205+
# SyntaxTree @eval should pass along expr_compat_mode
206+
@test JuliaLowering.include_string(test_mod, "@eval quote x end";
207+
expr_compat_mode=false) isa SyntaxTree
208+
@test JuliaLowering.include_string(test_mod, "@eval quote x end";
209+
expr_compat_mode=true) isa Expr
210+
@test JuliaLowering.include_string(test_mod, raw"""
211+
let T = :foo
212+
@eval @doc $"This is a $T" $T = 1
213+
end
214+
"""; expr_compat_mode=true) === 1
215+
205216
end

JuliaLowering/test/misc_ir.jl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -298,26 +298,36 @@ end
298298
# @eval without module
299299
@eval $f(x, y)
300300
#---------------------
301-
1 TestMod.f
302-
2 (call core.tuple %₁)
303-
3 (call JuliaLowering.interpolate_ast SyntaxTree (inert (call ($ f) x y)) %₂)
304-
4 (= slot₁/eval_result (call JuliaLowering.eval TestMod %₃))
305-
5 latestworld
306-
6 slot₁/eval_result
307-
7 (return %₆)
301+
1 JuliaLowering.eval
302+
2 (call core.tuple :expr_compat_mode)
303+
3 (call core.apply_type core.NamedTuple %₂)
304+
4 (call core.tuple false)
305+
5 (call %%₄)
306+
6 TestMod.f
307+
7 (call core.tuple %₆)
308+
8 (call JuliaLowering.interpolate_ast SyntaxTree (inert (call ($ f) x y)) %₇)
309+
9 (= slot₁/eval_result (call core.kwcall %%₁ TestMod %₈))
310+
10 latestworld
311+
11 slot₁/eval_result
312+
12 (return %₁₁)
308313

309314
########################################
310315
# @eval with module
311316
@eval mod $f(x, y)
312317
#---------------------
313-
1 TestMod.mod
314-
2 TestMod.f
315-
3 (call core.tuple %₂)
316-
4 (call JuliaLowering.interpolate_ast SyntaxTree (inert (call ($ f) x y)) %₃)
317-
5 (= slot₁/eval_result (call JuliaLowering.eval %%₄))
318-
6 latestworld
319-
7 slot₁/eval_result
320-
8 (return %₇)
318+
1 JuliaLowering.eval
319+
2 (call core.tuple :expr_compat_mode)
320+
3 (call core.apply_type core.NamedTuple %₂)
321+
4 (call core.tuple false)
322+
5 (call %%₄)
323+
6 TestMod.mod
324+
7 TestMod.f
325+
8 (call core.tuple %₇)
326+
9 (call JuliaLowering.interpolate_ast SyntaxTree (inert (call ($ f) x y)) %₈)
327+
10 (= slot₁/eval_result (call core.kwcall %%%%₉))
328+
11 latestworld
329+
12 slot₁/eval_result
330+
13 (return %₁₂)
321331

322332
########################################
323333
# Juxtaposition

0 commit comments

Comments
 (0)