Skip to content

Commit

Permalink
Improve error for unsupported container syntax like x[A][B] (#3756)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored May 26, 2024
1 parent 48a4f74 commit fad385c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Containers/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ function _parse_ref_sets(error_fn::Function, expr::Expr)
# `:(t[i, j; k])` is a `:ref`, while `:(t[i; j])` is a `:typed_vcat`. In
# both cases `:t` is the first argument.
if Meta.isexpr(c, :typed_vcat) || Meta.isexpr(c, :ref)
popfirst!(c.args)
name = popfirst!(c.args)
if !(name isa Symbol)
error_fn(
"Unsupported syntax: the expression `$name` cannot be used " *
"as a name.",
)
end
end
if Meta.isexpr(c, :vcat) || Meta.isexpr(c, :typed_vcat)
# An expression like `t[i; k]` or `[i; k]`. The filtering condition is
Expand Down
12 changes: 12 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2395,4 +2395,16 @@ function test_force_nonlinear()
return
end

function test_unsupported_name_syntax_multiple_ref()
model = Model()
@test_throws_parsetime(
ErrorException(
"In `@variable(model, (x[1:1])[1:2])`: Unsupported syntax: " *
"the expression `x[1:1]` cannot be used as a name.",
),
@variable(model, x[1:1][1:2]),
)
return
end

end # module

0 comments on commit fad385c

Please sign in to comment.