diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index e431de009affc..0df0ac036ccd5 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -2343,19 +2343,18 @@ function getfield_effects(𝕃::AbstractLattice, arginfo::ArgInfo, @nospecialize end function getglobal_effects(argtypes::Vector{Any}, @nospecialize(rt)) + 2 ≤ length(argtypes) ≤ 3 || return EFFECTS_THROWS consistent = inaccessiblememonly = ALWAYS_FALSE nothrow = false - if length(argtypes) ≥ 2 - M, s = argtypes[1], argtypes[2] - if getglobal_nothrow(M, s) - nothrow = true - # typeasserts below are already checked in `getglobal_nothrow` - Mval, sval = (M::Const).val::Module, (s::Const).val::Symbol - if isconst(Mval, sval) - consistent = ALWAYS_TRUE - if is_mutation_free_argtype(rt) - inaccessiblememonly = ALWAYS_TRUE - end + M, s = argtypes[1], argtypes[2] + if (length(argtypes) == 3 ? getglobal_nothrow(M, s, argtypes[3]) : getglobal_nothrow(M, s)) + nothrow = true + # typeasserts below are already checked in `getglobal_nothrow` + Mval, sval = (M::Const).val::Module, (s::Const).val::Symbol + if isconst(Mval, sval) + consistent = ALWAYS_TRUE + if is_mutation_free_argtype(rt) + inaccessiblememonly = ALWAYS_TRUE end end end diff --git a/test/compiler/effects.jl b/test/compiler/effects.jl index a4b21da523a8e..2cf421073a166 100644 --- a/test/compiler/effects.jl +++ b/test/compiler/effects.jl @@ -998,3 +998,15 @@ f50198() = (hf50198(Ref(:x)[]); nothing) f50311(x, s) = Symbol(s) g50311(x) = Val{f50311((1.0, x), "foo")}() @test fully_eliminated(g50311, Tuple{Float64}) + +# getglobal effects +const my_defined_var = 42 +@test Base.infer_effects() do + getglobal(@__MODULE__, :my_defined_var, :monotonic) +end |> Core.Compiler.is_foldable_nothrow +@test Base.infer_effects() do + getglobal(@__MODULE__, :my_defined_var, :foo) +end |> !Core.Compiler.is_nothrow +@test Base.infer_effects() do + getglobal(@__MODULE__, :my_defined_var, :foo, nothing) +end |> !Core.Compiler.is_nothrow