From 3566a9dd478f5cb6cedffbdc7628a4e976469b43 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Thu, 7 Mar 2019 16:54:40 +0100 Subject: [PATCH] only do the getfield optimization when it is safe --- src/JuliaInterpreter.jl | 6 ++++-- test/interpret.jl | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/JuliaInterpreter.jl b/src/JuliaInterpreter.jl index 74aca353417b1..4f946969e4079 100644 --- a/src/JuliaInterpreter.jl +++ b/src/JuliaInterpreter.jl @@ -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 diff --git a/test/interpret.jl b/test/interpret.jl index f2111578586af..02f5aeb2e184e 100644 --- a/test/interpret.jl +++ b/test/interpret.jl @@ -357,3 +357,21 @@ f113(;x) = x @test length(locals) == 3 @test JuliaInterpreter.Variable(3, :x, false) in locals end + +@testset "getfield replacements" begin + f_gf(x) = false ? some_undef_var_zzzzzzz : x + @test @interpret f_gf(2) == 2 + + function g_gf() + eval(:(z = 2)) + return z + end + @test @interpret g_gf() == 2 + + global q_gf = 0 + function h_gf() + eval(:(q_gf = 2)) + return q_gf + end + @test @interpret h_gf() == 2 +end