Skip to content

Commit ba82536

Browse files
committed
Fix various errors found by JET
1 parent ec5c0e3 commit ba82536

18 files changed

Lines changed: 177 additions & 176 deletions

src/Bridges/Constraint/bridges/IntegerToZeroOneBridge.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function MOI.Bridges.final_touch(
152152
bridge::IntegerToZeroOneBridge{T},
153153
model::MOI.ModelLike,
154154
) where {T}
155-
ret = MOI.Utilities.get_bounds(model, T, bridge.x)
155+
ret = MOI.Utilities.get_bounds(model, T, bridge.x)::Tuple{T,T}
156156
if ret === bridge.last_bounds
157157
return nothing # final_touch already called
158158
elseif ret[1] == typemin(T) || ret[2] == typemax(T)

src/Bridges/Constraint/bridges/SplitIntervalBridge.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,17 +325,15 @@ function MOI.get(
325325
upper_stat = MOI.get(model, attr, bridge.upper)
326326
if upper_stat == MOI.NONBASIC
327327
return MOI.NONBASIC_AT_UPPER
328+
elseif bridge.lower === nothing
329+
return upper_stat
328330
end
329-
end
330-
if bridge.lower === nothing
331-
if bridge.upper === nothing
332-
# The only case where the interval `[-∞, ∞]` is allowed is for
333-
# `VariableIndex` constraints but `ConstraintBasisStatus` is not
334-
# defined for `VariableIndex` constraints.
335-
msg = "Cannot get `$attr` for a constraint in the interval `[-Inf, Inf]`."
336-
throw(MOI.GetAttributeNotAllowed(attr, msg))
337-
end
338-
return upper_stat
331+
elseif bridge.lower === nothing
332+
# The only case where the interval `[-∞, ∞]` is allowed is for
333+
# `VariableIndex` constraints but `ConstraintBasisStatus` is not
334+
# defined for `VariableIndex` constraints.
335+
msg = "Cannot get `$attr` for a constraint in the interval `[-Inf, Inf]`."
336+
error(msg)
339337
end
340338
lower_stat = MOI.get(model, attr, bridge.lower)
341339
if lower_stat == MOI.NONBASIC

src/Bridges/Constraint/single_bridge_optimizer.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ mutable struct SingleBridgeOptimizer{BT<:AbstractBridge,OT<:MOI.ModelLike} <:
4848
map::Map # index of bridged constraint -> constraint bridge
4949
con_to_name::Dict{MOI.ConstraintIndex,String}
5050
name_to_con::Union{Dict{String,MOI.ConstraintIndex},Nothing}
51-
end
5251

53-
function SingleBridgeOptimizer{BT}(model::OT) where {BT,OT<:MOI.ModelLike}
54-
return SingleBridgeOptimizer{BT,OT}(
55-
model,
56-
Map(),
57-
Dict{MOI.ConstraintIndex,String}(),
58-
nothing,
59-
)
52+
function SingleBridgeOptimizer{BT}(model::OT) where {BT,OT<:MOI.ModelLike}
53+
return new{BT,OT}(
54+
model,
55+
Map(),
56+
Dict{MOI.ConstraintIndex,String}(),
57+
nothing,
58+
)
59+
end
60+
6061
end
6162

6263
bridges(::MOI.Bridges.AbstractBridgeOptimizer) = EmptyMap()

src/Bridges/Objective/single_bridge_optimizer.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ mutable struct SingleBridgeOptimizer{BT<:AbstractBridge,OT<:MOI.ModelLike} <:
4141
MOI.Bridges.AbstractBridgeOptimizer
4242
model::OT
4343
map::Map # `MOI.ObjectiveFunction` -> objective bridge
44-
end
4544

46-
function SingleBridgeOptimizer{BT}(model::OT) where {BT,OT<:MOI.ModelLike}
47-
return SingleBridgeOptimizer{BT,OT}(model, Map())
45+
function SingleBridgeOptimizer{BT}(model::OT) where {BT,OT<:MOI.ModelLike}
46+
return new{BT,OT}(model, Map())
47+
end
4848
end
4949

5050
bridges(::MOI.Bridges.AbstractBridgeOptimizer) = EmptyMap()

src/Bridges/Variable/single_bridge_optimizer.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ mutable struct SingleBridgeOptimizer{BT<:AbstractBridge,OT<:MOI.ModelLike} <:
5050
name_to_var::Union{Dict{String,MOI.VariableIndex},Nothing}
5151
con_to_name::Dict{MOI.ConstraintIndex,String}
5252
name_to_con::Union{Dict{String,MOI.ConstraintIndex},Nothing}
53-
end
5453

55-
function SingleBridgeOptimizer{BT}(model::OT) where {BT,OT<:MOI.ModelLike}
56-
return SingleBridgeOptimizer{BT,OT}(
57-
model,
58-
Map(),
59-
Dict{MOI.VariableIndex,String}(),
60-
nothing,
61-
Dict{MOI.ConstraintIndex,String}(),
62-
nothing,
63-
)
54+
function SingleBridgeOptimizer{BT}(model::OT) where {BT,OT<:MOI.ModelLike}
55+
return new{BT,OT}(
56+
model,
57+
Map(),
58+
Dict{MOI.VariableIndex,String}(),
59+
nothing,
60+
Dict{MOI.ConstraintIndex,String}(),
61+
nothing,
62+
)
63+
end
6464
end
6565

6666
bridges(::MOI.Bridges.AbstractBridgeOptimizer) = EmptyMap()

src/Bridges/bridge_optimizer.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,10 @@ end
23892389
Substitute any bridged [`MOI.VariableIndex`](@ref) in `value` by an equivalent
23902390
expression in terms of variables of `b.model`.
23912391
"""
2392-
function bridged_function(bridge::AbstractBridgeOptimizer, value)
2392+
function bridged_function(
2393+
bridge::AbstractBridgeOptimizer,
2394+
value::V,
2395+
)::V where {V}
23932396
if !Variable.has_bridges(Variable.bridges(bridge))
23942397
# Shortcut, this allows performance to be unaltered when no variable
23952398
# bridges are used.
@@ -2398,10 +2401,9 @@ function bridged_function(bridge::AbstractBridgeOptimizer, value)
23982401
# We assume that the type of `value` is not altered. This restricts
23992402
# variable bridges to only return `ScalarAffineFunction` but otherwise,
24002403
# the performance would be bad.
2401-
return MOI.Utilities.substitute_variables(
2402-
vi -> bridged_variable_function(bridge, vi),
2403-
value,
2404-
)::typeof(value)
2404+
return MOI.Utilities.substitute_variables(value) do vi
2405+
return bridged_variable_function(bridge, vi)
2406+
end
24052407
end
24062408

24072409
function bridged_function(b::AbstractBridgeOptimizer, func::MOI.VariableIndex)

src/FileFormats/LP/read.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ function _peek_inner(state::_LexerState)
523523
while (c = peek(state, Char)) !== nothing && c != '\n'
524524
_ = read(state, Char)
525525
end
526-
elseif isdigit(c) || (c == '-' && isdigit(peek(state, Char))) # Number
526+
elseif isdigit(c) || (c == '-' && isdigit(peek(state, Char)::Char)) # Number
527527
buf = IOBuffer()
528528
while (c = peek(state, Char)) !== nothing && _is_number(c)
529529
write(buf, c)
@@ -826,7 +826,7 @@ function _parse_term(
826826
# <quadratic-expression>
827827
return _parse_quadratic_expression(state, cache, prefix)
828828
end
829-
token = peek(state, _Token)
829+
token = peek(state, _Token)::_Token
830830
return _throw_parse_error(
831831
state,
832832
token,
@@ -1050,7 +1050,7 @@ function _parse_constraint_sos(
10501050
f, w = MOI.VectorOfVariables(MOI.VariableIndex[]), T[]
10511051
while true
10521052
if _next_token_is(state, _TOKEN_NEWLINE)
1053-
t = peek(state, _Token)
1053+
t = peek(state, _Token)::_Token
10541054
_throw_parse_error(
10551055
state,
10561056
t,
@@ -1096,7 +1096,7 @@ function _parse_constraint_indicator(
10961096
end
10971097
_ = read(state, _Token, _TOKEN_IMPLIES)
10981098
f = _parse_expression(state, cache)
1099-
set = _parse_set_suffix(state, cache)
1099+
set = _parse_set_suffix(state, cache)::MOI.AbstractScalarSet
11001100
return MOI.add_constraint(
11011101
cache.model,
11021102
MOI.Utilities.operate(vcat, T, z, f),
@@ -1117,7 +1117,7 @@ function _parse_constraint(state::_LexerState, cache::_ReadCache)
11171117
_parse_constraint_indicator(state, cache)
11181118
else
11191119
f = _parse_expression(state, cache)
1120-
set = _parse_set_suffix(state, cache)
1120+
set = _parse_set_suffix(state, cache)::MOI.AbstractScalarSet
11211121
MOI.add_constraint(cache.model, f, set)
11221122
end
11231123
if name !== nothing

src/FileFormats/MPS/read.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ function _add_indicator_constraint(
432432
scalar = MOI.ScalarAffineTerm(coef, variable_map[data.col_to_name[i]])
433433
push!(terms, MOI.VectorAffineTerm(2, scalar))
434434
end
435-
f = MOI.VectorAffineFunction(terms, zeros(T, 2))
435+
f = MOI.VectorAffineFunction(terms, zeros(T, 2)::Vector{T})
436436
c = MOI.add_constraint(model, f, MOI.Indicator{activate}(set))
437437
MOI.set(model, MOI.ConstraintName(), c, c_name)
438438
return

src/FileFormats/NL/read.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ _try_scalar_affine_function(x::MOI.VariableIndex) = x
271271
function _try_scalar_affine_function(expr::Expr)
272272
if expr.args[1] == :+
273273
args = _try_scalar_affine_function.(expr.args[2:end])
274-
if any(isnothing, args)
274+
if any(isnothing, args)::Bool
275275
return nothing
276276
end
277277
return MOI.Utilities.operate(+, Float64, args...)
278278
elseif expr.args[1] == :*
279279
args = _try_scalar_affine_function.(expr.args[2:end])
280-
if any(isnothing, args)
280+
if any(isnothing, args)::Bool
281281
return nothing
282282
end
283283
n_affine_terms = 0

src/Nonlinear/SymbolicAD/SymbolicAD.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,11 @@ function Evaluator(
13331333
constraint_index_by_hash = Dict{UInt64,Vector{Int}}()
13341334
jac_offset, hess_offset = 0, 0
13351335
if model.objective !== nothing
1336-
o_sym = _to_symbolic_form(model, model.objective, variable_to_column)
1336+
o_sym = _to_symbolic_form(
1337+
model,
1338+
model.objective::MOI.Nonlinear.Expression,
1339+
variable_to_column,
1340+
)
13371341
x, ∇f, H, ∇²f = gradient_and_hessian(x -> x.value > 0, o_sym.f)
13381342
dag = _DAG(model.operators, Any[o_sym.f; ∇f; ∇²f])
13391343
hash_to_dag[o_sym.hash] = dag

0 commit comments

Comments
 (0)