Skip to content

Commit

Permalink
only do the getfield optimization when it is safe
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Mar 7, 2019
1 parent a1b0efd commit 5b8fb74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/JuliaInterpreter.jl
Expand Up @@ -761,8 +761,10 @@ function lookup_global_refs!(ex::Expr)
for (i, a) in enumerate(ex.args)
ex.head == :(=) && i == 1 && continue # Don't look up globalrefs on the LHS of an assignment (issue #98)
if isa(a, GlobalRef)
r = getfield(a.mod, a.name)
ex.args[i] = QuoteNode(r)
if isdefined(a.mod, a.name) && isconst(a.mod, a.name)
r = getfield(a.mod, a.name)
ex.args[i] = QuoteNode(r)
end
elseif isa(a, Expr)
lookup_global_refs!(a)
end
Expand Down
19 changes: 19 additions & 0 deletions test/interpret.jl
Expand Up @@ -357,3 +357,22 @@ f113(;x) = x
@test length(locals) == 3
@test JuliaInterpreter.Variable(3, :x, false) in locals
end

@testset "getfield replacements" begin
f_gf(x) = false ? y : x
@test @interpret f_gf(2) == 2

function g_gf()
eval(:(z = 2))
return z
end
@test @interpret g_gf() == 2
end

# This can not be put in a testset
q_gf = 0
function h_gf()
eval(:(q_gf = 2))
return q_gf
end
@test @interpret h_gf() == 2

0 comments on commit 5b8fb74

Please sign in to comment.