Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix intercepting a struct or enum base method #71655

Merged
merged 2 commits into from
Jan 19, 2024

Conversation

RikkiGibson
Copy link
Contributor

@RikkiGibson RikkiGibson commented Jan 16, 2024

Closes #70841

When the original method is an instance method and interceptor is an extension, we have to adjust the receiver argument to pass it for the extension 'this' parameter. Receiver emit strategies such as 'ConstrainedCallVirt' are not available for an extension 'this' parameter, so we may need to insert an additional (generally boxing) conversion, to make the receiver argument actually match the parameter type.

See https://github.com/dotnet/roslyn/blob/main/docs/features/interceptors.md#signature-matching

When an interceptable instance method is compared to a static interceptor method (including a classic extension method), we use the method as if it is an extension in reduced form for comparison.

In test InterceptorEnumBaseMethod, for example, we lower the call value.ToString() as if it were calling OtherToString in reduced form. In that case, value must be boxed.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 16, 2024
@RikkiGibson RikkiGibson marked this pull request as ready for review January 16, 2024 19:15
@RikkiGibson RikkiGibson requested a review from a team as a code owner January 16, 2024 19:15
@RikkiGibson
Copy link
Contributor Author

@cston @jcouv Please take a look when you have the chance.

// Program.cs(16,6): error CS9148: Interceptor must have a 'this' parameter matching parameter 'ref Program this' on 'Program.InterceptableMethod()'.
// [InterceptsLocation("Program.cs", 10, 23)]
Diagnostic(ErrorCode.ERR_InterceptorMustHaveMatchingThisParameter, @"InterceptsLocation(""Program.cs"", 10, 23)").WithArguments("ref Program this", "Program.InterceptableMethod()").WithLocation(16, 6));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Consider testing readonly void InterceptableMethod() with static void Interceptor(this in Program), static void Interceptor(this ref readonly Program), and static void Interceptor(this ref Program).


class C
{
public static void M<T>(T t) where T : struct, I
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct,

Consider testing without the struct constraint as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternatively, perhaps remove all constraints and test a call to a virtual method on object.

static void M<T>(T t)
{
    t.ToString();
}

@RikkiGibson
Copy link
Contributor Author

@jcouv for second review

@jcouv jcouv added this to the 17.10 milestone Jan 19, 2024
@jcouv jcouv self-assigned this Jan 19, 2024
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (iteration 2)

@@ -272,7 +272,11 @@ private void EmbedIfNeedTo(BoundExpression receiver, ImmutableArray<PropertySymb
if (needToReduce)
{
Debug.Assert(methodThisParameter is not null);
arguments = arguments.Insert(0, receiverOpt!);
Debug.Assert(receiverOpt?.Type is not null);
Debug.Assert(receiverOpt.Type.Equals(interceptor.Parameters[0].Type, TypeCompareKind.AllIgnoreOptions)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to add a clarifying comment here in a follow-up PR.

@RikkiGibson RikkiGibson merged commit ab6ac27 into dotnet:main Jan 19, 2024
25 checks passed
@ghost ghost modified the milestones: 17.10, Next Jan 19, 2024
@RikkiGibson RikkiGibson deleted the ic-enum-base-method branch January 19, 2024 22:16
@Cosifne Cosifne modified the milestones: Next, 17.10 P1 Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid IL generated for intercepted callsite
4 participants