Skip to content

Commit 0b52431

Browse files
committed
fix definition of ntuple with a Val argument to not expect inference to be non-terminating
this same issue applied to several other similar functions similarly, need to avoid nesting the use of functional code (like map) too deeply inside the array code to avoid the appearance of indeterminate recursion in inference
1 parent 2d4ac2d commit 0b52431

File tree

8 files changed

+137
-205
lines changed

8 files changed

+137
-205
lines changed

base/abstractarray.jl

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,30 +1160,32 @@ cat_similar(A::AbstractArray, T, shape) = similar(A, T, shape)
11601160

11611161
cat_shape(dims, shape::Tuple) = shape
11621162
@inline cat_shape(dims, shape::Tuple, nshape::Tuple, shapes::Tuple...) =
1163-
cat_shape(dims, _cshp(dims, (), shape, nshape), shapes...)
1164-
1165-
_cshp(::Tuple{}, out, ::Tuple{}, ::Tuple{}) = out
1166-
_cshp(::Tuple{}, out, ::Tuple{}, nshape) = (out..., nshape...)
1167-
_cshp(dims, out, ::Tuple{}, ::Tuple{}) = (out..., map(b -> 1, dims)...)
1168-
@inline _cshp(dims, out, shape, ::Tuple{}) =
1169-
_cshp(tail(dims), (out..., shape[1] + dims[1]), tail(shape), ())
1170-
@inline _cshp(dims, out, ::Tuple{}, nshape) =
1171-
_cshp(tail(dims), (out..., nshape[1]), (), tail(nshape))
1172-
@inline function _cshp(::Tuple{}, out, shape, ::Tuple{})
1173-
_cs(length(out) + 1, false, shape[1], 1)
1174-
_cshp((), (out..., 1), tail(shape), ())
1175-
end
1176-
@inline function _cshp(::Tuple{}, out, shape, nshape)
1177-
next = _cs(length(out) + 1, false, shape[1], nshape[1])
1178-
_cshp((), (out..., next), tail(shape), tail(nshape))
1179-
end
1180-
@inline function _cshp(dims, out, shape, nshape)
1181-
next = _cs(length(out) + 1, dims[1], shape[1], nshape[1])
1182-
_cshp(tail(dims), (out..., next), tail(shape), tail(nshape))
1183-
end
1184-
1185-
_cs(d, concat, a, b) = concat ? (a + b) : (a == b ? a : throw(DimensionMismatch(string(
1186-
"mismatch in dimension ", d, " (expected ", a, " got ", b, ")"))))
1163+
cat_shape(dims, _cshp(1, dims, shape, nshape), shapes...)
1164+
1165+
_cshp(ndim::Int, ::Tuple{}, ::Tuple{}, ::Tuple{}) = ()
1166+
_cshp(ndim::Int, ::Tuple{}, ::Tuple{}, nshape) = nshape
1167+
_cshp(ndim::Int, dims, ::Tuple{}, ::Tuple{}) = ntuple(b -> 1, Val{length(dims)})
1168+
@inline _cshp(ndim::Int, dims, shape, ::Tuple{}) =
1169+
(shape[1] + dims[1], _cshp(ndim + 1, tail(dims), tail(shape), ())...)
1170+
@inline _cshp(ndim::Int, dims, ::Tuple{}, nshape) =
1171+
(nshape[1], _cshp(ndim + 1, tail(dims), (), tail(nshape))...)
1172+
@inline function _cshp(ndim::Int, ::Tuple{}, shape, ::Tuple{})
1173+
_cs(ndim, shape[1], 1)
1174+
(1, _cshp(ndim + 1, (), tail(shape), ())...)
1175+
end
1176+
@inline function _cshp(ndim::Int, ::Tuple{}, shape, nshape)
1177+
next = _cs(ndim, shape[1], nshape[1])
1178+
(next, _cshp(ndim + 1, (), tail(shape), tail(nshape))...)
1179+
end
1180+
@inline function _cshp(ndim::Int, dims, shape, nshape)
1181+
a = shape[1]
1182+
b = nshape[1]
1183+
next = dims[1] ? a + b : _cs(ndim, a, b)
1184+
(next, _cshp(ndim + 1, tail(dims), tail(shape), tail(nshape))...)
1185+
end
1186+
1187+
_cs(d, a, b) = (a == b ? a : throw(DimensionMismatch(
1188+
"mismatch in dimension $d (expected $a got $b)")))
11871189

11881190
dims2cat{n}(::Type{Val{n}}) = ntuple(i -> (i == n), Val{n})
11891191
dims2cat(dims) = ntuple(i -> (i in dims), maximum(dims))

base/array.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ end
7373
size(a::Array, d) = arraysize(a, d)
7474
size(a::Vector) = (arraysize(a,1),)
7575
size(a::Matrix) = (arraysize(a,1), arraysize(a,2))
76-
size(a::Array) = (@_inline_meta; _size((), a))
77-
_size(out::NTuple{N}, A::Array{_,N}) where {_,N} = out
78-
function _size(out::NTuple{M}, A::Array{_,N}) where _ where M where N
79-
@_inline_meta
80-
_size((out..., size(A,M+1)), A)
81-
end
76+
size(a::Array{<:Any,N}) where {N} = (@_inline_meta; ntuple(M -> size(a, M), Val{N}))
8277

8378
asize_from(a::Array, n) = n > ndims(a) ? () : (arraysize(a,n), asize_from(a, n+1)...)
8479

base/broadcast.jl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,22 @@ promote_containertype(::Type{T}, ::Type{T}) where {T} = T
4545
## Calculate the broadcast indices of the arguments, or error if incompatible
4646
# array inputs
4747
broadcast_indices() = ()
48-
broadcast_indices(A) = broadcast_indices(containertype(A), A)
49-
broadcast_indices(::ScalarType, A) = ()
50-
broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),)
51-
broadcast_indices(::Type{Array}, A::Ref) = ()
52-
broadcast_indices(::Type{Array}, A) = indices(A)
53-
@inline broadcast_indices(A, B...) = broadcast_shape((), broadcast_indices(A), map(broadcast_indices, B)...)
48+
broadcast_indices(A) = _broadcast_indices(containertype(A), A)
49+
@inline broadcast_indices(A, B...) = broadcast_shape(broadcast_indices(A), broadcast_indices(B...))
50+
_broadcast_indices(::Type, A) = ()
51+
_broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),)
52+
_broadcast_indices(::Type{Array}, A::Ref) = ()
53+
_broadcast_indices(::Type{Array}, A) = indices(A)
5454

5555
# shape (i.e., tuple-of-indices) inputs
5656
broadcast_shape(shape::Tuple) = shape
57-
@inline broadcast_shape(shape::Tuple, shape1::Tuple, shapes::Tuple...) = broadcast_shape(_bcs((), shape, shape1), shapes...)
57+
@inline broadcast_shape(shape::Tuple, shape1::Tuple, shapes::Tuple...) = broadcast_shape(_bcs(shape, shape1), shapes...)
5858
# _bcs consolidates two shapes into a single output shape
59-
_bcs(out, ::Tuple{}, ::Tuple{}) = out
60-
@inline _bcs(out, ::Tuple{}, newshape) = _bcs((out..., newshape[1]), (), tail(newshape))
61-
@inline _bcs(out, shape, ::Tuple{}) = _bcs((out..., shape[1]), tail(shape), ())
62-
@inline function _bcs(out, shape, newshape)
63-
newout = _bcs1(shape[1], newshape[1])
64-
_bcs((out..., newout), tail(shape), tail(newshape))
59+
_bcs(::Tuple{}, ::Tuple{}) = ()
60+
@inline _bcs(::Tuple{}, newshape::Tuple) = (newshape[1], _bcs((), tail(newshape))...)
61+
@inline _bcs(shape::Tuple, ::Tuple{}) = (shape[1], _bcs(tail(shape), ())...)
62+
@inline function _bcs(shape::Tuple, newshape::Tuple)
63+
return (_bcs1(shape[1], newshape[1]), _bcs(tail(shape), tail(newshape))...)
6564
end
6665
# _bcs1 handles the logic for a single dimension
6766
_bcs1(a::Integer, b::Integer) = a == 1 ? b : (b == 1 ? a : (a == b ? a : throw(DimensionMismatch("arrays could not be broadcast to a common size"))))

base/multidimensional.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ module IteratorsMD
138138
eachindex(::IndexCartesian, A::AbstractArray) = CartesianRange(indices(A))
139139

140140
@inline eachindex(::IndexCartesian, A::AbstractArray, B::AbstractArray...) =
141-
CartesianRange(maxsize((), A, B...))
142-
maxsize(sz) = sz
143-
@inline maxsize(sz, A, B...) = maxsize(maxt(sz, size(A)), B...)
141+
CartesianRange(maxsize(A, B...))
142+
maxsize() = ()
143+
@inline maxsize(A) = size(A)
144+
@inline maxsize(A, B...) = maxt(size(A), maxsize(B...))
144145
@inline maxt(a::Tuple{}, b::Tuple{}) = ()
145146
@inline maxt(a::Tuple{}, b::Tuple) = b
146147
@inline maxt(a::Tuple, b::Tuple{}) = a

0 commit comments

Comments
 (0)