Skip to content

Commit

Permalink
Improve error message when = is used instead of == (#3892)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 26, 2024
1 parent 1230ea6 commit 488a0db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Containers/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ function parse_macro_arguments(
)
elseif valid_kwargs !== nothing && !(arg.args[1] in valid_kwargs)
error_fn("unsupported keyword argument `$(arg.args[1])`.")
elseif !(arg.args[1] isa Symbol)
error_fn(
"Invalid keyword argument detected. If you are trying to " *
"construct an equality constraint, use `==` instead of `=`.",
)
end
kwargs[arg.args[1]] = arg.args[2]
else
Expand Down
12 changes: 12 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2514,4 +2514,16 @@ function test_do_not_mutate_expression_in_set()
return
end

function test_malformed_kwarg()
model = Model()
@variable(model, x[1:1])
@test_throws_parsetime(
ErrorException(
"In `@constraint(model, x[1] = 1)`: Invalid keyword argument detected. If you are trying to construct an equality constraint, use `==` instead of `=`.",
),
@constraint(model, x[1] = 1),
)
return
end

end # module

0 comments on commit 488a0db

Please sign in to comment.