From 488a0dbf0a0d34fcbc37344653edb7fdcf82794c Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 26 Nov 2024 15:37:44 +1300 Subject: [PATCH] Improve error message when = is used instead of == (#3892) --- src/Containers/macro.jl | 5 +++++ test/test_macros.jl | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Containers/macro.jl b/src/Containers/macro.jl index b20c6a76743..639573f5e64 100644 --- a/src/Containers/macro.jl +++ b/src/Containers/macro.jl @@ -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 diff --git a/test/test_macros.jl b/test/test_macros.jl index d923dfedacf..2fc24eabd6f 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -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