From fbe7c896d094847bdf888b3d6fab0da4284b5ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 21 Sep 2026 09:31:26 +0200 Subject: [PATCH] Check model ownership inside indexed expression templates --- src/JuMP_wrapper.jl | 29 +++++++++++++++++++++++++++++ src/operators.jl | 4 ++-- test/JuMP_wrapper.jl | 20 ++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/JuMP_wrapper.jl b/src/JuMP_wrapper.jl index 5bb6130..85f19f6 100644 --- a/src/JuMP_wrapper.jl +++ b/src/JuMP_wrapper.jl @@ -40,6 +40,35 @@ function Base.getindex(A::ArrayOfVariables{T}, I...) where {T} return JuMP.GenericVariableRef{T}(A.model, MOI.VariableIndex(index)) end +function JuMP.check_belongs_to_model( + array::ArrayOfVariables, + model::JuMP.AbstractModel, +) + if array.model !== model && !isempty(array) + throw(JuMP.VariableNotOwned(first(array))) + end + return +end + +_check_belongs_to_model(::Any, ::JuMP.AbstractModel) = nothing + +function _check_belongs_to_model( + expr::Union{JuMP.AbstractJuMPScalar,ArrayOfVariables}, + model::JuMP.AbstractModel, +) + return JuMP.check_belongs_to_model(expr, model) +end + +function _check_belongs_to_model( + expr::JuMP.GenericNonlinearExpr, + model::JuMP.AbstractModel, +) + for arg in expr.args + _check_belongs_to_model(arg, model) + end + return +end + JuMP._is_real(::ArrayOfVariables) = true function JuMP.moi_function(array::ArrayOfVariables) return ContiguousArrayOfVariables(array.offset, array.size) diff --git a/src/operators.jl b/src/operators.jl index d0f95c3..f6328ca 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -42,7 +42,7 @@ end JuMP.variable_ref_type(::Type{ExprTemplate{E,V}}) where {E,V} = V function JuMP.check_belongs_to_model(f::ExprTemplate, model) - return JuMP.check_belongs_to_model(f.expr, model) + return _check_belongs_to_model(f.expr, model) end """ @@ -277,7 +277,7 @@ Base.iszero(::_AnyLazySum) = false JuMP.owner_model(s::_AnyLazySum) = JuMP.owner_model(s.expr) function JuMP.check_belongs_to_model(s::_AnyLazySum, model::JuMP.AbstractModel) - return JuMP.check_belongs_to_model(s.expr, model) + return _check_belongs_to_model(s.expr, model) end function MA.promote_operation( diff --git a/test/JuMP_wrapper.jl b/test/JuMP_wrapper.jl index ff759e4..9f7c76d 100644 --- a/test/JuMP_wrapper.jl +++ b/test/JuMP_wrapper.jl @@ -61,6 +61,26 @@ function test_container() @test_broken JuMP.isequal_canonical(con_expr, expr) end +function test_generator_variable_ownership() + model, other = Model(), Model() + @variable(model, x[1:2]) + @variable(other, foreign[1:2]) + i = GenOpt.iterator([1, 2]) + @test JuMP.check_belongs_to_model(x[i], model) === nothing + @test JuMP.check_belongs_to_model(GenOpt.LazySum(x[i]), model) === nothing + for expr in (foreign[i], x[i] + foreign[i], (x[i] + 1.0) * foreign[i]) + @test_throws VariableNotOwned JuMP.check_belongs_to_model(expr, model) + end + @test_throws VariableNotOwned JuMP.check_belongs_to_model( + GenOpt.LazySum(foreign[i]), + model, + ) + constraint = + JuMP.build_constraint(error, x[i] + foreign[i], MOI.LessThan(0.0)) + @test_throws VariableNotOwned JuMP.check_belongs_to_model(constraint, model) + return +end + function test_ind2sub() # `bridge.jl` expands a generator with `CartesianIndices` so `_ind2sub` # must agree with it, otherwise `getindex` and the bridge would disagree