diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs b/src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs index 7231206417ec0a3e04bf84d3e0220003b81932ae..2476b61eb152961c6349d34306dda5dc9b06fcc7 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/ParameterHelpers.cs @@ -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: diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs index cee4d360bcce2e2b9dc0d53af66a3aca7b0d92bb..129f3c7e46201503ab6707a160c14a86b5d9c65a 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs @@ -15366,7 +15366,7 @@ List IBar.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]