Support lambda expressions and method groups as var initializers#55132
Support lambda expressions and method groups as var initializers#55132cston merged 2 commits intodotnet:mainfrom
var initializers#55132Conversation
| // (7,28): error CS0446: Foreach cannot operate on a 'anonymous method'. Did you intend to invoke the 'anonymous method'? | ||
| // foreach (var d3 in delegate () { }) { } | ||
| Diagnostic(ErrorCode.ERR_AnonMethGrpInForEach, "delegate () { }").WithArguments("anonymous method").WithLocation(7, 28)); | ||
| } |
There was a problem hiding this comment.
nit: consider another similar negative test with deconstruction declaration (var x, var y) = ... #Closed
There was a problem hiding this comment.
Also, something like (Action a, var b) = (null, () => {}); (I assume should work per spec, but may not work in current implementation)
There was a problem hiding this comment.
Please check GetTypeInfo and GetDeclaredSymbol in one of the positive tests
There was a problem hiding this comment.
Consider including a test with a non-simple signature (error)
There was a problem hiding this comment.
Added tests. Logged #55139 to investigate support for implicitly-typed variables in tuples and deconstruction.
| // var m = Main; // CS0815 | ||
| Diagnostic(ErrorCode.ERR_ImplicitlyTypedVariableAssignedBadValue, "m = Main").WithArguments("method group"), | ||
| // (7,13): error CS0815: Cannot assign lambda expression to an implicitly-typed variable | ||
| // (7,17): error CS8917: The delegate type could not be inferred. |
There was a problem hiding this comment.
Comment in var m = Main; // CS0815 is out-of-date #Closed
jcouv
left a comment
There was a problem hiding this comment.
Done with review pass (iteration 1)
jcouv
left a comment
There was a problem hiding this comment.
LGTM Thanks (iteration 2). How do you want to track the deconstruction issue?
|
@333fred @dotnet/roslyn-compiler for a second review. Thanks |
Logged #55139. |
|
Looking. |
Nit: consider renaming to BoundLambda Refers to: src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IAnonymousFunctionExpression.cs:190 in a884c74. [](commit_id = a884c74, deletion_comment = False) |
|
|
||
| var variableDeclarationGroupOperation = (IVariableDeclarationGroupOperation)semanticModel.GetOperation(variableDeclaration); | ||
| var variableTreeLambdaOperation = (IAnonymousFunctionOperation)variableDeclarationGroupOperation.Declarations.Single().Declarators.Single().Initializer.Value; | ||
| var variableTreeLambdaOperation = ((IDelegateCreationOperation)variableDeclarationGroupOperation.Declarations.Single().Declarators.Single().Initializer.Value).Target; |
There was a problem hiding this comment.
Will add to the next PR, thanks.
No longer correctly named, and probably not the correct location for this either, since it's no longer invalid. This test can probably just be removed. Refers to: src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_InvalidExpression.cs:333 in a884c74. [](commit_id = a884c74, deletion_comment = False) |
| } | ||
|
|
||
| [Fact] | ||
| public void ImplicitlyTypedVariables_04() |
| static void Main() | ||
| { | ||
| var d1 = F; | ||
| var d2 = (ref int x) => x; |
There was a problem hiding this comment.
Consider expanding these negative tests with ref returns as well.
333fred
left a comment
There was a problem hiding this comment.
I'm not going to block on any of my test suggestions, but please make sure they're in the next PR.
Will add to next PR. In reply to: 887712078 Refers to: src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_IAnonymousFunctionExpression.cs:190 in a884c74. [](commit_id = a884c74, deletion_comment = False) |
Will leave these tests here, to catch any regressions, but I'll rename the tests. In reply to: 887713598 Refers to: src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_InvalidExpression.cs:333 in a884c74. [](commit_id = a884c74, deletion_comment = False) |
|
Tell me when this is merged 😄 into a VS build. |
|
@cston, a small correction. The link to the proposal should be https://github.com/dotnet/csharplang/blob/main/proposals/csharp-10.0/lambda-improvements.md. |
Proposal: lambda-improvements.md
Test plan: #52192