From 27c701b6998e9a4979376be5bb8928c1c206c37b Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 15 Dec 2023 13:19:02 +1300 Subject: [PATCH] Fix escaping of base_name arguments in macros (#3631) --- src/Containers/macro.jl | 2 +- test/test_macros.jl | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Containers/macro.jl b/src/Containers/macro.jl index 997ec64e751..28ca2e5b8b6 100644 --- a/src/Containers/macro.jl +++ b/src/Containers/macro.jl @@ -425,7 +425,7 @@ function build_name_expr( kwargs::Dict{Symbol,Any}, ) base_name = get(kwargs, :base_name, string(something(name, ""))) - if base_name isa Expr + if !(base_name isa String) base_name = esc(base_name) end if isempty(index_vars) || base_name == "" diff --git a/test/test_macros.jl b/test/test_macros.jl index 25464e13967..46cf93d8c03 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -2167,4 +2167,22 @@ function test_expression_container_kwarg() return end +function test_base_name_escape() + model = Model() + x = @variable(model) + @test name(x) == "" + x = @variable(model, [1:2]) + @test name.(x) == ["", ""] + x = @variable(model, base_name = "x") + @test name(x) == "x" + x = @variable(model, [1:2], base_name = "x") + @test name.(x) == ["x[1]", "x[2]"] + y = "abc" + x = @variable(model, base_name = y) + @test name(x) == "abc" + x = @variable(model, [1:2], base_name = y) + @test name.(x) == ["abc[1]", "abc[2]"] + return +end + end # module