From fbc1612d48ff9d4bc3fa723efcf9fc4d3ee81ede Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Tue, 15 Jun 2021 09:00:56 +0200 Subject: [PATCH] Fix Rational{T} constructor for abstract T (#41229) (cherry picked from commit e34c77b41d75f5ad7b321de2f876e3a1b3594fc7) --- base/rational.jl | 7 ++++--- test/rational.jl | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 12b5113e8e475..fd1c9d3ac7f79 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -18,21 +18,22 @@ unsafe_rational(num::T, den::T) where {T<:Integer} = unsafe_rational(T, num, den unsafe_rational(num::Integer, den::Integer) = unsafe_rational(promote(num, den)...) @noinline __throw_rational_argerror_typemin(T) = throw(ArgumentError("invalid rational: denominator can't be typemin($T)")) -function checked_den(num::T, den::T) where T<:Integer +function checked_den(::Type{T}, num::T, den::T) where T<:Integer if signbit(den) den = -den - signbit(den) && __throw_rational_argerror_typemin(T) + signbit(den) && __throw_rational_argerror_typemin(typeof(den)) num = -num end return unsafe_rational(T, num, den) end +checked_den(num::T, den::T) where T<:Integer = checked_den(T, num, den) checked_den(num::Integer, den::Integer) = checked_den(promote(num, den)...) @noinline __throw_rational_argerror_zero(T) = throw(ArgumentError("invalid rational: zero($T)//zero($T)")) function Rational{T}(num::Integer, den::Integer) where T<:Integer iszero(den) && iszero(num) && __throw_rational_argerror_zero(T) num, den = divgcd(num, den) - return checked_den(T(num), T(den)) + return checked_den(T, T(num), T(den)) end Rational(n::T, d::T) where {T<:Integer} = Rational{T}(n, d) diff --git a/test/rational.jl b/test/rational.jl index d79725baffa7b..0184060bcce87 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -607,3 +607,7 @@ end @testset "checked_den with different integer types" begin @test Base.checked_den(Int8(4), Int32(8)) == Base.checked_den(Int32(4), Int32(8)) end + +@testset "Rational{T} with non-concrete T (issue #41222)" begin + @test @inferred(Rational{Integer}(2,3)) isa Rational{Integer} +end