Skip to content

Commit d6a97a1

Browse files
authored
Tweak order of operations to get nnz to infer as Int return type (#38302)
If the sparse array does not have a concrete index type, then union splitting occurs over the possible `<:Integer` types permitted by `SparseMatrixCSC`. It appears that union splitting over the subtraction by one includes an `Any` branch that widens the return type of `nnz`. By instead converting the index type to `Int` before subtracting, type inference is able to infer that all paths give an `Int` result.
1 parent b00802b commit d6a97a1

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ julia> nnz(A)
108108
3
109109
```
110110
"""
111-
nnz(S::AbstractSparseMatrixCSC) = Int(getcolptr(S)[size(S, 2) + 1] - 1)
111+
nnz(S::AbstractSparseMatrixCSC) = Int(getcolptr(S)[size(S, 2) + 1]) - 1
112112
nnz(S::ReshapedArray{<:Any,1,<:AbstractSparseMatrixCSC}) = nnz(parent(S))
113113
nnz(S::UpperTriangular{<:Any,<:AbstractSparseMatrixCSC}) = nnz1(S)
114114
nnz(S::LowerTriangular{<:Any,<:AbstractSparseMatrixCSC}) = nnz1(S)

0 commit comments

Comments
 (0)