internal/impl: support non-struct-pointer legacy message types

Support, to some limited degree, types which implement protoV1.Message
but which are not struct pointers. Our ability to work with these types
is largely limited to calling Marshal or Unmarshal methods, when
present.

Change-Id: Ie1b851d9e753e2b2cb189b17ffeefebe2d8b3a8f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/198237
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/impl/legacy_message.go b/internal/impl/legacy_message.go
index 703e865..938c44c 100644
--- a/internal/impl/legacy_message.go
+++ b/internal/impl/legacy_message.go
@@ -12,6 +12,7 @@
 
 	"google.golang.org/protobuf/internal/descopts"
 	ptag "google.golang.org/protobuf/internal/encoding/tag"
+	"google.golang.org/protobuf/internal/errors"
 	"google.golang.org/protobuf/internal/filedesc"
 	"google.golang.org/protobuf/internal/strs"
 	"google.golang.org/protobuf/reflect/protoreflect"
@@ -22,7 +23,11 @@
 // legacyWrapMessage wraps v as a protoreflect.ProtoMessage,
 // where v must be a *struct kind and not implement the v2 API already.
 func legacyWrapMessage(v reflect.Value) pref.ProtoMessage {
-	mt := legacyLoadMessageInfo(v.Type(), "")
+	typ := v.Type()
+	if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct {
+		return aberrantMessage{v: v}
+	}
+	mt := legacyLoadMessageInfo(typ, "")
 	return mt.MessageOf(v.Interface()).Interface()
 }
 
@@ -44,30 +49,12 @@
 	}
 
 	v := reflect.Zero(t).Interface()
-	type marshaler interface {
-		Marshal() ([]byte, error)
+	if _, ok := v.(legacyMarshaler); ok {
+		mi.methods.MarshalAppend = legacyMarshalAppend
+		mi.methods.Size = legacySize
 	}
-	if _, ok := v.(marshaler); ok {
-		mi.methods.MarshalAppend = func(b []byte, m pref.Message, _ piface.MarshalOptions) ([]byte, error) {
-			out, err := m.Interface().(unwrapper).protoUnwrap().(marshaler).Marshal()
-			if b != nil {
-				out = append(b, out...)
-			}
-			return out, err
-		}
-		mi.methods.Size = func(m pref.Message, _ piface.MarshalOptions) int {
-			// This is not at all efficient.
-			b, _ := m.Interface().(unwrapper).protoUnwrap().(marshaler).Marshal()
-			return len(b)
-		}
-	}
-	type unmarshaler interface {
-		Unmarshal([]byte) error
-	}
-	if _, ok := v.(unmarshaler); ok {
-		mi.methods.Unmarshal = func(b []byte, m pref.Message, _ piface.UnmarshalOptions) error {
-			return m.Interface().(unwrapper).protoUnwrap().(unmarshaler).Unmarshal(b)
-		}
+	if _, ok := v.(legacyUnmarshaler); ok {
+		mi.methods.Unmarshal = legacyUnmarshal
 	}
 
 	if mi, ok := legacyMessageTypeCache.LoadOrStore(t, mi); ok {
@@ -92,7 +79,7 @@
 	}
 
 	// Slow-path: initialize MessageDescriptor from the raw descriptor.
-	mv := reflect.New(t.Elem()).Interface()
+	mv := reflect.Zero(t).Interface()
 	if _, ok := mv.(pref.ProtoMessage); ok {
 		panic(fmt.Sprintf("%v already implements proto.Message", t))
 	}
@@ -120,7 +107,7 @@
 	aberrantMessageDescCache map[reflect.Type]protoreflect.MessageDescriptor
 )
 
-// aberrantLoadMessageDesc returns an EnumDescriptor derived from the Go type,
+// aberrantLoadMessageDesc returns an MessageDescriptor derived from the Go type,
 // which must not implement protoreflect.ProtoMessage or messageV1.
 //
 // This is a best-effort derivation of the message descriptor using the protobuf
@@ -143,10 +130,14 @@
 	// Cache the MessageDescriptor early on so that we can resolve internal
 	// cyclic references.
 	md := &filedesc.Message{L2: new(filedesc.MessageL2)}
-	md.L0.FullName = aberrantDeriveMessageName(t.Elem(), name)
+	md.L0.FullName = aberrantDeriveMessageName(t, name)
 	md.L0.ParentFile = filedesc.SurrogateProto2
 	aberrantMessageDescCache[t] = md
 
+	if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct {
+		return md
+	}
+
 	// Try to determine if the message is using proto3 by checking scalars.
 	for i := 0; i < t.Elem().NumField(); i++ {
 		f := t.Elem().Field(i)
@@ -222,8 +213,6 @@
 		}
 	}
 
-	// TODO: Use custom Marshal/Unmarshal methods for the fast-path?
-
 	return md
 }
 
@@ -233,13 +222,16 @@
 	}
 	func() {
 		defer func() { recover() }() // swallow possible nil panics
-		if m, ok := reflect.New(t).Interface().(interface{ XXX_MessageName() string }); ok {
+		if m, ok := reflect.Zero(t).Interface().(interface{ XXX_MessageName() string }); ok {
 			name = pref.FullName(m.XXX_MessageName())
 		}
 	}()
 	if name.IsValid() {
 		return name
 	}
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
 	return aberrantDeriveFullName(t)
 }
 
@@ -324,3 +316,125 @@
 func (placeholderEnumValues) ByNumber(n pref.EnumNumber) pref.EnumValueDescriptor {
 	return filedesc.PlaceholderEnumValue(pref.FullName(fmt.Sprintf("UNKNOWN_%d", n)))
 }
+
+// legacyMarshaler is the proto.Marshaler interface superseded by protoiface.Methoder.
+type legacyMarshaler interface {
+	Marshal() ([]byte, error)
+}
+
+// legacyUnmarshaler is the proto.Unmarshaler interface superseded by protoiface.Methoder.
+type legacyUnmarshaler interface {
+	Unmarshal([]byte) error
+}
+
+var legacyProtoMethods = &piface.Methods{
+	Size:          legacySize,
+	MarshalAppend: legacyMarshalAppend,
+	Unmarshal:     legacyUnmarshal,
+}
+
+func legacySize(m protoreflect.Message, opts piface.MarshalOptions) int {
+	b, _ := legacyMarshalAppend(nil, m, opts)
+	return len(b)
+}
+
+func legacyMarshalAppend(b []byte, m protoreflect.Message, opts piface.MarshalOptions) ([]byte, error) {
+	v := m.(unwrapper).protoUnwrap()
+	marshaler, ok := v.(legacyMarshaler)
+	if !ok {
+		return nil, errors.New("%T does not implement Marshal", v)
+	}
+	out, err := marshaler.Marshal()
+	if b != nil {
+		out = append(b, out...)
+	}
+	return out, err
+}
+
+func legacyUnmarshal(b []byte, m protoreflect.Message, opts piface.UnmarshalOptions) error {
+	v := m.(unwrapper).protoUnwrap()
+	unmarshaler, ok := v.(legacyUnmarshaler)
+	if !ok {
+		return errors.New("%T does not implement Marshal", v)
+	}
+	return unmarshaler.Unmarshal(b)
+}
+
+// aberrantMessageType implements MessageType for all types other than pointer-to-struct.
+type aberrantMessageType struct {
+	t reflect.Type
+}
+
+func (mt aberrantMessageType) New() pref.Message {
+	return aberrantMessage{reflect.Zero(mt.t)}
+}
+func (mt aberrantMessageType) Zero() pref.Message {
+	return aberrantMessage{reflect.Zero(mt.t)}
+}
+func (mt aberrantMessageType) GoType() reflect.Type {
+	return mt.t
+}
+func (mt aberrantMessageType) Descriptor() pref.MessageDescriptor {
+	return LegacyLoadMessageDesc(mt.t)
+}
+
+// aberrantMessage implements Message for all types other than pointer-to-struct.
+//
+// When the underlying type implements legacyMarshaler or legacyUnmarshaler,
+// the aberrant Message can be marshaled or unmarshaled. Otherwise, there is
+// not much that can be done with values of this type.
+type aberrantMessage struct {
+	v reflect.Value
+}
+
+func (m aberrantMessage) ProtoReflect() pref.Message {
+	return m
+}
+
+func (m aberrantMessage) Descriptor() pref.MessageDescriptor {
+	return LegacyLoadMessageDesc(m.v.Type())
+}
+func (m aberrantMessage) Type() pref.MessageType {
+	return aberrantMessageType{m.v.Type()}
+}
+func (m aberrantMessage) New() pref.Message {
+	return aberrantMessage{reflect.Zero(m.v.Type())}
+}
+func (m aberrantMessage) Interface() pref.ProtoMessage {
+	return m
+}
+func (m aberrantMessage) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
+}
+func (m aberrantMessage) Has(pref.FieldDescriptor) bool {
+	panic("invalid field descriptor")
+}
+func (m aberrantMessage) Clear(pref.FieldDescriptor) {
+	panic("invalid field descriptor")
+}
+func (m aberrantMessage) Get(pref.FieldDescriptor) pref.Value {
+	panic("invalid field descriptor")
+}
+func (m aberrantMessage) Set(pref.FieldDescriptor, pref.Value) {
+	panic("invalid field descriptor")
+}
+func (m aberrantMessage) Mutable(pref.FieldDescriptor) pref.Value {
+	panic("invalid field descriptor")
+}
+func (m aberrantMessage) NewField(pref.FieldDescriptor) pref.Value {
+	panic("invalid field descriptor")
+}
+func (m aberrantMessage) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor {
+	panic("invalid oneof descriptor")
+}
+func (m aberrantMessage) GetUnknown() pref.RawFields {
+	return nil
+}
+func (m aberrantMessage) SetUnknown(pref.RawFields) {
+	// SetUnknown discards its input on messages which don't support unknown field storage.
+}
+func (m aberrantMessage) ProtoMethods() *piface.Methods {
+	return legacyProtoMethods
+}
+func (m aberrantMessage) protoUnwrap() interface{} {
+	return m.v.Interface()
+}
diff --git a/internal/testprotos/irregular/irregular.go b/internal/testprotos/irregular/irregular.go
index be89d01..b328edb 100644
--- a/internal/testprotos/irregular/irregular.go
+++ b/internal/testprotos/irregular/irregular.go
@@ -116,3 +116,11 @@
     go_package: "google.golang.org/protobuf/internal/testprotos/irregular"
   }
 `
+
+type AberrantMessage int
+
+func (m AberrantMessage) ProtoMessage()            {}
+func (m AberrantMessage) Reset()                   {}
+func (m AberrantMessage) String() string           { return "" }
+func (m AberrantMessage) Marshal() ([]byte, error) { return nil, nil }
+func (m AberrantMessage) Unmarshal([]byte) error   { return nil }
diff --git a/internal/testprotos/irregular/irregular.proto b/internal/testprotos/irregular/irregular.proto
index 9d8cf52..949316b 100644
--- a/internal/testprotos/irregular/irregular.proto
+++ b/internal/testprotos/irregular/irregular.proto
@@ -14,3 +14,6 @@
   optional string s = 1;
 }
 
+// AberrantMessage is a message with an implementation with a non-struct underlying type.
+message AberrantMessage {
+}
diff --git a/internal/testprotos/irregular/test.pb.go b/internal/testprotos/irregular/test.pb.go
index df8ebf3..00e6ed4 100644
--- a/internal/testprotos/irregular/test.pb.go
+++ b/internal/testprotos/irregular/test.pb.go
@@ -29,7 +29,12 @@
 	MapMessage      map[string]*IrregularMessage `protobuf:"bytes,4,rep,name=map_message,json=mapMessage" json:"map_message,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
 	// Types that are assignable to Union:
 	//	*Message_OneofMessage
-	Union isMessage_Union `protobuf_oneof:"union"`
+	//	*Message_OneofAberrantMessage
+	Union                   isMessage_Union             `protobuf_oneof:"union"`
+	OptionalAberrantMessage *AberrantMessage            `protobuf:"bytes,7,opt,name=optional_aberrant_message,json=optionalAberrantMessage" json:"optional_aberrant_message,omitempty"`
+	RepeatedAberrantMessage []*AberrantMessage          `protobuf:"bytes,8,rep,name=repeated_aberrant_message,json=repeatedAberrantMessage" json:"repeated_aberrant_message,omitempty"`
+	RequiredAberrantMessage *AberrantMessage            `protobuf:"bytes,9,req,name=required_aberrant_message,json=requiredAberrantMessage" json:"required_aberrant_message,omitempty"`
+	MapAberrantMessage      map[string]*AberrantMessage `protobuf:"bytes,10,rep,name=map_aberrant_message,json=mapAberrantMessage" json:"map_aberrant_message,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
 }
 
 func (x *Message) Reset() {
@@ -101,6 +106,41 @@
 	return nil
 }
 
+func (x *Message) GetOneofAberrantMessage() *AberrantMessage {
+	if x, ok := x.GetUnion().(*Message_OneofAberrantMessage); ok {
+		return x.OneofAberrantMessage
+	}
+	return nil
+}
+
+func (x *Message) GetOptionalAberrantMessage() *AberrantMessage {
+	if x != nil {
+		return x.OptionalAberrantMessage
+	}
+	return nil
+}
+
+func (x *Message) GetRepeatedAberrantMessage() []*AberrantMessage {
+	if x != nil {
+		return x.RepeatedAberrantMessage
+	}
+	return nil
+}
+
+func (x *Message) GetRequiredAberrantMessage() *AberrantMessage {
+	if x != nil {
+		return x.RequiredAberrantMessage
+	}
+	return nil
+}
+
+func (x *Message) GetMapAberrantMessage() map[string]*AberrantMessage {
+	if x != nil {
+		return x.MapAberrantMessage
+	}
+	return nil
+}
+
 type isMessage_Union interface {
 	isMessage_Union()
 }
@@ -109,8 +149,14 @@
 	OneofMessage *IrregularMessage `protobuf:"bytes,5,opt,name=oneof_message,json=oneofMessage,oneof"`
 }
 
+type Message_OneofAberrantMessage struct {
+	OneofAberrantMessage *AberrantMessage `protobuf:"bytes,6,opt,name=oneof_aberrant_message,json=oneofAberrantMessage,oneof"`
+}
+
 func (*Message_OneofMessage) isMessage_Union() {}
 
+func (*Message_OneofAberrantMessage) isMessage_Union() {}
+
 var File_irregular_test_proto protoreflect.FileDescriptor
 
 var file_irregular_test_proto_rawDesc = []byte{
@@ -118,7 +164,7 @@
 	0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x17, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e,
 	0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x1a,
 	0x19, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x2f, 0x69, 0x72, 0x72, 0x65, 0x67,
-	0x75, 0x6c, 0x61, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xa3, 0x04, 0x0a, 0x07, 0x4d,
+	0x75, 0x6c, 0x61, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x94, 0x09, 0x0a, 0x07, 0x4d,
 	0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x54, 0x0a, 0x10, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e,
 	0x61, 0x6c, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b,
 	0x32, 0x29, 0x2e, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
@@ -145,18 +191,57 @@
 	0x28, 0x0b, 0x32, 0x29, 0x2e, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f,
 	0x74, 0x6f, 0x2e, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x2e, 0x49, 0x72, 0x72,
 	0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x48, 0x00, 0x52,
-	0x0c, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x68, 0x0a,
+	0x0c, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x60, 0x0a,
+	0x16, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x5f, 0x61, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x5f,
+	0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x28, 0x2e,
+	0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x69, 0x72,
+	0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x2e, 0x41, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74,
+	0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x48, 0x00, 0x52, 0x14, 0x6f, 0x6e, 0x65, 0x6f, 0x66,
+	0x41, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12,
+	0x64, 0x0a, 0x19, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x5f, 0x61, 0x62, 0x65, 0x72,
+	0x72, 0x61, 0x6e, 0x74, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x07, 0x20, 0x01,
+	0x28, 0x0b, 0x32, 0x28, 0x2e, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f,
+	0x74, 0x6f, 0x2e, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x2e, 0x41, 0x62, 0x65,
+	0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x17, 0x6f, 0x70,
+	0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x41, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65,
+	0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x64, 0x0a, 0x19, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65,
+	0x64, 0x5f, 0x61, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61,
+	0x67, 0x65, 0x18, 0x08, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x28, 0x2e, 0x67, 0x6f, 0x70, 0x72, 0x6f,
+	0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c,
+	0x61, 0x72, 0x2e, 0x41, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61,
+	0x67, 0x65, 0x52, 0x17, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x62, 0x65, 0x72,
+	0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x64, 0x0a, 0x19, 0x72,
+	0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x5f, 0x61, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74,
+	0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x09, 0x20, 0x02, 0x28, 0x0b, 0x32, 0x28,
+	0x2e, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x69,
+	0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x2e, 0x41, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e,
+	0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x17, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72,
+	0x65, 0x64, 0x41, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67,
+	0x65, 0x12, 0x6a, 0x0a, 0x14, 0x6d, 0x61, 0x70, 0x5f, 0x61, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e,
+	0x74, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x0b, 0x32,
+	0x38, 0x2e, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e,
+	0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67,
+	0x65, 0x2e, 0x4d, 0x61, 0x70, 0x41, 0x62, 0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73,
+	0x73, 0x61, 0x67, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x12, 0x6d, 0x61, 0x70, 0x41, 0x62,
+	0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x68, 0x0a,
 	0x0f, 0x4d, 0x61, 0x70, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x45, 0x6e, 0x74, 0x72, 0x79,
 	0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b,
 	0x65, 0x79, 0x12, 0x3f, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28,
 	0x0b, 0x32, 0x29, 0x2e, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74,
 	0x6f, 0x2e, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x2e, 0x49, 0x72, 0x72, 0x65,
 	0x67, 0x75, 0x6c, 0x61, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x05, 0x76, 0x61,
-	0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x07, 0x0a, 0x05, 0x75, 0x6e, 0x69, 0x6f, 0x6e,
-	0x42, 0x3a, 0x5a, 0x38, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x67, 0x6f, 0x6c, 0x61, 0x6e,
-	0x67, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x69,
-	0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74,
-	0x6f, 0x73, 0x2f, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72,
+	0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x1a, 0x6f, 0x0a, 0x17, 0x4d, 0x61, 0x70, 0x41, 0x62,
+	0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x45, 0x6e, 0x74,
+	0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
+	0x03, 0x6b, 0x65, 0x79, 0x12, 0x3e, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20,
+	0x01, 0x28, 0x0b, 0x32, 0x28, 0x2e, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x70, 0x72,
+	0x6f, 0x74, 0x6f, 0x2e, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x2e, 0x41, 0x62,
+	0x65, 0x72, 0x72, 0x61, 0x6e, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x05, 0x76,
+	0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x07, 0x0a, 0x05, 0x75, 0x6e, 0x69, 0x6f,
+	0x6e, 0x42, 0x3a, 0x5a, 0x38, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x67, 0x6f, 0x6c, 0x61,
+	0x6e, 0x67, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f,
+	0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f,
+	0x74, 0x6f, 0x73, 0x2f, 0x69, 0x72, 0x72, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72,
 }
 
 var (
@@ -171,24 +256,32 @@
 	return file_irregular_test_proto_rawDescData
 }
 
-var file_irregular_test_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
+var file_irregular_test_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
 var file_irregular_test_proto_goTypes = []interface{}{
 	(*Message)(nil),          // 0: goproto.proto.irregular.Message
 	nil,                      // 1: goproto.proto.irregular.Message.MapMessageEntry
-	(*IrregularMessage)(nil), // 2: goproto.proto.irregular.IrregularMessage
+	nil,                      // 2: goproto.proto.irregular.Message.MapAberrantMessageEntry
+	(*IrregularMessage)(nil), // 3: goproto.proto.irregular.IrregularMessage
+	(*AberrantMessage)(nil),  // 4: goproto.proto.irregular.AberrantMessage
 }
 var file_irregular_test_proto_depIdxs = []int32{
-	2, // 0: goproto.proto.irregular.Message.optional_message:type_name -> goproto.proto.irregular.IrregularMessage
-	2, // 1: goproto.proto.irregular.Message.repeated_message:type_name -> goproto.proto.irregular.IrregularMessage
-	2, // 2: goproto.proto.irregular.Message.required_message:type_name -> goproto.proto.irregular.IrregularMessage
-	1, // 3: goproto.proto.irregular.Message.map_message:type_name -> goproto.proto.irregular.Message.MapMessageEntry
-	2, // 4: goproto.proto.irregular.Message.oneof_message:type_name -> goproto.proto.irregular.IrregularMessage
-	2, // 5: goproto.proto.irregular.Message.MapMessageEntry.value:type_name -> goproto.proto.irregular.IrregularMessage
-	6, // [6:6] is the sub-list for method output_type
-	6, // [6:6] is the sub-list for method input_type
-	6, // [6:6] is the sub-list for extension type_name
-	6, // [6:6] is the sub-list for extension extendee
-	0, // [0:6] is the sub-list for field type_name
+	3,  // 0: goproto.proto.irregular.Message.optional_message:type_name -> goproto.proto.irregular.IrregularMessage
+	3,  // 1: goproto.proto.irregular.Message.repeated_message:type_name -> goproto.proto.irregular.IrregularMessage
+	3,  // 2: goproto.proto.irregular.Message.required_message:type_name -> goproto.proto.irregular.IrregularMessage
+	1,  // 3: goproto.proto.irregular.Message.map_message:type_name -> goproto.proto.irregular.Message.MapMessageEntry
+	3,  // 4: goproto.proto.irregular.Message.oneof_message:type_name -> goproto.proto.irregular.IrregularMessage
+	4,  // 5: goproto.proto.irregular.Message.oneof_aberrant_message:type_name -> goproto.proto.irregular.AberrantMessage
+	4,  // 6: goproto.proto.irregular.Message.optional_aberrant_message:type_name -> goproto.proto.irregular.AberrantMessage
+	4,  // 7: goproto.proto.irregular.Message.repeated_aberrant_message:type_name -> goproto.proto.irregular.AberrantMessage
+	4,  // 8: goproto.proto.irregular.Message.required_aberrant_message:type_name -> goproto.proto.irregular.AberrantMessage
+	2,  // 9: goproto.proto.irregular.Message.map_aberrant_message:type_name -> goproto.proto.irregular.Message.MapAberrantMessageEntry
+	3,  // 10: goproto.proto.irregular.Message.MapMessageEntry.value:type_name -> goproto.proto.irregular.IrregularMessage
+	4,  // 11: goproto.proto.irregular.Message.MapAberrantMessageEntry.value:type_name -> goproto.proto.irregular.AberrantMessage
+	12, // [12:12] is the sub-list for method output_type
+	12, // [12:12] is the sub-list for method input_type
+	12, // [12:12] is the sub-list for extension type_name
+	12, // [12:12] is the sub-list for extension extendee
+	0,  // [0:12] is the sub-list for field type_name
 }
 
 func init() { file_irregular_test_proto_init() }
@@ -213,6 +306,7 @@
 	}
 	file_irregular_test_proto_msgTypes[0].OneofWrappers = []interface{}{
 		(*Message_OneofMessage)(nil),
+		(*Message_OneofAberrantMessage)(nil),
 	}
 	type x struct{}
 	out := protoimpl.TypeBuilder{
@@ -220,7 +314,7 @@
 			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
 			RawDescriptor: file_irregular_test_proto_rawDesc,
 			NumEnums:      0,
-			NumMessages:   2,
+			NumMessages:   3,
 			NumExtensions: 0,
 			NumServices:   0,
 		},
diff --git a/internal/testprotos/irregular/test.proto b/internal/testprotos/irregular/test.proto
index 16ad47c..e6eb9bf 100644
--- a/internal/testprotos/irregular/test.proto
+++ b/internal/testprotos/irregular/test.proto
@@ -21,5 +21,11 @@
   map<string,IrregularMessage> map_message = 4;
   oneof union {
     IrregularMessage oneof_message = 5;
+    AberrantMessage oneof_aberrant_message = 6;
   }
+
+  optional AberrantMessage optional_aberrant_message = 7;
+  repeated AberrantMessage repeated_aberrant_message = 8;
+  required AberrantMessage required_aberrant_message = 9;
+  map<string,AberrantMessage> map_aberrant_message = 10;
 }
diff --git a/proto/methods_test.go b/proto/methods_test.go
index 436df6d..9b8535b 100644
--- a/proto/methods_test.go
+++ b/proto/methods_test.go
@@ -39,7 +39,7 @@
 }
 
 func TestLegacyMarshalMethod(t *testing.T) {
-	for _, test := range []*selfMarshaler{
+	for _, test := range []selfMarshaler{
 		{bytes: []byte("marshal")},
 		{bytes: []byte("marshal"), err: errors.New("some error")},
 	} {