Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List-patterns: list-patterns may be always-true #59478

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2761,6 +2761,39 @@ public static void Test5<T>(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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could verify y is definitely assigned after this statement.

_ = 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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the expectation here that user should simplify to var y?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user should simply write is var y (which would not warn).
List and slice patterns make assumptions about collections being well-behaved. If you grab the entire range, you should get the same collection, so that dicing wasn't necessary.

// (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()
{
Expand Down