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

Incorrect "CS0229 Ambiguity between" for partial prop with primary ctor, at design time only #76651

Closed
jnm2 opened this issue Jan 7, 2025 · 2 comments · Fixed by #76871
Closed

Comments

@jnm2
Copy link
Contributor

jnm2 commented Jan 7, 2025

Version Used: 17.13.0 Preview 2.1

Thanks to @RoccoZero who reported that this issue has been happening for a few months.

  1. Building succeeds, though the error list and code editor show the errors.
  2. The repro requires separate source files.
  3. The repro disappears if the primary ctor is deleted.
.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
  </PropertyGroup>

</Project>
File 1
partial class PartialClass
{
    public partial string Prop => null;
}

partial class PartialClass(int p)
{
    public partial string Prop { get; }
}
File 2
class Repro
{
    void M(PartialClass c)
    {
        // ❌ CS0229 Ambiguity between 'PartialClass.Prop' and 'PartialClass.Prop'
        _ = c.Prop;
        _ = c.Prop; // Yep, error still here.
        _ = new PartialClass(0).Prop; // No error here!
        _ = c.Prop; // ERROR DISAPPEARS
        _ = c.Prop; // IT'S STILL GONE
    }
}

Image

Image

@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 7, 2025
@jaredpar jaredpar added Feature - Primary Constructors and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 8, 2025
@jaredpar jaredpar added this to the 17.14 milestone Jan 8, 2025
@AlekseyTs
Copy link
Contributor

It looks like there is a race around merging of partial members that can be observed in the following unit-tests.

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76651")]
        public void PartialMemberAmbiguity_01()
        {
            var source1 = """
class Repro
{
    void M(PartialClass c)
    {
        _ = c.Prop;
    }
}
""";

            var source2 = """
class PartialClass(int p)
{
    public partial string Prop => null;
    public partial string Prop { get; }
}
""";
            var comp = CreateCompilation([source1, source2]);

            var tree = comp.SyntaxTrees[0];
            var model = comp.GetSemanticModel(tree, ignoreAccessibility: false);

            model.GetDiagnostics().Verify(
                // (5,15): error CS0229: Ambiguity between 'PartialClass.Prop' and 'PartialClass.Prop'
                //         _ = c.Prop;
                Diagnostic(ErrorCode.ERR_AmbigMember, "Prop").WithArguments("PartialClass.Prop", "PartialClass.Prop").WithLocation(5, 15)
                );
        }

        [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76651")]
        public void PartialMemberAmbiguity_02()
        {
            var source1 = """
class Repro
{
    void M(PartialClass c)
    {
        c.M();
    }
}
""";

            var source2 = """
class PartialClass(int p)
{
    public partial string M() => null;
    public partial string M();
}
""";
            var comp = CreateCompilation([source1, source2]);

            var tree = comp.SyntaxTrees[0];
            var model = comp.GetSemanticModel(tree, ignoreAccessibility: false);

            model.GetDiagnostics().Verify(
                // (5,11): error CS0121: The call is ambiguous between the following methods or properties: 'PartialClass.M()' and 'PartialClass.M()'
                //         c.M();
                Diagnostic(ErrorCode.ERR_AmbigCall, "M").WithArguments("PartialClass.M()", "PartialClass.M()").WithLocation(5, 11)
                );
        }

@AlekseyTs
Copy link
Contributor

It looks like #75002 is the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants