diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs index bbc6eeabe3f64..4075977e98ce0 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs @@ -71,13 +71,13 @@ private BoundExpression MakeIsPatternExpression( { case BoundConstantPattern _: case BoundITuplePattern _: - case BoundListPattern: // these patterns can fail in practice throw ExceptionUtilities.Unreachable; case BoundRelationalPattern _: case BoundTypePattern _: case BoundNegatedPattern _: case BoundBinaryPattern _: + case BoundListPattern: Debug.Assert(expression.Type is object); diagnostics.Add(ErrorCode.WRN_IsPatternAlways, node.Location, expression.Type); break; diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs index 495602792f55a..83eb5d29d3709 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs @@ -2761,6 +2761,41 @@ public static void Test5(T t) where T : IIndexable, ICountableViaLength, ICou ); } + [Fact, WorkItem(59466, "https://github.com/dotnet/roslyn/issues/59466")] + public void AlwaysTruePattern() + { + var source = @" +_ = new S() is [..var y]; +y.ToString(); + +_ = new S() is [..]; +_ = new S() is [..[..]]; +_ = new S() is not [..]; + +struct S +{ + public int Length => 1; + public int this[int i] => 42; + public S this[System.Range r] => default; +} +"; + var comp = CreateCompilationWithIndexAndRangeAndSpan(source); + comp.VerifyEmitDiagnostics( + // (2,5): warning CS8794: An expression of type 'S' always matches the provided pattern. + // _ = new S() is [..var y]; + Diagnostic(ErrorCode.WRN_IsPatternAlways, "new S() is [..var y]").WithArguments("S").WithLocation(2, 5), + // (5,5): warning CS8794: An expression of type 'S' always matches the provided pattern. + // _ = new S() is [..]; + Diagnostic(ErrorCode.WRN_IsPatternAlways, "new S() is [..]").WithArguments("S").WithLocation(5, 5), + // (6,5): warning CS8794: An expression of type 'S' always matches the provided pattern. + // _ = new S() is [..[..]]; + Diagnostic(ErrorCode.WRN_IsPatternAlways, "new S() is [..[..]]").WithArguments("S").WithLocation(6, 5), + // (7,5): error CS8518: An expression of type 'S' can never match the provided pattern. + // _ = new S() is not [..]; + Diagnostic(ErrorCode.ERR_IsPatternImpossible, "new S() is not [..]").WithArguments("S").WithLocation(7, 5) + ); + } + [Fact] public void ListPattern_ValEscape() {