Skip to content

Commit b71aebb

Browse files
committed
Normalize indices in promote_shape error messages.
Seeing implementation like `Base.OneTo` in error messages may be confusing to some users (cf discussion in JuliaLang#39242, [discourse](https://discourse.julialang.org/t/promote-shape-dimension-mismatch/57529/)). This PR turns ```julia julia> ones(2, 3) + ones(3, 2) ERROR: DimensionMismatch("dimensions must match: a has dims (Base.OneTo(2), Base.OneTo(3)), b has dims (Base.OneTo(3), Base.OneTo(2)), mismatch at 1") ``` into ```julia julia> ones(2, 3) + ones(3, 2) ERROR: DimensionMismatch("dimensions must match: a has axes (1:2, 1:3), b has axes (1:3, 1:2), mismatch at 1") ``` Fixes JuliaLang#40118. Acked-by: Tamas K. Papp <[email protected]>
1 parent 9f32653 commit b71aebb

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

base/indices.jl

+21-21
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,34 @@ IndexStyle(::IndexStyle, ::IndexStyle) = IndexCartesian()
106106

107107
promote_shape(::Tuple{}, ::Tuple{}) = ()
108108

109-
function promote_shape(a::Tuple{Int,}, b::Tuple{Int,})
110-
if a[1] != b[1]
111-
throw(DimensionMismatch("dimensions must match: a has dims $a, b has dims $b"))
109+
# Consistent error message for promote_shape mismatch, hiding implementation details like
110+
# OneTo. When b ≡ nothing, it is omitted; i can be supplied for an index.
111+
function throw_promote_shape_mismatch(a, b, i = nothing)
112+
_normalize(d) = map(x -> x isa AbstractUnitRange ? (firstindex(x):lastindex(x)) : x, d)
113+
msg = "dimensions must match: a has dims $(_normalize(a))"
114+
if b nothing
115+
msg *= ", b has dims $(_normalize(b))"
116+
end
117+
if i nothing
118+
msg *= ", mismatch at $(i)"
112119
end
120+
throw(DimensionMismatch(msg))
121+
end
122+
123+
function promote_shape(a::Tuple{Int,}, b::Tuple{Int,})
124+
a[1] != b[1] && throw_promote_shape_mismatch(a, b)
113125
return a
114126
end
115127

116128
function promote_shape(a::Tuple{Int,Int}, b::Tuple{Int,})
117-
if a[1] != b[1] || a[2] != 1
118-
throw(DimensionMismatch("dimensions must match: a has dims $a, b has dims $b"))
119-
end
129+
(a[1] != b[1] || a[2] != 1) && throw_promote_shape_mismatch(a, b)
120130
return a
121131
end
122132

123133
promote_shape(a::Tuple{Int,}, b::Tuple{Int,Int}) = promote_shape(b, a)
124134

125135
function promote_shape(a::Tuple{Int, Int}, b::Tuple{Int, Int})
126-
if a[1] != b[1] || a[2] != b[2]
127-
throw(DimensionMismatch("dimensions must match: a has dims $a, b has dims $b"))
128-
end
136+
(a[1] != b[1] || a[2] != b[2]) && throw_promote_shape_mismatch(a, b)
129137
return a
130138
end
131139

@@ -153,14 +161,10 @@ function promote_shape(a::Dims, b::Dims)
153161
return promote_shape(b, a)
154162
end
155163
for i=1:length(b)
156-
if a[i] != b[i]
157-
throw(DimensionMismatch("dimensions must match: a has dims $a, b has dims $b, mismatch at $i"))
158-
end
164+
a[i] != b[i] && throw_promote_shape_mismatch(a, b, i)
159165
end
160166
for i=length(b)+1:length(a)
161-
if a[i] != 1
162-
throw(DimensionMismatch("dimensions must match: a has dims $a, must have singleton at dim $i"))
163-
end
167+
a[i] != 1 && throw_promote_shape_mismatch(a, nothing, i)
164168
end
165169
return a
166170
end
@@ -174,14 +178,10 @@ function promote_shape(a::Indices, b::Indices)
174178
return promote_shape(b, a)
175179
end
176180
for i=1:length(b)
177-
if a[i] != b[i]
178-
throw(DimensionMismatch("dimensions must match: a has dims $a, b has dims $b, mismatch at $i"))
179-
end
181+
a[i] != b[i] && throw_promote_shape_mismatch(a, b, i)
180182
end
181183
for i=length(b)+1:length(a)
182-
if a[i] != 1:1
183-
throw(DimensionMismatch("dimensions must match: a has dims $a, must have singleton at dim $i"))
184-
end
184+
a[i] != 1:1 && throw_promote_shape_mismatch(a, nothing, i)
185185
end
186186
return a
187187
end

0 commit comments

Comments
 (0)