Skip to content

Commit b4f91ad

Browse files
KristofferCtimholy
authored andcommitted
fix for undefined GlobalRefs (#268)
Fixes #267
1 parent 45bb0de commit b4f91ad

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/optimize.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,21 @@ function smallest_ref(stmts, arg, idmin)
9292
return idmin
9393
end
9494

95+
function lookup_global_ref(a::GlobalRef)
96+
if isdefined(a.mod, a.name) && isconst(a.mod, a.name)
97+
r = getfield(a.mod, a.name)
98+
return QuoteNode(r)
99+
else
100+
return a
101+
end
102+
end
103+
95104
function lookup_global_refs!(ex::Expr)
96105
(ex.head == :isdefined || ex.head == :thunk || ex.head == :toplevel) && return nothing
97106
for (i, a) in enumerate(ex.args)
98107
ex.head == :(=) && i == 1 && continue # Don't look up globalrefs on the LHS of an assignment (issue #98)
99108
if isa(a, GlobalRef)
100-
if isdefined(a.mod, a.name) && isconst(a.mod, a.name)
101-
r = getfield(a.mod, a.name)
102-
ex.args[i] = QuoteNode(r)
103-
end
109+
ex.args[i] = lookup_global_ref(a)
104110
elseif isa(a, Expr)
105111
lookup_global_refs!(a)
106112
end
@@ -130,7 +136,7 @@ function optimize!(code::CodeInfo, scope)
130136
## Replace GlobalRefs with QuoteNodes
131137
for (i, stmt) in enumerate(code.code)
132138
if isa(stmt, GlobalRef)
133-
code.code[i] = QuoteNode(getfield(stmt.mod, stmt.name))
139+
code.code[i] = lookup_global_ref(stmt)
134140
elseif isa(stmt, Expr)
135141
if stmt.head == :call && stmt.args[1] == :cglobal # cglobal requires literals
136142
continue

test/interpret.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,17 @@ end
373373
return q_gf
374374
end
375375
@test @interpret h_gf() == 2
376+
377+
# https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/267
378+
function test_never_different(x)
379+
if x < 5
380+
for g in never_defined
381+
print(g)
382+
end
383+
end
384+
end
385+
@test @interpret(test_never_different(10)) === nothing
386+
376387
end
377388

378389
# https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/130

0 commit comments

Comments
 (0)