From 29ab6d22177bca597e5fe1cc582f880b1e0f882d Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Wed, 2 Aug 2023 14:53:44 -0400 Subject: [PATCH] effects: fix `:nothrow` modeling of `getglobal` --- base/compiler/tfuncs.jl | 21 ++++++++++----------- test/compiler/effects.jl | 12 ++++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) 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 c72fb9e1deb05..3e2df80c8d9d1 100644 --- a/test/compiler/effects.jl +++ b/test/compiler/effects.jl @@ -1011,3 +1011,15 @@ end isinf(y) && return zero(y) irinterp_nothrow_override(true, y) end |> Core.Compiler.is_nothrow + +# 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