Skip to content

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

Merged
jcouv merged 2 commits intodotnet:mainfrom
jcouv:list-crash2
Feb 11, 2022
Merged

List-patterns: list-patterns may be always-true#59478
jcouv merged 2 commits intodotnet:mainfrom
jcouv:list-crash2

Conversation

@jcouv
Copy link
Copy Markdown
Member

@jcouv jcouv commented Feb 11, 2022

Fixes #59466
Relates to test plan #51289
FYI @alrz

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
Copy Markdown
Member

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
Copy Markdown
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.

public void AlwaysTruePattern()
{
var source = @"
_ = new S() is [..var y];
Copy link
Copy Markdown
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.

@jcouv jcouv marked this pull request as ready for review February 11, 2022 18:19
@jcouv jcouv requested a review from a team as a code owner February 11, 2022 18:19
@jcouv jcouv merged commit 535f4bd into dotnet:main Feb 11, 2022
@jcouv jcouv deleted the list-crash2 branch February 11, 2022 18:49
@ghost ghost added this to the Next milestone Feb 11, 2022
@RikkiGibson RikkiGibson modified the milestones: Next, 17.2.P2 Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

List patterns: x is [..var y] with a custom collection type causes a crash

4 participants