Skip to content

Commit 8609799

Browse files
authored
Merge pull request #503 from JuliaArrays/ajf/noinference
Fix all the things
2 parents 81dd6ca + 03165ba commit 8609799

File tree

12 files changed

+104
-72
lines changed

12 files changed

+104
-72
lines changed

src/MArray.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ mutable struct MArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N}
3535
new{S,T,N,L}()
3636
end
3737

38-
# deprecated empty constructor
39-
function MArray{S,T,N,L}() where {S,T,N,L}
40-
Base.depwarn("`MArray{S,T,N,L}()` is deprecated, use `MArray{S,T,N,L}(undef)` instead", :MArray)
41-
return MArray{S,T,N,L}(undef)
38+
@static if VERSION < v"1.0"
39+
# deprecated empty constructor
40+
function MArray{S,T,N,L}() where {S,T,N,L}
41+
Base.depwarn("`MArray{S,T,N,L}()` is deprecated, use `MArray{S,T,N,L}(undef)` instead", :MArray)
42+
return MArray{S,T,N,L}(undef)
43+
end
4244
end
4345
end
4446

@@ -59,13 +61,15 @@ end
5961
@generated function (::Type{MArray{S}})(x::T) where {S, T <: Tuple}
6062
return quote
6163
$(Expr(:meta, :inline))
62-
MArray{S,$(promote_tuple_eltype(T)),$(tuple_length(S)),$(tuple_prod(S))}(x)
64+
MArray{S,promote_tuple_eltype(T),$(tuple_length(S)),$(tuple_prod(S))}(x)
6365
end
6466
end
6567

66-
function (::Type{MArray{S,T,N}})() where {S,T,N}
67-
Base.depwarn("`MArray{S,T,N}()` is deprecated, use `MArray{S,T,N}(undef)` instead", :MArray)
68-
return MArray{S,T,N}(undef)
68+
@static if VERSION < v"1.0"
69+
function (::Type{MArray{S,T,N}})() where {S,T,N}
70+
Base.depwarn("`MArray{S,T,N}()` is deprecated, use `MArray{S,T,N}(undef)` instead", :MArray)
71+
return MArray{S,T,N}(undef)
72+
end
6973
end
7074
@generated function (::Type{MArray{S,T,N}})(::UndefInitializer) where {S,T,N}
7175
return quote
@@ -74,9 +78,11 @@ end
7478
end
7579
end
7680

77-
function (::Type{MArray{S,T}})() where {S,T}
78-
Base.depwarn("`MArray{S,T}()` is deprecated, use `MArray{S,T}(undef)` instead", :MArray)
79-
return MArray{S,T}(undef)
81+
@static if VERSION < v"1.0"
82+
function (::Type{MArray{S,T}})() where {S,T}
83+
Base.depwarn("`MArray{S,T}()` is deprecated, use `MArray{S,T}(undef)` instead", :MArray)
84+
return MArray{S,T}(undef)
85+
end
8086
end
8187
@generated function (::Type{MArray{S,T}})(::UndefInitializer) where {S,T}
8288
return quote

src/MMatrix.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@ const MMatrix{S1, S2, T, L} = MArray{Tuple{S1, S2}, T, 2, L}
2222
if S1*S2 != L
2323
throw(DimensionMismatch("Incorrect matrix sizes. $S1 does not divide $L elements"))
2424
end
25-
T = promote_tuple_eltype(x)
26-
2725
return quote
2826
$(Expr(:meta, :inline))
29-
MMatrix{S1, $S2, $T, L}(x)
27+
T = eltype(typeof(x))
28+
MMatrix{S1, $S2, T, L}(x)
3029
end
3130
end
3231

3332
@generated function (::Type{MMatrix{S1,S2}})(x::NTuple{L}) where {S1,S2,L}
34-
T = promote_tuple_eltype(x)
35-
3633
return quote
3734
$(Expr(:meta, :inline))
38-
MMatrix{S1, S2, $T, L}(x)
35+
T = eltype(typeof(x))
36+
MMatrix{S1, S2, T, L}(x)
3937
end
4038
end
4139

@@ -46,9 +44,11 @@ end
4644
end
4745
end
4846

49-
function (::Type{MMatrix{S1,S2,T}})() where {S1,S2,T}
50-
Base.depwarn("`MMatrix{S1,S2,T}()` is deprecated, use `MMatrix{S1,S2,T}(undef)` instead", :MMatrix)
51-
return MMatrix{S1,S2,T}(undef)
47+
@static if VERSION < v"1.0"
48+
function (::Type{MMatrix{S1,S2,T}})() where {S1,S2,T}
49+
Base.depwarn("`MMatrix{S1,S2,T}()` is deprecated, use `MMatrix{S1,S2,T}(undef)` instead", :MMatrix)
50+
return MMatrix{S1,S2,T}(undef)
51+
end
5252
end
5353
@generated function (::Type{MMatrix{S1,S2,T}})(::UndefInitializer) where {S1,S2,T}
5454
return quote

src/SDiagonal.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ end
8484
# SDiagonal(I::UniformScaling) methods to replace eye
8585
(::Type{SD})(I::UniformScaling) where {N,SD<:SDiagonal{N}} = SD(ntuple(x->I.λ, Val(N)))
8686
# deprecate eye, keep around for as long as LinearAlgebra.eye exists
87-
@static if isdefined(LinearAlgebra, :eye)
87+
@static if VERSION < v"1.0"
8888
@deprecate eye(::Type{SDiagonal{N,T}}) where {N,T} SDiagonal{N,T}(I)
8989
end
9090

src/SMatrix.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,27 @@ const SMatrix{S1, S2, T, L} = SArray{Tuple{S1, S2}, T, 2, L}
2121
if S1*S2 != L
2222
throw(DimensionMismatch("Incorrect matrix sizes. $S1 does not divide $L elements"))
2323
end
24-
T = promote_tuple_eltype(x)
2524

2625
return quote
2726
$(Expr(:meta, :inline))
28-
SMatrix{S1, $S2, $T, L}(x)
27+
T = promote_tuple_eltype(typeof(x))
28+
SMatrix{S1, $S2, T, L}(x)
2929
end
3030
end
3131

3232
@generated function (::Type{SMatrix{S1,S2}})(x::NTuple{L,Any}) where {S1,S2,L}
33-
T = promote_tuple_eltype(x)
34-
3533
return quote
3634
$(Expr(:meta, :inline))
37-
SMatrix{S1, S2, $T, L}(x)
35+
T = promote_tuple_eltype(typeof(x))
36+
SMatrix{S1, S2, T, L}(x)
3837
end
3938
end
4039
SMatrixNoType{S1, S2, L, T} = SMatrix{S1, S2, T, L}
4140
@generated function (::Type{SMatrixNoType{S1, S2, L}})(x::NTuple{L,Any}) where {S1,S2,L}
42-
T = promote_tuple_eltype(x)
4341
return quote
4442
$(Expr(:meta, :inline))
45-
SMatrix{S1, S2, $T, L}(x)
43+
T = promote_tuple_eltype(typeof(x))
44+
SMatrix{S1, S2, T, L}(x)
4645
end
4746
end
4847

src/SizedArray.jl

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ struct SizedArray{S <: Tuple, T, N, M} <: StaticArray{S, T, N}
1919
new{S,T,N,M}(a)
2020
end
2121

22-
function SizedArray{S, T, N, M}() where {S, T, N, M}
22+
@static if VERSION < v"1.0"
23+
function SizedArray{S, T, N, M}() where {S, T, N, M}
24+
Base.depwarn("`SizedArray{S,T,N,M}()` is deprecated, use `SizedArray{S,T,N,M}(undef)` instead", :SizedArray)
25+
new{S, T, N, M}(Array{T, M}(undef, S.parameters...))
26+
end
27+
end
28+
29+
function SizedArray{S, T, N, M}(::UndefInitializer) where {S, T, N, M}
2330
new{S, T, N, M}(Array{T, M}(undef, S.parameters...))
2431
end
2532
end
@@ -28,8 +35,19 @@ end
2835
@inline SizedArray{S,T}(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
2936
@inline SizedArray{S}(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
3037

31-
@inline SizedArray{S,T,N}() where {S,T,N} = SizedArray{S,T,N,N}()
32-
@inline SizedArray{S,T}() where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}()
38+
@inline SizedArray{S,T,N}(::UndefInitializer) where {S,T,N} = SizedArray{S,T,N,N}(undef)
39+
@inline SizedArray{S,T}(::UndefInitializer) where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(undef)
40+
41+
@static if VERSION < v"1.0"
42+
@inline function SizedArray{S,T,N}(::UndefInitializer) where {S,T,N}
43+
Base.depwarn("`SizedArray{S,T,N}()` is deprecated, use `SizedArray{S,T,N}(undef)` instead", :SizedArray)
44+
SizedArray{S,T,N,N}(undef)
45+
end
46+
@inline function SizedArray{S,T}(::UndefInitializer) where {S,T}
47+
Base.depwarn("`SizedArray{S,T}()` is deprecated, use `SizedArray{S,T}(undef)` instead", :SizedArray)
48+
SizedArray{S,T,tuple_length(S),tuple_length(S)}(undef)
49+
end
50+
end
3351

3452
@generated function (::Type{SizedArray{S,T,N,M}})(x::NTuple{L,Any}) where {S,T,N,M,L}
3553
if L != tuple_prod(S)
@@ -38,7 +56,7 @@ end
3856
exprs = [:(a[$i] = x[$i]) for i = 1:L]
3957
return quote
4058
$(Expr(:meta, :inline))
41-
a = SizedArray{S,T,N,M}()
59+
a = SizedArray{S,T,N,M}(undef)
4260
@inbounds $(Expr(:block, exprs...))
4361
return a
4462
end

src/abstractarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ similar(::A,::Type{T},s::Size{S}) where {A<:AbstractArray,T,S} = similar(A,T,s)
9595
similar(::Type{A},::Type{T},s::Size{S}) where {A<:AbstractArray,T,S} = mutable_similar_type(T,s,length_val(s))(undef)
9696

9797
# both SizedArray and Array return SizedArray
98-
similar(::Type{SA},::Type{T},s::Size{S}) where {SA<:SizedArray,T,S} = sizedarray_similar_type(T,s,length_val(s))()
99-
similar(::Type{A},::Type{T},s::Size{S}) where {A<:Array,T,S} = sizedarray_similar_type(T,s,length_val(s))()
98+
similar(::Type{SA},::Type{T},s::Size{S}) where {SA<:SizedArray,T,S} = sizedarray_similar_type(T,s,length_val(s))(undef)
99+
similar(::Type{A},::Type{T},s::Size{S}) where {A<:Array,T,S} = sizedarray_similar_type(T,s,length_val(s))(undef)
100100

101101

102102
@inline reshape(a::StaticArray, s::Size) = similar_type(a, s)(Tuple(a))

src/broadcast.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,10 @@ scalar_getindex(x::Tuple{<: Any}) = x[1]
125125
end
126126
end
127127

128-
eltype_exprs = [t <: Union{AbstractArray, Ref} ? :(eltype($t)) : :($t) for t a]
129-
newtype_expr = :(return_type(f, Tuple{$(eltype_exprs...)}))
130-
131128
return quote
132129
@_inline_meta
133-
@inbounds return similar_type($first_staticarray, $newtype_expr, Size(newsize))(tuple($(exprs...)))
130+
@inbounds elements = tuple($(exprs...))
131+
@inbounds return similar_type($first_staticarray, eltype(elements), Size(newsize))(elements)
134132
end
135133
end
136134

src/linalg.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ end
261261
@inline norm(a::StaticArray) = _norm(Size(a), a)
262262
@generated function _norm(::Size{S}, a::StaticArray) where {S}
263263
if prod(S) == 0
264-
return zero(real(eltype(a)))
264+
return :(zero(real(eltype(a))))
265265
end
266266

267267
expr = :(abs2(a[1]))
@@ -280,7 +280,7 @@ _norm_p0(x) = x == 0 ? zero(x) : one(x)
280280
@inline norm(a::StaticArray, p::Real) = _norm(Size(a), a, p)
281281
@generated function _norm(::Size{S}, a::StaticArray, p::Real) where {S}
282282
if prod(S) == 0
283-
return zero(real(eltype(a)))
283+
return :(zero(real(eltype(a))))
284284
end
285285

286286
expr = :(abs(a[1])^p)
@@ -322,7 +322,7 @@ end
322322
end
323323

324324
if S[1] == 0
325-
return zero(eltype(a))
325+
return :(zero(eltype(a)))
326326
end
327327

328328
exprs = [:(a[$(LinearIndices(S)[i, i])]) for i = 1:S[1]]

src/mapreduce.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ end
2323
tmp = [:(a[$j][$i]) for j 1:length(a)]
2424
exprs[i] = :(f($(tmp...)))
2525
end
26-
eltypes = [eltype(a[j]) for j 1:length(a)] # presumably, `eltype` is "hyperpure"?
27-
newT = :(return_type(f, Tuple{$(eltypes...)}))
26+
2827
return quote
2928
@_inline_meta
30-
@inbounds return similar_type(typeof(_first(a...)), $newT, Size(S))(tuple($(exprs...)))
29+
@inbounds elements = tuple($(exprs...))
30+
@inbounds return similar_type(typeof(_first(a...)), eltype(elements), Size(S))(elements)
3131
end
3232
end
3333

@@ -103,9 +103,7 @@ end
103103
::Size{S}, a::StaticArray) where {S,D}
104104
N = length(S)
105105
Snew = ([n==D ? 1 : S[n] for n = 1:N]...,)
106-
T0 = eltype(a)
107-
T = :((T1 = return_type(f, Tuple{$T0}); return_type(op, Tuple{T1,T1})))
108-
106+
109107
exprs = Array{Expr}(undef, Snew)
110108
itr = [1:n for n Snew]
111109
for i Base.product(itr...)
@@ -121,12 +119,13 @@ end
121119

122120
return quote
123121
@_inline_meta
124-
@inbounds return similar_type(a, $T, Size($Snew))(tuple($(exprs...)))
122+
@inbounds elements = tuple($(exprs...))
123+
@inbounds return similar_type(a, eltype(elements), Size($Snew))(elements)
125124
end
126125
end
127126

128-
@generated function _mapreduce(f, op, dims::Val{D}, nt::NamedTuple{(:init,),Tuple{T}},
129-
::Size{S}, a::StaticArray) where {S,D,T}
127+
@generated function _mapreduce(f, op, dims::Val{D}, nt::NamedTuple{(:init,)},
128+
::Size{S}, a::StaticArray) where {S,D}
130129
N = length(S)
131130
Snew = ([n==D ? 1 : S[n] for n = 1:N]...,)
132131

@@ -145,7 +144,8 @@ end
145144

146145
return quote
147146
@_inline_meta
148-
@inbounds return similar_type(a, T, Size($Snew))(tuple($(exprs...)))
147+
@inbounds elements = tuple($(exprs...))
148+
@inbounds return similar_type(a, eltype(elements), Size($Snew))(elements)
149149
end
150150
end
151151

test/SizedArray.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
@test SizedArray{Tuple{2}, Int, 1}((3, 4)).data == [3, 4]
44
@test SizedArray{Tuple{2}, Int, 1}([3, 4]).data == [3, 4]
55
@test SizedArray{Tuple{2, 2}, Int, 2}(collect(3:6)).data == collect(3:6)
6-
@test size(SizedArray{Tuple{4, 5}, Int, 2}().data) == (4, 5)
7-
@test size(SizedArray{Tuple{4, 5}, Int}().data) == (4, 5)
6+
@test size(SizedArray{Tuple{4, 5}, Int, 2}(undef).data) == (4, 5)
7+
@test size(SizedArray{Tuple{4, 5}, Int}(undef).data) == (4, 5)
88

99
# Bad input
1010
@test_throws Exception SArray{Tuple{1},Int,1}([2 3])
1111

1212
# Bad parameters
13-
@test_throws Exception SizedArray{Tuple{1},Int,2}()
14-
@test_throws Exception SArray{Tuple{3, 4},Int,1}()
13+
@test_throws Exception SizedArray{Tuple{1},Int,2}(undef)
14+
@test_throws Exception SArray{Tuple{3, 4},Int,1}(undef)
1515

1616
# Parameter/input size mismatch
1717
@test_throws Exception SizedArray{Tuple{1},Int,2}([2; 3])
@@ -27,8 +27,8 @@
2727
# From Array, reshaped
2828
@test @inferred(SizedArray{Tuple{2,2}}([1,2,3,4]))::SizedArray{Tuple{2,2},Int,2,1} == [1 3; 2 4]
2929
# Uninitialized
30-
@test @inferred(SizedArray{Tuple{2,2},Int,2}()) isa SizedArray{Tuple{2,2},Int,2,2}
31-
@test @inferred(SizedArray{Tuple{2,2},Int}()) isa SizedArray{Tuple{2,2},Int,2,2}
30+
@test @inferred(SizedArray{Tuple{2,2},Int,2}(undef)) isa SizedArray{Tuple{2,2},Int,2,2}
31+
@test @inferred(SizedArray{Tuple{2,2},Int}(undef)) isa SizedArray{Tuple{2,2},Int,2,2}
3232

3333
# From Tuple
3434
@test @inferred(SizedArray{Tuple{2},Float64,1}((1,2)))::SizedArray{Tuple{2},Float64,1,1} == [1.0, 2.0]

0 commit comments

Comments
 (0)