Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer /: restrict fallback to same-type case (fix #19714) #19779

Merged
merged 1 commit into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Integer /: restrict fallback to same-type case (fix #19714)
The principle here is that if there's an implementation of a promoted
operator for some type and promotions for that type, unless other
methods are defined, the operation should always be "funnelled"
through the core operator definition for that type. The method for
`/(::Integer, ::Integer)` violated that principle since it would take
precedence in mixed-integer-type division cases even in the presence
of a same-type method definition for a custom integer type and the
appropriate promotion rules.
  • Loading branch information
StefanKarpinski committed Jan 2, 2017
commit d62fe5a4bbe5e9f6835c06437ec366fb4684ad8b
5 changes: 5 additions & 0 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ for (fJ, fC) in ((:+, :add), (:-,:sub), (:*, :mul),
end
end

/(x::BigInt, y::BigInt) = float(x)/float(y)

function invmod(x::BigInt, y::BigInt)
z = zero(BigInt)
ya = abs(y)
Expand Down Expand Up @@ -342,6 +344,9 @@ function *(x::BigInt, c::ClongMax)
end
*(c::ClongMax, x::BigInt) = x * c

/(x::BigInt, y::Union{ClongMax,CulongMax}) = float(x)/y
/(x::Union{ClongMax,CulongMax}, y::BigInt) = x/float(y)

# unary ops
for (fJ, fC) in ((:-, :neg), (:~, :com))
@eval begin
Expand Down
4 changes: 3 additions & 1 deletion base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ typealias BitUnsigned64T Union{Type{UInt8},Type{UInt16},Type{UInt32},Type{UInt64
+{T<:BitInteger}(x::T, y::T) = box(T, add_int(unbox(T,x),unbox(T,y)))
*{T<:BitInteger}(x::T, y::T) = box(T, mul_int(unbox(T,x),unbox(T,y)))

/(x::Integer, y::Integer) = float(x)/float(y)
inv(x::Integer) = float(one(x))/float(x)
/{T<:Integer}(x::T, y::T) = float(x)/float(y)
# skip promotion for system integer types
/(x::BitInteger, y::BitInteger) = float(x)/float(y)

"""
isodd(x::Integer) -> Bool
Expand Down
8 changes: 8 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ let xs = [[i:i+4;] for i in 1:10]
@test max.(xs[1:n]...) == [n:n+4;]
end
end

# issue #19714
immutable T19714 <: Integer end
Base.float(::T19714) = 19714.0
Base.:/(::T19714, ::T19714) = T19714()
Base.convert(::Type{T19714}, ::Int) = T19714()
Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714
@test T19714()/1 === 1/T19714() === T19714()