Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for common incorrect syntax in constraint macro #3781

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading