Skip to content

Report ST013 for a ref return of a ref struct or pointer - #363

Merged
kzu merged 1 commit into
mainfrom
alta/early-atoll-mqrc
Nov 29, 2026
Merged

kzu merged 1 commit into
mainfrom
alta/early-atoll-mqrc

Conversation

@kzu

@kzu kzu commented Nov 29, 2026

Copy link
Copy Markdown
Member

A member or delegate that returns a ref struct or pointer by reference made the generator emit code that does not compile, with no Stunts diagnostic saying why:

public interface IBuffers { ref Span<int> Current(); }

Stunt.Of<IBuffers>();
IBuffersStunt.cs(50,17): error CS8150: By-value returns may only be used in methods that return by value

The stunt returns the held value from a local slot, which a ref return cannot do. Ref<T>, which carries the other ref returns, cannot take a ref struct or pointer as T, so the pipeline cannot carry the signature under any build setting.

What changes

  • ST013 now also reports a ref return of a ref struct or pointer, for methods, properties, indexers and the Invoke of a delegate. Its message states the reason: 'IBuffers' cannot be proxied because 'IBuffers.Current()' returns a ref struct or pointer by reference.
  • The generator skips that stunt, as it already does for the other ST013 signatures, so the other stunts of the project are still generated.
  • RuntimeSignature had a walk of its own that also returned members the stunt does not intercept: an interface member the base class implements, or a member a derived class seals. It now reads the members from InterceptedMembers, the walk of UnsafeSignatureAnalyzer moved to a class both share, so ST013 no longer rejects a stunt for such a member.

The guide (docs/guide/diagnostics.md), the readme and AGENTS.md describe the new case.

Tests

StuntGeneratorTests: ReportsRefReturnsOfRefStructsAndPointers (method, property, indexer, pointer, delegate; it failed for all five before the fix) and DoesNotReportRefReturnsTheStuntDoesNotIntercept. The unit test project passes (418 tests).

🤖 Generated with Claude Code

This pull request was created by CodeAlta.

A member or delegate that returns a ref struct or pointer by reference
(ref Span<int> Current()) made the generator emit code that does not
compile (CS8150), with no Stunts diagnostic: the stunt returns the held
value from a local slot, which a ref return cannot do, and Ref<T>, which
carries the other ref returns, cannot take a ref struct or pointer as T.

RuntimeSignature now finds those signatures too, for methods, properties,
indexers and the Invoke of a delegate. ST013 reports them where the stunt
is asked for, with the reason in its message, and the generator skips
that stunt, so the other stunts of the project are still generated.

RuntimeSignature had a walk of its own that also returned members the
stunt does not intercept: an interface member the base class implements,
or a member a derived class seals. It now reads the members from
InterceptedMembers, the walk of UnsafeSignatureAnalyzer moved to a class
both share, so ST013 no longer rejects a stunt for such a member.

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:09
@kzu
kzu merged commit 270d808 into main Nov 29, 2026
6 checks passed
@kzu
kzu deleted the alta/early-atoll-mqrc branch November 29, 2026 06:16
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