From 6c7c5a5d2a6befdc4117242671612146efa50679 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 12 Dec 2023 14:03:01 -0800 Subject: [PATCH] [ObjC] Enable editions conformance tests. PiperOrigin-RevId: 590328722 --- conformance/BUILD.bazel | 2 ++ conformance/conformance_objc.m | 29 +++++++++++-------- objectivec/BUILD.bazel | 1 + protobuf.bzl | 14 +++++++-- src/google/protobuf/editions/BUILD | 22 ++++++++++++++ .../test_messages_proto2_editions.proto | 2 +- .../test_messages_proto3_editions.proto | 2 +- 7 files changed, 55 insertions(+), 17 deletions(-) diff --git a/conformance/BUILD.bazel b/conformance/BUILD.bazel index 99827fc59f..1d9ea1585c 100644 --- a/conformance/BUILD.bazel +++ b/conformance/BUILD.bazel @@ -328,6 +328,8 @@ objc_library( ":conformance_objc_proto", "//:test_messages_proto2_objc_proto", "//:test_messages_proto3_objc_proto", + "//src/google/protobuf/editions:test_messages_proto2_editions_objc_proto", + "//src/google/protobuf/editions:test_messages_proto3_editions_objc_proto", ], ) diff --git a/conformance/conformance_objc.m b/conformance/conformance_objc.m index 5060ce9ecf..8a37b72bec 100644 --- a/conformance/conformance_objc.m +++ b/conformance/conformance_objc.m @@ -10,6 +10,8 @@ #import "Conformance.pbobjc.h" #import "google/protobuf/TestMessagesProto2.pbobjc.h" #import "google/protobuf/TestMessagesProto3.pbobjc.h" +#import "google/protobuf/editions/golden/TestMessagesProto2Editions.pbobjc.h" +#import "google/protobuf/editions/golden/TestMessagesProto3Editions.pbobjc.h" static void Die(NSString *format, ...) __dead2; @@ -49,22 +51,25 @@ static ConformanceResponse *DoTest(ConformanceRequest *request) { break; case ConformanceRequest_Payload_OneOfCase_ProtobufPayload: { - Class msgClass = nil; - if ([request.messageType isEqual:@"protobuf_test_messages.proto3.TestAllTypesProto3"]) { - msgClass = [Proto3TestAllTypesProto3 class]; - } else if ([request.messageType - isEqual:@"protobuf_test_messages.proto2.TestAllTypesProto2"]) { - msgClass = [Proto2TestAllTypesProto2 class]; + NSDictionary *mappings = @{ + @"protobuf_test_messages.proto2.TestAllTypesProto2" : [Proto2TestAllTypesProto2 class], + @"protobuf_test_messages.proto3.TestAllTypesProto3" : [Proto3TestAllTypesProto3 class], + @"protobuf_test_messages.editions.proto2.TestAllTypesProto2" : + [EditionsProto2TestAllTypesProto2 class], + @"protobuf_test_messages.editions.proto3.TestAllTypesProto3" : + [EditionsProto3TestAllTypesProto3 class], + }; + Class msgClass = mappings[request.messageType]; + if (msgClass) { + NSError *error = nil; + testMessage = [msgClass parseFromData:request.protobufPayload error:&error]; + if (!testMessage) { + response.parseError = [NSString stringWithFormat:@"Parse error: %@", error]; + } } else { response.runtimeError = [NSString stringWithFormat:@"Protobuf request had an unknown message_type: %@", request.messageType]; - break; - } - NSError *error = nil; - testMessage = [msgClass parseFromData:request.protobufPayload error:&error]; - if (!testMessage) { - response.parseError = [NSString stringWithFormat:@"Parse error: %@", error]; } break; } diff --git a/objectivec/BUILD.bazel b/objectivec/BUILD.bazel index cb0d7c8034..d9e18ee1a5 100644 --- a/objectivec/BUILD.bazel +++ b/objectivec/BUILD.bazel @@ -160,6 +160,7 @@ objc_library( conformance_test( name = "conformance_test", failure_list = "//conformance:failure_list_objc.txt", + maximum_edition = "2023", target_compatible_with = ["@platforms//os:macos"], testee = "//conformance:conformance_objc", ) diff --git a/protobuf.bzl b/protobuf.bzl index 8fe845ab2d..3c8afcc17f 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -80,6 +80,7 @@ def _proto_gen_impl(ctx): srcs = ctx.files.srcs langs = ctx.attr.langs or [] out_type = ctx.attr.out_type + enable_editions = ctx.attr.enable_editions deps = depset(direct = ctx.files.srcs) source_dir = _SourceDir(ctx) gen_dir = _GenDir(ctx).rstrip("/") @@ -130,6 +131,8 @@ def _proto_gen_impl(ctx): generated_files = [] for src in srcs: args = [] + if enable_editions: + args.append("--experimental_editions") in_gen_dir = src.root.path == gen_dir if in_gen_dir: @@ -247,6 +250,7 @@ _proto_gen = rule( attrs = { "srcs": attr.label_list(allow_files = True), "deps": attr.label_list(providers = [ProtoGenInfo]), + "enable_editions": attr.bool(), "includes": attr.string_list(), "protoc": attr.label( cfg = "exec", @@ -406,11 +410,11 @@ def internal_objc_proto_library( includes = ["."], default_runtime = Label("//:protobuf_objc"), protoc = Label("//:protoc"), + enable_editions = False, testonly = None, visibility = ["//visibility:public"], **kwargs): - """Bazel rule to create a Objective-C protobuf library from proto source - files + """Bazel rule to create a Objective-C protobuf library from proto sources NOTE: the rule is only an internal workaround to generate protos. The interface may change and the rule may be removed when bazel has introduced @@ -423,9 +427,10 @@ def internal_objc_proto_library( outs: a list of expected output files. proto_deps: a list of proto file dependencies that don't have a objc_proto_library rule. - include: a string indicating the include path of the .proto files. + includes: a string indicating the include path of the .proto files. default_runtime: the Objective-C Protobuf runtime protoc: the label of the protocol compiler to generate the sources. + enable_editions: if editions should be enabled while invoking the compiler. testonly: common rule attribute (see: https://bazel.build/reference/be/common-definitions#common-attributes) visibility: the visibility of the generated files. @@ -440,6 +445,7 @@ def internal_objc_proto_library( testonly = testonly, srcs = proto_deps, protoc = protoc, + enable_editions = enable_editions, includes = includes, ) full_deps.append(":%s_deps_genproto" % name) @@ -454,6 +460,7 @@ def internal_objc_proto_library( out_type = "hdrs", includes = includes, protoc = protoc, + enable_editions = enable_editions, testonly = testonly, visibility = visibility, tags = ["manual"], @@ -467,6 +474,7 @@ def internal_objc_proto_library( out_type = "srcs", includes = includes, protoc = protoc, + enable_editions = enable_editions, testonly = testonly, visibility = visibility, tags = ["manual"], diff --git a/src/google/protobuf/editions/BUILD b/src/google/protobuf/editions/BUILD index e22c9acf53..96fc9b1385 100644 --- a/src/google/protobuf/editions/BUILD +++ b/src/google/protobuf/editions/BUILD @@ -3,6 +3,7 @@ load("//bazel:upb_proto_library.bzl", "upb_c_proto_library", "upb_proto_reflecti load("@rules_cc//cc:defs.bzl", "cc_proto_library") load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load(":defaults.bzl", "compile_edition_defaults", "embed_edition_defaults") +load("//:protobuf.bzl", "internal_objc_proto_library") bzl_library( name = "defaults", @@ -100,6 +101,14 @@ cc_proto_library( deps = [":test_messages_proto2_editions_proto"], ) +internal_objc_proto_library( + name = "test_messages_proto2_editions_objc_proto", + testonly = True, + srcs = ["golden/test_messages_proto2_editions.proto"], + enable_editions = True, + visibility = ["//conformance:__pkg__"], +) + py_proto_library( name = "test_messages_proto2_editions_py_pb2", testonly = True, @@ -142,6 +151,19 @@ cc_proto_library( deps = [":test_messages_proto3_editions_proto"], ) +internal_objc_proto_library( + name = "test_messages_proto3_editions_objc_proto", + testonly = True, + srcs = ["golden/test_messages_proto3_editions.proto"], + enable_editions = True, + includes = [ + ".", + "src", + ], + proto_deps = ["//:well_known_type_protos"], + visibility = ["//conformance:__pkg__"], +) + py_proto_library( name = "test_messages_proto3_editions_py_pb2", testonly = True, diff --git a/src/google/protobuf/editions/golden/test_messages_proto2_editions.proto b/src/google/protobuf/editions/golden/test_messages_proto2_editions.proto index 225e85dc28..9f4f198318 100644 --- a/src/google/protobuf/editions/golden/test_messages_proto2_editions.proto +++ b/src/google/protobuf/editions/golden/test_messages_proto2_editions.proto @@ -19,7 +19,7 @@ option features.enum_type = CLOSED; option features.repeated_field_encoding = EXPANDED; option features.utf8_validation = NONE; option java_package = "com.google.protobuf_test_messages.editions.proto2"; -option objc_class_prefix = "Proto2"; +option objc_class_prefix = "EditionsProto2"; // This is the default, but we specify it here explicitly. option optimize_for = SPEED; diff --git a/src/google/protobuf/editions/golden/test_messages_proto3_editions.proto b/src/google/protobuf/editions/golden/test_messages_proto3_editions.proto index 9dbf63e6a5..c092ea6c8d 100644 --- a/src/google/protobuf/editions/golden/test_messages_proto3_editions.proto +++ b/src/google/protobuf/editions/golden/test_messages_proto3_editions.proto @@ -24,7 +24,7 @@ import "google/protobuf/wrappers.proto"; option features.field_presence = IMPLICIT; option java_package = "com.google.protobuf_test_messages.editions.proto3"; -option objc_class_prefix = "Proto3"; +option objc_class_prefix = "EditionsProto3"; // This is the default, but we specify it here explicitly. option optimize_for = SPEED;