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

Fix constraint tags for broadcast constraints #3515

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 13 additions & 6 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")])
odow marked this conversation as resolved.
Show resolved Hide resolved
@test name.(c) == ["d[c]", "e[c]"]
return
end

end # module
Loading