Skip to content

Commit

Permalink
fix infinite recursion in promote_type for Irrational (#55870)
Browse files Browse the repository at this point in the history
Fixes #51001
  • Loading branch information
nsajko authored Oct 18, 2024
1 parent a64ffa3 commit ca3713e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ promote_rule(::Type{<:AbstractIrrational}, ::Type{Float16}) = Float16
promote_rule(::Type{<:AbstractIrrational}, ::Type{Float32}) = Float32
promote_rule(::Type{<:AbstractIrrational}, ::Type{<:AbstractIrrational}) = Float64
promote_rule(::Type{<:AbstractIrrational}, ::Type{T}) where {T<:Real} = promote_type(Float64, T)
promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational,T<:Number} = promote_type(promote_type(S, real(T)), T)

function promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational,T<:Number}
U = promote_type(S, real(T))
if S <: U
# prevent infinite recursion
promote_type(Float64, T)
else
promote_type(U, T)
end
end

AbstractFloat(x::AbstractIrrational) = Float64(x)::Float64
Float16(x::AbstractIrrational) = Float16(Float32(x)::Float32)
Expand Down
8 changes: 8 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,14 @@ end
@test log(π,ComplexF32(2)) isa ComplexF32
end

@testset "irrational promotion shouldn't recurse without bound, issue #51001" begin
for s (, :ℯ)
T = Irrational{s}
@test promote_type(Complex{T}, T) <: Complex
@test promote_type(T, Complex{T}) <: Complex
end
end

@testset "printing non finite floats" begin
let float_types = Set()
allsubtypes!(Base, AbstractFloat, float_types)
Expand Down

0 comments on commit ca3713e

Please sign in to comment.