From fad385c5aad5b375566914f021e7afc96a204403 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Sun, 26 May 2024 14:11:10 +1200 Subject: [PATCH] Improve error for unsupported container syntax like x[A][B] (#3756) --- src/Containers/macro.jl | 8 +++++++- test/test_macros.jl | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Containers/macro.jl b/src/Containers/macro.jl index 7b2322b532b..b20c6a76743 100644 --- a/src/Containers/macro.jl +++ b/src/Containers/macro.jl @@ -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 diff --git a/test/test_macros.jl b/test/test_macros.jl index 2321b8deaf9..d1a7f8dd9e3 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -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