-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Binding for deconstruction-declaration in 'for' statement #12302
Conversation
|
||
var result = new BoundLocalDeconstructionDeclaration(node, BindDeconstructionAssignment(node.Declaration.Deconstruction.Value, node.Declaration.Deconstruction.Value, variables, diagnostics)); | ||
var result = new BoundLocalDeconstructionDeclaration(statement, BindDeconstructionAssignment(declaration.Deconstruction.Value, declaration.Deconstruction.Value, variables, diagnostics)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why associate the deconstruction with the entire for
statement rather than the Declaration
syntax?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a check in BindStatement
(in Binder_Statement.cs
line 132) that all bound statements should be associated with a statement syntax.
In the for statement, the VariableDeclaration
used to initialize the loop is not a statement (it's just an expression).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But isn't that check for the BoundForStatement
in this case rather than any BoundLocalDeconstructionDeclaration
within that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm looking into this more. The binding for for (int i = 0; ...; ...) { ...}
avoids this problem, so I should be able to do the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found a solution :-)
@@ -616,7 +617,7 @@ private class PossiblyImplicitlyTypedDeconstructionLocalSymbol : SourceLocalSymb | |||
#if DEBUG | |||
SyntaxNode parent; | |||
Debug.Assert(SyntaxFacts.IsDeconstructionIdentifier(identifierToken, out parent)); | |||
Debug.Assert(parent.Parent?.Kind() == SyntaxKind.LocalDeclarationStatement); | |||
Debug.Assert(parent.Parent?.Kind() == SyntaxKind.LocalDeclarationStatement || parent.Parent?.Kind() == SyntaxKind.ForStatement); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider asserting parent.Parent != null
separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Pushed a change with the PR feedback so far and also some circularity tests to be fixed in a later PR (thanks Chuck for those suggestions!). |
LGTM |
Thanks! |
Allows
for(var(x, y) = M(); ..; ...) { ... }
The change has two main parts:
BindForParts
If the VariableDeclarationSyntax for the initialization part of the "for" is a deconstruction-declaration, the initializer bound node will be a
BoundLocalDeconstructionDeclaration
(which is the statement introduced in last PR).MakeLocals
When scanning the for syntax for locals, if the VariableDeclaration is a deconstruction-declaration, then collect locals from it.
Some of those may have an implicit type, which
PossiblyImplicitlyTypedDeconstructionLocalSymbol.InferTypeOfVarVariable
inSourceLocalSymbols.cs
will figure out.@dotnet/roslyn-compiler for review.
Issue #11299