internal/impl: support legacy extension fields

Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.

Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.

Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go

In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.

In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.

In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.

In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.

Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/internal/value/convert.go b/internal/value/convert.go
index e4692a7..ea29244 100644
--- a/internal/value/convert.go
+++ b/internal/value/convert.go
@@ -53,17 +53,23 @@
 // protoc-gen-go historically generated to be able to automatically wrap some
 // v1 messages generated by other forks of protoc-gen-go.
 func NewConverter(t reflect.Type, k pref.Kind) Converter {
-	return NewLegacyConverter(t, k, nil, nil)
+	return NewLegacyConverter(t, k, nil, nil, nil)
 }
 
+// Legacy enums and messages do not self-report their own protoreflect types.
+// Thus, the caller needs to provide functions for retrieving those when
+// a v1 enum or message is encountered.
+type (
+	enumTypeOf     = func(reflect.Type) pref.EnumType
+	messageTypeOf  = func(reflect.Type) pref.MessageType
+	messageValueOf = func(reflect.Value) pref.ProtoMessage
+)
+
 // NewLegacyConverter is identical to NewConverter,
 // but supports wrapping legacy v1 messages to implement the v2 message API
-// using the provided wrapEnum and wrapMessage functions.
+// using the provided enumTypeOf, messageTypeOf and messageValueOf functions.
 // The wrapped message must implement Unwrapper.
-func NewLegacyConverter(t reflect.Type, k pref.Kind, wrapEnum func(reflect.Value) pref.ProtoEnum, wrapMessage func(reflect.Value) pref.ProtoMessage) Converter {
-	if (wrapEnum == nil) != (wrapMessage == nil) {
-		panic("legacy enum and message wrappers must both be populated or nil")
-	}
+func NewLegacyConverter(t reflect.Type, k pref.Kind, etOf enumTypeOf, mtOf messageTypeOf, mvOf messageValueOf) Converter {
 	switch k {
 	case pref.BoolKind:
 		if t.Kind() == reflect.Bool {
@@ -125,8 +131,8 @@
 		}
 
 		// Handle v1 enums, which we identify as simply a named int32 type.
-		if wrapEnum != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
-			et := wrapEnum(reflect.Zero(t)).ProtoReflect().Type()
+		if etOf != nil && t.PkgPath() != "" && t.Kind() == reflect.Int32 {
+			et := etOf(t)
 			return Converter{
 				PBValueOf: func(v reflect.Value) pref.Value {
 					if v.Type() != t {
@@ -164,14 +170,14 @@
 		}
 
 		// Handle v1 messages, which we need to wrap as a v2 message.
-		if wrapMessage != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
-			mt := wrapMessage(reflect.New(t.Elem())).ProtoReflect().Type()
+		if mtOf != nil && t.Kind() == reflect.Ptr && t.Implements(messageIfaceV1) {
+			mt := mtOf(t)
 			return Converter{
 				PBValueOf: func(v reflect.Value) pref.Value {
 					if v.Type() != t {
 						panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
 					}
-					return pref.ValueOf(wrapMessage(v).ProtoReflect())
+					return pref.ValueOf(mvOf(v).ProtoReflect())
 				},
 				GoValueOf: func(v pref.Value) reflect.Value {
 					rv := reflect.ValueOf(v.Message().(Unwrapper).Unwrap())
diff --git a/internal/value/map.go b/internal/value/map.go
index c051074..d2ef659 100644
--- a/internal/value/map.go
+++ b/internal/value/map.go
@@ -77,7 +77,7 @@
 	}
 }
 func (ms mapReflect) Unwrap() interface{} {
-	return ms.v.Interface()
+	return ms.v.Addr().Interface()
 }
 func (ms mapReflect) ProtoMutable() {}
 
diff --git a/internal/value/vector.go b/internal/value/vector.go
index 664ece2..d6efd99 100644
--- a/internal/value/vector.go
+++ b/internal/value/vector.go
@@ -53,7 +53,7 @@
 	vs.v.Set(vs.v.Slice(0, i))
 }
 func (vs vectorReflect) Unwrap() interface{} {
-	return vs.v.Interface()
+	return vs.v.Addr().Interface()
 }
 func (vs vectorReflect) ProtoMutable() {}