Skip to content

Fix delegate stunts with ref returns, nullable values, long signatures or keyword parameter names - #362

Merged
kzu merged 2 commits into
mainfrom
alta/eager-forge-9jj5
Nov 29, 2026
Merged

kzu merged 2 commits into
mainfrom
alta/eager-forge-9jj5

Conversation

@kzu

@kzu kzu commented Nov 29, 2026

Copy link
Copy Markdown
Member

Some delegate signatures made the generator emit code that did not compile, with no Stunts diagnostic:

public delegate ref int Current();                                 // CS8189
public delegate int? MaybeNext(ref int value);                     // CS0266
public delegate int Many(int a1, /* ... */ int a17);               // CS1501
public delegate int? Swap(int? next, ref int? current);            // CS1503
public delegate int Keyword(int @class, ref int @event);           // syntax errors

DelegateScaffold writes the Invoke of a delegate stunt itself, and lacked what CSharpRewrite does for class and interface members.

Changes

  • Ref returns: Invoke is declared with the ref or ref readonly return of the delegate and returns through ExecuteRef/AsRef. As for class members, the value the implementation returns is copied into a Ref<T>; a behavior that returns its own Ref<T> gives the caller that reference.
  • More than 16 parameters: the invocation is created with an ArgumentCollection, since MethodInvocation.Create has no overload past 16 arguments.
  • Nullable value types: the implementation lambda keeps Nullable<T> as declared, for the returned local and for the arguments it reads from the invocation (GetNullable<int?>). A local of the underlying type could not hold null or go to a ref or out parameter.
  • Keyword parameter names: every reference to a parameter of Invoke is escaped with MemberScaffold.IdentifierToken (@class). The argument names the pipeline sees stay unescaped ("class").

Delegates without these shapes generate the same code as before, except that a nullable value-type argument is now read as GetNullable<int?> instead of GetNullable<int>.

Tests

  • Castle/DelegateTests.cs, through the real generator: a ref return with and without an implementation, with a ref argument, and ref readonly; a nullable return with a ref argument; nullable arguments by value, ref and out; 17 parameters, and 18 with ref and out; keyword-named parameters with ref and out.
  • Scenarios/SignatureRefs.cs: a ref return with a Span<int> argument.

The unit test project passes (412 tests). The Native AOT acceptance project was not run locally.

Not included

A ref return of a ref struct (ref Span<int> Current()) still fails with CS8150, for interfaces and delegates alike. It cannot go through Ref<T>, so it needs a diagnostic.

This pull request was created by CodeAlta.

🤖 Generated with Claude Code

kzu and others added 2 commits November 29, 2026 03:05
…stunts

Three delegate signatures made the generator emit code that did not
compile, with no diagnostic:

    delegate ref int Current();              // CS8189
    delegate int? MaybeNext(ref int value);  // CS0266
    delegate int Many(int a1, ..., int a17); // CS1501

DelegateScaffold now declares Invoke with the ref or ref readonly return
of the delegate and returns through ExecuteRef/AsRef, as CSharpRewrite
does for members. Past 16 parameters it passes an ArgumentCollection to
the MethodInvocation constructor, since MethodInvocation.Create has no
such overload.

A nullable value type is kept as declared in the implementation lambda:
the returned local, and the arguments read from the invocation, which
are now GetNullable<int?>. A local of the underlying type did not
compile for a ref or out int? parameter.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: CodeAlta <noreply@codealta.io>
A delegate with keyword-named parameters, such as

    delegate int Keyword(int @Class, ref int @event, out int @out);

generated an Invoke that did not compile: DelegateScaffold referenced
the parameters by their metadata name (class, event, out), while the
parameter list was escaped.

Every reference to a parameter of Invoke now goes through
MemberScaffold.IdentifierToken: the arguments of MethodInvocation.Create
and of the ArgumentCollection, the out initialization and the write-back
assignments. The argument names the pipeline sees stay unescaped.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: CodeAlta <noreply@codealta.io>
@kzu kzu added the bug Something isn't working label Nov 29, 2026
@kzu
kzu enabled auto-merge (rebase) November 29, 2026 06:06
@kzu
kzu merged commit d1c1807 into main Nov 29, 2026
6 checks passed
@kzu
kzu deleted the alta/eager-forge-9jj5 branch November 29, 2026 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant