From ed7e824dc55150790265f143d160540c074d1c22 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 10 Feb 2022 21:50:07 -0800 Subject: [PATCH 1/2] List-patterns: list-patterns may be always-true --- .../CSharp/Portable/Binder/Binder_Patterns.cs | 2 +- .../PatternMatchingTests_ListPatterns.cs | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) 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..701979aa7d384 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs @@ -2761,6 +2761,39 @@ 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]; +_ = 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), + // (3,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(3, 5), + // (4,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(4, 5), + // (5,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(5, 5) + ); + } + [Fact] public void ListPattern_ValEscape() { From c0452389a077f6836454bc11af1c55ec23bc41a8 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Fri, 11 Feb 2022 00:21:16 -0800 Subject: [PATCH 2/2] Check definite assignment --- .../Semantics/PatternMatchingTests_ListPatterns.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs index 701979aa7d384..83eb5d29d3709 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs @@ -2766,6 +2766,8 @@ public void AlwaysTruePattern() { var source = @" _ = new S() is [..var y]; +y.ToString(); + _ = new S() is [..]; _ = new S() is [..[..]]; _ = new S() is not [..]; @@ -2782,15 +2784,15 @@ struct S // (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), - // (3,5): warning CS8794: An expression of type 'S' always matches the provided pattern. + // (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(3, 5), - // (4,5): warning CS8794: An expression of type 'S' always matches the provided pattern. + 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(4, 5), - // (5,5): error CS8518: An expression of type 'S' can never match the provided pattern. + 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(5, 5) + Diagnostic(ErrorCode.ERR_IsPatternImpossible, "new S() is not [..]").WithArguments("S").WithLocation(7, 5) ); }