From d0a909b2a4f1cb6f89987f6e4daeeec892034b63 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 a998787f69685..ab4e7d6d0abbd 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 aa7d48fb52513..24b99cdc8b6d9 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -618,3 +618,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