From 6a2847e86350018482aa7cc32550d010809269a7 Mon Sep 17 00:00:00 2001 From: erikma Date: Wed, 18 May 2022 00:03:03 -0700 Subject: [PATCH] Fix up after removal of notnulls --- .../Google.Protobuf.Test/WellKnownTypes/AnyTest.cs | 2 +- csharp/src/Google.Protobuf/Collections/MapField.cs | 4 +++- .../src/Google.Protobuf/Collections/RepeatedField.cs | 6 +++--- .../src/Google.Protobuf/ParsingPrimitivesMessages.cs | 8 ++++---- .../src/Google.Protobuf/WellKnownTypes/AnyPartial.cs | 12 ++++++------ 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs index a54f8a7dbd2b1..f4ffc0153e0f9 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs @@ -177,7 +177,7 @@ public void Unpack_TypeRegistry() UnittestProto3Reflection.Descriptor); var unpacked = anyMessages.Select(any => any.Unpack(registry)).ToList(); var expected = (IMessage[]) messages.Clone(); - expected[4] = null; + expected[4] = null!; Assert.AreEqual(expected, unpacked); } } diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index 689ab77fbc9bc..7e59d8a6b215d 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -79,7 +79,9 @@ public sealed class MapField : IDeepCloneable KeyEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer(); // TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.) +#pragma warning disable CS8714 // "Nullability of type argument doesn't match 'notnull' constraint" - see TODO on notnull above. private readonly Dictionary>> map = new (KeyEqualityComparer); +#pragma warning restore CS8714 private readonly LinkedList> list = new (); /// @@ -371,7 +373,7 @@ public override int GetHashCode() int hash = 0; foreach (KeyValuePair pair in list) { - hash ^= keyComparer.GetHashCode(pair.Key) * 31 + valueComparer.GetHashCode(pair.Value); + hash ^= keyComparer.GetHashCode(pair.Key!) * 31 + valueComparer.GetHashCode(pair.Value!); } return hash; } diff --git a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs index 10c4892d6d452..948d9edca0eb1 100644 --- a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs +++ b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs @@ -143,7 +143,7 @@ public void AddEntriesFrom(ref ParseContext ctx, FieldCodec codec) // Only FieldCodecs with a fixed size can reach here, and they are all known // types that don't allow the user to specify a custom reader action. // reader action will never return null. - array[count++] = reader(ref ctx); + array[count++] = reader(ref ctx)!; } } else @@ -382,7 +382,7 @@ public bool Remove(T item) } Array.Copy(array, index + 1, array, index, count - index - 1); count--; - array[count] = default; + array[count] = default!; return true; } @@ -598,7 +598,7 @@ public void RemoveAt(int index) } Array.Copy(array, index + 1, array, index, count - index - 1); count--; - array[count] = default; + array[count] = default!; } /// diff --git a/csharp/src/Google.Protobuf/ParsingPrimitivesMessages.cs b/csharp/src/Google.Protobuf/ParsingPrimitivesMessages.cs index 2b2d3abd92afd..93540f97e19cc 100644 --- a/csharp/src/Google.Protobuf/ParsingPrimitivesMessages.cs +++ b/csharp/src/Google.Protobuf/ParsingPrimitivesMessages.cs @@ -33,8 +33,6 @@ using System; using System.Buffers; using System.Collections.Generic; -using System.IO; -using System.Runtime.CompilerServices; using System.Security; using Google.Protobuf.Collections; @@ -139,8 +137,10 @@ public static void ReadMessage(ref ParseContext ctx, IMessage message) } public static KeyValuePair ReadMapEntry(ref ParseContext ctx, MapField.Codec codec) - where TKey : notnull - where TValue : notnull + // TODO: Enable non-null constraints on a major version number change to avoid breaking any code on a minor version + // that declared key or value as nullable. + // where TKey : notnull + // where TValue : notnull { int length = ParsingPrimitives.ParseLength(ref ctx.buffer, ref ctx.state); if (ctx.state.recursionDepth >= ctx.state.recursionLimit) diff --git a/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs b/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs index 341130597fd1d..b72a4a6c40f7b 100644 --- a/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs +++ b/csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs @@ -89,7 +89,7 @@ public bool Is(MessageDescriptor descriptor) { // Note: this doesn't perform as well is it might. We could take a MessageParser in an alternative overload, // which would be expected to perform slightly better... although the difference is likely to be negligible. - T target = new T(); + var target = new T(); if (GetTypeName(TypeUrl) != target.Descriptor.FullName) { throw new InvalidProtocolBufferException( @@ -109,10 +109,10 @@ public bool Is(MessageDescriptor descriptor) { // Note: deliberately avoid writing anything to result until the end, in case it's being // monitored by other threads. (That would be a bug in the calling code, but let's not make it worse.) - T target = new T(); + var target = new T(); if (GetTypeName(TypeUrl) != target.Descriptor.FullName) { - result = default(T); // Can't use null as there's no class constraint, but this always *will* be null in real usage. + result = default; // Can't use null as there's no class constraint, but this always *will* be null in real usage. return false; } target.MergeFrom(Value); @@ -126,16 +126,16 @@ public bool Is(MessageDescriptor descriptor) /// /// The type registry to consult for messages. /// The unpacked message, or null if no matching message was found. - public IMessage Unpack(TypeRegistry registry) + public IMessage? Unpack(TypeRegistry registry) { string typeName = GetTypeName(TypeUrl); - MessageDescriptor descriptor = registry.Find(typeName); + MessageDescriptor? descriptor = registry.Find(typeName); if (descriptor == null) { return null; } - var message = descriptor.Parser.CreateTemplate(); + var message = descriptor.Parser!.CreateTemplate(); message.MergeFrom(Value); return message; }