-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Labels
Area-CompilersConcept-Diagnostic ClarityThe issues deals with the ease of understanding of errors and warnings.The issues deals with the ease of understanding of errors and warnings.
Description
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?).
ViktorHofer
Metadata
Metadata
Assignees
Labels
Area-CompilersConcept-Diagnostic ClarityThe issues deals with the ease of understanding of errors and warnings.The issues deals with the ease of understanding of errors and warnings.