Skip to content

Pass ref structs and pointers of delegate stunts through holders - #360

Merged
kzu merged 1 commit into
mainfrom
alta/mighty-kingfisher-545z
Nov 29, 2026
Merged

kzu merged 1 commit into
mainfrom
alta/mighty-kingfisher-545z

Conversation

@kzu

@kzu kzu commented Nov 29, 2026

Copy link
Copy Markdown
Member

A delegate stunt with a ref struct or pointer in its signature generated code that did not compile, and no diagnostic said why:

public delegate int Sum(Span<int> data);

Sum sum = Stunt.Of<Sum>(); // error CS9244 in the generated SumStunt

Framework delegates were affected too, such as System.Buffers.SpanAction<T, TArg>.

Changes

  • DelegateScaffold passes ref-struct and pointer parameters and returns of Invoke through the holders already used for class and interface members (SpanRef<T>, ReadOnlySpanRef<T>, StructRef<T>, PointerRef). Delegates without such types generate the same code as before.
  • The implementation lambda reads the holders from the invocation, like every other delegate argument, so a behavior can replace them.
  • StuntRegistrations binds an Invoke that has pointers in its signature inside an unsafe block (CS0214 otherwise).
  • UnsafeSignatureAnalyzer walks the Invoke method of a delegate, so ST009 and ST023 apply to delegate stunts: 'Sum.Invoke' uses a ref struct or pointer and requires compile-time stunts and AllowUnsafeBlocks.
  • The holder helpers (CreateHolder, RefSlot, HolderType) move from the private visitor of CSharpRewrite to the class, so both paths share one mapping.

Tests

  • ST009_UnsafeSignature: delegate cases replace DoesNotReportADelegate (unsafe blocks off, missing holders, SpanAction<int, int>, a pointer return, and no report for an ordinary delegate).
  • Scenarios/SignatureRefs.cs, through the real generator: a span parameter seen and changed by a behavior, a delegate without implementation, a span return, a ref struct by reference with an out argument, pointers by value, by reference and returned, SpanAction<int, int> and Func<int[], Span<int>>.

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

This pull request was created by CodeAlta.

🤖 Generated with Claude Code

@kzu kzu added the enhancement New feature or request label Nov 29, 2026
@kzu
kzu enabled auto-merge (rebase) November 29, 2026 05:17
A delegate stunt with a ref struct or pointer in its signature, such as
delegate int Sum(Span<int> data) or SpanAction<T, TArg>, generated code
that did not compile (CS9244), and no diagnostic said why.

DelegateScaffold now carries those parameters and returns through the
pipeline in the holders CSharpRewrite uses for class and interface
members (SpanRef, ReadOnlySpanRef, StructRef, PointerRef), and the
registration binds an Invoke with pointers in an unsafe block.
UnsafeSignatureAnalyzer walks the Invoke method of a delegate, so ST009
and ST023 apply to delegate stunts too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: CodeAlta <noreply@codealta.io>
@kzu
kzu force-pushed the alta/mighty-kingfisher-545z branch from 85c0b76 to 0d50bbe Compare November 29, 2026 05:46
@kzu
kzu merged commit 33311bf into main Nov 29, 2026
6 checks passed
@kzu
kzu deleted the alta/mighty-kingfisher-545z branch November 29, 2026 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant