Skip to content

Commit 2a76fa3

Browse files
committed
Pass correct Module object to GeneratedFunctionStub
Currently it looks like we are wrongly passing unintentional `mod::typeof(Base.mod)` object. Adds few type annotations to avoid similar regressions in the future.
1 parent fe491ec commit 2a76fa3

File tree

5 files changed

+14
-20
lines changed

5 files changed

+14
-20
lines changed

JuliaLowering/src/eval.jl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ end
120120

121121
# Convert SyntaxTree to the CodeInfo+Expr data stuctures understood by the
122122
# Julia runtime
123-
function to_code_info(ex, mod, funcname, slots)
124-
input_code = children(ex)
123+
function to_code_info(ex::SyntaxTree, mod::Module, slots::Vector{Slot})
125124
stmts = Any[]
126125

127126
current_codelocs_stack = ir_debug_info_state(ex)
@@ -223,11 +222,11 @@ function to_code_info(ex, mod, funcname, slots)
223222
)
224223
end
225224

226-
@fzone "JL: to_lowered_expr" function to_lowered_expr(mod, ex)
225+
@fzone "JL: to_lowered_expr" function to_lowered_expr(mod::Module, ex::SyntaxTree)
227226
_to_lowered_expr(mod, ex, 0)
228227
end
229228

230-
function _to_lowered_expr(mod, ex, stmt_offset)
229+
function _to_lowered_expr(mod::Module, ex::SyntaxTree, stmt_offset::Int)
231230
k = kind(ex)
232231
if is_literal(k)
233232
ex.value
@@ -268,10 +267,7 @@ function _to_lowered_expr(mod, ex, stmt_offset)
268267
e1 = ex[1]
269268
getmeta(ex, :as_Expr, false) ? QuoteNode(Expr(e1)) : e1
270269
elseif k == K"code_info"
271-
funcname = ex.is_toplevel_thunk ?
272-
"top-level scope" :
273-
"none" # FIXME
274-
ir = to_code_info(ex[1], mod, funcname, ex.slots)
270+
ir = to_code_info(ex[1], mod, ex.slots)
275271
if ex.is_toplevel_thunk
276272
Expr(:thunk, ir)
277273
else
@@ -400,4 +396,3 @@ function include_string(mod::Module, code::AbstractString, filename::AbstractStr
400396
expr_compat_mode=false)
401397
eval(mod, parseall(SyntaxTree, code; filename=filename); expr_compat_mode)
402398
end
403-

JuliaLowering/src/runtime.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,13 @@ function (g::GeneratedFunctionStub)(world::UInt, source::Method, @nospecialize a
352352
is_toplevel_thunk=Bool
353353
)
354354

355+
__module__ = source.module
356+
355357
# Macro expansion. Looking at Core.GeneratedFunctionStub, it seems that
356358
# macros emitted by the generator are currently expanded in the latest
357359
# world, so do that for compatibility.
358360
macro_world = typemax(UInt)
359-
ctx1 = MacroExpansionContext(graph, source.module, false, macro_world)
361+
ctx1 = MacroExpansionContext(graph, __module__, false, macro_world)
360362

361363
# Run code generator - this acts like a macro expander and like a macro
362364
# expander it gets a MacroContext.
@@ -397,7 +399,7 @@ function (g::GeneratedFunctionStub)(world::UInt, source::Method, @nospecialize a
397399
# Rest of lowering
398400
ctx4, ex4 = convert_closures(ctx3, ex3)
399401
ctx5, ex5 = linearize_ir(ctx4, ex4)
400-
ci = to_lowered_expr(mod, ex5)
402+
ci = to_lowered_expr(__module__, ex5)
401403
@assert ci isa Core.CodeInfo
402404

403405
# See GeneratedFunctionStub code in base/expr.jl

JuliaLowering/test/demo.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function formatsrc(ex; kws...)
3232
Text(JuliaSyntaxFormatter.formatsrc(ex; kws...))
3333
end
3434

35-
function debug_lower(mod, ex; expr_compat_mode=false, verbose=false, do_eval=false)
35+
function debug_lower(mod::Module, ex::SyntaxTree; expr_compat_mode::Bool=false, verbose::Bool=false, do_eval::Bool=false)
3636
ctx1, ex_macroexpand = JuliaLowering.expand_forms_1(mod, ex, expr_compat_mode, Base.get_world_counter())
3737

3838
verbose && @info "Macro expanded" formatsrc(ex_macroexpand, color_by=:scope_layer)
@@ -502,7 +502,7 @@ end
502502

503503
src = """
504504
function f(y)
505-
x =
505+
x =
506506
try
507507
try
508508
error("hi")
@@ -908,4 +908,3 @@ ex = parsestmt(SyntaxTree, src, filename="foo.jl")
908908
# for e in Meta.parseall(text).args
909909
# Meta.lower(JuliaLowering, e)
910910
# end
911-

JuliaLowering/test/repl_mode.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function is_incomplete(prompt_state)
1919
end
2020
end
2121

22-
function eval_ish(mod, ex, do_eval, do_print_ir)
22+
function eval_ish(mod::Module, ex::SyntaxTree, do_eval::Bool, do_print_ir::Bool)
2323
k = kind(ex)
2424
if k == K"toplevel"
2525
x = nothing
@@ -79,4 +79,3 @@ function __init__()
7979
end
8080

8181
end
82-

JuliaLowering/test/utils.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function watch_ir_tests(dir, delay=0.5)
262262
end
263263
end
264264

265-
function lower_str(mod, s)
265+
function lower_str(mod::Module, s::AbstractString)
266266
ex = parsestmt(JuliaLowering.SyntaxTree, s)
267267
return JuliaLowering.to_lowered_expr(mod, JuliaLowering.lower(mod, ex))
268268
end
@@ -356,7 +356,7 @@ end
356356
# Parse a file and lower the top level expression one child at a time, finding
357357
# any top level statement that fails lowering and producing a partially reduced
358358
# test case.
359-
function reduce_any_failing_toplevel(mod, filename; do_eval=false)
359+
function reduce_any_failing_toplevel(mod::Module, filename::AbstractString; do_eval::Bool=false)
360360
text = read(filename, String)
361361
ex0 = parseall(SyntaxTree, text; filename)
362362
for ex in children(ex0)
@@ -373,7 +373,7 @@ function reduce_any_failing_toplevel(mod, filename; do_eval=false)
373373
end
374374
(reduced,was_reduced) = block_reduction(e->throws_lowering_exc(mod,e), ex)
375375
if !was_reduced
376-
@info "No reduction possible"
376+
@info "No reduction possible"
377377
return ex
378378
else
379379
@info "Reduced code" reduced
@@ -383,4 +383,3 @@ function reduce_any_failing_toplevel(mod, filename; do_eval=false)
383383
end
384384
nothing
385385
end
386-

0 commit comments

Comments
 (0)