Skip to content

Commit

Permalink
Fix generic mul accidental type promotion Int32 -> Int64 (#35164)
Browse files Browse the repository at this point in the history
  • Loading branch information
haampie authored and KristofferC committed Apr 11, 2020
1 parent 1547522 commit c8d277f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/src/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/test/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c8d277f

Please sign in to comment.