Skip to content

Commit 7e79d4e

Browse files
committed
clarify error message
1 parent 405aa68 commit 7e79d4e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/Reshape/specializations/SubArray.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,31 @@ function _subarray_reshape_codegen(T, N::Int, M::Int, op_types::Core.SimpleVecto
164164
push!(tuple_parts, :(:))
165165
sizes_tuple_expr = Expr(:tuple, tuple_parts...)
166166

167+
# Compute the divisor that the parent dimension must satisfy
168+
middle_prod_parts = [:(sizes[$j]) for j in 2:(m_out - 1)]
169+
if isempty(middle_prod_parts)
170+
divisor_expr = :n
171+
else
172+
middle_prod_expr = length(middle_prod_parts) == 1 ? middle_prod_parts[1] : Expr(:call, :*, middle_prod_parts...)
173+
divisor_expr = :(n * $middle_prod_expr)
174+
end
175+
167176
push!(parent_ops, :(let sizes = ops[$k].sizes, n = $n_expr
168177
n > 0 || throw(ArgumentError("Split sizes must be positive; got n=$n"))
169178
$(m_out > 2 ? :(for j in 2:$(m_out - 1)
170179
sj = sizes[j]
171180
sj > 0 || throw(ArgumentError("Split sizes must be positive; got sizes[$j]=$sj"))
172181
end) : nothing)
182+
# Check parent dimension compatibility before attempting the Split
183+
parent_dim = size(parent(x), $pd_idx)
184+
divisor = $divisor_expr
185+
parent_dim % divisor == 0 || throw(DimensionMismatch(
186+
string(
187+
"Cannot reshape SubArray view: parent dimension ", parent_dim,
188+
" is not divisible by ", divisor,
189+
". Consider using copy() to materialize the view first."
190+
)
191+
))
173192
Split(1, $sizes_tuple_expr)
174193
end))
175194

0 commit comments

Comments
 (0)