From 86c7d0d09370ed8d8b4e937be62936eb7fedf7e2 Mon Sep 17 00:00:00 2001 From: Neal Gafter Date: Mon, 16 Sep 2019 15:38:48 -0700 Subject: [PATCH] Refine diagnostic for switch expression that is not exhaustive Fixes #38571 --- .../Portable/CSharpResources.Designer.cs | 4 ++-- .../CSharp/Portable/CSharpResources.resx | 4 ++-- .../CSharp/Test/Emit/CodeGen/PatternTests.cs | 4 ++-- .../IOperationTests_ISwitchExpression.cs | 12 +++++----- .../IOperationTests_ISwitchOperation.cs | 2 +- .../Semantics/PatternMatchingTests2.cs | 6 ++--- .../Semantics/PatternMatchingTests3.cs | 2 +- .../Semantics/PatternMatchingTests4.cs | 22 +++++++++---------- 8 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs b/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs index 59bddff89d383..a1da282f5e4da 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs +++ b/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs @@ -16108,7 +16108,7 @@ internal static string WRN_SequentialOnPartialClass_Title { } /// - /// Looks up a localized string similar to The switch expression does not handle all possible inputs (it is not exhaustive).. + /// Looks up a localized string similar to The switch expression does not handle all possible values of its input type (it is not exhaustive).. /// internal static string WRN_SwitchExpressionNotExhaustive { get { @@ -16117,7 +16117,7 @@ internal static string WRN_SwitchExpressionNotExhaustive { } /// - /// Looks up a localized string similar to The switch expression does not handle all possible inputs (it is not exhaustive).. + /// Looks up a localized string similar to The switch expression does not handle all possible values of its input type (it is not exhaustive).. /// internal static string WRN_SwitchExpressionNotExhaustive_Title { get { diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index 49d678f6ccdcb..129d5dc8005f3 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -5661,10 +5661,10 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ The syntax 'var' for a pattern is not permitted to refer to a type, but '{0}' is in scope here. - The switch expression does not handle all possible inputs (it is not exhaustive). + The switch expression does not handle all possible values of its input type (it is not exhaustive). - The switch expression does not handle all possible inputs (it is not exhaustive). + The switch expression does not handle all possible values of its input type (it is not exhaustive). The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs index 6c438c5c1c34b..b739c4601867f 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/PatternTests.cs @@ -3026,7 +3026,7 @@ static class C { "; var compilation = CreateEmptyCompilation(source, options: TestOptions.ReleaseDll); compilation.GetDiagnostics().Verify( - // (9,38): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (9,38): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // public static bool M(int i) => i switch { 1 => true }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 38) ); @@ -3036,7 +3036,7 @@ static class C { // (9,36): error CS0656: Missing compiler required member 'System.InvalidOperationException..ctor' // public static bool M(int i) => i switch { 1 => true }; Diagnostic(ErrorCode.ERR_MissingPredefinedMember, "i switch { 1 => true }").WithArguments("System.InvalidOperationException", ".ctor").WithLocation(9, 36), - // (9,38): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (9,38): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // public static bool M(int i) => i switch { 1 => true }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 38) ); diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchExpression.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchExpression.cs index e055bf7ccf56c..36a990d4861e1 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchExpression.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchExpression.cs @@ -74,7 +74,7 @@ void M(int? x, object y) Arms(0) "; var expectedDiagnostics = new[] { - // file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // y = /**/x switch { }/**/; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25) }; @@ -110,7 +110,7 @@ void M(int? x, object y) ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 5) (Syntax: '5') "; var expectedDiagnostics = new[] { - // file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // y = /**/x switch { => 5 }/**/; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25), // file.cs(7,34): error CS8504: Pattern missing @@ -355,7 +355,7 @@ void M(int? x, object y) ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 5) (Syntax: '5') "; var expectedDiagnostics = new[] { - // file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // y = /**/x switch { NotFound => 5 }/**/; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25), // file.cs(7,34): error CS0103: The name 'NotFound' does not exist in the current context @@ -512,7 +512,7 @@ void M(int? x, object y) ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 5) (Syntax: '5') "; var expectedDiagnostics = new[] { - // file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // y = /**/x switch { 1 => 2, _ when false => 5 }/**/; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25) }; @@ -589,7 +589,7 @@ void M(int? x, object y) ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 5) (Syntax: '5') "; var expectedDiagnostics = new[] { - // file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // y = /**/x switch { _ when NotFound => 5 }/**/; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25), // file.cs(7,41): error CS0103: The name 'NotFound' does not exist in the current context @@ -636,7 +636,7 @@ void M(int? x, object y) Local_2: System.Int32 z "; var expectedDiagnostics = new[] { - // file.cs(7,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // file.cs(7,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // y = /**/x switch { int z when x is int z => 5 }/**/; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 25), // file.cs(7,54): error CS0128: A local variable or function named 'z' is already defined in this scope diff --git a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs index 68195c994b4e2..7c0c349d50288 100644 --- a/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs +++ b/src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs @@ -3324,7 +3324,7 @@ public static void Main() } "; var expectedDiagnostics = new[] { - // file.cs(6,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // file.cs(6,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // var r = 1 switch { }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(6, 19), // file.cs(6,19): error CS8506: No best type was found for the switch expression. diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs index df535d151ccd1..9f6b272544bfc 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests2.cs @@ -494,7 +494,7 @@ public static void Main() } }"; CreatePatternCompilation(source).VerifyDiagnostics( - // (5,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (5,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // var r = 1 switch { }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(5, 19), // (5,19): error CS8506: No best type was found for the switch expression. @@ -518,7 +518,7 @@ public static void M() {} public delegate void D(); }"; CreatePatternCompilation(source).VerifyDiagnostics( - // (5,19): warning CS8409: The switch expression does not handle all possible inputs (it is not exhaustive). + // (5,19): warning CS8409: The switch expression does not handle all possible values of its input type (it is not exhaustive). // var x = 1 switch { 0 => M, 1 => new D(M), 2 => M }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(5, 19) ); @@ -609,7 +609,7 @@ public static void Main() }"; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (7,19): warning CS8409: The switch expression does not handle all possible inputs (it is not exhaustive). + // (7,19): warning CS8409: The switch expression does not handle all possible values of its input type (it is not exhaustive). // var c = a switch { var x2 when x2 is var x3 => x3 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 19) ); diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests3.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests3.cs index 244f6d56bfeec..0b85d77fe0da5 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests3.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests3.cs @@ -542,7 +542,7 @@ ref int M(bool b, ref int x, ref int y) }"; var compilation = CreateCompilationWithMscorlibAndSpan(source, options: TestOptions.DebugDll); compilation.VerifyDiagnostics( - // (6,23): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (6,23): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // return ref (b switch { true => ref x, false => ref y }); Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(6, 23), // (6,40): error CS1525: Invalid expression term 'ref' diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests4.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests4.cs index ecec734cded52..551964f9e47b1 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests4.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests4.cs @@ -1776,7 +1776,7 @@ static int M1(bool? b1, bool? b2) "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (6,25): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (6,25): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // return (b1, b2) switch { Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(6, 25) ); @@ -2048,7 +2048,7 @@ static void Main() "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (9,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (9,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // _ = t switch { (3, 4) => 1 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 19) ); @@ -2091,7 +2091,7 @@ public SwitchExpressionException() {} "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (9,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (9,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // _ = t switch { (3, 4) => 1 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 19) ); @@ -2134,7 +2134,7 @@ public class SwitchExpressionException : InvalidOperationException "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (9,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (9,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // _ = t switch { (3, 4) => 1 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 19) ); @@ -2176,7 +2176,7 @@ public class SwitchExpressionException : InvalidOperationException "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (8,24): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (8,24): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // _ = (1, 2) switch { (3, 4) => 1 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(8, 24) ); @@ -2223,7 +2223,7 @@ public SwitchExpressionException() {} "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (9,19): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (9,19): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // _ = r switch { (3, 4) => 1 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(9, 19) ); @@ -2815,7 +2815,7 @@ public static void Main() "; var compilation = CreatePatternCompilation(source, options: TestOptions.ReleaseExe); compilation.VerifyDiagnostics( - // (7,32): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (7,32): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // Console.Write((x, 300) switch { ((1, int x2), int y) => x2+y }); Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(7, 32) ); @@ -2891,7 +2891,7 @@ public static void Main() "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (6,15): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (6,15): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // _ = o switch { null => 1 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(6, 15) ); @@ -2930,7 +2930,7 @@ public int M(bool b) "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (22,18): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (22,18): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // return b switch Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(22, 18) ); @@ -3082,7 +3082,7 @@ public class SwitchExpressionException : InvalidOperationException "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (17,23): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (17,23): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // return (x, y) switch { (1, 2) => 3 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(17, 23) ); @@ -3218,7 +3218,7 @@ public class SwitchExpressionException : InvalidOperationException "; var compilation = CreatePatternCompilation(source); compilation.VerifyDiagnostics( - // (17,44): warning CS8509: The switch expression does not handle all possible inputs (it is not exhaustive). + // (17,44): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). // return (x, y, a, b, c, d, e, f, g) switch { (1, 2, _, _, _, _, _, _, _) => 3 }; Diagnostic(ErrorCode.WRN_SwitchExpressionNotExhaustive, "switch").WithLocation(17, 44) );