Skip to content

Fix various JET errors #2269

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

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Bridges/Bridges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ function runtests(
end
# Test other bridge functions
for b in values(Constraint.bridges(model))
_general_bridge_tests(b)
_general_bridge_tests(something(b))
end
for b in values(Objective.bridges(model))
_general_bridge_tests(b)
_general_bridge_tests(something(b))
end
for b in values(Variable.bridges(model))
_general_bridge_tests(b)
_general_bridge_tests(something(b))
end
_test_delete(Bridge, model, inner)
return
Expand Down
9 changes: 5 additions & 4 deletions src/Bridges/Variable/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function Base.empty!(map::Map)
map.unbridged_function =
Dict{MOI.VariableIndex,Tuple{Int64,MOI.AbstractScalarFunction}}()
else
empty!(map.unbridged_function)
empty!(something(map.unbridged_function))
end
empty!(map.parent_index)
map.current_context = 0
Expand Down Expand Up @@ -371,13 +371,14 @@ function add_keys_for_bridge(
MOI.VariableIndex(-(bridge_index - 1 + i)) for i in 1:MOI.dimension(set)
]
if map.unbridged_function !== nothing
mappings = unbridged_map(map.bridges[bridge_index], variables)
mappings =
unbridged_map(something(map.bridges[bridge_index]), variables)
if mappings === nothing
map.unbridged_function = nothing
else
for mapping in mappings
push!(
map.unbridged_function,
something(map.unbridged_function),
mapping.first => (bridge_index, mapping.second),
)
end
Expand Down Expand Up @@ -441,7 +442,7 @@ Return the expression of `vi` in terms of bridged variables.
"""
function unbridged_function(map::Map, vi::MOI.VariableIndex)
throw_if_cannot_unbridge(map)
context_func = get(map.unbridged_function, vi, nothing)
context_func = get(something(map.unbridged_function), vi, nothing)
if context_func === nothing
return nothing
end
Expand Down
2 changes: 1 addition & 1 deletion src/FileFormats/CBF/CBF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ end

Create an empty instance of `FileFormats.CBF.Model`.
"""
Model() = Model{Float64}()
Model(; kwargs...) = Model{Float64}()

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

Expand Down
2 changes: 1 addition & 1 deletion src/FileFormats/MPS/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ function parse_name_line(data::TempMPSModel, line::String)
if m === nothing
error("Malformed NAME line: ", line)
end
data.name = strip(m[1])
data.name = strip(m[1]::AbstractString)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's actually a bunch of problems where we assume that the regex finds something.

return
end

Expand Down
14 changes: 7 additions & 7 deletions src/FileFormats/NL/NL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -781,29 +781,29 @@ function _assert_has_model(::Nothing, attr)
)
end

_assert_has_model(::MOI.Utilities.UniversalFallback, ::Any) = nothing
_assert_has_model(model::MOI.Utilities.UniversalFallback, ::Any) = model

function MOI.get(model::Model, attr::MOI.AbstractModelAttribute)
_assert_has_model(model.model, attr)
return MOI.get(model.model, attr)
inner = _assert_has_model(model.model, attr)
return MOI.get(inner, attr)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously compiler couldn't prove that model.model was not nothing because the check happened in _assert_has_model. Okay at runtime, not at compilation.

end

function MOI.get(
model::Model,
attr::MOI.AbstractConstraintAttribute,
ci::MOI.ConstraintIndex,
)
_assert_has_model(model.model, attr)
return MOI.get(model.model, attr, ci)
inner = _assert_has_model(model.model, attr)
return MOI.get(inner, attr, ci)
end

function MOI.get(
model::Model,
attr::MOI.AbstractVariableAttribute,
x::MOI.VariableIndex,
)
_assert_has_model(model.model, attr)
return MOI.get(model.model, attr, x)
inner = _assert_has_model(model.model, attr)
return MOI.get(inner, attr, x)
end

end
42 changes: 22 additions & 20 deletions src/FileFormats/SDPA/SDPA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,17 @@ function _dim_to_set(s::AbstractString)
return MOI.Nonnegatives(-block_dim)
end
end
function _parse_dimensions(dims)
function _parse_dimensions(dims::AbstractString)
isvalid(char) = isdigit(char) || char == '-'
is_delimiter(char) = isspace(char) || char == ','
start = findfirst(isvalid, dims)
stop = findlast(isvalid, dims)
if isnothing(start)
if start === nothing
return Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[]
end
function is_delimiter(char)
return isspace(char) || char == ','
end
stop = findlast(isvalid, dims)::Int
s = split(dims[start:stop], is_delimiter)
s = filter(!isempty, s)
return Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[
_dim_to_set(dim) for dim in s
_dim_to_set(dim) for dim in filter!(!isempty, s)
]
end

Expand All @@ -324,19 +321,20 @@ end

Read `io` in the SDPA file format and store the result in `model`.
"""
function Base.read!(io::IO, model::Model{T}) where {T}
function Base.read!(io::IO, model::Model{T}) where {T<:Real}
if !MOI.is_empty(model)
error("Cannot read in file because model is not empty.")
end
num_variables_read = false
num_blocks = nothing
block_sets = nothing
block_sets = Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[]
block_sets_read = false
objective_read = false
integer_read = false
scalar_vars = nothing
scalar_vars = MOI.VariableIndex[]
intvar_idx = Int[]
c = nothing
funcs = nothing
funcs = MOI.VectorAffineFunction{T}[]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just a type stability issue, where various variables were initialized as nothing and then swapped to vectors.

MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
while !eof(io)
line = strip(readline(io))
Expand Down Expand Up @@ -375,22 +373,26 @@ function Base.read!(io::IO, model::Model{T}) where {T}
# According to http://plato.asu.edu/ftp/sdpa_format.txt,
# additional text after the number of blocks should be ignored.
num_blocks = parse(Int, split(line)[1])
elseif block_sets === nothing
elseif !block_sets_read
if isempty(line) && !iszero(num_blocks)
continue
end
block_sets = _parse_dimensions(line)
block_sets_read = true
if length(block_sets) != num_blocks
error(
"The number of blocks ($num_blocks) does not match the length of the list of blocks dimensions ($(length(block_sets))).",
)
end
funcs = [
MOI.VectorAffineFunction(
MOI.VectorAffineTerm{T}[],
zeros(T, MOI.dimension(block_sets[i])),
) for i in 1:num_blocks
]
for i in 1:num_blocks
push!(
funcs,
MOI.VectorAffineFunction(
MOI.VectorAffineTerm{T}[],
zeros(T, MOI.dimension(block_sets[i])),
),
)
end
elseif !objective_read
num_vars = MOI.get(model, MOI.NumberOfVariables())
if isempty(line) && !iszero(num_vars)
Expand Down Expand Up @@ -452,7 +454,7 @@ function Base.read!(io::IO, model::Model{T}) where {T}
end
end
end
for block in 1:num_blocks
for block in 1:(num_blocks::Int)
MOI.add_constraint(model, funcs[block], block_sets[block])
end
for var_idx in intvar_idx
Expand Down
2 changes: 1 addition & 1 deletion src/Nonlinear/evaluator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function MOI.objective_expr(evaluator::Evaluator)
end
return convert_to_expr(
evaluator,
evaluator.model.objective;
something(evaluator.model.objective);
moi_output_format = true,
)
end
Expand Down
2 changes: 1 addition & 1 deletion src/Nonlinear/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ function eval_univariate_hessian(
) where {T}
id = registry.univariate_operator_to_id[op]
if id <= registry.univariate_user_operator_start
return _eval_univariate_2nd_deriv(id, x)
return _eval_univariate_2nd_deriv(id, x)::T
end
offset = id - registry.univariate_user_operator_start
operator = registry.registered_univariate_operators[offset]
Expand Down
63 changes: 35 additions & 28 deletions src/Utilities/objective_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,21 @@ function MOI.get(
::MOI.ObjectiveFunction{F},
) where {T,F}
if o.scalar_affine !== nothing
return convert(F, o.scalar_affine)
return convert(F, something(o.scalar_affine))
elseif o.single_variable !== nothing
return convert(F, o.single_variable)
return convert(F, something(o.single_variable))
elseif o.scalar_quadratic !== nothing
return convert(F, o.scalar_quadratic)
return convert(F, something(o.scalar_quadratic))
elseif o.scalar_nonlinear !== nothing
return convert(F, o.scalar_nonlinear)
return convert(F, something(o.scalar_nonlinear))
elseif o.vector_variables !== nothing
return convert(F, o.vector_variables)
return convert(F, something(o.vector_variables))
elseif o.vector_affine !== nothing
return convert(F, o.vector_affine)
return convert(F, something(o.vector_affine))
elseif o.vector_quadratic !== nothing
return convert(F, o.vector_quadratic)
return convert(F, something(o.vector_quadratic))
elseif o.vector_nonlinear !== nothing
return convert(F, o.vector_nonlinear)
return convert(F, something(o.vector_nonlinear))
end
# The default if no objective is set.
return convert(F, zero(MOI.ScalarAffineFunction{T}))
Expand Down Expand Up @@ -262,11 +262,13 @@ function MOI.modify(
change::MOI.AbstractFunctionModification,
) where {T}
if o.single_variable !== nothing
o.single_variable = modify_function!(o.single_variable, change)
o.single_variable =
modify_function!(something(o.single_variable), change)
elseif o.scalar_affine !== nothing
o.scalar_affine = modify_function!(o.scalar_affine, change)
o.scalar_affine = modify_function!(something(o.scalar_affine), change)
elseif o.scalar_quadratic !== nothing
o.scalar_quadratic = modify_function!(o.scalar_quadratic, change)
o.scalar_quadratic =
modify_function!(something(o.scalar_quadratic), change)
elseif o.scalar_nonlinear !== nothing
throw(
MOI.ModifyObjectiveNotAllowed(
Expand All @@ -276,11 +278,13 @@ function MOI.modify(
),
)
elseif o.vector_variables !== nothing
o.vector_variables = modify_function!(o.vector_variables, change)
o.vector_variables =
modify_function!(something(o.vector_variables), change)
elseif o.vector_quadratic !== nothing
o.vector_quadratic = modify_function!(o.vector_quadratic, change)
o.vector_quadratic =
modify_function!(something(o.vector_quadratic), change)
elseif o.vector_affine !== nothing
o.vector_affine = modify_function!(o.vector_affine, change)
o.vector_affine = modify_function!(something(o.vector_affine), change)
elseif o.vector_nonlinear !== nothing
throw(
MOI.ModifyObjectiveNotAllowed(
Expand All @@ -304,13 +308,13 @@ end

function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex)
if o.single_variable !== nothing
if x == o.single_variable
if x == something(o.single_variable)
_empty_keeping_sense(o)
end
elseif o.scalar_affine !== nothing
o.scalar_affine = remove_variable(o.scalar_affine, x)
o.scalar_affine = remove_variable(something(o.scalar_affine), x)
elseif o.scalar_quadratic !== nothing
o.scalar_quadratic = remove_variable(o.scalar_quadratic, x)
o.scalar_quadratic = remove_variable(something(o.scalar_quadratic), x)
elseif o.scalar_nonlinear !== nothing
throw(
MOI.DeleteNotAllowed(
Expand All @@ -320,14 +324,14 @@ function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex)
),
)
elseif o.vector_variables !== nothing
o.vector_variables = remove_variable(o.vector_variables, x)
if isempty(o.vector_variables.variables)
o.vector_variables = remove_variable(something(o.vector_variables), x)
if isempty(something(o.vector_variables).variables)
_empty_keeping_sense(o)
end
elseif o.vector_affine !== nothing
o.vector_affine = remove_variable(o.vector_affine, x)
o.vector_affine = remove_variable(something(o.vector_affine), x)
elseif o.vector_quadratic !== nothing
o.vector_quadratic = remove_variable(o.vector_quadratic, x)
o.vector_quadratic = remove_variable(something(o.vector_quadratic), x)
elseif o.vector_nonlinear !== nothing
throw(
MOI.DeleteNotAllowed(
Expand All @@ -343,13 +347,14 @@ end
function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex})
keep = v -> !(v in x)
if o.single_variable !== nothing
if o.single_variable in x
if something(o.single_variable) in x
_empty_keeping_sense(o)
end
elseif o.scalar_affine !== nothing
o.scalar_affine = filter_variables(keep, o.scalar_affine)
o.scalar_affine = filter_variables(keep, something(o.scalar_affine))
elseif o.scalar_quadratic !== nothing
o.scalar_quadratic = filter_variables(keep, o.scalar_quadratic)
o.scalar_quadratic =
filter_variables(keep, something(o.scalar_quadratic))
elseif o.scalar_nonlinear !== nothing
throw(
MOI.DeleteNotAllowed(
Expand All @@ -359,14 +364,16 @@ function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex})
),
)
elseif o.vector_variables !== nothing
o.vector_variables = filter_variables(keep, o.vector_variables)
if isempty(o.vector_variables.variables)
o.vector_variables =
filter_variables(keep, something(o.vector_variables))
if isempty(something(o.vector_variables).variables)
_empty_keeping_sense(o)
end
elseif o.vector_affine !== nothing
o.vector_affine = filter_variables(keep, o.vector_affine)
o.vector_affine = filter_variables(keep, something(o.vector_affine))
elseif o.vector_quadratic !== nothing
o.vector_quadratic = filter_variables(keep, o.vector_quadratic)
o.vector_quadratic =
filter_variables(keep, something(o.vector_quadratic))
elseif o.vector_nonlinear !== nothing
throw(
MOI.DeleteNotAllowed(
Expand Down
8 changes: 4 additions & 4 deletions src/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function MOI.delete(uf::UniversalFallback, vi::MOI.VariableIndex)
delete!(d, vi)
end
if uf.objective !== nothing
uf.objective = remove_variable(uf.objective, vi)
uf.objective = remove_variable(something(uf.objective), vi)
end
for constraints in values(uf.single_variable_constraints)
_remove_variable(uf, constraints, vi)
Expand Down Expand Up @@ -309,7 +309,7 @@ function MOI.delete(uf::UniversalFallback, vis::Vector{MOI.VariableIndex})
end
end
if uf.objective !== nothing
uf.objective = remove_variable(uf.objective, vis)
uf.objective = remove_variable(something(uf.objective), vis)
end
for constraints in values(uf.single_variable_constraints)
for vi in vis
Expand Down Expand Up @@ -678,7 +678,7 @@ function MOI.get(
# to check for duplicate names.
MOI.get(uf.model, MOI.ConstraintIndex, name)
end
ci_uf = get(uf.name_to_con, name, nothing)
ci_uf = get(something(uf.name_to_con), name, nothing)
throw_if_multiple_with_name(ci_uf, name)
c = check_type_and_multiple_names(MOI.ConstraintIndex{F,S}, ci_uf, ci, name)
return c
Expand All @@ -692,7 +692,7 @@ function MOI.get(
if uf.name_to_con === nothing
uf.name_to_con = build_name_to_con_map(uf.con_to_name)
end
ci_uf = get(uf.name_to_con, name, nothing)
ci_uf = get(something(uf.name_to_con), name, nothing)
throw_if_multiple_with_name(ci_uf, name)
return check_type_and_multiple_names(
MOI.ConstraintIndex,
Expand Down