Skip to content

Commit c5de829

Browse files
authored
Fix various JET errors (#2269)
1 parent 692006d commit c5de829

File tree

10 files changed

+80
-70
lines changed

10 files changed

+80
-70
lines changed

src/Bridges/Bridges.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@ function runtests(
302302
end
303303
# Test other bridge functions
304304
for b in values(Constraint.bridges(model))
305-
_general_bridge_tests(b)
305+
_general_bridge_tests(something(b))
306306
end
307307
for b in values(Objective.bridges(model))
308-
_general_bridge_tests(b)
308+
_general_bridge_tests(something(b))
309309
end
310310
for b in values(Variable.bridges(model))
311-
_general_bridge_tests(b)
311+
_general_bridge_tests(something(b))
312312
end
313313
_test_delete(Bridge, model, inner)
314314
return

src/Bridges/Variable/map.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function Base.empty!(map::Map)
7171
map.unbridged_function =
7272
Dict{MOI.VariableIndex,Tuple{Int64,MOI.AbstractScalarFunction}}()
7373
else
74-
empty!(map.unbridged_function)
74+
empty!(something(map.unbridged_function))
7575
end
7676
empty!(map.parent_index)
7777
map.current_context = 0
@@ -371,13 +371,14 @@ function add_keys_for_bridge(
371371
MOI.VariableIndex(-(bridge_index - 1 + i)) for i in 1:MOI.dimension(set)
372372
]
373373
if map.unbridged_function !== nothing
374-
mappings = unbridged_map(map.bridges[bridge_index], variables)
374+
mappings =
375+
unbridged_map(something(map.bridges[bridge_index]), variables)
375376
if mappings === nothing
376377
map.unbridged_function = nothing
377378
else
378379
for mapping in mappings
379380
push!(
380-
map.unbridged_function,
381+
something(map.unbridged_function),
381382
mapping.first => (bridge_index, mapping.second),
382383
)
383384
end
@@ -441,7 +442,7 @@ Return the expression of `vi` in terms of bridged variables.
441442
"""
442443
function unbridged_function(map::Map, vi::MOI.VariableIndex)
443444
throw_if_cannot_unbridge(map)
444-
context_func = get(map.unbridged_function, vi, nothing)
445+
context_func = get(something(map.unbridged_function), vi, nothing)
445446
if context_func === nothing
446447
return nothing
447448
end

src/FileFormats/CBF/CBF.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ end
5959
6060
Create an empty instance of `FileFormats.CBF.Model`.
6161
"""
62-
Model() = Model{Float64}()
62+
Model(; kwargs...) = Model{Float64}()
6363

6464
Base.show(io::IO, ::Model) = print(io, "A Conic Benchmark Format (CBF) model")
6565

src/FileFormats/MPS/MPS.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ function parse_name_line(data::TempMPSModel, line::String)
13191319
if m === nothing
13201320
error("Malformed NAME line: ", line)
13211321
end
1322-
data.name = strip(m[1])
1322+
data.name = strip(m[1]::AbstractString)
13231323
return
13241324
end
13251325

src/FileFormats/NL/NL.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -781,29 +781,29 @@ function _assert_has_model(::Nothing, attr)
781781
)
782782
end
783783

784-
_assert_has_model(::MOI.Utilities.UniversalFallback, ::Any) = nothing
784+
_assert_has_model(model::MOI.Utilities.UniversalFallback, ::Any) = model
785785

786786
function MOI.get(model::Model, attr::MOI.AbstractModelAttribute)
787-
_assert_has_model(model.model, attr)
788-
return MOI.get(model.model, attr)
787+
inner = _assert_has_model(model.model, attr)
788+
return MOI.get(inner, attr)
789789
end
790790

791791
function MOI.get(
792792
model::Model,
793793
attr::MOI.AbstractConstraintAttribute,
794794
ci::MOI.ConstraintIndex,
795795
)
796-
_assert_has_model(model.model, attr)
797-
return MOI.get(model.model, attr, ci)
796+
inner = _assert_has_model(model.model, attr)
797+
return MOI.get(inner, attr, ci)
798798
end
799799

800800
function MOI.get(
801801
model::Model,
802802
attr::MOI.AbstractVariableAttribute,
803803
x::MOI.VariableIndex,
804804
)
805-
_assert_has_model(model.model, attr)
806-
return MOI.get(model.model, attr, x)
805+
inner = _assert_has_model(model.model, attr)
806+
return MOI.get(inner, attr, x)
807807
end
808808

809809
end

src/FileFormats/SDPA/SDPA.jl

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -302,20 +302,17 @@ function _dim_to_set(s::AbstractString)
302302
return MOI.Nonnegatives(-block_dim)
303303
end
304304
end
305-
function _parse_dimensions(dims)
305+
function _parse_dimensions(dims::AbstractString)
306306
isvalid(char) = isdigit(char) || char == '-'
307+
is_delimiter(char) = isspace(char) || char == ','
307308
start = findfirst(isvalid, dims)
308-
stop = findlast(isvalid, dims)
309-
if isnothing(start)
309+
if start === nothing
310310
return Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[]
311311
end
312-
function is_delimiter(char)
313-
return isspace(char) || char == ','
314-
end
312+
stop = findlast(isvalid, dims)::Int
315313
s = split(dims[start:stop], is_delimiter)
316-
s = filter(!isempty, s)
317314
return Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[
318-
_dim_to_set(dim) for dim in s
315+
_dim_to_set(dim) for dim in filter!(!isempty, s)
319316
]
320317
end
321318

@@ -324,19 +321,20 @@ end
324321
325322
Read `io` in the SDPA file format and store the result in `model`.
326323
"""
327-
function Base.read!(io::IO, model::Model{T}) where {T}
324+
function Base.read!(io::IO, model::Model{T}) where {T<:Real}
328325
if !MOI.is_empty(model)
329326
error("Cannot read in file because model is not empty.")
330327
end
331328
num_variables_read = false
332329
num_blocks = nothing
333-
block_sets = nothing
330+
block_sets = Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[]
331+
block_sets_read = false
334332
objective_read = false
335333
integer_read = false
336-
scalar_vars = nothing
334+
scalar_vars = MOI.VariableIndex[]
337335
intvar_idx = Int[]
338336
c = nothing
339-
funcs = nothing
337+
funcs = MOI.VectorAffineFunction{T}[]
340338
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
341339
while !eof(io)
342340
line = strip(readline(io))
@@ -375,22 +373,26 @@ function Base.read!(io::IO, model::Model{T}) where {T}
375373
# According to http://plato.asu.edu/ftp/sdpa_format.txt,
376374
# additional text after the number of blocks should be ignored.
377375
num_blocks = parse(Int, split(line)[1])
378-
elseif block_sets === nothing
376+
elseif !block_sets_read
379377
if isempty(line) && !iszero(num_blocks)
380378
continue
381379
end
382380
block_sets = _parse_dimensions(line)
381+
block_sets_read = true
383382
if length(block_sets) != num_blocks
384383
error(
385384
"The number of blocks ($num_blocks) does not match the length of the list of blocks dimensions ($(length(block_sets))).",
386385
)
387386
end
388-
funcs = [
389-
MOI.VectorAffineFunction(
390-
MOI.VectorAffineTerm{T}[],
391-
zeros(T, MOI.dimension(block_sets[i])),
392-
) for i in 1:num_blocks
393-
]
387+
for i in 1:num_blocks
388+
push!(
389+
funcs,
390+
MOI.VectorAffineFunction(
391+
MOI.VectorAffineTerm{T}[],
392+
zeros(T, MOI.dimension(block_sets[i])),
393+
),
394+
)
395+
end
394396
elseif !objective_read
395397
num_vars = MOI.get(model, MOI.NumberOfVariables())
396398
if isempty(line) && !iszero(num_vars)
@@ -452,7 +454,7 @@ function Base.read!(io::IO, model::Model{T}) where {T}
452454
end
453455
end
454456
end
455-
for block in 1:num_blocks
457+
for block in 1:(num_blocks::Int)
456458
MOI.add_constraint(model, funcs[block], block_sets[block])
457459
end
458460
for var_idx in intvar_idx

src/Nonlinear/evaluator.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function MOI.objective_expr(evaluator::Evaluator)
9090
end
9191
return convert_to_expr(
9292
evaluator,
93-
evaluator.model.objective;
93+
something(evaluator.model.objective);
9494
moi_output_format = true,
9595
)
9696
end

src/Nonlinear/operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ function eval_univariate_hessian(
575575
) where {T}
576576
id = registry.univariate_operator_to_id[op]
577577
if id <= registry.univariate_user_operator_start
578-
return _eval_univariate_2nd_deriv(id, x)
578+
return _eval_univariate_2nd_deriv(id, x)::T
579579
end
580580
offset = id - registry.univariate_user_operator_start
581581
operator = registry.registered_univariate_operators[offset]

src/Utilities/objective_container.jl

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,21 @@ function MOI.get(
121121
::MOI.ObjectiveFunction{F},
122122
) where {T,F}
123123
if o.scalar_affine !== nothing
124-
return convert(F, o.scalar_affine)
124+
return convert(F, something(o.scalar_affine))
125125
elseif o.single_variable !== nothing
126-
return convert(F, o.single_variable)
126+
return convert(F, something(o.single_variable))
127127
elseif o.scalar_quadratic !== nothing
128-
return convert(F, o.scalar_quadratic)
128+
return convert(F, something(o.scalar_quadratic))
129129
elseif o.scalar_nonlinear !== nothing
130-
return convert(F, o.scalar_nonlinear)
130+
return convert(F, something(o.scalar_nonlinear))
131131
elseif o.vector_variables !== nothing
132-
return convert(F, o.vector_variables)
132+
return convert(F, something(o.vector_variables))
133133
elseif o.vector_affine !== nothing
134-
return convert(F, o.vector_affine)
134+
return convert(F, something(o.vector_affine))
135135
elseif o.vector_quadratic !== nothing
136-
return convert(F, o.vector_quadratic)
136+
return convert(F, something(o.vector_quadratic))
137137
elseif o.vector_nonlinear !== nothing
138-
return convert(F, o.vector_nonlinear)
138+
return convert(F, something(o.vector_nonlinear))
139139
end
140140
# The default if no objective is set.
141141
return convert(F, zero(MOI.ScalarAffineFunction{T}))
@@ -262,11 +262,13 @@ function MOI.modify(
262262
change::MOI.AbstractFunctionModification,
263263
) where {T}
264264
if o.single_variable !== nothing
265-
o.single_variable = modify_function!(o.single_variable, change)
265+
o.single_variable =
266+
modify_function!(something(o.single_variable), change)
266267
elseif o.scalar_affine !== nothing
267-
o.scalar_affine = modify_function!(o.scalar_affine, change)
268+
o.scalar_affine = modify_function!(something(o.scalar_affine), change)
268269
elseif o.scalar_quadratic !== nothing
269-
o.scalar_quadratic = modify_function!(o.scalar_quadratic, change)
270+
o.scalar_quadratic =
271+
modify_function!(something(o.scalar_quadratic), change)
270272
elseif o.scalar_nonlinear !== nothing
271273
throw(
272274
MOI.ModifyObjectiveNotAllowed(
@@ -276,11 +278,13 @@ function MOI.modify(
276278
),
277279
)
278280
elseif o.vector_variables !== nothing
279-
o.vector_variables = modify_function!(o.vector_variables, change)
281+
o.vector_variables =
282+
modify_function!(something(o.vector_variables), change)
280283
elseif o.vector_quadratic !== nothing
281-
o.vector_quadratic = modify_function!(o.vector_quadratic, change)
284+
o.vector_quadratic =
285+
modify_function!(something(o.vector_quadratic), change)
282286
elseif o.vector_affine !== nothing
283-
o.vector_affine = modify_function!(o.vector_affine, change)
287+
o.vector_affine = modify_function!(something(o.vector_affine), change)
284288
elseif o.vector_nonlinear !== nothing
285289
throw(
286290
MOI.ModifyObjectiveNotAllowed(
@@ -304,13 +308,13 @@ end
304308

305309
function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex)
306310
if o.single_variable !== nothing
307-
if x == o.single_variable
311+
if x == something(o.single_variable)
308312
_empty_keeping_sense(o)
309313
end
310314
elseif o.scalar_affine !== nothing
311-
o.scalar_affine = remove_variable(o.scalar_affine, x)
315+
o.scalar_affine = remove_variable(something(o.scalar_affine), x)
312316
elseif o.scalar_quadratic !== nothing
313-
o.scalar_quadratic = remove_variable(o.scalar_quadratic, x)
317+
o.scalar_quadratic = remove_variable(something(o.scalar_quadratic), x)
314318
elseif o.scalar_nonlinear !== nothing
315319
throw(
316320
MOI.DeleteNotAllowed(
@@ -320,14 +324,14 @@ function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex)
320324
),
321325
)
322326
elseif o.vector_variables !== nothing
323-
o.vector_variables = remove_variable(o.vector_variables, x)
324-
if isempty(o.vector_variables.variables)
327+
o.vector_variables = remove_variable(something(o.vector_variables), x)
328+
if isempty(something(o.vector_variables).variables)
325329
_empty_keeping_sense(o)
326330
end
327331
elseif o.vector_affine !== nothing
328-
o.vector_affine = remove_variable(o.vector_affine, x)
332+
o.vector_affine = remove_variable(something(o.vector_affine), x)
329333
elseif o.vector_quadratic !== nothing
330-
o.vector_quadratic = remove_variable(o.vector_quadratic, x)
334+
o.vector_quadratic = remove_variable(something(o.vector_quadratic), x)
331335
elseif o.vector_nonlinear !== nothing
332336
throw(
333337
MOI.DeleteNotAllowed(
@@ -343,13 +347,14 @@ end
343347
function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex})
344348
keep = v -> !(v in x)
345349
if o.single_variable !== nothing
346-
if o.single_variable in x
350+
if something(o.single_variable) in x
347351
_empty_keeping_sense(o)
348352
end
349353
elseif o.scalar_affine !== nothing
350-
o.scalar_affine = filter_variables(keep, o.scalar_affine)
354+
o.scalar_affine = filter_variables(keep, something(o.scalar_affine))
351355
elseif o.scalar_quadratic !== nothing
352-
o.scalar_quadratic = filter_variables(keep, o.scalar_quadratic)
356+
o.scalar_quadratic =
357+
filter_variables(keep, something(o.scalar_quadratic))
353358
elseif o.scalar_nonlinear !== nothing
354359
throw(
355360
MOI.DeleteNotAllowed(
@@ -359,14 +364,16 @@ function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex})
359364
),
360365
)
361366
elseif o.vector_variables !== nothing
362-
o.vector_variables = filter_variables(keep, o.vector_variables)
363-
if isempty(o.vector_variables.variables)
367+
o.vector_variables =
368+
filter_variables(keep, something(o.vector_variables))
369+
if isempty(something(o.vector_variables).variables)
364370
_empty_keeping_sense(o)
365371
end
366372
elseif o.vector_affine !== nothing
367-
o.vector_affine = filter_variables(keep, o.vector_affine)
373+
o.vector_affine = filter_variables(keep, something(o.vector_affine))
368374
elseif o.vector_quadratic !== nothing
369-
o.vector_quadratic = filter_variables(keep, o.vector_quadratic)
375+
o.vector_quadratic =
376+
filter_variables(keep, something(o.vector_quadratic))
370377
elseif o.vector_nonlinear !== nothing
371378
throw(
372379
MOI.DeleteNotAllowed(

src/Utilities/universalfallback.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function MOI.delete(uf::UniversalFallback, vi::MOI.VariableIndex)
280280
delete!(d, vi)
281281
end
282282
if uf.objective !== nothing
283-
uf.objective = remove_variable(uf.objective, vi)
283+
uf.objective = remove_variable(something(uf.objective), vi)
284284
end
285285
for constraints in values(uf.single_variable_constraints)
286286
_remove_variable(uf, constraints, vi)
@@ -309,7 +309,7 @@ function MOI.delete(uf::UniversalFallback, vis::Vector{MOI.VariableIndex})
309309
end
310310
end
311311
if uf.objective !== nothing
312-
uf.objective = remove_variable(uf.objective, vis)
312+
uf.objective = remove_variable(something(uf.objective), vis)
313313
end
314314
for constraints in values(uf.single_variable_constraints)
315315
for vi in vis
@@ -678,7 +678,7 @@ function MOI.get(
678678
# to check for duplicate names.
679679
MOI.get(uf.model, MOI.ConstraintIndex, name)
680680
end
681-
ci_uf = get(uf.name_to_con, name, nothing)
681+
ci_uf = get(something(uf.name_to_con), name, nothing)
682682
throw_if_multiple_with_name(ci_uf, name)
683683
c = check_type_and_multiple_names(MOI.ConstraintIndex{F,S}, ci_uf, ci, name)
684684
return c
@@ -692,7 +692,7 @@ function MOI.get(
692692
if uf.name_to_con === nothing
693693
uf.name_to_con = build_name_to_con_map(uf.con_to_name)
694694
end
695-
ci_uf = get(uf.name_to_con, name, nothing)
695+
ci_uf = get(something(uf.name_to_con), name, nothing)
696696
throw_if_multiple_with_name(ci_uf, name)
697697
return check_type_and_multiple_names(
698698
MOI.ConstraintIndex,

0 commit comments

Comments
 (0)