Skip to content

Commit

Permalink
Revert missing to nothings
Browse files Browse the repository at this point in the history
#232 was found to be a huge mistake. For interface checks, you want `nothing` instead of `missing` because you want to handle the cases. The issue with `missing` is that it propagates, `missing + 1 == missing`. We don't want that here, we want clear and precise error messages at the spot where the `nothing` is found if it's not supposed to be there and it's supposed to be handled. That is antithetical to most of its usage here. So this is a strong revert with a breaking update, which should fix downstream libraries (LoopVectorization, OrdinaryDiffEq, etc.) which were never received the proper downstream PRs from the original change anyways.
  • Loading branch information
ChrisRackauckas committed Mar 1, 2022
1 parent 63617dd commit daed820
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ArrayInterface"
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
version = "4.0.4"
version = "5.0.0"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
14 changes: 7 additions & 7 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ julia> ArrayInterface.size(a)
(static(1), 3)

julia> ArrayInterface.known_size(typeof(a))
(1, missing)
(1, nothing)

```

Expand All @@ -62,8 +62,8 @@ Methods should avoid forcing conversion to static sizes when dynamic sizes could
Fore example, `fxn(x) = _fxn(Static.static(ArrayInterface.size(x)), x)` would result in dynamic dispatch if `x` is an instance of `Matrix`.
Additionally, `ArrayInterface.size` should only be used outside of generated functions to avoid possible world age issues.

Generally, `ArrayInterface.size` uses the return of `known_size` to form a static value for those dimensions with known length and only queries dimensions corresponding to `missing`.
For example, the previous example had a known size of `(1, missing)`.
Generally, `ArrayInterface.size` uses the return of `known_size` to form a static value for those dimensions with known length and only queries dimensions corresponding to `nothing`.
For example, the previous example had a known size of `(1, nothing)`.
Therefore, `ArrayInterface.size` would have compile time information about the first dimension returned as `static(1)` and would only look up the size of the second dimension at run time.
This means the above example `ArrayInterface.size(a)` would lower to code similar to this at compile time: `Static.StaticInt(1), Base.arraysize(x, 1)`.
Generic support for `ArrayInterface.known_size` relies on calling `known_length` for each type returned from `axes_types`.
Expand Down Expand Up @@ -108,13 +108,13 @@ ArrayInterface.dimnames(::StaticDimnames{dnames}) where {dnames} = static(dnames
struct DynamicDimnames{N}
dimnames::NTuple{N,Symbol}
end
ArrayInterface.known_dimnames(::Type{DynamicDimnames{N}}) where {N} = ntuple(_-> missing, Val(N))
ArrayInterface.known_dimnames(::Type{DynamicDimnames{N}}) where {N} = ntuple(_-> nothing, Val(N))
ArrayInterface.dimnames(x::DynamicDimnames) = getfield(x, :dimnames)

```

Notice that `DynamicDimnames` returns `missing` instead of a symbol for each dimension.
This indicates dimension names are present for `DynamicDimnames` but that information is missing at compile time.
Notice that `DynamicDimnames` returns `nothing` instead of a symbol for each dimension.
This indicates dimension names are present for `DynamicDimnames` but that information is nothing at compile time.

Dimension names should be appropriately propagated between nested arrays using `ArrayInterface.to_parent_dims`.
This allows types such as `SubArray` and `PermutedDimsArray` to work with named dimensions.
Expand Down Expand Up @@ -166,7 +166,7 @@ using ArrayInterface: axes_types, parent_type, to_dims
for dim in 1:ndims(A)
# offset relative to parent array
O = relative_known_offsets(A, dim)
if O === missing # offset is not known at compile time and is an `Int`
if O === nothing # offset is not known at compile time and is an `Int`
push!(out.args, :(IdOffsetRange{Int, axes_types($P, $(static(dim)))}))
else # offset is known, therefore it is a `StaticInt`
push!(out.args, :(IdOffsetRange{StaticInt{$O}, axes_types($P, $(static(dim))}))
Expand Down
14 changes: 7 additions & 7 deletions src/ArrayInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ buffer(x::SparseMatrixCSC) = getfield(x, :nzval)
buffer(x::SparseVector) = getfield(x, :nzval)

"""
known_length(::Type{T}) -> Union{Int,Missing}
known_length(::Type{T}) -> Union{Int,Nothing}
If `length` of an instance of type `T` is known at compile time, return it.
Otherwise, return `missing`.
Otherwise, return `nothing`.
"""
known_length(x) = known_length(typeof(x))
known_length(::Type{<:NamedTuple{L}}) where {L} = length(L)
Expand All @@ -94,7 +94,7 @@ known_length(::Type{<:Number}) = 1
known_length(::Type{<:AbstractCartesianIndex{N}}) where {N} = N
known_length(::Type{T}) where {T} = _maybe_known_length(Base.IteratorSize(T), T)
_maybe_known_length(::Base.HasShape, ::Type{T}) where {T} = prod(known_size(T))
_maybe_known_length(::Base.IteratorSize, ::Type) = missing
_maybe_known_length(::Base.IteratorSize, ::Type) = nothing
function known_length(::Type{<:Iterators.Flatten{I}}) where {I}
known_length(I) * known_length(eltype(I))
end
Expand Down Expand Up @@ -415,10 +415,10 @@ Indicates the most efficient way to access elements from the collection in low-l
For `GPUArrays`, will return `ArrayInterface.GPU()`.
For `AbstractArray` supporting a `pointer` method, returns `ArrayInterface.CPUPointer()`.
For other `AbstractArray`s and `Tuple`s, returns `ArrayInterface.CPUIndex()`.
Otherwise, returns `missing`.
Otherwise, returns `nothing`.
"""
device(A) = device(typeof(A))
device(::Type) = missing
device(::Type) = nothing
device(::Type{<:Tuple}) = CPUTuple()
device(::Type{T}) where {T<:Array} = CPUPointer()
device(::Type{T}) where {T<:AbstractArray} = _device(has_parent(T), T)
Expand Down Expand Up @@ -597,7 +597,7 @@ end

function Base.length(A::AbstractArray2)
len = known_length(A)
if len === missing
if len === nothing
return Int(prod(size(A)))
else
return Int(len)
Expand Down Expand Up @@ -1136,7 +1136,7 @@ function __init__()
Static.eachop_tuple(_offset_axis_type, Static.nstatic(Val(ndims(T))), ArrayInterface.parent_type(T))
end
function ArrayInterface.known_offsets(::Type{A}) where {A<:OffsetArrays.OffsetArray}
ntuple(identity -> missing, Val(ndims(A)))
ntuple(identity -> nothing, Val(ndims(A)))
end
function ArrayInterface.offsets(A::OffsetArrays.OffsetArray)
map(+, ArrayInterface.offsets(parent(A)), relative_offsets(A))
Expand Down
18 changes: 9 additions & 9 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function axes_types(::Type{T}) where {T<:PermutedDimsArray}
eachop_tuple(field_type, to_parent_dims(T), axes_types(parent_type(T)))
end
function axes_types(::Type{T}) where {T<:AbstractRange}
if known_length(T) === missing
if known_length(T) === nothing
return Tuple{OneTo{Int}}
else
return Tuple{SOneTo{known_length(T)}}
Expand All @@ -62,7 +62,7 @@ end
function _non_reshaped_axis_type(::Type{A}, d::StaticInt{D}) where {A,D}
paxis = axes_types(parent_type(A), d)
if D === 1
if known_length(paxis) === missing
if known_length(paxis) === nothing
return paxis
else
return SOneTo{div(known_length(paxis) * sizeof(eltype(parent_type(A))), sizeof(eltype(A)))}
Expand Down Expand Up @@ -191,7 +191,7 @@ end
# For now we just make sure the linear elements are accurate.
parent_type(::Type{LazyAxis{:,P}}) where {P<:Array} = OneTo{Int}
@inline function parent_type(::Type{LazyAxis{:,P}}) where {P}
if known_length(P) === missing
if known_length(P) === nothing
return OptionallyStaticUnitRange{StaticInt{1},Int}
else
return SOneTo{known_length(P)}
Expand All @@ -208,14 +208,14 @@ known_first(::Type{LazyAxis{N,P}}) where {N,P} = known_offsets(P, static(N))
known_first(::Type{LazyAxis{:,P}}) where {P} = 1
Base.firstindex(x::LazyAxis) = first(x)
@inline function Base.first(x::LazyAxis{N})::Int where {N}
if known_first(x) === missing
if known_first(x) === nothing
return Int(offsets(parent(x), static(N)))
else
return Int(known_first(x))
end
end
@inline function Base.first(x::LazyAxis{:})::Int
if known_first(x) === missing
if known_first(x) === nothing
return first(parent(x))
else
return known_first(x)
Expand All @@ -225,20 +225,20 @@ known_last(::Type{LazyAxis{N,P}}) where {N,P} = known_last(axes_types(P, static(
known_last(::Type{LazyAxis{:,P}}) where {P} = known_length(P)
Base.lastindex(x::LazyAxis) = last(x)
Base.last(x::LazyAxis) = _last(known_last(x), x)
_last(::Missing, x) = last(parent(x))
_last(::Nothing, x) = last(parent(x))
_last(N::Int, x) = N

known_length(::Type{LazyAxis{N,P}}) where {N,P} = known_size(P, static(N))
known_length(::Type{LazyAxis{:,P}}) where {P} = known_length(P)
@inline function Base.length(x::LazyAxis{N})::Int where {N}
if known_length(x) === missing
if known_length(x) === nothing
return size(getfield(x, :parent), static(N))
else
return known_length(x)
end
end
@inline function Base.length(x::LazyAxis{:})::Int
if known_length(x) === missing
if known_length(x) === nothing
return length(parent(x))
else
return known_length(x)
Expand All @@ -254,7 +254,7 @@ Base.axes1(x::Slice{<:LazyAxis}) = indices(parent(x.indices))
Base.to_shape(x::LazyAxis) = length(x)

@inline function Base.checkindex(::Type{Bool}, x::LazyAxis, i::Integer)
if known_first(x) === missing || known_last(x) === missing
if known_first(x) === nothing || known_last(x) === nothing
return checkindex(Bool, parent(x), i)
else # everything is static so we don't have to retrieve the axis
return (!(known_first(x) > i) || !(known_last(x) < i))
Expand Down
6 changes: 3 additions & 3 deletions src/dimensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ _is_named(x::NTuple{N,Symbol}) where {N} = x !== _nunderscore(Val(N))
_is_named(::Any) = true

"""
known_dimnames(::Type{T}) -> Tuple{Vararg{Union{Symbol,Missing}}}
known_dimnames(::Type{T}, dim::Union{Int,StaticInt}) -> Union{Symbol,Missing}
known_dimnames(::Type{T}) -> Tuple{Vararg{Union{Symbol,Nothing}}}
known_dimnames(::Type{T}, dim::Union{Int,StaticInt}) -> Union{Symbol,Nothing}
Return the names of the dimensions for `x`. `:_` is used to indicate a dimension does not
have a name.
Expand Down Expand Up @@ -229,7 +229,7 @@ An error is thrown if any keywords are used which do not occur in `nda`'s names.
1. parse into static dimnension names and key words.
2. find each dimnames in key words
3. if missing is found use Colon()
3. if nothing is found use Colon()
4. if (ndims - ncolon) === nkwargs then all were found, else error
=#
@generated function find_all_dimnames(x::Tuple{Vararg{Any,ND}}, nd::Tuple{Vararg{Any,NI}}, inds::Tuple, default) where {ND,NI}
Expand Down
2 changes: 1 addition & 1 deletion src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ previously executed `to_index(old_axis, arg) -> index`. `to_axis` assumes that
"""
@inline function to_axis(axis, inds)
if !can_change_size(axis) &&
(known_length(inds) !== missing && known_length(axis) === known_length(inds))
(known_length(inds) !== nothing && known_length(axis) === known_length(inds))
return axis
else
return to_axis(IndexStyle(axis), axis, inds)
Expand Down
40 changes: 20 additions & 20 deletions src/ranges.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

_cartesian_index(i::Tuple{Vararg{Int}}) = CartesianIndex(i)
_cartesian_index(::Any) = missing
_cartesian_index(::Any) = nothing

"""
known_first(::Type{T}) -> Union{Int,Missing}
known_first(::Type{T}) -> Union{Int,Nothing}
If `first` of an instance of type `T` is known at compile time, return it.
Otherwise, return `missing`.
Otherwise, return `nothing`.
```julia
julia> ArrayInterface.known_first(typeof(1:4))
missing
nothing
julia> ArrayInterface.known_first(typeof(Base.OneTo(4)))
1
Expand All @@ -19,7 +19,7 @@ julia> ArrayInterface.known_first(typeof(Base.OneTo(4)))
known_first(x) = known_first(typeof(x))
function known_first(::Type{T}) where {T}
if parent_type(T) <: T
return missing
return nothing
else
return known_first(parent_type(T))
end
Expand All @@ -30,14 +30,14 @@ function known_first(::Type{T}) where {N,R,T<:CartesianIndices{N,R}}
end

"""
known_last(::Type{T}) -> Union{Int,Missing}
known_last(::Type{T}) -> Union{Int,Nothing}
If `last` of an instance of type `T` is known at compile time, return it.
Otherwise, return `missing`.
Otherwise, return `nothing`.
```julia
julia> ArrayInterface.known_last(typeof(1:4))
missing
nothing
julia> ArrayInterface.known_first(typeof(static(1):static(4)))
4
Expand All @@ -47,7 +47,7 @@ julia> ArrayInterface.known_first(typeof(static(1):static(4)))
known_last(x) = known_last(typeof(x))
function known_last(::Type{T}) where {T}
if parent_type(T) <: T
return missing
return nothing
else
return known_last(parent_type(T))
end
Expand All @@ -57,14 +57,14 @@ function known_last(::Type{T}) where {N,R,T<:CartesianIndices{N,R}}
end

"""
known_step(::Type{T}) -> Union{Int,Missing}
known_step(::Type{T}) -> Union{Int,Nothing}
If `step` of an instance of type `T` is known at compile time, return it.
Otherwise, return `missing`.
Otherwise, return `nothing`.
```julia
julia> ArrayInterface.known_step(typeof(1:2:8))
missing
nothing
julia> ArrayInterface.known_step(typeof(1:4))
1
Expand All @@ -74,7 +74,7 @@ julia> ArrayInterface.known_step(typeof(1:4))
known_step(x) = known_step(typeof(x))
function known_step(::Type{T}) where {T}
if parent_type(T) <: T
return missing
return nothing
else
return known_step(parent_type(T))
end
Expand Down Expand Up @@ -215,21 +215,21 @@ known_last(::Type{<:OptionallyStaticUnitRange{<:Any,StaticInt{L}}}) where {L} =
known_last(::Type{<:OptionallyStaticStepRange{<:Any,<:Any,StaticInt{L}}}) where {L} = L::Int

@inline function Base.first(r::OptionallyStaticRange)::Int
if known_first(r) === missing
if known_first(r) === nothing
return getfield(r, :start)
else
return known_first(r)
end
end
function Base.step(r::OptionallyStaticStepRange)::Int
if known_step(r) === missing
if known_step(r) === nothing
return getfield(r, :step)
else
return known_step(r)
end
end
@inline function Base.last(r::OptionallyStaticRange)::Int
if known_last(r) === missing
if known_last(r) === nothing
return getfield(r, :stop)
else
return known_last(r)
Expand Down Expand Up @@ -306,9 +306,9 @@ end

@noinline unequal_error(x,y) = @assert false "Unequal Indices: x == $x != $y == y"
@inline check_equal(x, y) = x == y || unequal_error(x,y)
_try_static(::Missing, ::Missing) = missing
_try_static(x::Int, ::Missing) = x
_try_static(::Missing, y::Int) = y
_try_static(::Nothing, ::Nothing) = nothing
_try_static(x::Int, ::Nothing) = x
_try_static(::Nothing, y::Int) = y
@inline _try_static(::StaticInt{N}, ::StaticInt{N}) where {N} = StaticInt{N}()
@inline function _try_static(::StaticInt{M}, ::StaticInt{N}) where {M,N}
@assert false "Unequal Indices: StaticInt{$M}() != StaticInt{$N}()"
Expand All @@ -330,7 +330,7 @@ Base.lastindex(x::OptionallyStaticRange) = length(x)
end
end
Base.length(r::OptionallyStaticStepRange) = _range_length(first(r), step(r), last(r))
_range_length(start, s, stop) = missing
_range_length(start, s, stop) = nothing
@inline function _range_length(start::Int, s::Int, stop::Int)
if s > 0
if stop < start # isempty
Expand Down
10 changes: 5 additions & 5 deletions src/size.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ size(a::Array, dim::Integer) = Base.arraysize(a, convert(Int, dim))
function size(a::A, dim::Integer) where {A}
if parent_type(A) <: A
len = known_size(A, dim)
if len === missing
if len === nothing
return Int(length(axes(a, dim)))
else
return StaticInt(len)
Expand All @@ -77,10 +77,10 @@ size(x::Iterators.Zip) = Static.reduce_tup(promote_shape, map(size, getfield(x,

"""
known_size(::Type{T}) -> Tuple
known_size(::Type{T}, dim) -> Union{Int,Missing}
known_size(::Type{T}, dim) -> Union{Int,Nothing}
Returns the size of each dimension of `A` or along dimension `dim` of `A` that is known at
compile time. If a dimension does not have a known size along a dimension then `missing` is
compile time. If a dimension does not have a known size along a dimension then `nothing` is
returned in its position.
"""
known_size(x) = known_size(typeof(x))
Expand All @@ -98,10 +98,10 @@ end

# 1. `Zip` doesn't check that its collections are compatible (same size) at construction,
# but we assume as much b/c otherwise it will error while iterating. So we promote to the
# known size if matching a `Missing` and `Int` size.
# known size if matching a `Nothing` and `Int` size.
# 2. `promote_shape(::Tuple{Vararg{CanonicalInt}}, ::Tuple{Vararg{CanonicalInt}})` promotes
# trailing dimensions (which must be of size 1), to `static(1)`. We want to stick to
# `Missing` and `Int` types, so we do one last pass to ensure everything is dynamic
# `Nothing` and `Int` types, so we do one last pass to ensure everything is dynamic
@inline function known_size(::Type{<:Iterators.Zip{T}}) where {T}
dynamic(reduce_tup(_promote_shape, eachop(_unzip_size, nstatic(Val(known_length(T))), T)))
end
Expand Down
Loading

0 comments on commit daed820

Please sign in to comment.