Skip to content
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

Fix various JET errors #2271

Merged
merged 2 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
2 changes: 1 addition & 1 deletion src/Benchmarks/Benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ end
x = MOI.add_variables(model, 10_000)
MOI.add_constraints(
model,
[
MOI.ScalarAffineFunction{Float64}[
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, xi)], 0.0) for
xi in x
],
Expand Down
11 changes: 6 additions & 5 deletions src/Bridges/Constraint/bridges/all_different.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function MOI.get(
if bridge.y === nothing
return MOI.VariableIndex[]
end
return [bridge.y]
return MOI.VariableIndex[something(bridge.y)]
end

function MOI.get(
Expand All @@ -182,11 +182,12 @@ function MOI.get(
bridge::AllDifferentToCountDistinctBridge{T},
::MOI.ListOfConstraintIndices{MOI.VariableIndex,MOI.EqualTo{T}},
) where {T}
if bridge.y === nothing
return MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{T}}[]
F, S = MOI.VariableIndex, MOI.EqualTo{T}
ret = MOI.ConstraintIndex{F,S}[]
if bridge.y !== nothing
push!(ret, MOI.ConstraintIndex{F,S}(something(bridge.y).value))
end
ci = MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{T}}(bridge.y.value)
return [ci]
return ret
end

function MOI.get(
Expand Down
17 changes: 9 additions & 8 deletions src/FileFormats/LP/LP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ function _parse_section(
line::AbstractString,
)
if occursin(":", line) # Strip name of the objective
line = String(match(r"(.*?)\:(.*)", line)[2])
m = match(r"(.*?)\:(.*)", line)::RegexMatch
line = String(m[2]::AbstractString)
end
if occursin("^", line)
line = replace(line, "^" => " ^ ")
Expand Down Expand Up @@ -675,9 +676,9 @@ function _parse_section(
end
if isempty(cache.constraint_name)
if occursin(":", line)
m = match(r"(.*?)\:(.*)", line)
cache.constraint_name = String(m[1])
line = String(m[2])
m = match(r"(.*?)\:(.*)", line)::RegexMatch
cache.constraint_name = String(m[1]::AbstractString)
line = String(m[2]::AbstractString)
else
# Give it a temporary name for now
cache.constraint_name = "R$(cache.num_constraints)"
Expand Down Expand Up @@ -930,8 +931,8 @@ end

function _strip_comment(line::String)
if occursin("\\", line)
m = match(r"(.*?)\\(.*)", line)
return strip(String(m[1]))
m = match(r"(.*?)\\(.*)", line)::RegexMatch
return strip(String(m[1]::AbstractString))
else
return strip(line)
end
Expand Down Expand Up @@ -1010,11 +1011,11 @@ function _readline(io::IO, line::AbstractString)
return _readline(io, line)
elseif any(Base.Fix1(endswith, line), ('+', '-', '[', '='))
# If the line ends with a continuation character, read in the next line.
return _readline(io, string(line, ' ', peeked_line))
return _readline(io, string(line, " ", peeked_line))
elseif any(Base.Fix1(startswith, peeked_line), (']', '/'))
# Always read in the next line if it starts with ] or /, which are used
# in quadratic functions.
return _readline(io, string(line, ' ', peeked_line))
return _readline(io, string(line, " ", peeked_line))
end
return line, peeked_line
end
Expand Down
4 changes: 2 additions & 2 deletions src/FileFormats/MPS/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,8 @@ function TempMPSModel()
Float64[], # row_lower
Float64[], # row_upper
Sense[], # sense
Vector{Vector{Tuple{Int,Float64}}}[], # A
Bool[],
Vector{Tuple{Int,Float64}}[], # A
VType[],
Dict{String,Int}(),
String[],
Dict{String,Int}(),
Expand Down
12 changes: 6 additions & 6 deletions src/Nonlinear/ReverseAD/mathoptinterface_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function MOI.eval_objective(d::NLPEvaluator, x)
error("No nonlinear objective.")
end
_reverse_mode(d, x)
return d.objective.forward_storage[1]
return something(d.objective).forward_storage[1]
end

function MOI.eval_objective_gradient(d::NLPEvaluator, g, x)
Expand All @@ -185,7 +185,7 @@ function MOI.eval_objective_gradient(d::NLPEvaluator, g, x)
end
_reverse_mode(d, x)
fill!(g, 0.0)
_extract_reverse_pass(g, d, d.objective)
_extract_reverse_pass(g, d, something(d.objective))
return
end

Expand Down Expand Up @@ -297,7 +297,7 @@ function MOI.eval_hessian_objective(d::NLPEvaluator, H, x)
_reverse_mode(d, x)
fill!(d.input_ϵ, 0.0)
if d.objective !== nothing
_eval_hessian(d, d.objective, H, 1.0, 0)
_eval_hessian(d, something(d.objective), H, 1.0, 0)
end
return
end
Expand All @@ -316,7 +316,7 @@ function MOI.eval_hessian_lagrangian(d::NLPEvaluator, H, x, σ, μ)
fill!(d.input_ϵ, 0.0)
offset = 0
if d.objective !== nothing
offset += _eval_hessian(d, d.objective, H, σ, offset)::Int
offset += _eval_hessian(d, something(d.objective), H, σ, offset)::Int
end
for (i, ex) in enumerate(d.constraints)
offset += _eval_hessian(d, ex, H, μ[i], offset)::Int
Expand Down Expand Up @@ -356,7 +356,7 @@ function MOI.eval_hessian_lagrangian_product(d::NLPEvaluator, h, x, v, σ, μ)
if d.objective !== nothing
_forward_eval_ϵ(
d,
d.objective,
something(d.objective),
reinterpret(T, d.forward_storage_ϵ),
reinterpret(T, d.partials_storage_ϵ),
input_ϵ,
Expand All @@ -365,7 +365,7 @@ function MOI.eval_hessian_lagrangian_product(d::NLPEvaluator, h, x, v, σ, μ)
)
_reverse_eval_ϵ(
output_ϵ,
d.objective,
something(d.objective),
reinterpret(T, d.reverse_storage_ϵ),
reinterpret(T, d.partials_storage_ϵ),
d.subexpression_reverse_values,
Expand Down
4 changes: 2 additions & 2 deletions src/Nonlinear/ReverseAD/reverse_mode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ end
_extract_reverse_pass(
g::AbstractVector{T},
d::NLPEvaluator,
f::Union{_FunctionStorage,_SubexpressionStorage},
f::_FunctionStorage,
) where {T}

Fill the gradient vector `g` with the values from the reverse pass. Assumes you
Expand All @@ -323,7 +323,7 @@ have already called `_reverse_eval_all(d, x)`.
function _extract_reverse_pass(
g::AbstractVector{T},
d::NLPEvaluator,
f::Union{_FunctionStorage,_SubexpressionStorage},
f::_FunctionStorage,
) where {T}
for i in f.dependent_subexpressions
d.subexpression_reverse_values[i] = 0.0
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
hash,
inverse_hash,
true,
K[],
V[],
OrderedCollections.OrderedDict{K,V}(),
)
end
Expand Down
7 changes: 5 additions & 2 deletions src/Utilities/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,11 @@ Do not name `VariableIndex` constraints.
* `x^2` does NOT currently parse. Instead, write `x * x`.
"""
function loadfromstring!(model, s)
parsedlines = filter(ex -> ex !== nothing, Meta.parse.(split(s, "\n")))
for line in parsedlines
for string_line in split(s, "\n")
line = Meta.parse(string_line)
if line === nothing
continue
end
label, ex = _separate_label(line)
T, label = _split_type(label)
if label == :variables
Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/struct_of_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ function struct_of_constraint_code(struct_name, types, field_types = nothing)
model::$typed_struct,
::Type{<:$fun},
::Type{<:$set},
)::$(field_type) where {$T}
) where {$T}
if model.$field === nothing
model.$field = $(field_type)()
$MOI.Utilities._add_variables(
model.$field,
model.num_variables,
)
end
return model.$field
return something(model.$field)
end
)
if type_parametrized
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ function MOI.get(
if uf.objective === nothing
return MOI.get(uf.model, attr)
end
return uf.objective
return something(uf.objective)
end

function MOI.set(
Expand Down
Loading