Skip to content

Commit 0d50bbe

Browse files
kzuclaudecodealta-agent
committed
Pass ref structs and pointers of delegate stunts through holders
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>
1 parent fb11b45 commit 0d50bbe

8 files changed

Lines changed: 436 additions & 114 deletions

File tree

‎AGENTS.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,8 @@ runtime for Native AOT consumers.
219219
`UnsafeSignatureAnalyzer` reports `ST009` and `ST023` where a stunt is asked for: a call to a
220220
generator method, or a registration attribute of the assembly (`SymbolExtensions.IsRegistrationAttribute`,
221221
shared with the generator). It walks what the generator intercepts: overridable and interface
222-
members, and for compile-time stunts the accessible constructors of the base class (run-time
223-
stunts do not intercept constructors). Delegate stunts are left out: `DelegateScaffold` passes the
224-
arguments of `Invoke` to `MethodInvocation.Create` without holders, so a ref struct or pointer in
225-
a delegate signature does not compile under any setting.
222+
members, the `Invoke` method of a delegate, and for compile-time stunts the accessible constructors
223+
of the base class (run-time stunts do not intercept constructors).
226224

227225
Visual Basic only supports run-time stunts: `Stunts.targets` forces `EnableCompileTimeStunts`
228226
to false for VB and warns (`ST011`) unless Stunts.DynamicProxy is installed. `GenericWrapperAnalyzer`
@@ -345,6 +343,9 @@ overload limit use an `ArgumentCollection` instead. Ref-struct and pointer acces
345343
holder path as methods. Ref-struct holders are invalidated in `finally`, including constructor
346344
interception; constructor output arguments already initialized by the base call must be preserved.
347345
Mixed holder and ordinary `ref`/`out` signatures copy pipeline outputs back to the caller.
346+
`DelegateScaffold` writes the `Invoke` of a delegate stunt itself (`CSharpRewrite` skips it), with
347+
the same holders (`CSharpRewrite.CreateHolder`, `HolderType`). Its implementation lambda reads
348+
every argument, holders included, from the invocation, so a behavior can replace them.
348349

349350
`InterfaceImplementation` is shared by the analyzer and generator. `ST012` rejects abstract
350351
interface members inaccessible to the consuming assembly, including internal setters:

‎src/Stunts.CodeAnalysis/UnsafeSignatureAnalyzer.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ static string Name(ISymbol member)
139139

140140
static ISymbol? Find(INamedTypeSymbol type, IAssemblySymbol assembly, bool constructors, Func<ITypeSymbol, bool> matches)
141141
{
142-
// A delegate stunt does not pass a ref struct or pointer through the pipeline: no
143-
// build property or language version makes its generated code compile.
142+
// A delegate stunt intercepts the Invoke method of the delegate.
144143
if (type.TypeKind == TypeKind.Delegate)
145-
return null;
144+
return type.DelegateInvokeMethod is IMethodSymbol invoke && Uses(invoke, matches) ? invoke : null;
146145

147146
if (type.TypeKind == TypeKind.Interface)
148147
{

‎src/Stunts.CompiledProxy/Processors/CSharpRewrite.cs‎

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,66 @@ public SyntaxNode Process(SyntaxNode syntax, ProcessorContext context)
5656
return syntax = new CSharpRewriteVisitor(virtualEvents).Visit(syntax);
5757
}
5858

59+
// The holder that carries a ref struct or pointer parameter through the pipeline.
60+
internal static ExpressionSyntax CreateHolder(ParameterSyntax parameter)
61+
{
62+
if (parameter.HasAnnotation(Annotations.PointerRef))
63+
{
64+
return ObjectCreationExpression(
65+
IdentifierName("PointerRef"),
66+
Argument(CastExpression(
67+
PointerType(PredefinedType(Token(SyntaxKind.VoidKeyword))),
68+
IdentifierName(parameter.Identifier))));
69+
}
70+
71+
return ObjectCreationExpression(
72+
HolderType(parameter.Type!),
73+
RefSlot(parameter));
74+
}
75+
76+
// The slot of a ref struct parameter, which its holder points at.
77+
internal static ArgumentSyntax RefSlot(ParameterSyntax parameter)
78+
{
79+
ExpressionSyntax target = IdentifierName(parameter.Identifier);
80+
if (parameter.Modifiers.Any(SyntaxKind.ReadOnlyKeyword) || parameter.Modifiers.Any(SyntaxKind.InKeyword))
81+
{
82+
target = InvocationExpression(
83+
MemberAccessExpression(ParseName("global::System.Runtime.CompilerServices.Unsafe"), IdentifierName("AsRef")),
84+
Argument(target).WithRefKindKeyword(Token(SyntaxKind.InKeyword)));
85+
}
86+
87+
return Argument(target).WithRefKindKeyword(Token(SyntaxKind.RefKeyword));
88+
}
89+
90+
internal static TypeSyntax HolderType(TypeSyntax type)
91+
{
92+
var inner = type is RefTypeSyntax refType ? refType.Type : type;
93+
if (TrySpan(inner, "Span", out var argument))
94+
return GenericName("SpanRef", argument);
95+
if (TrySpan(inner, "ReadOnlySpan", out argument))
96+
return GenericName("ReadOnlySpanRef", argument);
97+
if (inner is PointerTypeSyntax || inner is FunctionPointerTypeSyntax)
98+
return IdentifierName("PointerRef");
99+
return GenericName("StructRef", inner);
100+
}
101+
102+
static bool TrySpan(TypeSyntax type, string name, out TypeSyntax argument)
103+
{
104+
switch (type)
105+
{
106+
case GenericNameSyntax generic when generic.Identifier.ValueText == name && generic.TypeArgumentList.Arguments.Count == 1:
107+
argument = generic.TypeArgumentList.Arguments[0];
108+
return true;
109+
case QualifiedNameSyntax qualified:
110+
return TrySpan(qualified.Right, name, out argument);
111+
case AliasQualifiedNameSyntax alias:
112+
return TrySpan(alias.Name, name, out argument);
113+
default:
114+
argument = type;
115+
return false;
116+
}
117+
}
118+
59119
class EventVisitor : CSharpSyntaxWalker
60120
{
61121
public List<TypeDeclarationSyntax> Types { get; } = new();
@@ -933,64 +993,6 @@ static bool CapturedByRef(ParameterSyntax parameter)
933993
static string HolderName(string parameter, string prefix)
934994
=> prefix + prefix + "argument_" + parameter;
935995

936-
static ExpressionSyntax CreateHolder(ParameterSyntax parameter)
937-
{
938-
if (parameter.HasAnnotation(Annotations.PointerRef))
939-
{
940-
return ObjectCreationExpression(
941-
IdentifierName("PointerRef"),
942-
Argument(CastExpression(
943-
PointerType(PredefinedType(Token(SyntaxKind.VoidKeyword))),
944-
IdentifierName(parameter.Identifier))));
945-
}
946-
947-
return ObjectCreationExpression(
948-
HolderType(parameter.Type!),
949-
RefSlot(parameter));
950-
}
951-
952-
static ArgumentSyntax RefSlot(ParameterSyntax parameter)
953-
{
954-
ExpressionSyntax target = IdentifierName(parameter.Identifier);
955-
if (parameter.Modifiers.Any(SyntaxKind.ReadOnlyKeyword) || parameter.Modifiers.Any(SyntaxKind.InKeyword))
956-
{
957-
target = InvocationExpression(
958-
MemberAccessExpression(ParseName("global::System.Runtime.CompilerServices.Unsafe"), IdentifierName("AsRef")),
959-
Argument(target).WithRefKindKeyword(Token(SyntaxKind.InKeyword)));
960-
}
961-
962-
return Argument(target).WithRefKindKeyword(Token(SyntaxKind.RefKeyword));
963-
}
964-
965-
static TypeSyntax HolderType(TypeSyntax type)
966-
{
967-
var inner = type is RefTypeSyntax refType ? refType.Type : type;
968-
if (TrySpan(inner, "Span", out var argument))
969-
return GenericName("SpanRef", argument);
970-
if (TrySpan(inner, "ReadOnlySpan", out argument))
971-
return GenericName("ReadOnlySpanRef", argument);
972-
if (inner is PointerTypeSyntax || inner is FunctionPointerTypeSyntax)
973-
return IdentifierName("PointerRef");
974-
return GenericName("StructRef", inner);
975-
}
976-
977-
static bool TrySpan(TypeSyntax type, string name, out TypeSyntax argument)
978-
{
979-
switch (type)
980-
{
981-
case GenericNameSyntax generic when generic.Identifier.ValueText == name && generic.TypeArgumentList.Arguments.Count == 1:
982-
argument = generic.TypeArgumentList.Arguments[0];
983-
return true;
984-
case QualifiedNameSyntax qualified:
985-
return TrySpan(qualified.Right, name, out argument);
986-
case AliasQualifiedNameSyntax alias:
987-
return TrySpan(alias.Name, name, out argument);
988-
default:
989-
argument = type;
990-
return false;
991-
}
992-
}
993-
994996
static InvocationExpressionSyntax RewriteCall(InvocationExpressionSyntax call, SeparatedSyntaxList<ParameterSyntax> parameters, string prefix)
995997
{
996998
var args = call.ArgumentList.Arguments;

0 commit comments

Comments
 (0)