Skip to content

Commit 9e6b118

Browse files
authored
Merge pull request #120 from JuliaDebug/kc/getfield
only do the getfield optimization when it is safe
2 parents a1b0efd + 3566a9d commit 9e6b118

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/JuliaInterpreter.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,10 @@ function lookup_global_refs!(ex::Expr)
761761
for (i, a) in enumerate(ex.args)
762762
ex.head == :(=) && i == 1 && continue # Don't look up globalrefs on the LHS of an assignment (issue #98)
763763
if isa(a, GlobalRef)
764-
r = getfield(a.mod, a.name)
765-
ex.args[i] = QuoteNode(r)
764+
if isdefined(a.mod, a.name) && isconst(a.mod, a.name)
765+
r = getfield(a.mod, a.name)
766+
ex.args[i] = QuoteNode(r)
767+
end
766768
elseif isa(a, Expr)
767769
lookup_global_refs!(a)
768770
end

test/interpret.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,21 @@ f113(;x) = x
357357
@test length(locals) == 3
358358
@test JuliaInterpreter.Variable(3, :x, false) in locals
359359
end
360+
361+
@testset "getfield replacements" begin
362+
f_gf(x) = false ? some_undef_var_zzzzzzz : x
363+
@test @interpret f_gf(2) == 2
364+
365+
function g_gf()
366+
eval(:(z = 2))
367+
return z
368+
end
369+
@test @interpret g_gf() == 2
370+
371+
global q_gf = 0
372+
function h_gf()
373+
eval(:(q_gf = 2))
374+
return q_gf
375+
end
376+
@test @interpret h_gf() == 2
377+
end

0 commit comments

Comments
 (0)