Registry Mirror
Skip to content

Conversation

@siegfriedpammer

Copy link
Copy Markdown
Member

With UseLambdaSyntax enabled, anonymous functions were emitted as lambdas only when an expression body was possible; every statement-bodied anonymous function still decompiled to C# 2 delegate syntax. This PR extends the setting to emit statement lambdas: any anonymous function whose parameter shape a lambda can express now uses lambda syntax. delegate syntax remains for ref/out/in and params parameters, and for pre-C# 3 language profiles (SetLanguageVersion still disables the setting there).

Two latent issues surfaced by the wider lambda coverage and are fixed here:

  1. DeclareVariables.ResolveCollisions stopped its insertion-point walk at any node whose parent is a LambdaExpression, assuming an expression body that declaration insertion then converts to a block. A statement-bodied lambda's own block now reaches that path and tripped a Debug.Assert; the stop now applies only to actual expression bodies.
  2. Anonymous methods declared without a parameter list carry compiler-generated parameter names (<p0>, <sender>) that are not valid C# identifiers. The old output hid them by omitting the parameter list; a lambda cannot, so AssignVariableNames now treats an invalid metadata name like a missing one and regenerates it from the parameter type: (object obj, EventArgs e) => {...}.

A visible side effect captured in the fixtures: an explicit parameter list can make a delegate-creation cast redundant that bare delegate needed for overload resolution - new Thread((ThreadStart)delegate { }) becomes new Thread(() => { }).

Verification: full decompiler suite green (3317 passed). Differential check with decompdiff (master vs. this branch, fresh Release builds) over 12 real-world assemblies / 2341 types - Newtonsoft.Json 9, EntityFramework 6.2, Autofac, Castle.Core, Moq, NLog, log4net, Dapper, RestSharp, SharpZipLib, Mono.Cecil, NUnit 3.5: 68 types changed, every changed line is the delegate-to-lambda rewrite (parameter names from metadata are preserved), 0 new errors, and no deltas in lines, gotos, //IL_ warnings, or compiler-generated name leaks.


This PR description was written by the Claude Code agent session that authored the change.

🤖 Generated with Claude Code

@christophwille christophwille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Automated high-effort review (Claude Code, multi-agent find + adversarial-verify) of head 0116f24.

The direction looks good, and the head commit ("Keep 'delegate {}' when a parameter type is inaccessible") already resolves one issue the review had flagged against the earlier state: lambda expansion forcing unnameable delegate parameter types into an emitted parameter list. Six findings remain, posted inline:

  1. Correctness (confirmed): DeclareVariables.ResolveCollisions can emit invalid C# like x => int num = x; when the expression-lambda body preserved by the new stop condition is itself the matching assignment. Pre-existing on master, but this PR touches exactly that stop condition.
  2. Output regression (confirmed): unused-parameter anonymous methods no longer collapse to delegate {} when the parameter types are accessible, so synthetic-but-valid parameter names (ilasm A_0/A_1, obfuscators) now leak into output - visible in this PR's own Issue1038 expectation change.
  3. Comment accuracy: ref/out/in are expressible in explicitly-typed lambda parameter lists; the comment's "shapes a lambda cannot express" rationale is wrong for those modifiers.
  4. Test coverage: the new invalid-name (<p0>) branch in AssignVariableNames is exercised by no fixture after the Bug971 rewrite (the new InaccessibleParameterTypes.il uses empty parameter names, which take the variables[i].Name fallback instead).
  5. Test coverage: the retained delegate-syntax fallback for ref/out/in/params parameters has no fixture.
  6. Simplification: HasGeneratedName = true in the invalid-name branch is redundant; the continue is the load-bearing part.

@siegfriedpammer
siegfriedpammer force-pushed the lambda-statement-syntax branch 2 times, most recently from 87e477f to af37a1d Compare August 13, 2026 05:56
@siegfriedpammer

Copy link
Copy Markdown
Member Author

All six findings are addressed in the force-pushed branch (rebased on master, fixes squashed into the commits they belong to). Full decompiler suite green locally (3371 tests) and CI is green on all platforms.

1. x => int num = x; — fixed in CombineDeclarationAndInitializer: it now declines when the insertion point's parent is a LambdaExpression, so the separate-declaration path handles it, and that path already converts the expression body to a block. Output becomes (int x) => { int num; return num = x; }. New ILPretty fixture AnonymousMethodEdgeCases.AssignmentIsTheLambdaBody pins it - verified red before the fix with exactly the reported shape (return (int x) => int num = x;).

2. Synthetic parameter names leaking — ParametersAreNameable now gates the lambda form: when no parameter is used and any metadata name is missing or not a valid identifier, delegate {} is kept. Bug971_DelegateWithoutParameterList is restored to delegate { } in the fixture, which also restores the parameter-list-less input the suite had lost.

One honest limitation: Issue1038's A_0/A_1 are real names in the metadata (ilasm writes them for the empty parameter names in the .il), not absent ones, so nothing distinguishes them from source-level names without guessing at name shapes. That case still renders as (TK A_0, TR A_1) => {}. The absent/invalid half of the finding is fixed; the "generated-looking but valid" half is left alone deliberately.

3. Comment accuracy — rewritten. It no longer claims ref/out/in are inexpressible in a lambda; it now says the delegate form is kept where dropping the parameter list is the better rendering, with the modifiers called out as a deliberately conservative choice.

4. <p0> branch untested — new ILPretty fixture AnonymousMethodEdgeCases.UsedParameterWithInvalidName: a literally '<p0>'-named parameter that IS used, so the name has to be regenerated ((object value) => ...). A C#-authored fixture cannot reach this branch, since a parameter-list-less anonymous method can never use its parameters.

5. ref/out/in/params fallback untested — new Pretty fixture AnonymousMethodWithByRefParameters with RefAction/OutAction delegate types, pinning the retained delegate (ref int reference) { ... } form. params is not covered: C# rejects it in an anonymous-method parameter list (CS1670), so only hand-written IL could exercise that half.

6. Redundant HasGeneratedName = true — dropped; only the continue remains, with a comment naming AssignName as what regenerates the name.

Worth recording from the CI round-trip: restoring delegate { } in Bug971 broke the legacy csc configurations, which are Windows-only. Legacy csc names the parameter of a parameter-list-less anonymous method param0 - a perfectly valid identifier that therefore becomes a lambda - while Roslyn emits <p0>. The fixture member is now guarded with #if ROSLYN.

Posted by an AI agent (Claude) on Siegfried's behalf.

Under UseLambdaSyntax, anonymous functions became lambdas only when an
expression body was possible; statement-bodied ones kept C# 2 delegate
syntax. Now every anonymous function whose parameter shape a lambda can
express uses lambda syntax; delegate syntax remains for ref/out/in and
params parameters and for pre-C# 3 language profiles.

Two latent issues surfaced by the wider lambda coverage: DeclareVariables
assumed an insertion point directly under a LambdaExpression is an
expression body it must convert to a block, which block-bodied lambdas
now violate; and anonymous methods declared without a parameter list
carry compiler-generated parameter names like '<p0>' that are not valid
identifiers, so the lambda's mandatory parameter list regenerates such
names from the parameter type: (object obj, EventArgs e) => ...
A side effect visible in fixtures: an explicit parameter list can make
a delegate-creation cast redundant that bare 'delegate' syntax needed
for overload resolution, e.g. new Thread((ThreadStart)delegate { })
becomes new Thread(() => { }).

Assisted-by: Claude:claude-fable-5:Claude Code
The parameter-list-less anonymous method form is compatible with any
delegate signature, and C# code must rely on exactly that when a
delegate's parameter types cannot be named at the use site: IL, unlike
C#, permits a delegate signature to reference less accessible types.
Expanding such an anonymous method into a lambda would force the
unnameable type into a parameter list. Keep the delegate form, with its
parameter list dropped, when the parameters are unused and one of their
types is not accessible from the current context.

Assisted-by: Claude:claude-fable-5:Claude Code
@siegfriedpammer
siegfriedpammer force-pushed the lambda-statement-syntax branch from af37a1d to a22b545 Compare August 13, 2026 08:18
@siegfriedpammer
siegfriedpammer merged commit 60c08fc into master Aug 13, 2026
15 checks passed
@siegfriedpammer
siegfriedpammer deleted the lambda-statement-syntax branch August 13, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants