Skip to content

Commit a10b94f

Browse files
nsajkogiordano
authored andcommitted
fix infinite recursion in promote_type for Irrational (#55870)
Fixes #51001 (cherry picked from commit ca3713e)
1 parent 581e65d commit a10b94f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

base/irrationals.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ promote_rule(::Type{<:AbstractIrrational}, ::Type{Float16}) = Float16
4545
promote_rule(::Type{<:AbstractIrrational}, ::Type{Float32}) = Float32
4646
promote_rule(::Type{<:AbstractIrrational}, ::Type{<:AbstractIrrational}) = Float64
4747
promote_rule(::Type{<:AbstractIrrational}, ::Type{T}) where {T<:Real} = promote_type(Float64, T)
48-
promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational,T<:Number} = promote_type(promote_type(S, real(T)), T)
48+
49+
function promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational,T<:Number}
50+
U = promote_type(S, real(T))
51+
if S <: U
52+
# prevent infinite recursion
53+
promote_type(Float64, T)
54+
else
55+
promote_type(U, T)
56+
end
57+
end
4958

5059
AbstractFloat(x::AbstractIrrational) = Float64(x)::Float64
5160
Float16(x::AbstractIrrational) = Float16(Float32(x)::Float32)

test/numbers.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,6 +2897,14 @@ end
28972897
@test log(π,ComplexF32(2)) isa ComplexF32
28982898
end
28992899

2900+
@testset "irrational promotion shouldn't recurse without bound, issue #51001" begin
2901+
for s (, :ℯ)
2902+
T = Irrational{s}
2903+
@test promote_type(Complex{T}, T) <: Complex
2904+
@test promote_type(T, Complex{T}) <: Complex
2905+
end
2906+
end
2907+
29002908
@testset "printing non finite floats" begin
29012909
let float_types = Set()
29022910
allsubtypes!(Base, AbstractFloat, float_types)

0 commit comments

Comments
 (0)