From ea356eccd49d01c76d96c70b470c9be7e943ebf6 Mon Sep 17 00:00:00 2001 From: odow Date: Sat, 13 Jul 2024 16:26:00 +1200 Subject: [PATCH] Improve error message for common incorrect syntax in constraint macro --- src/macros/@constraint.jl | 19 +++++++++++++++++++ test/test_macros.jl | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/macros/@constraint.jl b/src/macros/@constraint.jl index 87d1e47726b..9789971cbe6 100644 --- a/src/macros/@constraint.jl +++ b/src/macros/@constraint.jl @@ -417,6 +417,25 @@ function parse_constraint_head(error_fn::Function, ::Val{T}, args...) where {T} ) end +function parse_constraint_head( + error_fn::Function, + ::Union{Val{:vect},Val{:vcat}}, + args..., +) + return error_fn( + """ + Unsupported constraint expression: we don't know how to parse a + `[ ]` block as a constraint. Have you written: + ```julia + @constraint(model, name, [...], ...) + ``` + instead of: + ```julia + @constraint(model, name[...], ...) + ```""", + ) +end + function parse_constraint_head( error_fn::Function, ::Val{:call}, diff --git a/test/test_macros.jl b/test/test_macros.jl index 6b559667f22..bb80c711579 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -2407,4 +2407,40 @@ function test_unsupported_name_syntax_multiple_ref() return end +function test_constraint_vect_vcat() + model = Model() + @variable(model, x) + @test_throws_parsetime( + ErrorException( + """ + In `@constraint(model, c, [k in 1:2], x <= k)`: Unsupported constraint expression: we don't know how to parse a + `[ ]` block as a constraint. Have you written: + ```julia + @constraint(model, name, [...], ...) + ``` + instead of: + ```julia + @constraint(model, name[...], ...) + ```""", + ), + @constraint(model, c, [k in 1:2], x <= k), + ) + @test_throws_parsetime( + ErrorException( + """ + In `@constraint(model, c, [k in 1:2; isodd(k)], x <= k)`: Unsupported constraint expression: we don't know how to parse a + `[ ]` block as a constraint. Have you written: + ```julia + @constraint(model, name, [...], ...) + ``` + instead of: + ```julia + @constraint(model, name[...], ...) + ```""", + ), + @constraint(model, c, [k in 1:2; isodd(k)], x <= k), + ) + return +end + end # module