Skip to content

Commit 7cb5597

Browse files
authored
Overcome the ref struct limitation for pre-roslyn compilers by introducing GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE for generated code (#7490)
* enable compatibility mode in codegen * regenerate protos * improve readability * more robust way of figuring out path to old C# compiler * add recent C# changes
1 parent c0b79c5 commit 7cb5597

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+12165
-643
lines changed

CHANGES.txt

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ Unreleased Changes
1313
* Improved the error message when AttributeError is returned from __getattr__
1414
in EnumTypeWrapper.
1515

16+
C#:
17+
* Dropped support for netstandard1.0 (replaced by support for netstandard1.1).
18+
This was required to modernize the parsing stack to use the `Span<byte>`
19+
type internally. (#7351)
20+
* Add `ParseFrom(ReadOnlySequence<byte>)` method to enable GC friendly
21+
parsing with reduced allocations and buffer copies. (#7351)
22+
* Add `GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE` define to make
23+
generated code compatible with old C# compilers (pre-roslyn compilers
24+
from .NET framework and old versions of mono) that do not support
25+
ref structs. (#7490)
26+
1627
2020-05-26 version 3.12.2 (C++/Java/Python/PHP/Objective-C/C#/Ruby/JavaScript)
1728

1829
C++

Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ csharp_EXTRA_DIST= \
127127
csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs \
128128
csharp/src/Google.Protobuf.Test/Proto3OptionalTest.cs \
129129
csharp/src/Google.Protobuf.Test/ReadOnlySequenceFactory.cs \
130+
csharp/src/Google.Protobuf.Test/RefStructCompatibilityTest.cs \
130131
csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs \
131132
csharp/src/Google.Protobuf.Test/Reflection/DescriptorDeclarationTest.cs \
132133
csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs \

csharp/src/AddressBook/Addressbook.cs

+89-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ static AddressbookReflection() {
4949
/// <summary>
5050
/// [START messages]
5151
/// </summary>
52-
public sealed partial class Person : pb::IMessage<Person>, pb::IBufferMessage {
52+
public sealed partial class Person : pb::IMessage<Person>
53+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
54+
, pb::IBufferMessage
55+
#endif
56+
{
5357
private static readonly pb::MessageParser<Person> _parser = new pb::MessageParser<Person>(() => new Person());
5458
private pb::UnknownFieldSet _unknownFields;
5559
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
@@ -256,9 +260,44 @@ public void MergeFrom(Person other) {
256260

257261
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
258262
public void MergeFrom(pb::CodedInputStream input) {
263+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
259264
input.ReadRawMessage(this);
265+
#else
266+
uint tag;
267+
while ((tag = input.ReadTag()) != 0) {
268+
switch(tag) {
269+
default:
270+
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
271+
break;
272+
case 10: {
273+
Name = input.ReadString();
274+
break;
275+
}
276+
case 16: {
277+
Id = input.ReadInt32();
278+
break;
279+
}
280+
case 26: {
281+
Email = input.ReadString();
282+
break;
283+
}
284+
case 34: {
285+
phones_.AddEntriesFrom(input, _repeated_phones_codec);
286+
break;
287+
}
288+
case 42: {
289+
if (lastUpdated_ == null) {
290+
LastUpdated = new global::Google.Protobuf.WellKnownTypes.Timestamp();
291+
}
292+
input.ReadMessage(LastUpdated);
293+
break;
294+
}
295+
}
296+
}
297+
#endif
260298
}
261299

300+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
262301
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
263302
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
264303
uint tag;
@@ -293,6 +332,7 @@ public void MergeFrom(pb::CodedInputStream input) {
293332
}
294333
}
295334
}
335+
#endif
296336

297337
#region Nested types
298338
/// <summary>Container for nested types declared in the Person message type.</summary>
@@ -304,7 +344,11 @@ public enum PhoneType {
304344
[pbr::OriginalName("WORK")] Work = 2,
305345
}
306346

307-
public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber>, pb::IBufferMessage {
347+
public sealed partial class PhoneNumber : pb::IMessage<PhoneNumber>
348+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
349+
, pb::IBufferMessage
350+
#endif
351+
{
308352
private static readonly pb::MessageParser<PhoneNumber> _parser = new pb::MessageParser<PhoneNumber>(() => new PhoneNumber());
309353
private pb::UnknownFieldSet _unknownFields;
310354
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
@@ -441,9 +485,29 @@ public void MergeFrom(PhoneNumber other) {
441485

442486
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
443487
public void MergeFrom(pb::CodedInputStream input) {
488+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
444489
input.ReadRawMessage(this);
490+
#else
491+
uint tag;
492+
while ((tag = input.ReadTag()) != 0) {
493+
switch(tag) {
494+
default:
495+
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
496+
break;
497+
case 10: {
498+
Number = input.ReadString();
499+
break;
500+
}
501+
case 16: {
502+
Type = (global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneType) input.ReadEnum();
503+
break;
504+
}
505+
}
506+
}
507+
#endif
445508
}
446509

510+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
447511
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
448512
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
449513
uint tag;
@@ -463,6 +527,7 @@ public void MergeFrom(pb::CodedInputStream input) {
463527
}
464528
}
465529
}
530+
#endif
466531

467532
}
468533

@@ -474,7 +539,11 @@ public void MergeFrom(pb::CodedInputStream input) {
474539
/// <summary>
475540
/// Our address book file is just one of these.
476541
/// </summary>
477-
public sealed partial class AddressBook : pb::IMessage<AddressBook>, pb::IBufferMessage {
542+
public sealed partial class AddressBook : pb::IMessage<AddressBook>
543+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
544+
, pb::IBufferMessage
545+
#endif
546+
{
478547
private static readonly pb::MessageParser<AddressBook> _parser = new pb::MessageParser<AddressBook>(() => new AddressBook());
479548
private pb::UnknownFieldSet _unknownFields;
480549
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
@@ -579,9 +648,25 @@ public void MergeFrom(AddressBook other) {
579648

580649
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
581650
public void MergeFrom(pb::CodedInputStream input) {
651+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
582652
input.ReadRawMessage(this);
653+
#else
654+
uint tag;
655+
while ((tag = input.ReadTag()) != 0) {
656+
switch(tag) {
657+
default:
658+
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
659+
break;
660+
case 10: {
661+
people_.AddEntriesFrom(input, _repeated_people_codec);
662+
break;
663+
}
664+
}
665+
}
666+
#endif
583667
}
584668

669+
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
585670
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
586671
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
587672
uint tag;
@@ -597,6 +682,7 @@ public void MergeFrom(pb::CodedInputStream input) {
597682
}
598683
}
599684
}
685+
#endif
600686

601687
}
602688

0 commit comments

Comments
 (0)