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

Assignment allowed in macros where it shouldn't be #2289

Closed
mlubin opened this issue Jul 18, 2020 · 4 comments
Closed

Assignment allowed in macros where it shouldn't be #2289

mlubin opened this issue Jul 18, 2020 · 4 comments
Labels
Type: Error Messages Can be fixed with better error message

Comments

@mlubin
Copy link
Member

mlubin commented Jul 18, 2020

Simplified from https://discourse.julialang.org/t/how-to-print-the-values-of-constraints/40040:

julia> m = Model();

julia> @variable(m, x);

julia> @constraint(m, (a = x) <= 1)
x  1.0

I'm not sure how exactly, but this should produce an error.

@mlubin mlubin added the Type: Error Messages Can be fixed with better error message label Jul 18, 2020
@blegat
Copy link
Member

blegat commented Jul 19, 2020

Why shouldn't it be allowed, this just stes the value of a as expected. The macro rewrites the expression to exploit mutability, nothing more.

@odow
Copy link
Member

odow commented Dec 15, 2020

Agreed with @blegat. The macro effectively expands to

ss0 = JuMP.MutableArithmetics.Zero()
ss1 = JuMP.MutableArithmetics.operate!(
    JuMP.MutableArithmetics.add_mul, 
    ss0, 
    $(Expr(:(=), :a, :x))
)
ss2 = JuMP.MutableArithmetics.operate!(
    JuMP.MutableArithmetics.sub_mul, 
    ss1, 
    1,
)

The interpolation evaluates a = x, returns the RHS x, substitutes it in, and away we go. So we can't intercept it at a functional level.

I guess you could walk the expression of the constraint and see if there are any :(=), then throw an error, but it would get tricky to work around generators etc.

@odow
Copy link
Member

odow commented Feb 18, 2021

I bet there is also at least one person relying on this with some weird expression, so erroring on this would break code.

julia> model = Model();

julia> @variable(model, x);

julia> @constraint(model, (a = 2; a^2) * x <= 1)
4 x  1.0

I vote we close this, because there's nothing technically wrong (and people may be relying on it), and it has only come up once on Discourse. We can revisit if there are more reports.

@odow
Copy link
Member

odow commented Mar 31, 2021

Closing this as won't-fix for the reasons above. We can re-open if it comes up again.

@odow odow closed this as completed Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Error Messages Can be fixed with better error message
Development

No branches or pull requests

3 participants