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

Conversation

jcouv
Copy link
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
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.

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.

@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