Skip to content

Commit a761177

Browse files
jckingcopybara-github
authored andcommitted
Update overflow internals to use absl::optional
PiperOrigin-RevId: 738820220
1 parent ccc9a70 commit a761177

11 files changed

+487
-591
lines changed

common/values/error_value.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,62 @@ ErrorValue IndexOutOfBoundsError(ptrdiff_t index) {
8888
absl::InvalidArgumentError(absl::StrCat("index out of bounds: ", index)));
8989
}
9090

91+
ErrorValue IntDivisionByZeroError() {
92+
return ErrorValue(absl::InvalidArgumentError("int division by zero"));
93+
}
94+
95+
ErrorValue UintDivisionByZeroError() {
96+
return ErrorValue(absl::InvalidArgumentError("uint division by zero"));
97+
}
98+
99+
ErrorValue IntModuloByZeroError() {
100+
return ErrorValue(absl::InvalidArgumentError("int modulo by zero"));
101+
}
102+
103+
ErrorValue UintModuloByZeroError() {
104+
return ErrorValue(absl::InvalidArgumentError("uint modulo by zero"));
105+
}
106+
107+
ErrorValue IntOverflowError() {
108+
return ErrorValue(absl::OutOfRangeError("int overflow"));
109+
}
110+
111+
ErrorValue UintOverflowError() {
112+
return ErrorValue(absl::OutOfRangeError("uint overflow"));
113+
}
114+
115+
ErrorValue DurationOverflowError() {
116+
return ErrorValue(absl::OutOfRangeError("duration overflow"));
117+
}
118+
119+
ErrorValue TimestampOverflowError() {
120+
return ErrorValue(absl::OutOfRangeError("timestamp overflow"));
121+
}
122+
123+
ErrorValue DoubleToIntOutOfRangeError() {
124+
return ErrorValue(absl::OutOfRangeError("double out of int range"));
125+
}
126+
127+
ErrorValue DoubleToUintOutOfRangeError() {
128+
return ErrorValue(absl::OutOfRangeError("double out of uint range"));
129+
}
130+
131+
ErrorValue IntToUintOutOfRangeError() {
132+
return ErrorValue(absl::OutOfRangeError("int out of uint range"));
133+
}
134+
135+
ErrorValue UintToIntOutOfRangeError() {
136+
return ErrorValue(absl::OutOfRangeError("uint out of int range"));
137+
}
138+
139+
ErrorValue Int64ToInt32OutOfRangeError() {
140+
return ErrorValue(absl::OutOfRangeError("int64 out of int32_t range"));
141+
}
142+
143+
ErrorValue Uint64ToUint32OutOfRangeError() {
144+
return ErrorValue(absl::OutOfRangeError("uint64 out of uint32_t range"));
145+
}
146+
91147
bool IsNoSuchField(const ErrorValue& value) {
92148
return absl::IsNotFound(value.NativeValue()) &&
93149
absl::StartsWith(value.NativeValue().message(), "no_such_field");

common/values/error_value.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,34 @@ ErrorValue IndexOutOfBoundsError(size_t index);
183183

184184
ErrorValue IndexOutOfBoundsError(ptrdiff_t index);
185185

186+
ErrorValue IntDivisionByZeroError();
187+
188+
ErrorValue UintDivisionByZeroError();
189+
190+
ErrorValue IntModuloByZeroError();
191+
192+
ErrorValue UintModuloByZeroError();
193+
194+
ErrorValue IntOverflowError();
195+
196+
ErrorValue UintOverflowError();
197+
198+
ErrorValue DurationOverflowError();
199+
200+
ErrorValue TimestampOverflowError();
201+
202+
ErrorValue DoubleToIntOutOfRangeError();
203+
204+
ErrorValue DoubleToUintOutOfRangeError();
205+
206+
ErrorValue IntToUintOutOfRangeError();
207+
208+
ErrorValue UintToIntOutOfRangeError();
209+
210+
ErrorValue Int64ToInt32OutOfRangeError();
211+
212+
ErrorValue Uint64ToUint32OutOfRangeError();
213+
186214
// Catch other integrals and forward them to the above ones. This is needed to
187215
// avoid ambiguous overload issues for smaller integral types like `int`.
188216
template <typename T>

eval/public/structs/cel_proto_wrap_util.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ google::protobuf::Message* Int32FromValue(const google::protobuf::Message* proto
814814
if (!value.GetValue(&val)) {
815815
return nullptr;
816816
}
817-
if (!cel::internal::CheckedInt64ToInt32(val).ok()) {
817+
if (!cel::internal::CheckedInt64ToInt32(val)) {
818818
return nullptr;
819819
}
820820
int32_t ival = static_cast<int32_t>(val);
@@ -882,7 +882,7 @@ google::protobuf::Message* UInt32FromValue(const google::protobuf::Message* prot
882882
if (!value.GetValue(&val)) {
883883
return nullptr;
884884
}
885-
if (!cel::internal::CheckedUint64ToUint32(val).ok()) {
885+
if (!cel::internal::CheckedUint64ToUint32(val)) {
886886
return nullptr;
887887
}
888888
uint32_t ival = static_cast<uint32_t>(val);

eval/public/structs/field_access_impl.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "absl/strings/str_cat.h"
3131
#include "absl/strings/string_view.h"
3232
#include "absl/strings/substitute.h"
33+
#include "absl/types/optional.h"
3334
#include "eval/public/structs/cel_proto_wrap_util.h"
3435
#include "internal/casts.h"
3536
#include "internal/overflow.h"
@@ -357,9 +358,9 @@ class FieldSetter {
357358
if (!cel_value.GetValue(&value)) {
358359
return false;
359360
}
360-
absl::StatusOr<int32_t> checked_cast =
361+
absl::optional<int32_t> checked_cast =
361362
cel::internal::CheckedInt64ToInt32(value);
362-
if (!checked_cast.ok()) {
363+
if (!checked_cast) {
363364
return false;
364365
}
365366
static_cast<const Derived*>(this)->SetInt32(*checked_cast);
@@ -371,7 +372,7 @@ class FieldSetter {
371372
if (!cel_value.GetValue(&value)) {
372373
return false;
373374
}
374-
if (!cel::internal::CheckedUint64ToUint32(value).ok()) {
375+
if (!cel::internal::CheckedUint64ToUint32(value)) {
375376
return false;
376377
}
377378
static_cast<const Derived*>(this)->SetUInt32(value);
@@ -437,7 +438,7 @@ class FieldSetter {
437438
if (!cel_value.GetValue(&value)) {
438439
return false;
439440
}
440-
if (!cel::internal::CheckedInt64ToInt32(value).ok()) {
441+
if (!cel::internal::CheckedInt64ToInt32(value)) {
441442
return false;
442443
}
443444
static_cast<const Derived*>(this)->SetEnum(value);

internal/BUILD

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,12 @@ cc_library(
8484

8585
cc_library(
8686
name = "overflow",
87-
srcs = ["overflow.cc"],
8887
hdrs = ["overflow.h"],
8988
deps = [
90-
":status_macros",
9189
":time",
92-
"@com_google_absl//absl/status",
93-
"@com_google_absl//absl/status:statusor",
90+
"@com_google_absl//absl/base:config",
9491
"@com_google_absl//absl/time",
92+
"@com_google_absl//absl/types:optional",
9593
],
9694
)
9795

@@ -101,9 +99,10 @@ cc_test(
10199
deps = [
102100
":overflow",
103101
":testing",
102+
":time",
104103
"@com_google_absl//absl/functional:function_ref",
105-
"@com_google_absl//absl/status",
106104
"@com_google_absl//absl/time",
105+
"@com_google_absl//absl/types:optional",
107106
],
108107
)
109108

0 commit comments

Comments
 (0)