Skip to content

Commit 66cff0e

Browse files
Fix handling of map fields inheriting DELIMITED
1 parent 3ecce91 commit 66cff0e

File tree

6 files changed

+471
-156
lines changed

6 files changed

+471
-156
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ regenerate-fuzz-protos: build ${PROTOC_GEN_SWIFT}
353353
SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS= \
354354
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test.proto \
355355
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_test2.proto \
356+
Protos/SwiftProtobufPluginLibraryTests/pluginlib_descriptor_delimited.proto \
356357
Protos/SwiftProtobufPluginLibraryTests/google/protobuf/unittest_delimited.proto \
357358
Protos/SwiftProtobufPluginLibraryTests/google/protobuf/unittest_delimited_import.proto \
358359
Protos/SwiftProtobufPluginLibrary/google/protobuf/compiler/plugin.proto \
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Protos/pluginlib_descriptor_test.proto - test proto
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
// -----------------------------------------------------------------------------
12+
///
13+
/// Test proto for Tests/SwiftProtobufPluginLibraryTests/Test_Descriptor.swift
14+
///
15+
// -----------------------------------------------------------------------------
16+
17+
edition = "2023";
18+
19+
package swift_descriptor_test;
20+
21+
option features.message_encoding = DELIMITED;
22+
23+
message EditionsMessageForDelimited {
24+
int32 scalar_field = 1;
25+
map<int32, string> map_field = 2;
26+
map<int32, EditionsMessageForDelimited> message_map_field = 3;
27+
EditionsMessageForDelimited delimited_field = 4;
28+
EditionsMessageForDelimited length_prefixed_field = 5 [
29+
features.message_encoding = LENGTH_PREFIXED
30+
];
31+
}

Sources/SwiftProtobufPluginLibrary/Descriptor.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,9 @@ public final class FieldDescriptor {
791791
static let kLastReservedNumber: Int = 19999
792792

793793
/// Declared type of this field.
794-
public let type: Google_Protobuf_FieldDescriptorProto.TypeEnum
794+
public var type: Google_Protobuf_FieldDescriptorProto.TypeEnum { return _type }
795+
private var _type: Google_Protobuf_FieldDescriptorProto.TypeEnum
796+
795797
/// optional/required/repeated
796798
public let label: Google_Protobuf_FieldDescriptorProto.Label
797799

@@ -1001,9 +1003,9 @@ public final class FieldDescriptor {
10011003
// help ensure basic transforms from .proto2 to .edition2023 generate the
10021004
// same code/behaviors.
10031005
if proto.type == .message && self.features.messageEncoding == .delimited {
1004-
self.type = .group
1006+
self._type = .group
10051007
} else {
1006-
self.type = proto.type
1008+
self._type = proto.type
10071009
}
10081010
// This remapping is based follow part of what upstream
10091011
// `DescriptorBuilder::PostProcessFieldFeatures()` does. If generators use
@@ -1046,7 +1048,14 @@ public final class FieldDescriptor {
10461048
if type == .enum {
10471049
_fieldTypeStorage = .enum(UnownedBox(value: registry.enumDescriptor(named: typeName)!))
10481050
} else {
1049-
_fieldTypeStorage = .message(UnownedBox(value: registry.descriptor(named: typeName)!))
1051+
let msgtype = registry.descriptor(named: typeName)!
1052+
_fieldTypeStorage = .message(UnownedBox(value: msgtype))
1053+
if type == .group && (
1054+
msgtype.options.mapEntry ||
1055+
(_containingType != nil && _containingType!.options.mapEntry)
1056+
) {
1057+
_type = .message
1058+
}
10501059
}
10511060
}
10521061
}

0 commit comments

Comments
 (0)