Skip to content

Extensions: allow cref references to extension members #78735

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented May 28, 2025

Relates to test plan #76130
Review of public API: #78738

@jcouv jcouv self-assigned this May 28, 2025
@jcouv jcouv added Area-Compilers Feature - Extension Everything The extension everything feature labels May 28, 2025
@jcouv jcouv force-pushed the extensions-cref branch from 34b59c7 to 34ef1fb Compare May 28, 2025 21:34
@dotnet-policy-service dotnet-policy-service bot added the Needs API Review Needs to be reviewed by the API review council label May 28, 2025
@jcouv jcouv force-pushed the extensions-cref branch from 34ef1fb to 15fdb0b Compare May 28, 2025 23:01
@@ -181,7 +181,7 @@ public override object VisitNamedType(NamedTypeSymbol symbol, StringBuilder buil
builder.Append('.');
}

builder.Append(symbol.IsExtension ? symbol.ExtensionName : symbol.Name);
builder.Append(symbol.IsExtension ? escape(symbol.ExtensionName) : symbol.Name);
Copy link
Member Author

@jcouv jcouv May 29, 2025

Choose a reason for hiding this comment

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

📝 I debated whether to escape here, or push that responsibility to the caller of DocumentationCommentIDVisitor. What do you think? #Resolved

Copy link
Contributor

Choose a reason for hiding this comment

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

Since we replace </> with {/} for type argument list below, it probably makes sense to avoid using them here as well.

@jcouv jcouv marked this pull request as ready for review May 29, 2025 17:30
@jcouv jcouv requested review from a team as code owners May 29, 2025 17:30
<Field Name="DotToken" Type="SyntaxToken">
<Kind Name="DotToken"/>
</Field>
<Field Name="Member" Type="MemberCrefSyntax"/>
Copy link
Contributor

Choose a reason for hiding this comment

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

"MemberCrefSyntax"

This feels too flexible. For example, this cannot be ExtensionMemberCrefSyntax. Could we restrict allowed nodes by kind? If not, perhaps for the syntax model it is fine as is. Is there a corresponding proposal for the grammar change?

}

SyntaxToken dotToken = EatToken(SyntaxKind.DotToken);
MemberCrefSyntax member = ParseMemberCref();
Copy link
Contributor

Choose a reason for hiding this comment

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

ParseMemberCref()

It looks like this is going to allow an indirect recursion, which is not expected.


TypeArgumentListSyntax? extensionTypeArguments = syntax.TypeArgumentList;
int extensionArity = extensionTypeArguments?.Arguments.Count ?? 0;
sortedSymbols = computeSortedAndFilteredCrefExtensionMembers(namedContainer, simpleName.Identifier.ValueText, extensionArity, arity, extensionTypeArguments, diagnostics, syntax);
Copy link
Contributor

Choose a reason for hiding this comment

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

ValueText

Do we have a test with escaped identifier at this position?

if (sortedSymbols.IsDefaultOrEmpty)
{
ambiguityWinner = null;
return [];
Copy link
Contributor

Choose a reason for hiding this comment

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

return [];

Consider leaving a follow up comment to handle operators.

refCustomModifiers: [],
explicitInterfaceImplementations: []);

ImmutableArray<ParameterSymbol> extensionParameterSymbols = syntax.Parameters is { } extensionParameterListSyntax ? BindCrefParameters(extensionParameterListSyntax, diagnostics) : default;
Copy link
Contributor

Choose a reason for hiding this comment

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

syntax.Parameters is { } extensionParameterListSyntax

Can this be false?


var constructedNested = (NamedTypeSymbol)ConstructWithCrefTypeParameters(extensionArity, extensionTypeArguments, nested);

var candidateExtensionSignature = new SignatureOnlyMethodSymbol(
Copy link
Contributor

Choose a reason for hiding this comment

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

var candidateExtensionSignature = new SignatureOnlyMethodSymbol(

Consider adding a comment that we are using signature method symbols to match extension blocks


ImmutableArray<ParameterSymbol> extensionParameterSymbols = syntax.Parameters is { } extensionParameterListSyntax ? BindCrefParameters(extensionParameterListSyntax, diagnostics) : default;

var providedExtensionSignature = new SignatureOnlyMethodSymbol(
Copy link
Contributor

Choose a reason for hiding this comment

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

providedExtensionSignature

It looks like we can initialize outside of the loop

refCustomModifiers: [],
explicitInterfaceImplementations: []);

ImmutableArray<ParameterSymbol> extensionParameterSymbols = syntax.Parameters is { } extensionParameterListSyntax ? BindCrefParameters(extensionParameterListSyntax, diagnostics) : default;
Copy link
Contributor

Choose a reason for hiding this comment

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

BindCrefParameters(extensionParameterListSyntax, diagnostics)

It looks like we should attempt to bind parameters even when the enclosing loop never reaches this line.

@@ -357,6 +357,8 @@ internal static bool HasParameterList(CrefSyntax crefSyntax)
return ((OperatorMemberCrefSyntax)crefSyntax).Parameters != null;
case SyntaxKind.ConversionOperatorMemberCref:
return ((ConversionOperatorMemberCrefSyntax)crefSyntax).Parameters != null;
case SyntaxKind.ExtensionMemberCref:
return HasParameterList(((ExtensionMemberCrefSyntax)crefSyntax).Member);
Copy link
Contributor

@AlekseyTs AlekseyTs May 30, 2025

Choose a reason for hiding this comment

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

return HasParameterList(((ExtensionMemberCrefSyntax)crefSyntax).Member);

What about ExtensionMemberCrefSyntax.Parameters? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

Parameters on extension are intentionally not considered here.
The value from HasParameterList affects the result kind from GetCrefSymbolInfo below, to report "ambiguous" vs. "overload resolution failure". In E.extension(int).M... we only allow the last parameter list to be omitted, so the presence/absence of the parameter list on extension is not relevant to was result kind we report.

N(SyntaxKind.CloseParenToken);
}
N(SyntaxKind.DotToken);
N(SyntaxKind.ExtensionMemberCref);
Copy link
Contributor

Choose a reason for hiding this comment

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

N(SyntaxKind.ExtensionMemberCref);

The nested ExtensionMemberCref feels wrong

@@ -1915,7 +1915,10 @@ void I.M() { }
comp.VerifyEmitDiagnostics(
// (10,16): error CS0541: 'Extensions.extension(object).M()': explicit interface declaration can only be declared in a class, record, struct or interface
// void I.M() { }
Diagnostic(ErrorCode.ERR_ExplicitInterfaceImplementationInNonClassOrStruct, "M").WithArguments("Extensions.extension(object).M()").WithLocation(10, 16));
Diagnostic(ErrorCode.ERR_ExplicitInterfaceImplementationInNonClassOrStruct, "M").WithArguments("Extensions.extension(object).M()").WithLocation(10, 16),
// (10,16): error CS9282: Extension declarations can include only methods or properties
Copy link
Contributor

Choose a reason for hiding this comment

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

// (10,16): error CS9282: Extension declarations can include only methods or properties

Technically this error reported for a method.

public void Cref_44()
{
var src = """
/// <see cref="extension(int).M"/>
Copy link
Contributor

Choose a reason for hiding this comment

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

extension(int).M

Do we have a test for a success scenario with syntax like that?

/// <see cref="E.extension(int).M2"/>
static class E
{
extension(int i)
Copy link
Contributor

Choose a reason for hiding this comment

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

extension(int i)

Consider adding a test where ambiguous candidates are in different blocks.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Extension Everything The extension everything feature Needs API Review Needs to be reviewed by the API review council
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants