Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generic mul accidental type promotion Int32 -> Int64 #35164

Merged
Merged
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
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