Skip to content

Commit ab82996

Browse files
BrianUnityEmandM
andauthored
test: avoid C# 14 ambiguous Contains call in UniversalRpcTests (#4193)
From C# 14, an array converts implicitly to ReadOnlySpan<T>, so recipient.Contains(c) on a ulong[] matches both MemoryExtensions.Contains and Unity.Collections' NativeArrayExtensions.Contains, and the compiler reports CS0121. Add a ContainsId extension method backed by Array.IndexOf and use it at the 12 affected call sites. It compiles the same way on C# 9. Co-authored-by: Emma <emma.mcmillan@unity3d.com>
1 parent 72a7a0e commit ab82996

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

‎com.unity.netcode.gameobjects/Tests/Runtime/Rpc/UniversalRpcTests.cs‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,12 @@ public void SelfRecursiveRpc()
528528
}
529529
}
530530

531+
internal static class ClientIdArrayExtensions
532+
{
533+
// Not named Contains: from C# 14, ids.Contains(id) on an array is ambiguous between span extension methods.
534+
public static bool ContainsId(this ulong[] ids, ulong id) => Array.IndexOf(ids, id) >= 0;
535+
}
536+
531537
internal class UniversalRpcTestsBase : NetcodeIntegrationTest
532538
{
533539
public static int YieldCheck = 0;
@@ -1557,12 +1563,12 @@ private void SendingWithGroupOverride(SendTo defaultSendTo, ulong[] recipient, u
15571563
var sendMethod = senderObject.GetType().GetMethod(sendMethodName);
15581564
sendMethod.Invoke(senderObject, new object[] { (RpcParams)target });
15591565

1560-
VerifyRemoteReceived(objectOwner, sender, sendMethodName, s_ClientIds.Where(c => recipient.Contains(c)).ToArray(), false);
1561-
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => !recipient.Contains(c)).ToArray());
1566+
VerifyRemoteReceived(objectOwner, sender, sendMethodName, s_ClientIds.Where(c => recipient.ContainsId(c)).ToArray(), false);
1567+
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => !recipient.ContainsId(c)).ToArray());
15621568

15631569
// Pass some time to make sure that no other client ever receives this
15641570
TimeTravel(1f, 30);
1565-
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => !recipient.Contains(c)).ToArray());
1571+
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => !recipient.ContainsId(c)).ToArray());
15661572
}
15671573

15681574
private void SendingWithGroupNotOverride(SendTo defaultSendTo, ulong[] recipient, ulong objectOwner, ulong sender, AllocationType allocationType)
@@ -1600,12 +1606,12 @@ private void SendingWithGroupNotOverride(SendTo defaultSendTo, ulong[] recipient
16001606
var sendMethod = senderObject.GetType().GetMethod(sendMethodName);
16011607
sendMethod.Invoke(senderObject, new object[] { (RpcParams)target });
16021608

1603-
VerifyRemoteReceived(objectOwner, sender, sendMethodName, s_ClientIds.Where(c => !recipient.Contains(c)).ToArray(), false);
1604-
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => recipient.Contains(c)).ToArray());
1609+
VerifyRemoteReceived(objectOwner, sender, sendMethodName, s_ClientIds.Where(c => !recipient.ContainsId(c)).ToArray(), false);
1610+
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => recipient.ContainsId(c)).ToArray());
16051611

16061612
// Pass some time to make sure that no other client ever receives this
16071613
TimeTravel(1f, 30);
1608-
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => recipient.Contains(c)).ToArray());
1614+
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => recipient.ContainsId(c)).ToArray());
16091615
}
16101616
#endregion
16111617

@@ -2028,12 +2034,12 @@ [Values] AllocationType allocationType
20282034
var sendMethod = senderObject.GetType().GetMethod(sendMethodName);
20292035
sendMethod.Invoke(senderObject, new object[] { (RpcParams)target });
20302036

2031-
VerifyRemoteReceived(objectOwner, sender, sendMethodName, s_ClientIds.Where(c => recipient.Contains(c)).ToArray(), false);
2032-
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => !recipient.Contains(c)).ToArray());
2037+
VerifyRemoteReceived(objectOwner, sender, sendMethodName, s_ClientIds.Where(c => recipient.ContainsId(c)).ToArray(), false);
2038+
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => !recipient.ContainsId(c)).ToArray());
20332039

20342040
// Pass some time to make sure that no other client ever receives this
20352041
TimeTravel(1f, 30);
2036-
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => !recipient.Contains(c)).ToArray());
2042+
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => !recipient.ContainsId(c)).ToArray());
20372043
}
20382044

20392045
}
@@ -2105,12 +2111,12 @@ [Values] AllocationType allocationType
21052111
var sendMethod = senderObject.GetType().GetMethod(sendMethodName);
21062112
sendMethod.Invoke(senderObject, new object[] { (RpcParams)target });
21072113

2108-
VerifyRemoteReceived(objectOwner, sender, sendMethodName, s_ClientIds.Where(c => !recipient.Contains(c)).ToArray(), false);
2109-
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => recipient.Contains(c)).ToArray());
2114+
VerifyRemoteReceived(objectOwner, sender, sendMethodName, s_ClientIds.Where(c => !recipient.ContainsId(c)).ToArray(), false);
2115+
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => recipient.ContainsId(c)).ToArray());
21102116

21112117
// Pass some time to make sure that no other client ever receives this
21122118
TimeTravel(1f, 30);
2113-
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => recipient.Contains(c)).ToArray());
2119+
VerifyNotReceived(objectOwner, s_ClientIds.Where(c => recipient.ContainsId(c)).ToArray());
21142120
}
21152121

21162122
}

0 commit comments

Comments
 (0)