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

Open
jnm2 opened this issue Jan 7, 2025 · 1 comment

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

@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)
                );
        }

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

No branches or pull requests

3 participants