Skip to content

Commit 6b42c7a

Browse files
authored
Fix core lowering hook for testing purposes (#97)
Also fix a small bug in `_eval` when file is `nothing`
1 parent 8142feb commit 6b42c7a

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/eval.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,9 @@ function _eval(mod, iter)
475475
break
476476
elseif type == :begin_module
477477
push!(modules, mod)
478+
filename = something(thunk[4].file, :none)
478479
mod = @ccall jl_begin_new_module(mod::Any, thunk[2]::Symbol, thunk[3]::Cint,
479-
thunk[4].file::Cstring, thunk[4].line::Cint)::Module
480+
filename::Cstring, thunk[4].line::Cint)::Module
480481
new_mod = mod
481482
elseif type == :end_module
482483
@ccall jl_end_new_module(mod::Module)::Cvoid

src/hooks.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ function core_lowering_hook(@nospecialize(code), mod::Module,
1818
local st0 = nothing
1919
try
2020
st0 = code isa Expr ? expr_to_syntaxtree(code, LineNumberNode(line, file)) : code
21+
if kind(st0) in KSet"toplevel module"
22+
return Core.svec(code)
23+
elseif kind(st0) === K"doc" && numchildren(st0) >= 2 && kind(st0[2]) === K"module"
24+
# TODO: this ignores module docstrings for now
25+
return Core.svec(Expr(st0[2]))
26+
end
2127
ctx1, st1 = expand_forms_1( mod, st0, true, world)
2228
ctx2, st2 = expand_forms_2( ctx1, st1)
2329
ctx3, st3 = resolve_scopes( ctx2, st2)

test/hooks.jl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,38 @@ const JL = JuliaLowering
4646
@test isdefined(test_mod.M, :x)
4747

4848
# Tricky cases with symbols
49-
out = jeval("""module M
49+
out = jeval("""module M2
5050
Base.@constprop :aggressive function f(x); x; end
5151
const what = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), Core.nothing)
5252
end""")
5353
@test out isa Module
54-
@test isdefined(test_mod, :M)
55-
@test isdefined(test_mod.M, :f)
56-
@test isdefined(test_mod.M, :what)
54+
@test isdefined(test_mod, :M2)
55+
@test isdefined(test_mod.M2, :f)
56+
@test isdefined(test_mod.M2, :what)
57+
58+
out = jeval(""" "docstring" module M3 end """)
59+
@test out isa Module
60+
@test isdefined(test_mod, :M3)
61+
62+
# Macros may produce toplevel expressions. Note that julia handles
63+
# this case badly (macro expansion replaces M5_inner with a
64+
# globalref) and we handle esc(:M5_inner) badly
65+
out = jeval("""module M5
66+
macro newmod()
67+
return quote
68+
let a = 1
69+
$(Expr(:toplevel,
70+
Expr(:module, true, :M5_inner,
71+
Expr(:block, :(global asdf = 1)))))
72+
end
73+
end
74+
end
75+
@newmod()
76+
end""")
77+
@test out isa Module
78+
@test isdefined(test_mod, :M5)
79+
@test isdefined(test_mod.M5, :M5_inner)
80+
@test isdefined(test_mod.M5.M5_inner, :asdf)
5781

5882
# TODO: broken, commented to prevent error logging
5983
# @test jeval("Base.@propagate_inbounds @inline meta_double_quote_issue(x) = x") isa Function

0 commit comments

Comments
 (0)