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

Deconstruction-declaration parsing for local declaration, for and foreach #12104

Merged
merged 13 commits into from
Jun 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions docs/features/deconstruction.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,61 +121,63 @@ Note that tuples (`System.ValueTuple`) don't need to invoke Deconstruct.
declaration_statement
: local_variable_declaration ';'
| local_constant_declaration ';'
| local_variable_combo_declaration ';' // new
;

local_variable_combo_declaration
: local_variable_combo_declaration_lhs '=' expression
local_variable_declaration
: local_variable_type local_variable_declarators
| deconstruction_declaration // new
;

local_variable_combo_declaration_lhs
: 'var' '(' identifier_list ')'
| '(' local_variable_list ')'
;
deconstruction_declaration // new
: deconstruction_variables '=' expression
;

identifier_list
: identifier ',' identifier
| identifier_list ',' identifier
;
deconstuction_variables
: '(' deconstuction_variables_nested (',' deconstuction_variables_nested)* ')'
| 'var' deconstruction_identifiers
;

local_variable_list
: local_variable_type identifier ',' local_variable_type identifier
| local_variable_list ',' local_variable_type identifier
;
deconstuction_variables_nested // new
: deconstuction_variables
| type identifier
;

deconstruction_identifiers
: '(' deconstruction_identifiers_nested (',' deconstruction_identifiers_nested)* ')'
;

deconstruction_identifiers_nested // new
: deconstruction_identifiers
| identifier
;

foreach_statement
: 'foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement
| 'foreach' '(' local_variable_combo_declaration_lhs 'in' expression ')' embedded_statement // new
| 'foreach' '(' deconstruction_variables 'in' expression ')' embedded_statement // new
;

for_statement
: 'for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement
;
Copy link
Member

Choose a reason for hiding this comment

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

Has for_statement changed or is the same as without deconstruction variables?

Copy link
Member Author

Choose a reason for hiding this comment

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

In the grammar, I kept for_statement as before. The difference appears below, in for_initializer.


for_initializer
: local_variable_declaration
| local_variable_combo_declaration // new
| deconstruction_declaration // new
| statement_expression_list
;

let_clause
: 'let' identifier '=' expression
| 'let' '(' identifier_list ')' '=' expression // new
| 'let' deconstruction_identifiers '=' expression // new
;

from_clause // not sure
from_clause
: 'from' type? identifier 'in' expression
;

join_clause // not sure
: 'join' type? identifier 'in' expression 'on' expression 'equals' expression
;

join_into_clause // not sure
: 'join' type? identifier 'in' expression 'on' expression 'equals' expression 'into' identifier
;

constant_declarator // not sure
: identifier '=' constant_expression
| 'from' deconstuction_variables 'in' expression // new
| 'from' deconstruction_identifiers 'in' expression // new
;
```

Treat deconstruction of a tuple into new variables as a new kind of node (not AssignmentExpression).
It would pick up the behavior of each contexts where new variables can be declared (TODO: need to list). For instance, in LINQ, new variables go into a transparent identifiers.
It is seen as deconstructing into separate variables (we don't introduce transparent identifiers in contexts where they didn't exist previously).

Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpCodeAnalysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@
<Compile Include="Syntax\DelegateDeclarationSyntax.cs" />
<Compile Include="Syntax\DirectiveTriviaSyntax.cs" />
<Compile Include="Syntax\ExpressionStatementSyntax.cs" />
<Compile Include="Syntax\ForEachStatementSyntax.cs" />
<Compile Include="Syntax\GenericNameSyntax.cs" />
<Compile Include="Syntax\InternalSyntax\BinaryExpressionSyntax.cs" />
<Compile Include="Syntax\CSharpSyntaxTree.DebuggerSyntaxTree.cs" />
Expand Down Expand Up @@ -854,6 +855,7 @@
<Compile Include="Syntax\SyntaxTriviaListBuilder.cs" />
<Compile Include="Syntax\TypeDeclarationSyntax.cs" />
<Compile Include="Syntax\TypeSyntax.cs" />
<Compile Include="Syntax\VariableDeclarationSyntax.cs" />
<Compile Include="Syntax\XmlNameAttributeElementKind.cs" />
<Compile Include="Utilities\EnumExtensions.cs" />
<Compile Include="Utilities\FirstAmongEqualsSet.cs" />
Expand Down
18 changes: 18 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4896,4 +4896,10 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_CannotDeconstructDynamic" xml:space="preserve">
<value>Cannot deconstruct dynamic objects.</value>
</data>
<data name="ERR_DeconstructTooFewElements" xml:space="preserve">
<value>Deconstruction must contain at least two variables.</value>
</data>
<data name="TypeMustBeVar" xml:space="preserve">
<value>The type must be 'var'.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,5 +1398,6 @@ internal enum ErrorCode
ERR_DeconstructRequiresExpression = 8210,
ERR_DeconstructWrongCardinality = 8211,
ERR_CannotDeconstructDynamic = 8212,
ERR_DeconstructTooFewElements = 8213,
}
}
7 changes: 6 additions & 1 deletion src/Compilers/CSharp/Portable/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.WhenClause(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.WhenClauseSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.IncompleteMember(Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.IncompleteMemberSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ReturnStatement(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.ReturnStatementSyntax")]

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.DeconstructionVariablesVar(Microsoft.CodeAnalysis.CSharp.Syntax.TupleExpressionSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.DeconstructionVariablesVarSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.LocalDeclarationStatement(Microsoft.CodeAnalysis.SyntaxTokenList)~Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.VariableDeconstructionDeclarator(Microsoft.CodeAnalysis.SeparatedSyntaxList{Microsoft.CodeAnalysis.CSharp.Syntax.VariableDeclarationSyntax})~Microsoft.CodeAnalysis.CSharp.Syntax.VariableDeconstructionDeclaratorSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.VariableDeclaration(Microsoft.CodeAnalysis.SeparatedSyntaxList{Microsoft.CodeAnalysis.CSharp.Syntax.VariableDeclaratorSyntax})~Microsoft.CodeAnalysis.CSharp.Syntax.VariableDeclarationSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.FieldDeclaration(Microsoft.CodeAnalysis.CSharp.Syntax.VariableDeclarationSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.FieldDeclarationSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.EventFieldDeclaration(Microsoft.CodeAnalysis.CSharp.Syntax.VariableDeclarationSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.EventFieldDeclarationSyntax")]
Copy link
Contributor

@AlekseyTs AlekseyTs Jun 23, 2016

Choose a reason for hiding this comment

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

Is the intent to ship with these suppressions, or is this something we are going to look at later? #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.

We have an open issue to remove those suppressions.
They are introduced by new compat rules which the source generator breaks...

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 issue to track this is #11538

Loading