internal/fileinit: prevent map entry descriptors from implementing MessageType
The protobuf type system hacks the representation of map entries into that
of a pseudo-message descriptor.
Previously, we made all message descriptors implement MessageType
where type descriptors had a GoType method that simply returned nil.
Unfortunately, this violates a nice property in the Go type system
where being able to assert to a MessageType guarantees that Go type
information is truly associated with that descriptor.
This CL makes it such that message descriptors for map entries
do not implement MessageType.
Change-Id: I23873cb71fe0ab3c0befd8052830ea6e53c97ca9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168399
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/fileinit/fileinit_test.go b/internal/fileinit/fileinit_test.go
index f5a9923..e900168 100644
--- a/internal/fileinit/fileinit_test.go
+++ b/internal/fileinit/fileinit_test.go
@@ -68,6 +68,15 @@
}
}
+ // Verify that message descriptors for map entries have no Go type info.
+ mapEntryName := protoreflect.FullName("goproto.proto.test.TestAllTypes.MapInt32Int32Entry")
+ d := testpb.File_test_test_proto.DescriptorByName(mapEntryName)
+ if _, ok := d.(protoreflect.MessageDescriptor); !ok {
+ t.Errorf("message descriptor for %v not found", mapEntryName)
+ }
+ if _, ok := d.(protoreflect.MessageType); ok {
+ t.Errorf("message descriptor for %v must not implement protoreflect.MessageType", mapEntryName)
+ }
}
// visitFields calls f for every field set in m and its children.