|
18 | 18 | using Roslyn.Utilities; |
19 | 19 | using Xunit; |
20 | 20 | using ReferenceEqualityComparer = Roslyn.Utilities.ReferenceEqualityComparer; |
21 | | -using static Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics.NativeIntegerTests.ExpectedConversion; |
| 21 | +using System.Diagnostics; |
22 | 22 |
|
23 | 23 | namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics |
24 | 24 | { |
25 | 25 | public class NativeIntegerTests : CSharpTestBase |
26 | 26 | { |
| 27 | + internal static readonly ConversionKind[] Identity = new[] { ConversionKind.Identity }; |
| 28 | + internal static readonly ConversionKind[] NoConversion = new[] { ConversionKind.NoConversion }; |
| 29 | + internal static readonly ConversionKind[] Boxing = new[] { ConversionKind.Boxing }; |
| 30 | + internal static readonly ConversionKind[] Unboxing = new[] { ConversionKind.Unboxing }; |
| 31 | + internal static readonly ConversionKind[] IntPtrConversion = new[] { ConversionKind.IntPtr }; |
| 32 | + internal static readonly ConversionKind[] ImplicitNumeric = new[] { ConversionKind.ImplicitNumeric }; |
| 33 | + internal static readonly ConversionKind[] ExplicitIntegerToPointer = new[] { ConversionKind.ExplicitIntegerToPointer }; |
| 34 | + internal static readonly ConversionKind[] ExplicitPointerToInteger = new[] { ConversionKind.ExplicitPointerToInteger }; |
| 35 | + internal static readonly ConversionKind[] ExplicitEnumeration = new[] { ConversionKind.ExplicitEnumeration }; |
| 36 | + internal static readonly ConversionKind[] ExplicitNumeric = new[] { ConversionKind.ExplicitNumeric }; |
| 37 | + |
| 38 | + internal static readonly ConversionKind[] ImplicitNullableNumeric = new[] { ConversionKind.ImplicitNullable, ConversionKind.ImplicitNumeric }; |
| 39 | + internal static readonly ConversionKind[] ImplicitNullableIdentity = new[] { ConversionKind.ImplicitNullable, ConversionKind.Identity }; |
| 40 | + |
| 41 | + internal static readonly ConversionKind[] ExplicitNullableEnumeration = new[] { ConversionKind.ExplicitNullable, ConversionKind.ExplicitEnumeration }; |
| 42 | + internal static readonly ConversionKind[] ExplicitNullableImplicitNumeric = new[] { ConversionKind.ExplicitNullable, ConversionKind.ImplicitNumeric }; |
| 43 | + internal static readonly ConversionKind[] ExplicitNullableNumeric = new[] { ConversionKind.ExplicitNullable, ConversionKind.ExplicitNumeric }; |
| 44 | + internal static readonly ConversionKind[] ExplicitNullablePointerToInteger = new[] { ConversionKind.ExplicitNullable, ConversionKind.ExplicitPointerToInteger }; |
| 45 | + internal static readonly ConversionKind[] ExplicitNullableIdentity = new[] { ConversionKind.ExplicitNullable, ConversionKind.Identity }; |
| 46 | + |
| 47 | + internal static bool IsNoConversion(ConversionKind[] conversionKinds) |
| 48 | + { |
| 49 | + return conversionKinds is [ConversionKind.NoConversion]; |
| 50 | + } |
| 51 | + |
| 52 | + internal static void AssertMatches(ConversionKind[] expected, Conversion conversion) |
| 53 | + { |
| 54 | + IEnumerable<ConversionKind> actualConversionKinds = new[] { conversion.Kind }; |
| 55 | + if (!conversion.UnderlyingConversions.IsDefault) |
| 56 | + { |
| 57 | + actualConversionKinds = actualConversionKinds.Concat(conversion.UnderlyingConversions.Select(c => c.Kind)); |
| 58 | + } |
| 59 | + Assert.Equal(expected, actualConversionKinds); |
| 60 | + } |
| 61 | + |
27 | 62 | [Fact] |
28 | 63 | public void LanguageVersion() |
29 | 64 | { |
@@ -7285,56 +7320,6 @@ .locals init (ulong V_0) |
7285 | 7320 | "); |
7286 | 7321 | } |
7287 | 7322 |
|
7288 | | - internal class ExpectedConversion |
7289 | | - { |
7290 | | - private ConversionKind[] kinds; |
7291 | | - |
7292 | | - public static ExpectedConversion Identity = ConversionKind.Identity; |
7293 | | - public static ExpectedConversion NoConversion = ConversionKind.NoConversion; |
7294 | | - public static ExpectedConversion Boxing = ConversionKind.Boxing; |
7295 | | - public static ExpectedConversion Unboxing = ConversionKind.Unboxing; |
7296 | | - public static ExpectedConversion IntPtrConversion = ConversionKind.IntPtr; |
7297 | | - public static ExpectedConversion ImplicitNumeric = ConversionKind.ImplicitNumeric; |
7298 | | - public static ExpectedConversion ExplicitIntegerToPointer = ConversionKind.ExplicitIntegerToPointer; |
7299 | | - public static ExpectedConversion ExplicitPointerToInteger = ConversionKind.ExplicitPointerToInteger; |
7300 | | - public static ExpectedConversion ExplicitEnumeration = ConversionKind.ExplicitEnumeration; |
7301 | | - public static ExpectedConversion ExplicitNumeric = ConversionKind.ExplicitNumeric; |
7302 | | - |
7303 | | - public static ExpectedConversion ImplicitNullableNumeric = new[] { ConversionKind.ImplicitNullable, ConversionKind.ImplicitNumeric }; |
7304 | | - public static ExpectedConversion ImplicitNullableIdentity = new[] { ConversionKind.ImplicitNullable, ConversionKind.Identity }; |
7305 | | - |
7306 | | - public static ExpectedConversion ExplicitNullableEnumeration = new[] { ConversionKind.ExplicitNullable, ConversionKind.ExplicitEnumeration }; |
7307 | | - public static ExpectedConversion ExplicitNullableImplicitNumeric = new[] { ConversionKind.ExplicitNullable, ConversionKind.ImplicitNumeric }; |
7308 | | - public static ExpectedConversion ExplicitNullableNumeric = new[] { ConversionKind.ExplicitNullable, ConversionKind.ExplicitNumeric }; |
7309 | | - public static ExpectedConversion ExplicitNullablePointerToInteger = new[] { ConversionKind.ExplicitNullable, ConversionKind.ExplicitPointerToInteger }; |
7310 | | - public static ExpectedConversion ExplicitNullableIdentity = new[] { ConversionKind.ExplicitNullable, ConversionKind.Identity }; |
7311 | | - |
7312 | | - public static implicit operator ExpectedConversion(ConversionKind conversionKind) |
7313 | | - { |
7314 | | - return new ExpectedConversion() { kinds = new[] { conversionKind } }; |
7315 | | - } |
7316 | | - |
7317 | | - public static implicit operator ExpectedConversion(ConversionKind[] conversionKinds) |
7318 | | - { |
7319 | | - return new ExpectedConversion() { kinds = conversionKinds }; |
7320 | | - } |
7321 | | - |
7322 | | - public bool IsNoConversion() |
7323 | | - { |
7324 | | - return kinds is [ConversionKind.NoConversion]; |
7325 | | - } |
7326 | | - |
7327 | | - public void AssertMatches(Conversion conversion) |
7328 | | - { |
7329 | | - IEnumerable<ConversionKind> actualConversionKinds = new[] { conversion.Kind }; |
7330 | | - if (!conversion.UnderlyingConversions.IsDefault) |
7331 | | - { |
7332 | | - actualConversionKinds = actualConversionKinds.Concat(conversion.UnderlyingConversions.Select(c => c.Kind)); |
7333 | | - } |
7334 | | - Assert.Equal(kinds, actualConversionKinds); |
7335 | | - } |
7336 | | - } |
7337 | | - |
7338 | 7323 | [Fact] |
7339 | 7324 | public void Conversions() |
7340 | 7325 | { |
@@ -7542,11 +7527,11 @@ .locals init ({sourceType}? V_0, |
7542 | 7527 | IL_0021: newobj ""{destType}?..ctor({destType})"" |
7543 | 7528 | IL_0026: ret |
7544 | 7529 | }}"; |
7545 | | - void conversions(string sourceType, string destType, ExpectedConversion expectedConversions, string expectedImplicitIL, string expectedExplicitIL, string expectedCheckedIL = null) |
| 7530 | + void conversions(string sourceType, string destType, ConversionKind[] expectedConversions, string expectedImplicitIL, string expectedExplicitIL, string expectedCheckedIL = null) |
7546 | 7531 | { |
7547 | 7532 | if (expectedExplicitIL is not null) |
7548 | 7533 | { |
7549 | | - Assert.False(expectedConversions.IsNoConversion()); |
| 7534 | + Assert.False(IsNoConversion(expectedConversions)); |
7550 | 7535 | } |
7551 | 7536 |
|
7552 | 7537 | // https://github.com/dotnet/roslyn/issues/42834: Invalid code generated for nullable conversions |
@@ -9106,7 +9091,7 @@ void convert(string sourceType, |
9106 | 9091 | bool useExplicitCast, |
9107 | 9092 | bool useChecked, |
9108 | 9093 | bool verify, |
9109 | | - ExpectedConversion expectedConversions, |
| 9094 | + ConversionKind[] expectedConversions, |
9110 | 9095 | ErrorCode expectedErrorCode) |
9111 | 9096 | { |
9112 | 9097 | bool useUnsafeContext = useUnsafe(sourceType) || useUnsafe(destType); |
@@ -9143,7 +9128,7 @@ enum E {{ }} |
9143 | 9128 | if (!useExplicitCast) |
9144 | 9129 | { |
9145 | 9130 | var destTypeSymbol = ((MethodSymbol)comp.GetMember("Program.Convert")).ReturnType.GetPublicSymbol(); |
9146 | | - expectedConversions.AssertMatches(model.ClassifyConversion(expr, destTypeSymbol)); |
| 9131 | + AssertMatches(expectedConversions, model.ClassifyConversion(expr, destTypeSymbol)); |
9147 | 9132 | } |
9148 | 9133 |
|
9149 | 9134 | if (!skipTypeChecks) |
|
0 commit comments