Skip to content

Commit

Permalink
Fix up after removal of notnulls
Browse files Browse the repository at this point in the history
  • Loading branch information
erikmav committed May 18, 2022
1 parent d41a7b5 commit 6a2847e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 3 additions & 1 deletion csharp/src/Google.Protobuf/Collections/MapField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValu
private static readonly EqualityComparer<TKey> KeyEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer<TKey>();

// 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<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> map = new (KeyEqualityComparer);
#pragma warning restore CS8714
private readonly LinkedList<KeyValuePair<TKey, TValue>> list = new ();

/// <summary>
Expand Down Expand Up @@ -371,7 +373,7 @@ public override int GetHashCode()
int hash = 0;
foreach (KeyValuePair<TKey, TValue> 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;
}
Expand Down
6 changes: 3 additions & 3 deletions csharp/src/Google.Protobuf/Collections/RepeatedField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void AddEntriesFrom(ref ParseContext ctx, FieldCodec<T> 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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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!;
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions csharp/src/Google.Protobuf/ParsingPrimitivesMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -139,8 +137,10 @@ public static void ReadMessage(ref ParseContext ctx, IMessage message)
}

public static KeyValuePair<TKey, TValue> ReadMapEntry<TKey, TValue>(ref ParseContext ctx, MapField<TKey, TValue>.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)
Expand Down
12 changes: 6 additions & 6 deletions csharp/src/Google.Protobuf/WellKnownTypes/AnyPartial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public bool Is(MessageDescriptor descriptor)
{
// Note: this doesn't perform as well is it might. We could take a MessageParser<T> 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(
Expand All @@ -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);
Expand All @@ -126,16 +126,16 @@ public bool Is(MessageDescriptor descriptor)
/// </summary>
/// <param name="registry">The type registry to consult for messages.</param>
/// <returns>The unpacked message, or <c>null</c> if no matching message was found.</returns>
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;
}
Expand Down

0 comments on commit 6a2847e

Please sign in to comment.