Skip to content

Conversation

@AlekseyTs
Copy link
Contributor

@AlekseyTs AlekseyTs commented Mar 14, 2025

Specifically, two static extension methods are allowed to have the same signature (according to the current language rules) as long as all the following conditions are met:

  • methods have different return type and/or different return ref-ness
  • methods extending different types (ref-ness of the receiver parameter is not considered)

Relates to test plan #76130

Specifically, two static extension methods are allowed to have the same signature
(according to the current language rules) as long as all the following conditions are met:
- methods have different return type and/or different return ref-ness
- methods extending different types (ref-ness of the receiver parameter is not considered)
@AlekseyTs AlekseyTs added Area-Compilers Feature - Extension Everything The extension everything feature labels Mar 14, 2025
@AlekseyTs AlekseyTs requested review from jcouv and jjonescz March 14, 2025 02:34
@AlekseyTs AlekseyTs requested a review from a team as a code owner March 14, 2025 02:34
@ghost ghost added the untriaged Issues and PRs which have not yet been triaged by a lead label Mar 14, 2025
@AlekseyTs
Copy link
Contributor Author

@jcouv, @jjonescz, @dotnet/roslyn-compiler Please review

@jcouv jcouv self-assigned this Mar 14, 2025

staticExtensionImplementationsWithoutReturn ??= new Dictionary<MethodSymbol, OneOrMany<MethodSymbol>>(MemberSignatureComparer.DuplicateSourceComparer);

if (staticExtensionImplementationsWithoutReturn.TryGetValue(staticExtension, out OneOrMany<MethodSymbol> previousStaticExtension))
Copy link
Member

Choose a reason for hiding this comment

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

previousStaticExtension

nit: plural?

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 1)

@AlekseyTs
Copy link
Contributor Author

@jjonescz, @dotnet/roslyn-compiler For the second review

// static public string M2(int x) => throw null;
Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "M2").WithArguments("M2", "Extensions").WithLocation(12, 30)
);
}
Copy link
Member

@jcouv jcouv Mar 15, 2025

Choose a reason for hiding this comment

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

Do we expect this error?

    [Fact]
    public void TODO2()
    {
        var src = """
static class E
{
    extension(object)
    {
        public void Method() {  }
        public static void Method() { }
    }
}
""";
        var comp = CreateCompilation(src);
        comp.VerifyEmitDiagnostics(
            // (6,28): error CS0111: Type 'E.extension(object)' already defines a member called 'Method' with the same parameter types
            //         public static void Method() { }
            Diagnostic(ErrorCode.ERR_MemberAlreadyExists, "Method").WithArguments("Method", "E.extension(object)").WithLocation(6, 28));
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we expect this error?

Given the current rules, we do. This looks like M9 scenario in SignatureConflict_02. There is a PROTOTYPE comment regarding the current behavior

Copy link
Member

Choose a reason for hiding this comment

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

The M9 scenario in SignatureConflict_02 is:

    extension(int receiver)
    {
        public void M9() {}
    }

    extension(int receiver)
    {
        public static void M9(int x) {}
    }

M9 generates conflicting signatures in metadata, so should generate an error.
But in the scenario above, the implementation methods have different signatures, so I would expect no error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so I would expect no error.

Are both methods static extensions?

@AlekseyTs AlekseyTs merged commit 587b27c into dotnet:features/extensions Mar 17, 2025
24 checks passed
AlekseyTs added a commit that referenced this pull request Mar 17, 2025
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 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.

3 participants