Skip to content

Commit 4b1afb6

Browse files
authored
Merge pull request #448 from JuliaArrays/teh/errmsg
Activate helpful error message for undefined-Size conversions
2 parents 5a9c7f7 + 59791a5 commit 4b1afb6

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

src/FixedSizeArrays.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,29 +109,29 @@ macro fixed_vector(name, parent)
109109
size_or(::Type{$(name){S, T} where T}, or) where {S} = Size{(S,)}()
110110
size_or(::Type{$(name){S, T}}, or) where {S, T} = (S,)
111111
# Array constructor
112-
@inline function (::Type{$(name){S}})(x::AbstractVector{T}) where {S, T}
112+
@inline function $(name){S}(x::AbstractVector{T}) where {S, T}
113113
@assert S <= length(x)
114114
$(name){S, T}(ntuple(i-> x[i], Val(S)))
115115
end
116-
@inline function (::Type{$(name){S, T1}})(x::AbstractVector{T2}) where {S, T1, T2}
116+
@inline function $(name){S, T1}(x::AbstractVector{T2}) where {S, T1, T2}
117117
@assert S <= length(x)
118118
$(name){S, T1}(ntuple(i-> T1(x[i]), Val(S)))
119119
end
120120

121-
@inline function (::Type{$(name){S, T}})(x) where {S, T}
121+
@inline function $(name){S, T}(x) where {S, T}
122122
$(name){S, T}(ntuple(i-> T(x), Val(S)))
123123
end
124124

125125

126-
@inline function (::Type{$(name){S}})(x::T) where {S, T}
126+
@inline function $(name){S}(x::T) where {S, T}
127127
$(name){S, T}(ntuple(i-> x, Val(S)))
128128
end
129-
@inline function (::Type{$(name){1, T}})(x::T) where T
129+
@inline function $(name){1, T}(x::T) where T
130130
$(name){1, T}((x,))
131131
end
132-
@inline (::Type{$(name)})(x::NTuple{S}) where {S} = $(name){S}(x)
133-
@inline (::Type{$(name)})(x::T) where {S, T <: Tuple{Vararg{Any, S}}} = $(name){S, StaticArrays.promote_tuple_eltype(T)}(x)
134-
@inline function (::Type{$(name){S}})(x::T) where {S, T <: Tuple}
132+
@inline $(name)(x::NTuple{S}) where {S} = $(name){S}(x)
133+
@inline $(name)(x::T) where {S, T <: Tuple{Vararg{Any, S}}} = $(name){S, StaticArrays.promote_tuple_eltype(T)}(x)
134+
@inline function $(name){S}(x::T) where {S, T <: Tuple}
135135
$(name){S, StaticArrays.promote_tuple_eltype(T)}(x)
136136
end
137137
$(name){S, T}(x::StaticVector) where {S, T} = $(name){S, T}(Tuple(x))

src/SDiagonal.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ diagtype(::Type{SDiagonal}) = SVector
2323
# this is to deal with convert.jl
2424
@inline (::Type{SD})(a::AbstractVector) where {SD <: SDiagonal} = SDiagonal(convert(diagtype(SD), a))
2525
@inline (::Type{SD})(a::Tuple) where {SD <: SDiagonal} = SDiagonal(convert(diagtype(SD), a))
26-
@inline (::Type{SDiagonal})(a::SVector{N,T}) where {N,T} = SDiagonal{N,T}(a)
26+
@inline SDiagonal(a::SVector{N,T}) where {N,T} = SDiagonal{N,T}(a)
2727

2828
@generated function SDiagonal(a::StaticMatrix{N,N,T}) where {N,T}
2929
expr = [:(a[$i,$i]) for i=1:N]

src/SVector.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ compiler (the element type may optionally also be specified).
1515
"""
1616
const SVector{S, T} = SArray{Tuple{S}, T, 1, S}
1717

18-
@inline (::Type{SVector})(x::NTuple{S,Any}) where {S} = SVector{S}(x)
19-
@inline (::Type{SVector{S}})(x::NTuple{S,T}) where {S, T} = SVector{S,T}(x)
20-
@inline (::Type{SVector{S}})(x::T) where {S, T <: Tuple} = SVector{S,promote_tuple_eltype(T)}(x)
18+
@inline SVector(x::NTuple{S,Any}) where {S} = SVector{S}(x)
19+
@inline SVector{S}(x::NTuple{S,T}) where {S, T} = SVector{S,T}(x)
20+
@inline SVector{S}(x::T) where {S, T <: Tuple} = SVector{S,promote_tuple_eltype(T)}(x)
2121

2222
# conversion from AbstractVector / AbstractArray (better inference than default)
2323
#@inline convert{S,T}(::Type{SVector{S}}, a::AbstractArray{T}) = SVector{S,T}((a...))

src/SizedArray.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ struct SizedArray{S <: Tuple, T, N, M} <: StaticArray{S, T, N}
2424
end
2525
end
2626

27-
@inline (::Type{SizedArray{S,T,N}})(a::Array{T,M}) where {S,T,N,M} = SizedArray{S,T,N,M}(a)
28-
@inline (::Type{SizedArray{S,T}})(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
29-
@inline (::Type{SizedArray{S}})(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
27+
@inline SizedArray{S,T,N}(a::Array{T,M}) where {S,T,N,M} = SizedArray{S,T,N,M}(a)
28+
@inline SizedArray{S,T}(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
29+
@inline SizedArray{S}(a::Array{T,M}) where {S,T,M} = SizedArray{S,T,tuple_length(S),M}(a)
3030

31-
@inline (::Type{SizedArray{S,T,N}})() where {S,T,N} = SizedArray{S,T,N,N}()
32-
@inline (::Type{SizedArray{S,T}})() where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}()
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)}()
3333

3434
@generated function (::Type{SizedArray{S,T,N,M}})(x::NTuple{L,Any}) where {S,T,N,M,L}
3535
if L != tuple_prod(S)
@@ -44,18 +44,18 @@ end
4444
end
4545
end
4646

47-
@inline (::Type{SizedArray{S,T,N}})(x::Tuple) where {S,T,N} = SizedArray{S,T,N,N}(x)
48-
@inline (::Type{SizedArray{S,T}})(x::Tuple) where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(x)
49-
@inline (::Type{SizedArray{S}})(x::NTuple{L,T}) where {S,T,L} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(x)
47+
@inline SizedArray{S,T,N}(x::Tuple) where {S,T,N} = SizedArray{S,T,N,N}(x)
48+
@inline SizedArray{S,T}(x::Tuple) where {S,T} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(x)
49+
@inline SizedArray{S}(x::NTuple{L,T}) where {S,T,L} = SizedArray{S,T,tuple_length(S),tuple_length(S)}(x)
5050

5151
# Overide some problematic default behaviour
5252
@inline convert(::Type{SA}, sa::SizedArray) where {SA<:SizedArray} = SA(sa.data)
5353
@inline convert(::Type{SA}, sa::SA) where {SA<:SizedArray} = sa
5454

5555
# Back to Array (unfortunately need both convert and construct to overide other methods)
56-
@inline (::Type{Array})(sa::SizedArray) = sa.data
57-
@inline (::Type{Array{T}})(sa::SizedArray{S,T}) where {T,S} = sa.data
58-
@inline (::Type{Array{T,N}})(sa::SizedArray{S,T,N}) where {T,S,N} = sa.data
56+
@inline Array(sa::SizedArray) = sa.data
57+
@inline Array{T}(sa::SizedArray{S,T}) where {T,S} = sa.data
58+
@inline Array{T,N}(sa::SizedArray{S,T,N}) where {T,S,N} = sa.data
5959

6060
@inline convert(::Type{Array}, sa::SizedArray) = sa.data
6161
@inline convert(::Type{Array{T}}, sa::SizedArray{S,T}) where {T,S} = sa.data
@@ -65,12 +65,12 @@ end
6565
@propagate_inbounds setindex!(a::SizedArray, v, i::Int) = setindex!(a.data, v, i)
6666

6767
SizedVector{S,T,M} = SizedArray{Tuple{S},T,1,M}
68-
@inline (::Type{SizedVector{S}})(a::Array{T,M}) where {S,T,M} = SizedArray{Tuple{S},T,1,M}(a)
69-
@inline (::Type{SizedVector{S}})(x::NTuple{L,T}) where {S,T,L} = SizedArray{Tuple{S},T,1,1}(x)
68+
@inline SizedVector{S}(a::Array{T,M}) where {S,T,M} = SizedArray{Tuple{S},T,1,M}(a)
69+
@inline SizedVector{S}(x::NTuple{L,T}) where {S,T,L} = SizedArray{Tuple{S},T,1,1}(x)
7070

7171
SizedMatrix{S1,S2,T,M} = SizedArray{Tuple{S1,S2},T,2,M}
72-
@inline (::Type{SizedMatrix{S1,S2}})(a::Array{T,M}) where {S1,S2,T,M} = SizedArray{Tuple{S1,S2},T,2,M}(a)
73-
@inline (::Type{SizedMatrix{S1,S2}})(x::NTuple{L,T}) where {S1,S2,T,L} = SizedArray{Tuple{S1,S2},T,2,2}(x)
72+
@inline SizedMatrix{S1,S2}(a::Array{T,M}) where {S1,S2,T,M} = SizedArray{Tuple{S1,S2},T,2,M}(a)
73+
@inline SizedMatrix{S1,S2}(x::NTuple{L,T}) where {S1,S2,T,L} = SizedArray{Tuple{S1,S2},T,2,2}(x)
7474

7575
if isdefined(Base, :dataids) # v0.7-
7676
Base.dataids(sa::SizedArray) = Base.dataids(sa.data)

src/traits.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868

6969
Base.show(io::IO, ::Size{S}) where {S} = print(io, "Size", S)
7070

71-
#= There seems to be a subtyping/specialization bug...
71+
Size(a::T) where {T<:AbstractArray} = Size(T)
7272
function Size(::Type{SA}) where {SA <: StaticArray} # A nice, default error message for when S not defined
7373
error("""
7474
The size of type `$SA` is not known.
@@ -82,9 +82,8 @@ function Size(::Type{SA}) where {SA <: StaticArray} # A nice, default error mess
8282
SMatrix(m) # this error
8383
SMatrix{3,3}(m) # correct - size is inferrable
8484
""")
85-
end =#
86-
Size(a::T) where {T<:AbstractArray} = Size(T)
87-
Size(::Type{<:StaticArray{S}}) where {S} = Size(S)
85+
end
86+
Size(::Type{SA}) where {SA <: StaticArray{S}} where {S<:Tuple} = Size(S) # S defined as a Tuple
8887
@pure Size(::Type{<:AbstractArray{<:Any, N}}) where {N} = Size(ntuple(_ -> Dynamic(), N))
8988

9089
struct Length{L}

test/core.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@
133133
@test @inferred(convert(Array, ma)) == a
134134
@test @inferred(convert(Array{Int}, ma)) == a
135135
@test @inferred(convert(Array{Int,2}, ma)) == a
136+
137+
try
138+
convert(SVector, [1,2,3])
139+
catch err
140+
@test isa(err, ErrorException)
141+
@test startswith(err.msg, "The size of type `StaticArrays.SArray{Tuple{S},T,1,S} where T where S` is not known.")
142+
end
136143
end
137144
@test_throws Exception Length{2.5}()
138145
@test Length(2) == Length{2}()

test/fixed_size_arrays.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ const binaryOps = (
419419
@test r[j] == op(v1[j], v2[j])
420420
end
421421
end
422+
catch
422423
end
423424
end
424425
end
@@ -436,6 +437,7 @@ const binaryOps = (
436437
@test v[i] == op(t[i])
437438
end
438439
end
440+
catch
439441
end
440442
end
441443
end

0 commit comments

Comments
 (0)