From cd68571afa8bde3fca5fa1277047ce10e31e8eff Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Mon, 14 Jun 2021 15:41:02 -0400 Subject: [PATCH] fix #34288, method errors in nothrow inference of division intrinsics (#41178) --- base/compiler/tfuncs.jl | 9 ++++++--- test/compiler/inference.jl | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index 56fe9d1591848..57b86bf504217 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -1568,6 +1568,10 @@ end # Query whether the given intrinsic is nothrow +_iszero(x) = x === Intrinsics.xor_int(x, x) +_isneg1(x) = _iszero(Intrinsics.not_int(x)) +_istypemin(x) = !_iszero(x) && Intrinsics.neg_int(x) === x + function intrinsic_nothrow(f::IntrinsicFunction, argtypes::Array{Any, 1}) # First check that we have the correct number of arguments iidx = Int(reinterpret(Int32, f::IntrinsicFunction)) + 1 @@ -1594,11 +1598,10 @@ function intrinsic_nothrow(f::IntrinsicFunction, argtypes::Array{Any, 1}) return false end den_val = argtypes[2].val - den_val !== zero(typeof(den_val)) || return false + _iszero(den_val) && return false f !== Intrinsics.checked_sdiv_int && return true # Nothrow as long as we additionally don't do typemin(T)/-1 - return den_val !== -1 || (isa(argtypes[1], Const) && - argtypes[1].val !== typemin(typeof(den_val))) + return !_isneg1(den_val) || (isa(argtypes[1], Const) && !_istypemin(argtypes[1].val)) end if f === Intrinsics.pointerref # Nothrow as long as the types are ok. N.B.: dereferencability is not diff --git a/test/compiler/inference.jl b/test/compiler/inference.jl index 76fd93bb75763..52602a58c517b 100644 --- a/test/compiler/inference.jl +++ b/test/compiler/inference.jl @@ -3319,3 +3319,7 @@ end |> first === Tuple{Int, String} # issue #40804 @test Base.return_types(()) do; ===(); end == Any[Union{}] @test Base.return_types(()) do; typeassert(); end == Any[Union{}] + +primitive type UInt24ish 24 end +f34288(x) = Core.Intrinsics.checked_sdiv_int(x, Core.Intrinsics.trunc_int(UInt24ish, 0)) +@test Base.return_types(f34288, (UInt24ish,)) == Any[UInt24ish]