Skip to content

Commit 5179455

Browse files
Merge pull request #26790 from JuliaLang/jb/promotenum
RFC: remove rule requiring Number types to implement specific promotions
2 parents 2c369bf + 619c881 commit 5179455

File tree

4 files changed

+11
-21
lines changed

4 files changed

+11
-21
lines changed

base/int.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ end
745745
for op in (:+, :-, :*, :&, :|, :xor)
746746
@eval function $op(a::Integer, b::Integer)
747747
T = promote_typeof(a, b)
748-
return $op(a % T, b % T)
748+
aT, bT = a % T, b % T
749+
not_sametype((a, b), (aT, bT))
750+
return $op(aT, bT)
749751
end
750752
end

base/promotion.jl

-19
Original file line numberDiff line numberDiff line change
@@ -275,25 +275,6 @@ end
275275

276276
## promotions in arithmetic, etc. ##
277277

278-
# Because of the promoting fallback definitions for Number, we need
279-
# a special case for undefined promote_rule on numeric types.
280-
# Otherwise, typejoin(T,S) is called (returning Number) so no conversion
281-
# happens, and +(promote(x,y)...) is called again, causing a stack
282-
# overflow.
283-
function promote_result(::Type{T},::Type{S},::Type{Bottom},::Type{Bottom}) where {T<:Number,S<:Number}
284-
@_inline_meta
285-
promote_to_supertype(T, S, typejoin(T,S))
286-
end
287-
288-
# promote numeric types T and S to typejoin(T,S) if T<:S or S<:T
289-
# for example this makes promote_type(Integer,Real) == Real without
290-
# promoting arbitrary pairs of numeric types to Number.
291-
promote_to_supertype(::Type{T}, ::Type{T}, ::Type{T}) where {T<:Number} = T
292-
promote_to_supertype(::Type{T}, ::Type{S}, ::Type{T}) where {T<:Number,S<:Number} = T
293-
promote_to_supertype(::Type{T}, ::Type{S}, ::Type{S}) where {T<:Number,S<:Number} = S
294-
promote_to_supertype(::Type{T}, ::Type{S}, ::Type) where {T<:Number,S<:Number} =
295-
error("no promotion exists for ", T, " and ", S)
296-
297278
promote() = ()
298279
promote(x) = (x,)
299280

test/core.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ mutable struct SIQ{A,B} <: Number
17201720
end
17211721
import Base: promote_rule
17221722
promote_rule(A::Type{SIQ{T,T2}},B::Type{SIQ{S,S2}}) where {T,T2,S,S2} = SIQ{promote_type(T,S)}
1723-
@test_throws ErrorException promote_type(SIQ{Int},SIQ{Float64})
1723+
@test promote_type(SIQ{Int},SIQ{Float64}) == SIQ
17241724
f4731(x::T...) where {T} = ""
17251725
f4731(x...) = 0
17261726
g4731() = f4731()

test/int.jl

+7
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,10 @@ end
280280
@test_throws ArgumentError big"1_0_0_0_"
281281
@test_throws ArgumentError big"_1_0_0_0"
282282
end
283+
284+
# issue #26779
285+
struct MyInt26779 <: Integer
286+
x::Int
287+
end
288+
@test promote_type(MyInt26779, Int) == Integer
289+
@test_throws ErrorException MyInt26779(1) + 1

0 commit comments

Comments
 (0)