Skip to content

Commit af5487d

Browse files
authored
Propagate nullability (#4959)
1 parent d4ed0ad commit af5487d

30 files changed

+153
-196
lines changed

ortools/math_opt/core/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ cc_library(
210210
"//ortools/math_opt:result_cc_proto",
211211
"//ortools/util:solve_interrupter",
212212
"@abseil-cpp//absl/base:core_headers",
213+
"@abseil-cpp//absl/base:nullability",
213214
"@abseil-cpp//absl/log",
214215
"@abseil-cpp//absl/log:check",
215216
"@abseil-cpp//absl/log:die_if_null",
@@ -455,6 +456,7 @@ cc_library(
455456
"//ortools/math_opt:parameters_cc_proto",
456457
"//ortools/math_opt:result_cc_proto",
457458
"//ortools/util:solve_interrupter",
459+
"@abseil-cpp//absl/base:nullability",
458460
"@abseil-cpp//absl/status:statusor",
459461
"@abseil-cpp//absl/strings",
460462
"@abseil-cpp//absl/strings:string_view",

ortools/math_opt/core/base_solver.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <string>
2020
#include <vector>
2121

22+
#include "absl/base/nullability.h"
2223
#include "absl/status/statusor.h"
2324
#include "ortools/math_opt/callback.pb.h"
2425
#include "ortools/math_opt/infeasible_subsystem.pb.h"
@@ -78,7 +79,7 @@ class BaseSolver {
7879

7980
// An optional interrupter that the solver can use to interrupt the solve
8081
// early.
81-
const SolveInterrupter* interrupter = nullptr;
82+
const SolveInterrupter* absl_nullable interrupter = nullptr;
8283

8384
friend std::ostream& operator<<(std::ostream& out, const SolveArgs& args);
8485
};
@@ -96,7 +97,7 @@ class BaseSolver {
9697

9798
// An optional interrupter that the solver can use to interrupt the solve
9899
// early.
99-
const SolveInterrupter* interrupter = nullptr;
100+
const SolveInterrupter* absl_nullable interrupter = nullptr;
100101

101102
friend std::ostream& operator<<(std::ostream& out,
102103
const ComputeInfeasibleSubsystemArgs& args);

ortools/math_opt/core/solver_interface.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class SolverInterface {
146146
const ModelSolveParametersProto& model_parameters,
147147
MessageCallback message_cb,
148148
const CallbackRegistrationProto& callback_registration, Callback cb,
149-
const SolveInterrupter* interrupter) = 0;
149+
const SolveInterrupter* absl_nullable interrupter) = 0;
150150

151151
// Updates the model to solve and returns true, or returns false if this
152152
// update is not supported.
@@ -173,9 +173,9 @@ class SolverInterface {
173173
// When parameter `message_cb` is not null and the underlying solver does not
174174
// supports message callbacks, it should ignore it.
175175
virtual absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
176-
ComputeInfeasibleSubsystem(const SolveParametersProto& parameters,
177-
MessageCallback message_cb,
178-
const SolveInterrupter* interrupter) = 0;
176+
ComputeInfeasibleSubsystem(
177+
const SolveParametersProto& parameters, MessageCallback message_cb,
178+
const SolveInterrupter* absl_nullable interrupter) = 0;
179179
};
180180

181181
class AllSolversRegistry {

ortools/math_opt/core/solver_interface_mock.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
#include <memory>
7272
#include <utility>
7373

74+
#include "absl/base/attributes.h"
75+
#include "absl/base/nullability.h"
7476
#include "absl/base/thread_annotations.h"
7577
#include "absl/log/die_if_null.h"
7678
#include "absl/status/statusor.h"
@@ -98,7 +100,7 @@ class SolverInterfaceMock : public SolverInterface {
98100
const ModelSolveParametersProto& model_parameters,
99101
MessageCallback message_cb,
100102
const CallbackRegistrationProto& callback_registration,
101-
Callback cb, const SolveInterrupter* interrupter),
103+
Callback cb, const SolveInterrupter* absl_nullable interrupter),
102104
(override));
103105

104106
MOCK_METHOD(absl::StatusOr<bool>, Update,
@@ -107,7 +109,8 @@ class SolverInterfaceMock : public SolverInterface {
107109
MOCK_METHOD(absl::StatusOr<ComputeInfeasibleSubsystemResultProto>,
108110
ComputeInfeasibleSubsystem,
109111
(const SolveParametersProto& parameters,
110-
MessageCallback message_cb, const SolveInterrupter* interrupter),
112+
MessageCallback message_cb,
113+
const SolveInterrupter* absl_nullable interrupter),
111114
(override));
112115
};
113116

@@ -136,7 +139,7 @@ class DelegatingSolver : public SolverInterface {
136139
const ModelSolveParametersProto& model_parameters,
137140
MessageCallback message_cb,
138141
const CallbackRegistrationProto& callback_registration, Callback cb,
139-
const SolveInterrupter* const interrupter) override {
142+
const SolveInterrupter* absl_nullable const interrupter) override {
140143
return solver_->Solve(parameters, model_parameters, std::move(message_cb),
141144
callback_registration, std::move(cb), interrupter);
142145
};
@@ -148,7 +151,7 @@ class DelegatingSolver : public SolverInterface {
148151
absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
149152
ComputeInfeasibleSubsystem(
150153
const SolveParametersProto& parameters, MessageCallback message_cb,
151-
const SolveInterrupter* const interrupter) override {
154+
const SolveInterrupter* absl_nullable const interrupter) override {
152155
return solver_->ComputeInfeasibleSubsystem(
153156
parameters, std::move(message_cb), interrupter);
154157
}

ortools/math_opt/cpp/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ cc_library(
517517
"//ortools/math_opt:model_cc_proto",
518518
"//ortools/math_opt:model_update_cc_proto",
519519
"//ortools/math_opt/storage:model_storage",
520+
"@abseil-cpp//absl/log:die_if_null",
520521
"@abseil-cpp//absl/status",
521522
"@abseil-cpp//absl/status:statusor",
522523
"@abseil-cpp//absl/strings",
@@ -531,6 +532,7 @@ cc_library(
531532
"//ortools/base:logging",
532533
"//ortools/base:source_location",
533534
"@abseil-cpp//absl/base:core_headers",
535+
"@abseil-cpp//absl/log:die_if_null",
534536
"@abseil-cpp//absl/strings",
535537
"@abseil-cpp//absl/synchronization",
536538
"@abseil-cpp//absl/types:span",
@@ -559,6 +561,7 @@ cc_library(
559561
"//ortools/base:status_macros",
560562
"//ortools/math_opt/storage:model_storage",
561563
"//ortools/util:solve_interrupter",
564+
"@abseil-cpp//absl/base:nullability",
562565
"@abseil-cpp//absl/container:flat_hash_set",
563566
"@abseil-cpp//absl/status",
564567
],

ortools/math_opt/cpp/compute_infeasible_subsystem_arguments.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef ORTOOLS_MATH_OPT_CPP_COMPUTE_INFEASIBLE_SUBSYSTEM_ARGUMENTS_H_
1515
#define ORTOOLS_MATH_OPT_CPP_COMPUTE_INFEASIBLE_SUBSYSTEM_ARGUMENTS_H_
1616

17+
#include "absl/base/nullability.h"
1718
#include "ortools/math_opt/cpp/message_callback.h" // IWYU pragma: export
1819
#include "ortools/math_opt/cpp/parameters.h" // IWYU pragma: export
1920
#include "ortools/util/solve_interrupter.h" // IWYU pragma: export
@@ -59,7 +60,7 @@ struct ComputeInfeasibleSubsystemArguments {
5960
// ComputeInfeasibleSubsystem(model, SolverType::kGurobi,
6061
// { .interrupter = interrupter.get() });
6162
//
62-
const SolveInterrupter* interrupter = nullptr;
63+
const SolveInterrupter* absl_nullable interrupter = nullptr;
6364
};
6465

6566
} // namespace operations_research::math_opt

ortools/math_opt/cpp/message_callback.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <vector>
2121

2222
#include "absl/base/thread_annotations.h"
23+
#include "absl/log/die_if_null.h"
2324
#include "absl/strings/string_view.h"
2425
#include "absl/synchronization/mutex.h"
2526
#include "absl/types/span.h"

ortools/math_opt/cpp/solve_arguments.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,12 @@
2424

2525
namespace operations_research::math_opt {
2626

27-
absl::Status SolveArguments::CheckModelStorageAndCallback(
27+
absl::Status SolveArguments::CheckModelStorage(
2828
const ModelStorageCPtr expected_storage) const {
2929
RETURN_IF_ERROR(model_parameters.CheckModelStorage(expected_storage))
3030
<< "invalid model_parameters";
3131
RETURN_IF_ERROR(callback_registration.CheckModelStorage(expected_storage))
3232
<< "invalid callback_registration";
33-
34-
if (callback == nullptr && !callback_registration.events.empty()) {
35-
return absl::InvalidArgumentError(
36-
"no callback was provided to run, but callback events were registered");
37-
}
3833
return absl::OkStatus();
3934
}
4035

ortools/math_opt/cpp/solve_arguments.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef ORTOOLS_MATH_OPT_CPP_SOLVE_ARGUMENTS_H_
1818
#define ORTOOLS_MATH_OPT_CPP_SOLVE_ARGUMENTS_H_
1919

20+
#include "absl/base/nullability.h"
2021
#include "absl/status/status.h"
2122
#include "ortools/math_opt/cpp/callback.h" // IWYU pragma: export
2223
#include "ortools/math_opt/cpp/message_callback.h" // IWYU pragma: export
@@ -82,13 +83,11 @@ struct SolveArguments {
8283
// Solve(model, SolverType::kGlop,
8384
// { .interrupter = interrupter.get() });
8485
//
85-
const SolveInterrupter* interrupter = nullptr;
86+
const SolveInterrupter* absl_nullable interrupter = nullptr;
8687

8788
// Returns a failure if the referenced variables and constraints don't belong
88-
// to the input expected_storage (which must not be nullptr). Also returns a
89-
// failure if callback events are registered but no callback is provided.
90-
absl::Status CheckModelStorageAndCallback(
91-
ModelStorageCPtr expected_storage) const;
89+
// to the input expected_storage (which must not be nullptr).
90+
absl::Status CheckModelStorage(ModelStorageCPtr expected_storage) const;
9291
};
9392

9493
} // namespace operations_research::math_opt

ortools/math_opt/cpp/solve_arguments_test.cc

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace {
2727
using ::testing::HasSubstr;
2828
using ::testing::status::StatusIs;
2929

30-
TEST(CheckModelStorageAndCallbackTest, CorrectModelAndCallback) {
30+
TEST(CheckModelStorageTest, CorrectModelAndCallback) {
3131
Model model;
3232
const Variable x = model.AddVariable("x");
3333

@@ -41,10 +41,10 @@ TEST(CheckModelStorageAndCallbackTest, CorrectModelAndCallback) {
4141
.callback = [](const CallbackData&) { return CallbackResult{}; },
4242
};
4343

44-
EXPECT_OK(args.CheckModelStorageAndCallback(model.storage()));
44+
EXPECT_OK(args.CheckModelStorage(model.storage()));
4545
}
4646

47-
TEST(CheckModelStorageAndCallbackTest, WrongModelInModelParameters) {
47+
TEST(CheckModelStorageTest, WrongModelInModelParameters) {
4848
Model model;
4949
const Variable x = model.AddVariable("x");
5050

@@ -53,12 +53,12 @@ TEST(CheckModelStorageAndCallbackTest, WrongModelInModelParameters) {
5353
};
5454

5555
Model other_model;
56-
EXPECT_THAT(args.CheckModelStorageAndCallback(other_model.storage()),
56+
EXPECT_THAT(args.CheckModelStorage(other_model.storage()),
5757
StatusIs(absl::StatusCode::kInvalidArgument,
5858
HasSubstr("model_parameters")));
5959
}
6060

61-
TEST(CheckModelStorageAndCallbackTest, WrongModelInCallbackRegistration) {
61+
TEST(CheckModelStorageTest, WrongModelInCallbackRegistration) {
6262
Model model;
6363
const Variable x = model.AddVariable("x");
6464

@@ -72,26 +72,11 @@ TEST(CheckModelStorageAndCallbackTest, WrongModelInCallbackRegistration) {
7272
};
7373

7474
Model other_model;
75-
EXPECT_THAT(args.CheckModelStorageAndCallback(other_model.storage()),
75+
EXPECT_THAT(args.CheckModelStorage(other_model.storage()),
7676
StatusIs(absl::StatusCode::kInvalidArgument,
7777
AllOf(HasSubstr("callback_registration"),
7878
HasSubstr("mip_solution_filter"))));
7979
}
8080

81-
TEST(CheckModelStorageAndCallbackTest, NoCallbackWithRegisteredEvents) {
82-
Model model;
83-
84-
const SolveArguments args = {
85-
.callback_registration =
86-
{
87-
.events = {CallbackEvent::kMipSolution},
88-
},
89-
};
90-
91-
EXPECT_THAT(
92-
args.CheckModelStorageAndCallback(model.storage()),
93-
StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("no callback")));
94-
}
95-
9681
} // namespace
9782
} // namespace operations_research::math_opt

0 commit comments

Comments
 (0)