Skip to content

Commit b6ff501

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Migrate absl::visit to std::visit.
PiperOrigin-RevId: 928845975
1 parent f5d0d5f commit b6ff501

5 files changed

Lines changed: 81 additions & 82 deletions

File tree

common/legacy_value.cc

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

2425
#include "google/protobuf/struct.pb.h"
@@ -34,7 +35,6 @@
3435
#include "absl/strings/string_view.h"
3536
#include "absl/types/optional.h"
3637
#include "absl/types/span.h"
37-
#include "absl/types/variant.h"
3838
#include "base/attribute.h"
3939
#include "common/casting.h"
4040
#include "common/kind.h"
@@ -1003,7 +1003,7 @@ absl::Status LegacyStructValue::Qualify(
10031003
const auto* access_apis =
10041004
message_wrapper.legacy_type_info()->GetAccessApis(message_wrapper);
10051005
if (ABSL_PREDICT_FALSE(access_apis == nullptr)) {
1006-
absl::string_view field_name = absl::visit(
1006+
absl::string_view field_name = std::visit(
10071007
absl::Overload(
10081008
[](const FieldSpecifier& field) -> absl::string_view {
10091009
return field.name;

common/value.cc

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string>
2222
#include <type_traits>
2323
#include <utility>
24+
#include <variant>
2425

2526
#include "google/protobuf/struct.pb.h"
2627
#include "absl/base/attributes.h"
@@ -37,7 +38,6 @@
3738
#include "absl/strings/string_view.h"
3839
#include "absl/time/time.h"
3940
#include "absl/types/optional.h"
40-
#include "absl/types/variant.h"
4141
#include "common/allocator.h"
4242
#include "common/memory.h"
4343
#include "common/optional_ref.h"
@@ -933,7 +933,7 @@ void StringRepeatedFieldAccessor(
933933
ABSL_DCHECK_LT(index, reflection->FieldSize(*message, field));
934934

935935
std::string scratch;
936-
absl::visit(
936+
std::visit(
937937
absl::Overload(
938938
[&](absl::string_view string) {
939939
if (string.data() == scratch.data() &&
@@ -1000,7 +1000,7 @@ void BytesRepeatedFieldAccessor(
10001000
ABSL_DCHECK_LT(index, reflection->FieldSize(*message, field));
10011001

10021002
std::string scratch;
1003-
absl::visit(
1003+
std::visit(
10041004
absl::Overload(
10051005
[&](absl::string_view string) {
10061006
if (string.data() == scratch.data() &&
@@ -1154,49 +1154,49 @@ struct OwningWellKnownTypesValueVisitor {
11541154
std::string* absl_nonnull scratch;
11551155

11561156
Value operator()(well_known_types::BytesValue&& value) const {
1157-
return absl::visit(absl::Overload(
1158-
[&](absl::string_view string) -> BytesValue {
1159-
if (string.empty()) {
1160-
return BytesValue();
1161-
}
1162-
if (scratch->data() == string.data() &&
1163-
scratch->size() == string.size()) {
1164-
return BytesValue(arena, std::move(*scratch));
1165-
}
1166-
return BytesValue(arena, string);
1167-
},
1168-
[&](absl::Cord&& cord) -> BytesValue {
1169-
if (cord.empty()) {
1170-
return BytesValue();
1171-
}
1172-
return BytesValue(arena, cord);
1173-
}),
1174-
well_known_types::AsVariant(std::move(value)));
1157+
return std::visit(absl::Overload(
1158+
[&](absl::string_view string) -> BytesValue {
1159+
if (string.empty()) {
1160+
return BytesValue();
1161+
}
1162+
if (scratch->data() == string.data() &&
1163+
scratch->size() == string.size()) {
1164+
return BytesValue(arena, std::move(*scratch));
1165+
}
1166+
return BytesValue(arena, string);
1167+
},
1168+
[&](absl::Cord&& cord) -> BytesValue {
1169+
if (cord.empty()) {
1170+
return BytesValue();
1171+
}
1172+
return BytesValue(arena, cord);
1173+
}),
1174+
well_known_types::AsVariant(std::move(value)));
11751175
}
11761176

11771177
Value operator()(well_known_types::StringValue&& value) const {
1178-
return absl::visit(absl::Overload(
1179-
[&](absl::string_view string) -> StringValue {
1180-
if (string.empty()) {
1181-
return StringValue();
1182-
}
1183-
if (scratch->data() == string.data() &&
1184-
scratch->size() == string.size()) {
1185-
return StringValue(arena, std::move(*scratch));
1186-
}
1187-
return StringValue(arena, string);
1188-
},
1189-
[&](absl::Cord&& cord) -> StringValue {
1190-
if (cord.empty()) {
1191-
return StringValue();
1192-
}
1193-
return StringValue(arena, cord);
1194-
}),
1195-
well_known_types::AsVariant(std::move(value)));
1178+
return std::visit(absl::Overload(
1179+
[&](absl::string_view string) -> StringValue {
1180+
if (string.empty()) {
1181+
return StringValue();
1182+
}
1183+
if (scratch->data() == string.data() &&
1184+
scratch->size() == string.size()) {
1185+
return StringValue(arena, std::move(*scratch));
1186+
}
1187+
return StringValue(arena, string);
1188+
},
1189+
[&](absl::Cord&& cord) -> StringValue {
1190+
if (cord.empty()) {
1191+
return StringValue();
1192+
}
1193+
return StringValue(arena, cord);
1194+
}),
1195+
well_known_types::AsVariant(std::move(value)));
11961196
}
11971197

11981198
Value operator()(well_known_types::ListValue&& value) const {
1199-
return absl::visit(
1199+
return std::visit(
12001200
absl::Overload(
12011201
[&](well_known_types::ListValueConstRef value) -> ListValue {
12021202
auto* cloned = value.get().New(arena);
@@ -1215,7 +1215,7 @@ struct OwningWellKnownTypesValueVisitor {
12151215
}
12161216

12171217
Value operator()(well_known_types::Struct&& value) const {
1218-
return absl::visit(
1218+
return std::visit(
12191219
absl::Overload(
12201220
[&](well_known_types::StructConstRef value) -> MapValue {
12211221
auto* cloned = value.get().New(arena);
@@ -1254,7 +1254,7 @@ struct BorrowingWellKnownTypesValueVisitor {
12541254
std::string* absl_nonnull scratch;
12551255

12561256
Value operator()(well_known_types::BytesValue&& value) const {
1257-
return absl::visit(
1257+
return std::visit(
12581258
absl::Overload(
12591259
[&](absl::string_view string) -> BytesValue {
12601260
if (string.data() == scratch->data() &&
@@ -1272,7 +1272,7 @@ struct BorrowingWellKnownTypesValueVisitor {
12721272
}
12731273

12741274
Value operator()(well_known_types::StringValue&& value) const {
1275-
return absl::visit(
1275+
return std::visit(
12761276
absl::Overload(
12771277
[&](absl::string_view string) -> StringValue {
12781278
if (string.data() == scratch->data() &&
@@ -1290,7 +1290,7 @@ struct BorrowingWellKnownTypesValueVisitor {
12901290
}
12911291

12921292
Value operator()(well_known_types::ListValue&& value) const {
1293-
return absl::visit(
1293+
return std::visit(
12941294
absl::Overload(
12951295
[&](well_known_types::ListValueConstRef value)
12961296
-> ParsedJsonListValue {
@@ -1309,7 +1309,7 @@ struct BorrowingWellKnownTypesValueVisitor {
13091309
}
13101310

13111311
Value operator()(well_known_types::Struct&& value) const {
1312-
return absl::visit(
1312+
return std::visit(
13131313
absl::Overload(
13141314
[&](well_known_types::StructConstRef value) -> ParsedJsonMapValue {
13151315
return ParsedJsonMapValue(&value.get(),
@@ -1360,7 +1360,7 @@ Value Value::FromMessage(
13601360
if (ABSL_PREDICT_FALSE(!status_or_adapted.ok())) {
13611361
return ErrorValue(std::move(status_or_adapted).status());
13621362
}
1363-
return absl::visit(
1363+
return std::visit(
13641364
absl::Overload(OwningWellKnownTypesValueVisitor{
13651365
/* .arena = */ arena, /* .scratch = */ &scratch},
13661366
[&](std::monostate) -> Value {
@@ -1388,7 +1388,7 @@ Value Value::FromMessage(
13881388
if (ABSL_PREDICT_FALSE(!status_or_adapted.ok())) {
13891389
return ErrorValue(std::move(status_or_adapted).status());
13901390
}
1391-
return absl::visit(
1391+
return std::visit(
13921392
absl::Overload(OwningWellKnownTypesValueVisitor{
13931393
/* .arena = */ arena, /* .scratch = */ &scratch},
13941394
[&](std::monostate) -> Value {
@@ -1418,7 +1418,7 @@ Value Value::WrapMessage(
14181418
if (ABSL_PREDICT_FALSE(!adapted_value.ok())) {
14191419
return ErrorValue(std::move(adapted_value).status());
14201420
}
1421-
return absl::visit(
1421+
return std::visit(
14221422
absl::Overload(BorrowingWellKnownTypesValueVisitor{
14231423
/* .message = */ message, /* .arena = */ arena,
14241424
/* .scratch = */ &scratch},
@@ -1452,7 +1452,7 @@ Value Value::WrapMessageUnsafe(
14521452
if (ABSL_PREDICT_FALSE(!adapted_value.ok())) {
14531453
return ErrorValue(std::move(adapted_value).status());
14541454
}
1455-
return absl::visit(
1455+
return std::visit(
14561456
absl::Overload(BorrowingWellKnownTypesValueVisitor{
14571457
/* .message = */ message, /* .arena = */ arena,
14581458
/* .scratch = */ &scratch},
@@ -1552,7 +1552,7 @@ Value WrapFieldImpl(
15521552
return BoolValue(reflection->GetBool(*message, field));
15531553
case google::protobuf::FieldDescriptor::TYPE_STRING: {
15541554
std::string scratch;
1555-
return absl::visit(
1555+
return std::visit(
15561556
absl::Overload(
15571557
[&](absl::string_view string) -> StringValue {
15581558
if (string.data() == scratch.data() &&
@@ -1590,7 +1590,7 @@ Value WrapFieldImpl(
15901590
}
15911591
case google::protobuf::FieldDescriptor::TYPE_BYTES: {
15921592
std::string scratch;
1593-
return absl::visit(
1593+
return std::visit(
15941594
absl::Overload(
15951595
[&](absl::string_view string) -> BytesValue {
15961596
if (string.data() == scratch.data() &&
@@ -1681,7 +1681,7 @@ Value WrapRepeatedFieldImpl(
16811681
return BoolValue(reflection->GetRepeatedBool(*message, field, index));
16821682
case google::protobuf::FieldDescriptor::TYPE_STRING: {
16831683
std::string scratch;
1684-
return absl::visit(
1684+
return std::visit(
16851685
absl::Overload(
16861686
[&](absl::string_view string) -> StringValue {
16871687
if (string.data() == scratch.data() &&
@@ -1715,7 +1715,7 @@ Value WrapRepeatedFieldImpl(
17151715
}
17161716
case google::protobuf::FieldDescriptor::TYPE_BYTES: {
17171717
std::string scratch;
1718-
return absl::visit(
1718+
return std::visit(
17191719
absl::Overload(
17201720
[&](absl::string_view string) -> BytesValue {
17211721
if (string.data() == scratch.data() &&

internal/well_known_types.cc

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <functional>
2020
#include <string>
2121
#include <utility>
22+
#include <variant>
2223
#include <vector>
2324

2425
#include "google/protobuf/any.pb.h"
@@ -44,7 +45,6 @@
4445
#include "absl/strings/string_view.h"
4546
#include "absl/strings/strip.h"
4647
#include "absl/time/time.h"
47-
#include "absl/types/variant.h"
4848
#include "common/json.h"
4949
#include "common/memory.h"
5050
#include "extensions/protobuf/internal/map_reflection.h"
@@ -85,7 +85,7 @@ FieldDescriptor::Label GetFieldLabel(
8585
absl::string_view FlatStringValue(
8686
const StringValue& value ABSL_ATTRIBUTE_LIFETIME_BOUND,
8787
std::string& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) {
88-
return absl::visit(
88+
return std::visit(
8989
absl::Overload(
9090
[](absl::string_view string) -> absl::string_view { return string; },
9191
[&](const absl::Cord& cord) -> absl::string_view {
@@ -101,7 +101,7 @@ absl::string_view FlatStringValue(
101101
StringValue CopyStringValue(const StringValue& value,
102102
std::string& scratch
103103
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
104-
return absl::visit(
104+
return std::visit(
105105
absl::Overload(
106106
[&](absl::string_view string) -> StringValue {
107107
if (string.data() != scratch.data()) {
@@ -116,7 +116,7 @@ StringValue CopyStringValue(const StringValue& value,
116116

117117
BytesValue CopyBytesValue(const BytesValue& value,
118118
std::string& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) {
119-
return absl::visit(
119+
return std::visit(
120120
absl::Overload(
121121
[&](absl::string_view string) -> BytesValue {
122122
if (string.data() != scratch.data()) {
@@ -381,18 +381,18 @@ absl::Status CheckMapField(const FieldDescriptor* absl_nonnull field) {
381381
} // namespace
382382

383383
bool StringValue::ConsumePrefix(absl::string_view prefix) {
384-
return absl::visit(absl::Overload(
385-
[&](absl::string_view& value) {
386-
return absl::ConsumePrefix(&value, prefix);
387-
},
388-
[&](absl::Cord& cord) {
389-
if (cord.StartsWith(prefix)) {
390-
cord.RemovePrefix(prefix.size());
391-
return true;
392-
}
393-
return false;
394-
}),
395-
AsVariant(*this));
384+
return std::visit(absl::Overload(
385+
[&](absl::string_view& value) {
386+
return absl::ConsumePrefix(&value, prefix);
387+
},
388+
[&](absl::Cord& cord) {
389+
if (cord.StartsWith(prefix)) {
390+
cord.RemovePrefix(prefix.size());
391+
return true;
392+
}
393+
return false;
394+
}),
395+
AsVariant(*this));
396396
}
397397

398398
StringValue GetStringField(const google::protobuf::Reflection* absl_nonnull reflection,
@@ -1980,14 +1980,14 @@ absl::StatusOr<Unique<google::protobuf::Message>> AdaptAny(
19801980
}
19811981
BytesValue value = reflection.GetValue(*to_unwrap, value_scratch);
19821982
Unique<google::protobuf::Message> unpacked = WrapUnique(prototype->New(arena), arena);
1983-
const bool ok = absl::visit(absl::Overload(
1984-
[&](absl::string_view string) -> bool {
1985-
return unpacked->ParseFromString(string);
1986-
},
1987-
[&](const absl::Cord& cord) -> bool {
1988-
return unpacked->ParseFromString(cord);
1989-
}),
1990-
AsVariant(value));
1983+
const bool ok = std::visit(absl::Overload(
1984+
[&](absl::string_view string) -> bool {
1985+
return unpacked->ParseFromString(string);
1986+
},
1987+
[&](const absl::Cord& cord) -> bool {
1988+
return unpacked->ParseFromString(cord);
1989+
}),
1990+
AsVariant(value));
19911991
if (!ok) {
19921992
return absl::InvalidArgumentError(absl::StrCat(
19931993
"failed to unpack protocol buffer message: ", type_url_view));

parser/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ cc_library(
6767
"@com_google_absl//absl/strings:str_format",
6868
"@com_google_absl//absl/types:optional",
6969
"@com_google_absl//absl/types:span",
70-
"@com_google_absl//absl/types:variant",
7170
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
7271
],
7372
)

parser/parser.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string>
2929
#include <tuple>
3030
#include <utility>
31+
#include <variant>
3132
#include <vector>
3233

3334
#include "cel/expr/syntax.pb.h"
@@ -51,7 +52,6 @@
5152
#include "absl/strings/string_view.h"
5253
#include "absl/types/optional.h"
5354
#include "absl/types/span.h"
54-
#include "absl/types/variant.h"
5555
#include "antlr4-runtime.h"
5656
#include "common/ast.h"
5757
#include "common/ast/expr_proto.h"
@@ -257,7 +257,7 @@ class ParserMacroExprFactory final : public MacroExprFactory {
257257
if (auto it = macro_calls_.find(expr.id()); it != macro_calls_.end()) {
258258
return NewUnspecified(expr.id());
259259
}
260-
return absl::visit(
260+
return std::visit(
261261
absl::Overload(
262262
[this, &expr](const UnspecifiedExpr&) -> Expr {
263263
return NewUnspecified(expr.id());

0 commit comments

Comments
 (0)