-
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
Deconstruction assignment: basic implementation #11384
Conversation
|
||
// bind the variables and check they can be assigned to | ||
var checkedVariablesBuilder = ArrayBuilder<BoundExpression>.GetInstance(arguments.Count); | ||
for (int i = 0, l = numElements; i < l; i++) |
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.
foreach
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 can't. I need the index i
below.
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.
It looks like the index is only used to index into arguments
though. i
and l
seem unnecessary.
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.
Ah, yes. Sorry about that.
} | ||
|
||
[Fact] | ||
public void LambdaStillNotValidateStatement() |
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.
ValidStatement
?
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.
Fixed.
if (numElements < 2) | ||
{ | ||
// this should be a parse error already. | ||
BoundExpression[] args = numElements == 1 ? |
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.
Actually, it should have parsed as a parenthesized expression, not a tuple expression. I think you can assert that this does not occur.
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.
Changed.
|
||
<Field Name="LeftVariables" Type="ImmutableArray<BoundExpression>"/> | ||
|
||
<Field Name="DeconstructReceiver" Type="BoundExpression"/> |
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'd just call it "Right"
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.
And you actually call it loweredRight after lowering
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.
5438da3
to
b3cf56f
Compare
Treat deconstruction of a tuple into existing variables as a kind of assignment, using the existing AssignmentExpression. | ||
|
||
|
||
###Deconstruction-assignment (deconstruction into into existing variables): |
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.
"into into"
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.
Fixed.
846ef81
to
85e1f4d
Compare
LGTM |
Thanks all for the review and feedback. I'll merge shortly. |
LGTM. We need to figure what nesting on the left means and what is the type of the whole expression. |
; | ||
|
||
join_clause // not sure | ||
: 'join' type? identifier 'in' expression 'on' expression 'equals' 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.
This is an existing production, no? Are you intending to add an alternative with local_variable_combo_declaration_lhs
? Here and below two productions.
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.
The list above resulted from my fishing for syntax nodes that look like they declare variables and so would be candidates for deconstruction-declaration.
For those marked "not sure", I wasn't sure whether they are good candidates and what the updated syntax would be.
Here's a first basic implementation of deconstruction-assignment. I'm wondering if this should go into a feature branch instead as I only did limited IDE testing so far.
In terms of design:
ScanTupleType
method can now return a newScanTypeFlags
type. This only affects two callers:IsPossibleLocalDeclarationStatement
(see code snippet at the bottom) andBoundDeconstructionAssignmentOperator
). This bound node contains the list of variables on the left (bound and checked), the receiver forDeconstruct
, the symbol for theDeconstruct
method and information to complete the assignment of individual values (including conversions).-The
Deconstruct
method is resolved with restrictive rules at this point (we should find exactly one).Deconstruct
below),Deconstruct
with those out parameters,Vlad, the initial approach that we discussed for binding was very difficult (significant refactoring on tricky code). So the bound node holds onto the assignment expressions for each variable, as opposed to a Conversion object. Not only was this much simpler and natural, I think this will actually help later (when dealing with nesting case).
@dotnet/roslyn-compiler for review.
From
IsPossibleLocalDeclarationStatement
:ScanType
used to returnMustBeType
orNotType
, but we need to care about "possibly-tuple-type-but-might-also-be-tuple-expression" (the newTupleType
option).