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 pattern syntax #51299

Merged
merged 28 commits into from
Apr 20, 2021
Merged

Conversation

alrz
Copy link
Member

@alrz alrz commented Feb 18, 2021

@alrz alrz requested a review from a team as a code owner February 18, 2021 05:22
@alrz alrz marked this pull request as draft February 18, 2021 05:23
@alrz

This comment has been minimized.

.editorconfig Outdated Show resolved Hide resolved
@alrz alrz marked this pull request as ready for review February 27, 2021 18:11
@jcouv
Copy link
Member

jcouv commented Mar 1, 2021

I couldn't find the csharplang spec/proposal. Was it uploaded? #Closed

@jcouv jcouv added this to the C# 10 milestone Mar 1, 2021
@alrz
Copy link
Member Author

alrz commented Mar 2, 2021

I couldn't find the csharplang spec/proposal. Was it uploaded?

Not yet. The proposal is updated at dotnet/csharplang#3245 #Closed

@jcouv jcouv self-assigned this Mar 9, 2021
@alrz

This comment has been minimized.

@alrz alrz marked this pull request as draft March 20, 2021 08:41
@@ -124,7 +124,7 @@ private static SyntaxToken TryGetOpenBraceOrCommaInPropertyPatternClause(SyntaxT
return default;
}

return token.Parent.IsKind(SyntaxKind.PropertyPatternClause) ? token : default;
return token.Parent.IsKind(SyntaxKind.PropertyPatternClause, SyntaxKind.ListPatternClause) ? token : default;
Copy link
Member

Choose a reason for hiding this comment

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

Is there a test for this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

Lots of failing tests for this completion provider.

{ $$ } always see a list pattern clause.

@@ -219,7 +220,7 @@ private void AddBlockIndentationOperation(List<IndentBlockOperation> list, Synta
}

// for lambda, set alignment around braces so that users can put brace wherever they want
if (node.IsLambdaBodyBlock() || node.IsAnonymousMethodBlock() || node.IsKind(SyntaxKind.PropertyPatternClause) || node.IsKind(SyntaxKind.SwitchExpression))
if (node.IsLambdaBodyBlock() || node.IsAnonymousMethodBlock() || node.IsKind(SyntaxKind.PropertyPatternClause, SyntaxKind.ListPatternClause) || node.IsKind(SyntaxKind.SwitchExpression))
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a corresponding test?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just one, failing in the previous test runs.

For cases with no failing test I added a comment to cover later.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

Compiler changes LGTM Thanks (iteration 23)

@jcouv
Copy link
Member

jcouv commented Mar 24, 2021

The correctness leg reports a diagnostic that probably needs to be suppressed:
error RS0027: Symbol 'PropertyPatternClause' violates the backcompat requirement: 'Public API with optional parameter(s) should have the most parameters amongst its public overloads'. See 'https://github.com/dotnet/roslyn/blob/master/docs/Adding%20Optional%20Parameters%20in%20Public%20API.md' for details. #Closed

@jcouv
Copy link
Member

jcouv commented Mar 31, 2021

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

<Field Name="DotDotToken" Type="SyntaxToken">
<Kind Name="DotDotToken"/>
</Field>
<Field Name="Pattern" Type="PatternSyntax" Optional="true"/>
Copy link
Member

Choose a reason for hiding this comment

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

Is there an example for what this field is being used for? The proposal implied that nothing can be listed after the .. in the list or that there was still an open question there.

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 proposal implied that nothing can be listed after the .. in the list

That's true only for enumerable and multi-dimensional arrays.

For compatible types this optionally matches the returned slice. See https://github.com/dotnet/csharplang/blob/main/proposals/list-patterns.md#lowering-on-countableindexeablesliceable-type

}

[Fact]
public void SlicePattern_10()
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to have variations on these slice tests that have the .. after the other pattern component. Also, some pattern combinations with positional and length patterns would be good.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a few tests. Let me know if you have any specific scenario that is not covered.

Copy link
Member

Choose a reason for hiding this comment

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

I'd expect to see some of the previous variations you had here. For example, c is { {}.. } from SlicePattern_05. I'd go through your existing tests and add flipped versions of these.


In reply to: 608770979 [](ancestors = 608770979)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@333fred
Copy link
Member

333fred commented Apr 5, 2021

Done review pass (commit 24). Code looks good, but I'd like a few more tests before signing off. #Closed

@jcouv jcouv requested a review from 333fred April 7, 2021 19:26
@333fred
Copy link
Member

333fred commented Apr 7, 2021

Done review pass (commit 25)

…t-patterns-syntax

# Conflicts:
#	src/Compilers/CSharp/Portable/CSharpResources.resx
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
#	src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
@jcouv
Copy link
Member

jcouv commented Apr 19, 2021

@333fred Please take another look

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 28). @jcouv, do you want to take another look?

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

Still LGTM Thanks (iteration 28)

@jcouv jcouv merged commit 65fce0c into dotnet:features/list-patterns Apr 20, 2021
@jcouv
Copy link
Member

jcouv commented Apr 20, 2021

Merged/squashed. Thanks!

@alrz
Copy link
Member Author

alrz commented Apr 20, 2021

FYI, this is pending a few open questions especially around binding which I'm going to do next.
(while we're awaiting LDM I'm open to move forward with the other PR if there's enough bandwidth)

@jcouv
Copy link
Member

jcouv commented May 6, 2021

@alrz I reached out on discord. I didn't realize there were blocking questions for LDM.
Do you mean the question about multi-dimensional arrays described here? Anything else?

@alrz
Copy link
Member Author

alrz commented May 6, 2021

@jcouv That's not all. Look for "open question" in the document. A few of those are critical to move forward with the first iteration of list patterns.

@jcouv
Copy link
Member

jcouv commented May 7, 2021

@alrz Collected the open issues from the list-pattern spec:

  • Open question: Should we accept a general pattern following .. in a slice_pattern? Answer: let's say yes for now. Allowing some nested patterns versus allowing all isn't a deep change. We can add that before shipping.
  • Open question: Should we support all these combinations? Answer: LDM already confirmed, yes.
  • Open question: We should define the exact binding rules for any of these members and decide if we want to diverge from the range spec. Answer: we should stick to the same behavior as ranges. In short, we bind to indexers if available, otherwise special Slice method.
  • Open question: Should extension methods play a role in sliceability? Answer: we may change that later (for slicing and slice pattern, thus transforming an error scenario into a working scenario) but should assume "no" for now (keep the existing rules from ranges).
  • Open question: The pattern {..} tests for expr.Length >= 0. Should we omit such test (assuming Length is always non-negative)? Answer: this doesn't need to be spec'ed, it is an implementation detail. We are free to change/optimize what we emit for this case. I'd start without the optimization. We can add if we have time.
  • Question on multi-dimensional arrays: I didn't quite understand this one...

Tagging @dotnet/roslyn-compiler as FYI

@alrz
Copy link
Member Author

alrz commented May 7, 2021

@jcouv Ok, that is not very distant from my draft except for "explicit range indexer support" that needs to be added. I will tidy it up and finish the array match lowering for review. Would you prefer a single PR or should I divide it up?

Question on multi-dimensional arrays: I didn't quite understand this one...

That's only a question on the lowering strategy. Given array is {{1}} which one of the following is expected:

array.GetLength(0) == 1 && array.GetLength(1) == 1 && array[0, 0] is 1

array.GetLength(0) == 1 && array.GetLength(1) == 1 &&
array.GetLowerBound(0) is var b0 && array.GetLowerBound(1) is var b1 &&
array[b0 + 0, b1 + 0] is 1

or something else? I think the latter is actually the correct one, but needed a confirmation.

EDIT: I will do md arrays separately, I think that makes it a lot more manageable.

@alrz
Copy link
Member Author

alrz commented May 20, 2021

A few questions on md arrays in addition to the lowering issue above:

  • Should we allow length patterns with md arrays (probably no, unless we want to change the syntax to accept [0,0] etc)
  • Should we allow length patterns in nested lists? (probably no, there's no point to give a length when all of them must match)
  • Should we allow slice patterns (without subpatterns) in a nested list? (this is possible unless we decide against it)
  • Should we treat {} as an empty list in a nested pattern? (in that case new int[,] {{},{}} is {{},{}} returns true)

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.

5 participants