From c8d277f46e1c6337e7175fe70185d3fe975bfaec Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Tue, 24 Mar 2020 16:33:28 +0100 Subject: [PATCH] Fix generic mul accidental type promotion Int32 -> Int64 (#35164) --- stdlib/LinearAlgebra/src/matmul.jl | 8 ++++---- stdlib/LinearAlgebra/test/matmul.jl | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/stdlib/LinearAlgebra/src/matmul.jl b/stdlib/LinearAlgebra/src/matmul.jl index e5c33388a9af03..66242177f2ea6b 100644 --- a/stdlib/LinearAlgebra/src/matmul.jl +++ b/stdlib/LinearAlgebra/src/matmul.jl @@ -649,7 +649,7 @@ function generic_matvecmul!(C::AbstractVector{R}, tA, A::AbstractVecOrMat, B::Ab for k = 1:mA aoffs = (k-1)*Astride if mB == 0 - s = zero(R) + s = false else s = zero(A[aoffs + 1]*B[1] + A[aoffs + 1]*B[1]) end @@ -662,7 +662,7 @@ function generic_matvecmul!(C::AbstractVector{R}, tA, A::AbstractVecOrMat, B::Ab for k = 1:mA aoffs = (k-1)*Astride if mB == 0 - s = zero(R) + s = false else s = zero(A[aoffs + 1]*B[1] + A[aoffs + 1]*B[1]) end @@ -676,14 +676,14 @@ function generic_matvecmul!(C::AbstractVector{R}, tA, A::AbstractVecOrMat, B::Ab if !iszero(_add.beta) C[i] *= _add.beta elseif mB == 0 - C[i] = zero(R) + C[i] = false else C[i] = zero(A[i]*B[1] + A[i]*B[1]) end end for k = 1:mB aoffs = (k-1)*Astride - b = _add(B[k], 0) + b = _add(B[k], false) for i = 1:mA C[i] += A[aoffs + i] * b end diff --git a/stdlib/LinearAlgebra/test/matmul.jl b/stdlib/LinearAlgebra/test/matmul.jl index 587dc66e4e6a92..a607604e56cc78 100644 --- a/stdlib/LinearAlgebra/test/matmul.jl +++ b/stdlib/LinearAlgebra/test/matmul.jl @@ -394,6 +394,13 @@ end end end +@testset "#35163" begin + # typemax(Int32) * Int32(1) + Int32(1) * Int32(1) should wrap around + # not promote to Int64, convert to Int32 and throw inexacterror + val = mul!(Int32[1], fill(typemax(Int32), 1, 1), Int32[1], Int32(1), Int32(1)) + @test val[1] == typemin(Int32) +end + # Number types that lack conversion to the destination type struct RootInt i::Int