-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
Use let model=model in variable macro to improve type stability #3500
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #3500 +/- ##
=======================================
Coverage 98.13% 98.14%
=======================================
Files 37 37
Lines 5536 5545 +9
=======================================
+ Hits 5433 5442 +9
Misses 103 103
☔ View full report in Codecov by Sentry. |
@ExpandingMan could you try this branch? |
This does seem to work yes. To be honest, I'm rather confused about what's going on here, I'm running inside a function where there should be no problems with inference. Guess it can be chalked up to some weird compiler quirk? 🤷 Regardless, looks fixed, thanks. |
Do you have a reproducible example with the What's going on here is a little complicated. The The solution is to add a |
I did get the Since I'm not entirely sure what's going on here, I should resist the urge to be sanctimonious, but, for whatever it's worth, I think this package's macros should output a lot less code, and the vast majority of macros should be exchangeable with simple function calls. Elaborate macro code is way too hard to diagnose and reason about. |
I think we all agree on this. It's an item on our roadmap: https://jump.dev/JuMP.jl/stable/developers/roadmap/ |
src/macros.jl
Outdated
|
||
# Wrap the entire code block in a let statement to make the model act as | ||
# a type stable local variable. | ||
creation_code = quote |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be done in _finalize_macro
to fix other macros as well ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it has to go here, because it if we move it to _finalize_macro
, it'll create something like:
quote
JuMP._valid_model(m, :m)
let m = m
begin
JuMP._error_if_cannot_register(m, :x)
var"#3###277" = begin
(JuMP.Containers.container)(((var"##278",)->begin
JuMP.add_variable(m, JuMP.model_convert(m, JuMP.build_variable(JuMP.var"#_error#115"{LineNumberNode}(:(#= REPL[5]:1 =#), Core.Box((:m, :(x[1:0])))), JuMP.VariableInfo(false, NaN, false, NaN, false, NaN, false, NaN, false, false))), if JuMP.set_string_names_on_creation(m)
JuMP.string("x", "[", JuMP.string(var"##278"), "]")
else
""
end)
end), (JuMP.Containers).vectorized_product((JuMP.Base).OneTo(1:0)), JuMP.Containers.AutoContainerType, Any[Symbol("##278")])
end
m[:x] = var"#3###277"
x = var"#3###277"
end
end
end
and then the x = var"#3###277"
is a local variable inside the let
scope.
I've added it to the other macros.
d0c4e26
to
be6beb3
Compare
This seems to have broken extensions: https://github.com/jump-dev/JuMP.jl/actions/runs/6179053153 |
Ah julia> using JuMP
julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
julia> @variable(model, x[1:0])
VariableRef[]
julia> m = (; m = model)
(m = A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x,)
julia> @variable(m.m, y[1:0])
ERROR: syntax: invalid let syntax around /Users/oscar/.julia/dev/JuMP/src/macros.jl:3035
Stacktrace:
[1] top-level scope
@ REPL[8]:1 |
Okay, updated. We can fix something like This leads to some weird behavior, like |
I think fixing |
Closes #3499
We probably need something similar in the other macros as well.
This is quite hard to test, because wrapping things in a function fixes the problem...