Skip to content

Commit d929f0b

Browse files
authored
refactor cartesian.jl to use dispatch in macros (#24450)
add a couple helpful type declarations
1 parent ae51693 commit d929f0b

File tree

4 files changed

+18
-50
lines changed

4 files changed

+18
-50
lines changed

base/abstractarray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ similar(a::AbstractArray, ::Type{T}, dims::Dims{N}) where {T,N} = Array{T,N}(
529529

530530
to_shape(::Tuple{}) = ()
531531
to_shape(dims::Dims) = dims
532-
to_shape(dims::DimsOrInds) = map(to_shape, dims)
532+
to_shape(dims::DimsOrInds) = map(to_shape, dims)::DimsOrInds
533533
# each dimension
534534
to_shape(i::Int) = i
535535
to_shape(i::Integer) = Int(i)

base/array.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ else
645645
end
646646

647647
_array_for(::Type{T}, itr, ::HasLength) where {T} = Array{T,1}(Int(length(itr)::Integer))
648-
_array_for(::Type{T}, itr, ::HasShape) where {T} = similar(Array{T}, indices(itr))
648+
_array_for(::Type{T}, itr, ::HasShape) where {T} = similar(Array{T}, indices(itr))::Array{T}
649649

650650
function collect(itr::Generator)
651651
isz = iteratorsize(itr.iter)

base/cartesian.jl

+16-44
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,8 @@ julia> @macroexpand Base.Cartesian.@nref 3 A i
8181
:(A[i_1, i_2, i_3])
8282
```
8383
"""
84-
macro nref(N, A, sym)
85-
_nref(N, A, sym)
86-
end
87-
88-
function _nref(N::Int, A::Symbol, ex)
89-
vars = [ inlineanonymous(ex,i) for i = 1:N ]
84+
macro nref(N::Int, A::Symbol, ex)
85+
vars = Any[ inlineanonymous(ex,i) for i = 1:N ]
9086
Expr(:escape, Expr(:ref, A, vars...))
9187
end
9288

@@ -105,14 +101,10 @@ while `@ncall 2 func a b i->c[i]` yields
105101
func(a, b, c[1], c[2])
106102
107103
"""
108-
macro ncall(N, f, sym...)
109-
_ncall(N, f, sym...)
110-
end
111-
112-
function _ncall(N::Int, f, args...)
104+
macro ncall(N::Int, f, args...)
113105
pre = args[1:end-1]
114106
ex = args[end]
115-
vars = [ inlineanonymous(ex,i) for i = 1:N ]
107+
vars = Any[ inlineanonymous(ex,i) for i = 1:N ]
116108
Expr(:escape, Expr(:call, f, pre..., vars...))
117109
end
118110

@@ -132,12 +124,8 @@ quote
132124
end
133125
```
134126
"""
135-
macro nexprs(N, ex)
136-
_nexprs(N, ex)
137-
end
138-
139-
function _nexprs(N::Int, ex::Expr)
140-
exs = [ inlineanonymous(ex,i) for i = 1:N ]
127+
macro nexprs(N::Int, ex::Expr)
128+
exs = Any[ inlineanonymous(ex,i) for i = 1:N ]
141129
Expr(:escape, Expr(:block, exs...))
142130
end
143131

@@ -159,17 +147,13 @@ while `@nextract 3 x d->y[2d-1]` yields
159147
x_3 = y[5]
160148
161149
"""
162-
macro nextract(N, esym, isym)
163-
_nextract(N, esym, isym)
164-
end
165-
166-
function _nextract(N::Int, esym::Symbol, isym::Symbol)
167-
aexprs = [Expr(:escape, Expr(:(=), inlineanonymous(esym, i), :(($isym)[$i]))) for i = 1:N]
150+
macro nextract(N::Int, esym::Symbol, isym::Symbol)
151+
aexprs = Any[ Expr(:escape, Expr(:(=), inlineanonymous(esym, i), :(($isym)[$i]))) for i = 1:N ]
168152
Expr(:block, aexprs...)
169153
end
170154

171-
function _nextract(N::Int, esym::Symbol, ex::Expr)
172-
aexprs = [Expr(:escape, Expr(:(=), inlineanonymous(esym, i), inlineanonymous(ex,i))) for i = 1:N]
155+
macro nextract(N::Int, esym::Symbol, ex::Expr)
156+
aexprs = Any[ Expr(:escape, Expr(:(=), inlineanonymous(esym, i), inlineanonymous(ex,i))) for i = 1:N ]
173157
Expr(:block, aexprs...)
174158
end
175159

@@ -182,15 +166,11 @@ evaluate to `true`.
182166
`@nall 3 d->(i_d > 1)` would generate the expression `(i_1 > 1 && i_2 > 1 && i_3 > 1)`. This
183167
can be convenient for bounds-checking.
184168
"""
185-
macro nall(N, criterion)
186-
_nall(N, criterion)
187-
end
188-
189-
function _nall(N::Int, criterion::Expr)
169+
macro nall(N::Int, criterion::Expr)
190170
if criterion.head != :->
191171
throw(ArgumentError("second argument must be an anonymous function expression yielding the criterion"))
192172
end
193-
conds = [Expr(:escape, inlineanonymous(criterion, i)) for i = 1:N]
173+
conds = Any[ Expr(:escape, inlineanonymous(criterion, i)) for i = 1:N ]
194174
Expr(:&&, conds...)
195175
end
196176

@@ -202,15 +182,11 @@ evaluate to `true`.
202182
203183
`@nany 3 d->(i_d > 1)` would generate the expression `(i_1 > 1 || i_2 > 1 || i_3 > 1)`.
204184
"""
205-
macro nany(N, criterion)
206-
_nany(N, criterion)
207-
end
208-
209-
function _nany(N::Int, criterion::Expr)
185+
macro nany(N::Int, criterion::Expr)
210186
if criterion.head != :->
211187
error("Second argument must be an anonymous function expression yielding the criterion")
212188
end
213-
conds = [Expr(:escape, inlineanonymous(criterion, i)) for i = 1:N]
189+
conds = Any[ Expr(:escape, inlineanonymous(criterion, i)) for i = 1:N ]
214190
Expr(:||, conds...)
215191
end
216192

@@ -220,12 +196,8 @@ end
220196
Generates an `N`-tuple. `@ntuple 2 i` would generate `(i_1, i_2)`, and `@ntuple 2 k->k+1`
221197
would generate `(2,3)`.
222198
"""
223-
macro ntuple(N, ex)
224-
_ntuple(N, ex)
225-
end
226-
227-
function _ntuple(N::Int, ex)
228-
vars = [ inlineanonymous(ex,i) for i = 1:N ]
199+
macro ntuple(N::Int, ex)
200+
vars = Any[ inlineanonymous(ex,i) for i = 1:N ]
229201
Expr(:escape, Expr(:tuple, vars...))
230202
end
231203

base/precompile.jl

-4
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,6 @@ precompile(Tuple{typeof(Base.length), Tuple{DataType, DataType}})
818818
precompile(Tuple{Type{BoundsError}, Array{Int64, 2}, Tuple{Base.UnitRange{Int64}, Int64}})
819819
precompile(Tuple{typeof(Base.throw_boundserror), Array{Int64, 2}, Tuple{Base.UnitRange{Int64}, Int64}})
820820
precompile(Tuple{getfield(Base.Cartesian, Symbol("#@nexprs")), Int64, Expr})
821-
precompile(Tuple{typeof(Base.Cartesian._nexprs), Int64, Expr})
822821
precompile(Tuple{typeof(Core.Inference.builtin_tfunction), typeof(===), Array{Any, 1}, Core.Inference.InferenceState, Core.Inference.InferenceParams})
823822
precompile(Tuple{typeof(Core.Inference.typeinf_frame), Core.MethodInstance, Bool, Bool, Core.Inference.InferenceParams})
824823
precompile(Tuple{typeof(Core.Inference.typeinf), Core.Inference.InferenceState})
@@ -837,14 +836,11 @@ precompile(Tuple{typeof(Base.Cartesian.lreplace!), String, Base.Cartesian.LRepla
837836
precompile(Tuple{typeof(Base.Cartesian.exprresolve), Expr})
838837
precompile(Tuple{Type{BoundsError}, Array{Expr, 1}, Base.UnitRange{Int64}})
839838
precompile(Tuple{getfield(Base.Cartesian, Symbol("#@ncall")), Int64, Symbol, Symbol})
840-
precompile(Tuple{typeof(Base.Cartesian._ncall), Int64, Symbol, Symbol})
841839
precompile(Tuple{typeof(Base.getindex), Tuple{Symbol}, Base.UnitRange{Int64}})
842840
precompile(Tuple{getfield(Base.Cartesian, Symbol("#@ncall")), Int64, Symbol, Symbol, Expr})
843-
precompile(Tuple{typeof(Base.Cartesian._ncall), Int64, Symbol, Symbol, Expr})
844841
precompile(Tuple{typeof(Base.endof), Tuple{Symbol, Expr}})
845842
precompile(Tuple{typeof(Base.getindex), Tuple{Symbol, Expr}, Base.UnitRange{Int64}})
846843
precompile(Tuple{getfield(Base.Cartesian, Symbol("#@nloops")), Int64, Symbol, Expr, Expr})
847-
precompile(Tuple{typeof(Base.Cartesian._nloops), Int64, Symbol, Expr, Expr})
848844
precompile(Tuple{typeof(Base.endof), Tuple{Expr}})
849845
precompile(Tuple{typeof(Base.endof), Tuple{Symbol, Symbol, Symbol}})
850846
precompile(Tuple{typeof(Base.getindex), Tuple{Symbol, Symbol, Symbol}, Base.UnitRange{Int64}})

0 commit comments

Comments
 (0)