Skip to content

Ambiguous predefined type should result in clearer diagnostic #79195

@jjonescz

Description

@jjonescz

Version Used: current main (2d9f760)

Steps to Reproduce:

[Fact]
public void CS0518ERR_PredefinedTypeAmbiguous()
{
    string code = """
        var range = 1..2;
        """;

    CreateCompilation(code, targetFramework: TargetFramework.NetStandard20).VerifyDiagnostics(
        // (1,13): error CS0518: Predefined type 'System.Range' is not defined or imported
        // var range = 1..2;
        Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "1..2").WithArguments("System.Range").WithLocation(1, 13),
        // (1,13): error CS0518: Predefined type 'System.Index' is not defined or imported
        // var range = 1..2;
        Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "1").WithArguments("System.Index").WithLocation(1, 13),
        // (1,16): error CS0518: Predefined type 'System.Index' is not defined or imported
        // var range = 1..2;
        Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "2").WithArguments("System.Index").WithLocation(1, 16));

    CreateCompilation(code, [createReference("Ref0")], targetFramework: TargetFramework.NetStandard20).VerifyDiagnostics();

    CreateCompilation(code, [createReference("Ref1"), createReference("Ref2")], targetFramework: TargetFramework.NetStandard20).VerifyDiagnostics(
        // (1,13): error CS0518: Predefined type 'System.Range' is not defined or imported
        // var range = 1..2;
        Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "1..2").WithArguments("System.Range").WithLocation(1, 13),
        // (1,13): error CS0518: Predefined type 'System.Index' is not defined or imported
        // var range = 1..2;
        Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "1").WithArguments("System.Index").WithLocation(1, 13),
        // (1,16): error CS0518: Predefined type 'System.Index' is not defined or imported
        // var range = 1..2;
        Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "2").WithArguments("System.Index").WithLocation(1, 16));

    static MetadataReference createReference(string name)
    {
        string code = """
            namespace System;
            public struct Index
            {
                public int GetOffset(int length) => 0;
                public static implicit operator Index(int value) => default;
            }
            public struct Range
            {
                public Range(Index start, Index end) { }
                public Index Start => default;
                public Index End => default;
            }
            """;
        return CreateCompilation(code, assemblyName: name)
            .VerifyDiagnostics()
            .EmitToPortableExecutableReference();
    }
}

Expected Behavior: When the predefined type is ambiguous (it is defined in two referenced assemblies), the error should make that clear. In a real-world case of this, I had to debug the compiler to figure out which references are the culprit.

Actual Behavior: The error for ambiguous predefined type says "Predefined type '...' is not defined or imported". See the unit test above, it captures the current behavior. That is both wrong (the type is imported) and makes fixing the error difficult (how to determine which references cause the ambiguity?).

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions