internal/impl: assume legacy Marshal method supports deterministic

The v1 implementation calls Marshal methods when deterministic
serialization is requested, even though it has no way to verify that the
method supports determinism. Preserve this behavior.

Change-Id: I383f2ec4bd4d5b996d96d604e92dfa43cb6f1bdc
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/205719
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/impl/legacy_message.go b/internal/impl/legacy_message.go
index 938c44c..dbc99ed 100644
--- a/internal/impl/legacy_message.go
+++ b/internal/impl/legacy_message.go
@@ -52,6 +52,12 @@
 	if _, ok := v.(legacyMarshaler); ok {
 		mi.methods.MarshalAppend = legacyMarshalAppend
 		mi.methods.Size = legacySize
+
+		// We have no way to tell whether the type's Marshal method
+		// supports deterministic serialization or not, but this
+		// preserves the v1 implementation's behavior of always
+		// calling Marshal methods when present.
+		mi.methods.Flags |= piface.SupportMarshalDeterministic
 	}
 	if _, ok := v.(legacyUnmarshaler); ok {
 		mi.methods.Unmarshal = legacyUnmarshal
@@ -331,6 +337,12 @@
 	Size:          legacySize,
 	MarshalAppend: legacyMarshalAppend,
 	Unmarshal:     legacyUnmarshal,
+
+	// We have no way to tell whether the type's Marshal method
+	// supports deterministic serialization or not, but this
+	// preserves the v1 implementation's behavior of always
+	// calling Marshal methods when present.
+	Flags: piface.SupportMarshalDeterministic,
 }
 
 func legacySize(m protoreflect.Message, opts piface.MarshalOptions) int {
diff --git a/proto/methods_test.go b/proto/methods_test.go
index 9b8535b..cc5dfce 100644
--- a/proto/methods_test.go
+++ b/proto/methods_test.go
@@ -58,6 +58,13 @@
 		if err != test.err || !bytes.Equal(b, want) {
 			t.Errorf("MarshalAppend(%v, %v) = %v, %v; want %v, %v", prefix, test, b, err, test.bytes, test.err)
 		}
+
+		b, err = proto.MarshalOptions{
+			Deterministic: true,
+		}.MarshalAppend(nil, m)
+		if err != test.err || !bytes.Equal(b, test.bytes) {
+			t.Errorf("MarshalOptions{Deterministic:true}.MarshalAppend(nil, %v) = %v, %v; want %v, %v", test, b, err, test.bytes, test.err)
+		}
 	}
 }