From dc77f32911e7876cce952d31930452e392ff79bf Mon Sep 17 00:00:00 2001 From: Thibaut Cuvelier Date: Fri, 24 Apr 2020 04:45:41 +0200 Subject: [PATCH] Clean PR wrt #2229. --- src/macros.jl | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/macros.jl b/src/macros.jl index a3553fb531e..7f24e9be6e9 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -156,37 +156,24 @@ function parse_one_operator_constraint(_error::Function, vectorized::Bool, return parse_code, _build_call(_error, vectorized, variable, set) end -function rewrite_call_expression(_error::Function, head::Val{F}, args...) where F - _error("function $F not implemented.") -end - _functionize(v::VariableRef) = convert(AffExpr, v) _functionize(v::AbstractArray{VariableRef}) = _functionize.(v) _functionize(x) = x function parse_one_operator_constraint(_error::Function, vectorized::Bool, sense::Val, lhs, rhs) - parse_code_rhs, build_code_rhs, new_rhs = :(), :(), rhs - if isexpr(rhs, :call) && applicable(rewrite_call_expression, Val(rhs.args[1]), rhs.args[2:end]...) - parse_code_rhs, build_code_rhs, new_rhs = rewrite_call_expression(_error, Val(rhs.args[1]), rhs.args[2:end]...) - end - # Simple comparison - move everything to the LHS. - - # TODO: bug in MutableArithmetics? The returned code does not find $new_rhs (ERROR: UndefVarError: #642###976 not defined). - if rhs == new_rhs - if vectorized - func = :($lhs .- $new_rhs) - else - func = :($lhs - $new_rhs) - end - variable, parse_code = _MA.rewrite(func) + # + # Note: We add the +0 to this term to account for the pathological case that + # the `lhs` is a `VariableRef` and the `rhs` is a summation with no terms. + # Without the `+0` term, `aff` would evaluate to a `VariableRef` when we + # really want it to be a `GenericAffExpr`. + if vectorized + func = :($lhs .- $rhs) else - variable, parse_code = _MA.rewrite(lhs) - parse_code = :($parse_code; $variable = _MA.mutable_operate!(-, convert(AffExpr, $variable), $new_rhs)) + func = :($lhs - $rhs) end - set = sense_to_set(_error, sense) - build_code = _build_call(_error, vectorized, :(_functionize($variable)), set) - return :($parse_code_rhs; $parse_code), :($build_code_rhs; $build_code) + variable, parse_code = _MA.rewrite(func) + return parse_code, _build_call(_error, vectorized, :(_functionize($variable)), set) end function parse_constraint(_error::Function, ::Val{:call}, args...)