Skip to content

Commit ca3713e

Browse files
authored
fix infinite recursion in promote_type for Irrational (#55870)
Fixes #51001
1 parent a64ffa3 commit ca3713e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

base/irrationals.jl

+10-1
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

+8
Original file line numberDiff line numberDiff line change
@@ -2937,6 +2937,14 @@ end
29372937
@test log(π,ComplexF32(2)) isa ComplexF32
29382938
end
29392939

2940+
@testset "irrational promotion shouldn't recurse without bound, issue #51001" begin
2941+
for s (, :ℯ)
2942+
T = Irrational{s}
2943+
@test promote_type(Complex{T}, T) <: Complex
2944+
@test promote_type(T, Complex{T}) <: Complex
2945+
end
2946+
end
2947+
29402948
@testset "printing non finite floats" begin
29412949
let float_types = Set()
29422950
allsubtypes!(Base, AbstractFloat, float_types)

0 commit comments

Comments
 (0)