Skip to content
Open
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
4 changes: 2 additions & 2 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ end
Rational(x::Rational) = x

Bool(x::Rational) = x==0 ? false : x==1 ? true :
throw(InexactError(:Bool, Bool, x)) # to resolve ambiguity
throw(InexactError(:convert, Bool, x)) # to resolve ambiguity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can stay as :Bool?

(::Type{T})(x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num)::T :
throw(InexactError(nameof(T), T, x)))
throw(InexactError(:convert, T, x)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could put a sentinel value here like Symbol(""), in which case the printer for InexactError knows to show the type as the called object.


AbstractFloat(x::Rational) = (float(x.num)/float(x.den))::AbstractFloat
function (::Type{T})(x::Rational{S}) where T<:AbstractFloat where S
Expand Down
6 changes: 6 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,12 @@ end
@test_throws InexactError Rational{Integer}(Int8(-1), UInt8(1))
end

struct Issue55756A <: Integer end
struct Issue55756B <: Integer end
@testset "inexact conversion to `Integer`, issue #55756" begin
@test_throws InexactError Union{Issue55756A,Issue55756B}(1//2)
end

@testset "issue #41489" begin
@test Core.Compiler.return_type(+, NTuple{2, Rational}) == Rational
@test Core.Compiler.return_type(-, NTuple{2, Rational}) == Rational
Expand Down