-
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
ExpressionLambda Rewriter Failure when assigning to lambda parameter to self #3826
Comments
The legacy compiler raises CS0832 An expression tree may not contain an assignment operator. Feature request: Can you please remove that error? |
Assigning to anything other than the parameter itself works. |
The bug is caused by http://source.roslyn.io/#Microsoft.CodeAnalysis.CSharp/Lowering/DiagnosticsPass_ExpressionTrees.cs,126, which suppresses the CS0832 in favor of CS1717 (why?) |
@SLaks: Why is assignment forbidden? |
https://msdn.microsoft.com/en-us/library/bb384223
On the other hand, it seems to be perfectly possible: var param = Expression.Parameter(typeof(int));
var f = Expression.Lambda<Func<int, int>>(
Expression.Assign(param, param),
param
); This works fine. |
@SLaks: Seems like an unnecessary limitation or justification. Like your example, an expression is just access to syntax at the runtime. Even if C# allowed compile-time manipulation, I cant see it being a problem. |
True. Allowing this (including assignments to fields in closure classes) would probably also be useful for more sophisticated expression-based runtime configuration systems. |
@SLaks: Mind you, it is probably bad style, in Scheme you would rather do this:
instead of
Either way, it gets compiled into the same thing (based on SSA). But on a semantic/scope level it is different (not to You could probably do the same with some LINQ and/or nested lambdas in C#. BTW: Nice catch :D I like to break things too! |
Expression trees do not currently support mutation operations or statements, even though underlying trees for most of them are defined. We are definitely interested in expanding the set of supported operations in the future. |
All assignments in an expression tree should be an error. Self assignment was being flagged as only a warning which lead to later errors in code generation. Changed the behavior to warn and error for self assignment (matches native compiler behavior). close dotnet#3826
Source:
VS says: "csc2.exe" exited with code -2146232797.
The compiler throws
Unexpected value 'AssignmentOperator' of type 'Microsoft.CodeAnalysis.CSharp.BoundKind'
Stack trace:
The text was updated successfully, but these errors were encountered: