提交 b2717f87 编写于 作者: N Nikolay 提交者: Neal Gafter

Improve error priorities in case of using static class as a generic argument (#36203) (#37613)

上级 a3522b7c
......@@ -289,26 +289,29 @@ private static ThreeState TryGetBestResult(ArrayBuilder<MemberResolutionResult<T
return;
}
// Otherwise, if there is any such method that has a bad argument conversion or out/ref mismatch
// then the first such method found is the best bad method.
// Otherwise, if there is any such method where type inference succeeded but inferred
// type arguments that violate the constraints on the method, then the first such method is
// the best bad method.
if (HadBadArguments(diagnostics, binder, name, arguments, symbols, location, binder.Flags, isMethodGroupConversion))
if (HadConstraintFailure(location, diagnostics))
{
return;
}
// Since we didn't return...
AssertNone(MemberResolutionKind.BadArgumentConversion);
AssertNone(MemberResolutionKind.ConstraintFailure);
// Otherwise, if there is any such method where type inference succeeded but inferred
// type arguments that violate the constraints on the method, then the first such method is
// the best bad method.
// Otherwise, if there is any such method that has a bad argument conversion or out/ref mismatch
// then the first such method found is the best bad method.
if (HadConstraintFailure(location, diagnostics))
if (HadBadArguments(diagnostics, binder, name, arguments, symbols, location, binder.Flags, isMethodGroupConversion))
{
return;
}
// Since we didn't return...
AssertNone(MemberResolutionKind.BadArgumentConversion);
// Otherwise, if there is any such method where type inference succeeded but inferred
// a parameter type that violates its own constraints then the first such method is
// the best bad method.
......
......@@ -2816,12 +2816,12 @@ static void Main()
ReturnedValue:
IParameterReferenceOperation: x (OperationKind.ParameterReference, Type: ?) (Syntax: 'x')
", new DiagnosticDescription[] {
// file.cs(9,18): error CS1929: 'string' does not contain a definition for 'Cast' and the best extension method overload 'Queryable.Cast<GC>(IQueryable)' requires a receiver of type 'IQueryable'
// file.cs(9,31): error CS0718: 'GC': static types cannot be used as type arguments
// var q2 = string.Empty.Cast<GC>().Select(x => x);
Diagnostic(ErrorCode.ERR_BadInstanceArgType, "string.Empty").WithArguments("string", "Cast", "System.Linq.Queryable.Cast<System.GC>(System.Linq.IQueryable)", "System.Linq.IQueryable").WithLocation(9, 18),
// file.cs(10,41): error CS1929: 'string' does not contain a definition for 'Cast' and the best extension method overload 'Queryable.Cast<GC>(IQueryable)' requires a receiver of type 'IQueryable'
Diagnostic(ErrorCode.ERR_GenericArgIsStaticClass, "Cast<GC>").WithArguments("System.GC").WithLocation(9, 31),
// file.cs(10,28): error CS0718: 'GC': static types cannot be used as type arguments
// var q1 = /*<bind>*/from GC x in string.Empty select x/*</bind>*/;
Diagnostic(ErrorCode.ERR_BadInstanceArgType, "string.Empty").WithArguments("string", "Cast", "System.Linq.Queryable.Cast<System.GC>(System.Linq.IQueryable)", "System.Linq.IQueryable").WithLocation(10, 41)
Diagnostic(ErrorCode.ERR_GenericArgIsStaticClass, "from GC x in string.Empty").WithArguments("System.GC").WithLocation(10, 28)
});
}
......
......@@ -10488,6 +10488,26 @@ static void Main()
new ErrorDescription[] { new ErrorDescription { Code = (int)ErrorCode.ERR_AnonMethGrpInForEach, Line = 7, Column = 27 } });
}
[Fact]
[WorkItem(36203, "https://github.com/dotnet/roslyn/issues/36203")]
public void CS0452_GenericConstraintError_HasHigherPriorityThanMethodOverloadError()
{
var code = @"
class Code
{
void GenericMethod<T>(int i) where T: class => throw null;
void GenericMethod<T>(string s) => throw null;
void IncorrectMethodCall()
{
GenericMethod<int>(1);
}
}";
CreateCompilation(code).VerifyDiagnostics(
// (9,9): error CS0452: The type 'int' must be a reference type in order to use it as parameter 'T' in the generic type or method 'Code.GenericMethod<T>(int)'
Diagnostic(ErrorCode.ERR_RefConstraintNotSatisfied, "GenericMethod<int>").WithArguments("Code.GenericMethod<T>(int)", "T", "int").WithLocation(9, 9));
}
[Fact]
public void CS0457ERR_AmbigUDConv()
{
......@@ -11413,6 +11433,28 @@ public static void Main()
new ErrorDescription[] { new ErrorDescription { Code = (int)ErrorCode.ERR_ConvertToStaticClass, Line = 12, Column = 34 } });
}
[Fact]
[WorkItem(36203, "https://github.com/dotnet/roslyn/issues/36203")]
public void CS0718_StaticClassError_HasHigherPriorityThanMethodOverloadError()
{
var code = @"
static class StaticClass { }
class Code
{
void GenericMethod<T>(int i) => throw null;
void GenericMethod<T>(string s) => throw null;
void IncorrectMethodCall()
{
GenericMethod<StaticClass>(1);
}
}";
CreateCompilation(code).VerifyDiagnostics(
// (11,9): error CS0718: 'StaticClass': static types cannot be used as type arguments
Diagnostic(ErrorCode.ERR_GenericArgIsStaticClass, "GenericMethod<StaticClass>").WithArguments("StaticClass").WithLocation(11, 9));
}
[Fact]
public void CS0723ERR_VarDeclIsStaticClass_Locals()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册