Skip to content

Commit

Permalink
Improve error message for common incorrect syntax in constraint macro
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Jul 13, 2024
1 parent 301d46e commit ea356ec
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/macros/@constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
36 changes: 36 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ea356ec

Please sign in to comment.