Skip to content

Commit e48d2f0

Browse files
authored
Replace plain error with DimensionMismatch/ArgumentError (#728)
* replace plain `error` with DimensionMismatch in some places * plain `error` -> `ArgumentError` in some macros * add some @ boundscheck annotations
1 parent 26d0e05 commit e48d2f0

File tree

7 files changed

+17
-18
lines changed

7 files changed

+17
-18
lines changed

src/FieldArray.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,10 @@ array operations as in the example below.
111111
abstract type FieldVector{N, T} <: FieldArray{Tuple{N}, T, 1} end
112112

113113
@inline function (::Type{FA})(x::Tuple{Vararg{Any, N}}) where {N, FA <: FieldArray}
114-
if length(FA) == length(x)
115-
FA(x...)
116-
else
117-
throw(DimensionMismatch("No precise constructor for $FA found. Length of input was $(length(x))."))
118-
end
114+
@boundscheck if length(FA) != length(x)
115+
throw(DimensionMismatch("No precise constructor for $FA found. Length of input was $(length(x))."))
116+
end
117+
return FA(x...)
119118
end
120119

121120
@propagate_inbounds getindex(a::FieldArray, i::Int) = getfield(a, i)

src/MArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ macro MArray(ex)
134134
s2s = map(i -> ((isa(ex.args[i], Expr) && ex.args[i].head == :row) ? length(ex.args[i].args) : 1), 1:s1)
135135
s2 = minimum(s2s)
136136
if maximum(s2s) != s2
137-
error("Rows must be of matching lengths")
137+
throw(ArgumentError("Rows must be of matching lengths"))
138138
end
139139

140140
exprs = [ex.args[i].args[j] for i = 1:s1, j = 1:s2]

src/MMatrix.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ macro MMatrix(ex)
8888
s2s = map(i -> ((isa(ex.args[i], Expr) && ex.args[i].head == :row) ? length(ex.args[i].args) : 1), 1:s1)
8989
s2 = minimum(s2s)
9090
if maximum(s2s) != s2
91-
error("Rows must be of matching lengths")
91+
throw(ArgumentError("Rows must be of matching lengths"))
9292
end
9393

9494
exprs = [ex.args[i].args[j] for i = 1:s1, j = 1:s2]
@@ -103,7 +103,7 @@ macro MMatrix(ex)
103103
s2s = map(i -> ((isa(ex.args[i+1], Expr) && ex.args[i+1].head == :row) ? length(ex.args[i+1].args) : 1), 1:s1)
104104
s2 = minimum(s2s)
105105
if maximum(s2s) != s2
106-
error("Rows must be of matching lengths")
106+
throw(ArgumentError("Rows must be of matching lengths"))
107107
end
108108

109109
exprs = [ex.args[i+1].args[j] for i = 1:s1, j = 1:s2]

src/SArray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ macro SArray(ex)
9393
s2s = map(i -> ((isa(ex.args[i], Expr) && ex.args[i].head == :row) ? length(ex.args[i].args) : 1), 1:s1)
9494
s2 = minimum(s2s)
9595
if maximum(s2s) != s2
96-
error("Rows must be of matching lengths")
96+
throw(ArgumentError("Rows must be of matching lengths"))
9797
end
9898

9999
exprs = [ex.args[i].args[j] for i = 1:s1, j = 1:s2]
@@ -108,7 +108,7 @@ macro SArray(ex)
108108
s2s = map(i -> ((isa(ex.args[i+1], Expr) && ex.args[i+1].head == :row) ? length(ex.args[i+1].args) : 1), 1:s1)
109109
s2 = minimum(s2s)
110110
if maximum(s2s) != s2
111-
error("Rows must be of matching lengths")
111+
throw(ArgumentError("Rows must be of matching lengths"))
112112
end
113113

114114
exprs = [ex.args[i+1].args[j] for i = 1:s1, j = 1:s2]

src/SMatrix.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ macro SMatrix(ex)
9393
s2s = map(i -> ((isa(ex.args[i], Expr) && ex.args[i].head == :row) ? length(ex.args[i].args) : 1), 1:s1)
9494
s2 = minimum(s2s)
9595
if maximum(s2s) != s2
96-
error("Rows must be of matching lengths")
96+
throw(ArgumentError("Rows must be of matching lengths"))
9797
end
9898

9999
exprs = [ex.args[i].args[j] for i = 1:s1, j = 1:s2]
@@ -108,7 +108,7 @@ macro SMatrix(ex)
108108
s2s = map(i -> ((isa(ex.args[i+1], Expr) && ex.args[i+1].head == :row) ? length(ex.args[i+1].args) : 1), 1:s1)
109109
s2 = minimum(s2s)
110110
if maximum(s2s) != s2
111-
error("Rows must be of matching lengths")
111+
throw(ArgumentError("Rows must be of matching lengths"))
112112
end
113113

114114
exprs = [ex.args[i+1].args[j] for i = 1:s1, j = 1:s2]

src/SizedArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct SizedArray{S <: Tuple, T, N, M} <: StaticArray{S, T, N}
1616

1717
function SizedArray{S, T, N, M}(a::Array) where {S, T, N, M}
1818
if length(a) != tuple_prod(S)
19-
error("Dimensions $(size(a)) don't match static size $S")
19+
throw(DimensionMismatch("Dimensions $(size(a)) don't match static size $S"))
2020
end
2121
if size(a) != size_to_tuple(S)
2222
Base.depwarn("Construction of `SizedArray` with an `Array` of a different

src/indexing.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ end
141141
exprs = [:(a[$i] = v[$i]) for i = 1:L]
142142
return quote
143143
@_propagate_inbounds_meta
144-
if length(v) != L
144+
@boundscheck if length(v) != L
145145
throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$L destination"))
146146
end
147147
@inbounds $(Expr(:block, exprs...))
@@ -152,7 +152,7 @@ end
152152
exprs = [:(a[$i] = v[$i]) for i = 1:L]
153153
return quote
154154
@_propagate_inbounds_meta
155-
if Length(typeof(v)) != L
155+
@boundscheck if Length(typeof(v)) != L
156156
throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$L destination"))
157157
end
158158
$(Expr(:block, exprs...))
@@ -176,7 +176,7 @@ end
176176
exprs = [:(a[inds[$i]] = v[$i]) for i = 1:prod(S)]
177177
return quote
178178
@_propagate_inbounds_meta
179-
if length(v) != $(prod(S))
179+
@boundscheck if length(v) != $(prod(S))
180180
throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$(length(inds)) destination"))
181181
end
182182
$(Expr(:block, exprs...))
@@ -187,7 +187,7 @@ end
187187
exprs = [:(a[inds[$i]] = v[$i]) for i = 1:prod(S)]
188188
return quote
189189
@_propagate_inbounds_meta
190-
if Length(typeof(v)) != Length(s)
190+
@boundscheck if Length(typeof(v)) != Length(s)
191191
throw(DimensionMismatch("tried to assign $(length(v))-element array to length-$(length(inds)) destination"))
192192
end
193193
$(Expr(:block, exprs...))
@@ -342,7 +342,7 @@ end
342342
end
343343

344344
if v <: StaticArray
345-
if Length(v) != prod(linearsizes)
345+
@boundscheck if Length(v) != prod(linearsizes)
346346
return DimensionMismatch("tried to assign $(length(v))-element array to $newsize destination")
347347
end
348348
quote

0 commit comments

Comments
 (0)