Repository navigation
Fix delegate stunts with ref returns, nullable values, long signatures or keyword parameter names - #362
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some delegate signatures made the generator emit code that did not compile, with no Stunts diagnostic:
DelegateScaffoldwrites theInvokeof a delegate stunt itself, and lacked whatCSharpRewritedoes for class and interface members.Changes
Invokeis declared with thereforref readonlyreturn of the delegate and returns throughExecuteRef/AsRef. As for class members, the value the implementation returns is copied into aRef<T>; a behavior that returns its ownRef<T>gives the caller that reference.ArgumentCollection, sinceMethodInvocation.Createhas no overload past 16 arguments.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 holdnullor go to areforoutparameter.Invokeis escaped withMemberScaffold.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 ofGetNullable<int>.Tests
Castle/DelegateTests.cs, through the real generator: a ref return with and without an implementation, with arefargument, andref readonly; a nullable return with arefargument; nullable arguments by value,refandout; 17 parameters, and 18 withrefandout; keyword-named parameters withrefandout.Scenarios/SignatureRefs.cs: a ref return with aSpan<int>argument.The unit test project passes (412 tests). The Native AOT acceptance project was not run locally.
Not included
A
refreturn of a ref struct (ref Span<int> Current()) still fails withCS8150, for interfaces and delegates alike. It cannot go throughRef<T>, so it needs a diagnostic.This pull request was created by CodeAlta.
🤖 Generated with Claude Code