From fc287d1b202570eca577c83212397fd6392172c6 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Sep 2023 15:14:27 +1200 Subject: [PATCH 1/2] Fix constraint tags for broadcast constraints --- src/macros.jl | 19 ++++++++++++------ test/test_macros.jl | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/macros.jl b/src/macros.jl index afbd777b460..8195ddb18cc 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -55,12 +55,19 @@ julia> call ``` """ function _add_positional_args(call, args) - kw_args = filter(arg -> isexpr(arg, :kw), call.args) - filter!(arg -> !isexpr(arg, :kw), call.args) - for arg in args - push!(call.args, esc(arg)) - end - append!(call.args, kw_args) + call_args = call.args + if Meta.isexpr(call, :.) + # call is broadcasted + call_args = call.args[2].args + end + # Cache all keyword arguments + kw_args = filter(Base.Fix2(Meta.isexpr, :kw), call_args) + # Remove keyowrd arguments from the end + filter!(!Base.Fix2(Meta.isexpr, :kw), call_args) + # Add the new positional arguments + append!(call_args, esc.(args)) + # Re-add the cached keyword arguments back to the end + append!(call_args, kw_args) return end diff --git a/test/test_macros.jl b/test/test_macros.jl index 340a69f73df..5fdc4fd1dcf 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -170,6 +170,18 @@ function test_add_positional_args() call = :(f(1; a = 2)) @test JuMP._add_positional_args(call, [:(MyObject)]) isa Nothing @test call == :(f(1, $(Expr(:escape, :MyObject)); a = 2)) + call = :(f(1)) + JuMP._add_positional_args(call, [2, 3]) + @test call == :(f(1, $(esc(2)), $(esc(3)))) + call = :(f.(1)) + JuMP._add_positional_args(call, [2, 3]) + @test call == :(f.(1, $(esc(2)), $(esc(3)))) + call = :(f(1; a = 4)) + JuMP._add_positional_args(call, [2, 3]) + @test call == :(f(1, $(esc(2)), $(esc(3)); a = 4)) + call = :(f.(1; a = 4)) + JuMP._add_positional_args(call, [2, 3]) + @test call == :(f.(1, $(esc(2)), $(esc(3)); a = 4)) return end @@ -2083,4 +2095,40 @@ function test_wrap_let_symbol_models() return end +struct Issue3514Tag + name::String +end + +Base.broadcastable(x::Issue3514Tag) = Ref(x) + +struct Issue3514Type{S} <: AbstractConstraint + name::String + f::AffExpr + s::S +end + +function JuMP.build_constraint( + _error::Function, + f::AffExpr, + set::MOI.AbstractScalarSet, + extra::Issue3514Tag, +) + return Issue3514Type(extra.name, f, set) +end + +function JuMP.add_constraint(model::Model, c::Issue3514Type, name::String) + data = ScalarConstraint(c.f, c.s) + return add_constraint(model, data, "$(c.name)[$(name)]") +end + +function test_issue_3514() + model = Model() + @variable(model, x[1:2]) + @constraint(model, b, 2x .<= 1, Issue3514Tag("a")) + @test name.(b) == ["a[b]", "a[b]"] + @constraint(model, c, 2x .>= 1, [Issue3514Tag("d"),Issue3514Tag("e")]) + @test name.(c) == ["d[c]", "e[c]"] + return +end + end # module From 300f92b35c02eef99f000f33ec32d401f0fc5314 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 19 Sep 2023 15:48:27 +1200 Subject: [PATCH 2/2] Update test/test_macros.jl --- test/test_macros.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_macros.jl b/test/test_macros.jl index 5fdc4fd1dcf..76db0db35a7 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -2126,7 +2126,7 @@ function test_issue_3514() @variable(model, x[1:2]) @constraint(model, b, 2x .<= 1, Issue3514Tag("a")) @test name.(b) == ["a[b]", "a[b]"] - @constraint(model, c, 2x .>= 1, [Issue3514Tag("d"),Issue3514Tag("e")]) + @constraint(model, c, 2x .>= 1, [Issue3514Tag("d"), Issue3514Tag("e")]) @test name.(c) == ["d[c]", "e[c]"] return end