提交 419b7358 编写于 作者: A AlekseyTs

Report ERR_NoConversionForNubDefaultParam even if default parameter value is not default(T).

Fixes #6186.
上级 3d8c525c
......@@ -268,7 +268,7 @@ internal static class ParameterHelpers
hasErrors = true;
}
else if (conversion.IsNullable && defaultExpression.Kind == BoundKind.DefaultOperator && !defaultExpression.Type.IsNullableType() &&
else if (conversion.IsNullable && !defaultExpression.Type.IsNullableType() &&
!(parameterType.GetNullableUnderlyingType().IsEnumType() || parameterType.GetNullableUnderlyingType().IsIntrinsicType()))
{
// We can do:
......
......@@ -15366,7 +15366,7 @@ List<IFoo> IBar<string>.GetList()
Diagnostic(ErrorCode.ERR_GenericsUsedInNoPIAType));
}
[Fact]
[Fact, WorkItem(6186, "https://github.com/dotnet/roslyn/issues/6186")]
public void CS1770ERR_NoConversionForNubDefaultParam()
{
var text = @"using System;
......@@ -15378,12 +15378,22 @@ public enum E { None }
public void Foo1(int? x = default(int)) { }
public void Foo2(E? x = default(E)) { }
public void Foo3(DateTime? x = default(DateTime?)) { }
public void Foo4(DateTime? x = new DateTime?()) { }
// Error:
public void Foo(DateTime? x = default(DateTime)) { }
public void Foo11(DateTime? x = default(DateTime)) { }
public void Foo12(DateTime? x = new DateTime()) { }
}";
var comp = DiagnosticsUtils.VerifyErrorsAndGetCompilationWithMscorlib(text,
new ErrorDescription { Code = 1770, Line = 12, Column = 31 });
var comp = CreateCompilationWithMscorlib(text);
comp.VerifyDiagnostics(
// (13,33): error CS1770: A value of type 'DateTime' cannot be used as default parameter for nullable parameter 'x' because 'DateTime' is not a simple type
// public void Foo11(DateTime? x = default(DateTime)) { }
Diagnostic(ErrorCode.ERR_NoConversionForNubDefaultParam, "x").WithArguments("System.DateTime", "x").WithLocation(13, 33),
// (14,33): error CS1770: A value of type 'DateTime' cannot be used as default parameter for nullable parameter 'x' because 'DateTime' is not a simple type
// public void Foo12(DateTime? x = new DateTime()) { }
Diagnostic(ErrorCode.ERR_NoConversionForNubDefaultParam, "x").WithArguments("System.DateTime", "x").WithLocation(14, 33)
);
}
[Fact]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册