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: report error when no suitable length member #59475

Merged
merged 5 commits into from
Feb 16, 2022

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented Feb 11, 2022

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

@@ -359,7 +359,12 @@ private bool IsCountableAndIndexable(SyntaxNode node, TypeSymbol inputType, out
}
else
{
hasErrors |= !TryBindLengthOrCount(node, receiverPlaceholder, out lengthAccess, diagnostics);
Copy link
Member

@alrz alrz Feb 11, 2022

Choose a reason for hiding this comment

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

Surprised that this wasn't sufficient. Pretty sure we had tests with missing members. How this was still possible? #Resolved

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 tests with missing/bad length all used an int indexer, so produced other errors. But when we have a System.Index indexer, we'd produce a bound tree with an error but no diagnostics.

@jcouv jcouv marked this pull request as ready for review February 11, 2022 20:14
@jcouv jcouv requested a review from a team as a code owner February 11, 2022 20:14
@jcouv
Copy link
Member Author

jcouv commented Feb 15, 2022

@333fred @dotnet/roslyn-compiler for review. Thanks

@@ -359,7 +359,12 @@ private bool IsCountableAndIndexable(SyntaxNode node, TypeSymbol inputType, out
}
else
{
hasErrors |= !TryBindLengthOrCount(node, receiverPlaceholder, out lengthAccess, diagnostics);
var foundLengthOrCount = TryBindLengthOrCount(node, receiverPlaceholder, out lengthAccess, diagnostics);
if (!foundLengthOrCount && !hasErrors)
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 15, 2022

Choose a reason for hiding this comment

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

&& !hasErrors

Why not always report the error? #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the only situation when hasErrors can be true here is when inputType.IsDynamic(). Why even trying to locate Length/Count in that case, we know it is going to fail. It might make sense to refactor the code according to this, i.e. base the logic on the type, rather than on hasErrors.

@@ -3651,7 +3657,7 @@ public void ListPattern_StringLength()
class C
{
public string Length => throw null;
public int this[int i] => throw null;
public int this[System.Index i] => throw null;
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 15, 2022

Choose a reason for hiding this comment

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

System.Index

I think we want to keep the original test as well. Consider adding a new unit test instead of changing the scenario. #Closed

@@ -7727,7 +7745,7 @@ public void ListPattern_SetOnlyIndexers()

class C
{
public int Length { set { } }
public int Length => 0;
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 15, 2022

Choose a reason for hiding this comment

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

=> 0;

It is not clear to me why the scenario got changed. I think we should still test it. #Closed

// (2,16): error CS8985: List patterns may not be used for a value of type 'S'. No suitable 'Length' or 'Count' property was found.
// _ = new S() is [];
Diagnostic(ErrorCode.ERR_ListPatternRequiresLength, "[]").WithArguments("S").WithLocation(2, 16),
// (2,16): error CS1503: Argument 1: cannot convert from 'System.Index' to 'int'
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 15, 2022

Choose a reason for hiding this comment

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

error CS1503: Argument 1: cannot convert from 'System.Index' to 'int'

This error feels confusing. I have no idea what it is trying to tell me. #WontFix

Copy link
Member Author

@jcouv jcouv Feb 15, 2022

Choose a reason for hiding this comment

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

Agreed the error could be confusing.
FWIW, is the same error you get if you try to index with System.Index into this type:

_ = new S()[^1]; // error CS1503: Argument 1: cannot convert from 'System.Index' to 'int'

struct S
{
    public int this[int i] => i;
}

I'm not planning to address in this PR, feel free to open an issue.

}

[Fact, WorkItem(59465, "https://github.com/dotnet/roslyn/issues/59465")]
public void MissingLength_PrivateLength()
Copy link
Contributor

@AlekseyTs AlekseyTs Feb 15, 2022

Choose a reason for hiding this comment

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

MissingLength_PrivateLength

It looks like all tests below, but the last one, target System.Index indexer. Do we have similar tests for int indexer? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, all the previous tests for those scenarios (bad Length, such as ListPattern_StringLength, SlicePattern_VoidReturn, SlicePattern_LengthAndIndexAndSliceAreStatic, ListPattern_UintCount, ListPattern_LengthFieldNotApplicable, ...) used int Indexer. That's why this problem was missed.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 2)

@jcouv jcouv requested a review from AlekseyTs February 15, 2022 19:27
Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 3)

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

LGTM (commit 5)

@jcouv jcouv enabled auto-merge (squash) February 16, 2022 18:36
@jcouv
Copy link
Member Author

jcouv commented Feb 16, 2022

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@jcouv jcouv merged commit f0a81c8 into dotnet:main Feb 16, 2022
@ghost ghost added this to the Next milestone Feb 16, 2022
@jcouv jcouv deleted the missing-length branch February 16, 2022 19:46
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: no error is emitted when Length is missing or inaccessible
5 participants