提交 66f88c70 编写于 作者: J Julien Couvreur 提交者: GitHub

Suppress cascading diagnostic in default value for parameter (#20077)

上级 93e489f7
......@@ -187,6 +187,10 @@ protected ConstantValue MakeDefaultExpression(DiagnosticBag diagnostics, Binder
BoundExpression valueBeforeConversion;
var convertedExpression = binderForDefault.BindParameterDefaultValue(defaultSyntax, parameterType, diagnostics, out valueBeforeConversion);
if (valueBeforeConversion.HasErrors)
{
return ConstantValue.Bad;
}
bool hasErrors = ParameterHelpers.ReportDefaultParameterErrors(binder, ContainingSymbol, parameterSyntax, this, valueBeforeConversion, diagnostics);
if (hasErrors)
......
......@@ -845,10 +845,7 @@ static int Main()
CreateCompilationWithMscorlib45(source).VerifyDiagnostics(
// (10,32): error CS4032: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<int>'.
// static int Foo(int[] arr = await t)
Diagnostic(ErrorCode.ERR_BadAwaitWithoutAsyncMethod, "await t").WithArguments("int").WithLocation(10, 32),
// (10,26): error CS1750: A value of type 'void' cannot be used as a default parameter because there are no standard conversions to type 'int[]'
// static int Foo(int[] arr = await t)
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "arr").WithArguments("void", "int[]").WithLocation(10, 26)
Diagnostic(ErrorCode.ERR_BadAwaitWithoutAsyncMethod, "await t").WithArguments("int").WithLocation(10, 32)
);
}
......
......@@ -1960,25 +1960,16 @@ partial class C
CreateStandardCompilation(source).VerifyDiagnostics(
// (4,18): error CS0721: 'S': static types cannot be used as parameters
// partial void M(S s = new A());
Diagnostic(ErrorCode.ERR_ParameterIsStaticClass, "M").WithArguments("S"),
Diagnostic(ErrorCode.ERR_ParameterIsStaticClass, "M").WithArguments("S").WithLocation(4, 18),
// (5,18): error CS0721: 'S': static types cannot be used as parameters
// partial void M(S s = new B()) { }
Diagnostic(ErrorCode.ERR_ParameterIsStaticClass, "M").WithArguments("S"),
Diagnostic(ErrorCode.ERR_ParameterIsStaticClass, "M").WithArguments("S").WithLocation(5, 18),
// (5,30): error CS0246: The type or namespace name 'B' could not be found (are you missing a using directive or an assembly reference?)
// partial void M(S s = new B()) { }
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("B"),
// (5,22): error CS1750: A value of type 'B' cannot be used as a default parameter because there are no standard conversions to type 'S'
// partial void M(S s = new B()) { }
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s").WithArguments("B", "S"),
// (5,22): warning CS1066: The default value specified for parameter 's' will have no effect because it applies to a member that is used in contexts that do not allow optional arguments
// partial void M(S s = new B()) { }
Diagnostic(ErrorCode.WRN_DefaultValueForUnconsumedLocation, "s").WithArguments("s"),
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("B").WithLocation(5, 30),
// (4,30): error CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
// partial void M(S s = new A());
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("A"),
// (4,22): error CS1750: A value of type 'A' cannot be used as a default parameter because there are no standard conversions to type 'S'
// partial void M(S s = new A());
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s").WithArguments("A", "S")
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "A").WithArguments("A").WithLocation(4, 30)
);
}
......
......@@ -2700,18 +2700,12 @@ public ValueTuple(T1 item1, T2 item2)
// (6,61): error CS0103: The name 'z1' does not exist in the current context
// void Local1(bool b = M(arg is int z1, z1), int s1 = z1) {}
Diagnostic(ErrorCode.ERR_NameNotInContext, "z1").WithArguments("z1").WithLocation(6, 61),
// (6,56): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local1(bool b = M(arg is int z1, z1), int s1 = z1) {}
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s1").WithArguments("?", "int").WithLocation(6, 56),
// (7,30): error CS1736: Default parameter value for 'b' must be a compile-time constant
// void Local2(bool b = M(M(out int z2), z2), int s2 = z2) {}
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "M(M(out int z2), z2)").WithArguments("b").WithLocation(7, 30),
// (7,61): error CS0103: The name 'z2' does not exist in the current context
// void Local2(bool b = M(M(out int z2), z2), int s2 = z2) {}
Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(7, 61),
// (7,56): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local2(bool b = M(M(out int z2), z2), int s2 = z2) {}
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s2").WithArguments("?", "int").WithLocation(7, 56),
// (8,35): error CS8185: A declaration is not allowed in this context.
// void Local3(bool b = M(M((int z3, int a2) = (1, 2)), z3), int a3 = z3) {}
Diagnostic(ErrorCode.ERR_DeclarationExpressionNotPermitted, "int z3").WithLocation(8, 35),
......@@ -2721,27 +2715,18 @@ public ValueTuple(T1 item1, T2 item2)
// (8,76): error CS0103: The name 'z3' does not exist in the current context
// void Local3(bool b = M(M((int z3, int a2) = (1, 2)), z3), int a3 = z3) {}
Diagnostic(ErrorCode.ERR_NameNotInContext, "z3").WithArguments("z3").WithLocation(8, 76),
// (8,71): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local3(bool b = M(M((int z3, int a2) = (1, 2)), z3), int a3 = z3) {}
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "a3").WithArguments("?", "int").WithLocation(8, 71),
// (10,30): error CS1736: Default parameter value for 'b' must be a compile-time constant
// void Local4(bool b = M(arg is var z4, z4), int s1 = z4) {}
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "M(arg is var z4, z4)").WithArguments("b").WithLocation(10, 30),
// (10,61): error CS0103: The name 'z4' does not exist in the current context
// void Local4(bool b = M(arg is var z4, z4), int s1 = z4) {}
Diagnostic(ErrorCode.ERR_NameNotInContext, "z4").WithArguments("z4").WithLocation(10, 61),
// (10,56): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local4(bool b = M(arg is var z4, z4), int s1 = z4) {}
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s1").WithArguments("?", "int").WithLocation(10, 56),
// (11,30): error CS1736: Default parameter value for 'b' must be a compile-time constant
// void Local5(bool b = M(M(out var z5), z5), int s2 = z5) {}
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "M(M(out var z5), z5)").WithArguments("b").WithLocation(11, 30),
// (11,61): error CS0103: The name 'z5' does not exist in the current context
// void Local5(bool b = M(M(out var z5), z5), int s2 = z5) {}
Diagnostic(ErrorCode.ERR_NameNotInContext, "z5").WithArguments("z5").WithLocation(11, 61),
// (11,56): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local5(bool b = M(M(out var z5), z5), int s2 = z5) {}
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s2").WithArguments("?", "int").WithLocation(11, 56),
// (12,35): error CS8185: A declaration is not allowed in this context.
// void Local6(bool b = M(M((var z6, int a2) = (1, 2)), z6), int a3 = z6) {}
Diagnostic(ErrorCode.ERR_DeclarationExpressionNotPermitted, "var z6").WithLocation(12, 35),
......@@ -2751,9 +2736,6 @@ public ValueTuple(T1 item1, T2 item2)
// (12,76): error CS0103: The name 'z6' does not exist in the current context
// void Local6(bool b = M(M((var z6, int a2) = (1, 2)), z6), int a3 = z6) {}
Diagnostic(ErrorCode.ERR_NameNotInContext, "z6").WithArguments("z6").WithLocation(12, 76),
// (12,71): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local6(bool b = M(M((var z6, int a2) = (1, 2)), z6), int a3 = z6) {}
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "a3").WithArguments("?", "int").WithLocation(12, 71),
// (14,17): error CS0103: The name 'z1' does not exist in the current context
// int t = z1 + z2 + z3 + z4 + z5 + z6;
Diagnostic(ErrorCode.ERR_NameNotInContext, "z1").WithArguments("z1").WithLocation(14, 17),
......
......@@ -10414,10 +10414,7 @@ static bool TakeOutParam(int y, out int x)
// (11,25): error CS0841: Cannot use local variable 'x4' before it is declared
// void Test4(bool p = x4 && TakeOutParam(4, out int x4))
Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(11, 25),
// (11,21): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'bool'
// void Test4(bool p = x4 && TakeOutParam(4, out int x4))
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "bool").WithLocation(11, 21),
// (15,50): error CS0128: A local variable named 'x5' is already defined in this scope
// (15,50): error CS0128: A local variable or function named 'x5' is already defined in this scope
// TakeOutParam(52, out int x5) &&
Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(15, 50),
// (14,25): error CS1736: Default parameter value for 'p' must be a compile-time constant
......@@ -10525,10 +10522,7 @@ static bool TakeOutParam(int y, out int x)
// (11,25): error CS0841: Cannot use local variable 'x4' before it is declared
// void Test4(bool p = x4 && TakeOutParam(4, out var x4))
Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(11, 25),
// (11,21): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'bool'
// void Test4(bool p = x4 && TakeOutParam(4, out var x4))
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "bool").WithLocation(11, 21),
// (15,50): error CS0128: A local variable named 'x5' is already defined in this scope
// (15,50): error CS0128: A local variable or function named 'x5' is already defined in this scope
// TakeOutParam(52, out var x5) &&
Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(15, 50),
// (14,25): error CS1736: Default parameter value for 'p' must be a compile-time constant
......@@ -31362,9 +31356,6 @@ public static void Main(int arg)
// (6,61): error CS0103: The name 'z1' does not exist in the current context
// void Local2(bool b = M(M(out int z1), z1), int s2 = z1) { var t = z1; }
Diagnostic(ErrorCode.ERR_NameNotInContext, "z1").WithArguments("z1").WithLocation(6, 61),
// (6,56): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local2(bool b = M(M(out int z1), z1), int s2 = z1) { var t = z1; }
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s2").WithArguments("?", "int").WithLocation(6, 56),
// (7,75): error CS0103: The name 'z2' does not exist in the current context
// void Local5(bool b = M(M(out var z2), z2), int s2 = z2) { var t = z2; }
Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(7, 75),
......@@ -31374,9 +31365,6 @@ public static void Main(int arg)
// (7,61): error CS0103: The name 'z2' does not exist in the current context
// void Local5(bool b = M(M(out var z2), z2), int s2 = z2) { var t = z2; }
Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(7, 61),
// (7,56): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local5(bool b = M(M(out var z2), z2), int s2 = z2) { var t = z2; }
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s2").WithArguments("?", "int").WithLocation(7, 56),
// (9,17): error CS0103: The name 'z1' does not exist in the current context
// int x = z1 + z2;
Diagnostic(ErrorCode.ERR_NameNotInContext, "z1").WithArguments("z1").WithLocation(9, 17),
......@@ -32048,9 +32036,6 @@ public static void Main(int arg)
// (6,69): error CS0103: The name 'z1' does not exist in the current context
// void Local2(bool b = M(nameof(M(out int z1)), z1), int s2 = z1) { var t = z1; }
Diagnostic(ErrorCode.ERR_NameNotInContext, "z1").WithArguments("z1").WithLocation(6, 69),
// (6,64): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local2(bool b = M(nameof(M(out int z1)), z1), int s2 = z1) { var t = z1; }
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s2").WithArguments("?", "int").WithLocation(6, 64),
// (7,83): error CS0103: The name 'z2' does not exist in the current context
// void Local5(bool b = M(nameof(M(out var z2)), z2), int s2 = z2) { var t = z2; }
Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(7, 83),
......@@ -32063,9 +32048,6 @@ public static void Main(int arg)
// (7,69): error CS0103: The name 'z2' does not exist in the current context
// void Local5(bool b = M(nameof(M(out var z2)), z2), int s2 = z2) { var t = z2; }
Diagnostic(ErrorCode.ERR_NameNotInContext, "z2").WithArguments("z2").WithLocation(7, 69),
// (7,64): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// void Local5(bool b = M(nameof(M(out var z2)), z2), int s2 = z2) { var t = z2; }
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "s2").WithArguments("?", "int").WithLocation(7, 64),
// (9,17): error CS0103: The name 'z1' does not exist in the current context
// int x = z1 + z2;
Diagnostic(ErrorCode.ERR_NameNotInContext, "z1").WithArguments("z1").WithLocation(9, 17),
......@@ -3707,15 +3707,9 @@ void Test72(bool p = x7 > 2)
// (11,25): error CS0841: Cannot use local variable 'x4' before it is declared
// void Test4(bool p = x4 && 4 is int x4)
Diagnostic(ErrorCode.ERR_VariableUsedBeforeDeclaration, "x4").WithArguments("x4").WithLocation(11, 25),
// (11,21): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'bool'
// void Test4(bool p = x4 && 4 is int x4)
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "bool").WithLocation(11, 21),
// (15,35): error CS0128: A local variable named 'x5' is already defined in this scope
// (15,35): error CS0128: A local variable or function named 'x5' is already defined in this scope
// 52 is int x5 &&
Diagnostic(ErrorCode.ERR_LocalDuplicate, "x5").WithArguments("x5").WithLocation(15, 35),
// (14,21): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'bool'
// void Test5(bool p = 51 is int x5 &&
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "bool").WithLocation(14, 21),
// (19,27): error CS1736: Default parameter value for 'p1' must be a compile-time constant
// void Test61(bool p1 = 6 is int x6 && x6 > 0, bool p2 = 6 is int x6 && x6 > 0)
Diagnostic(ErrorCode.ERR_DefaultValueMustBeConstant, "6 is int x6 && x6 > 0").WithArguments("p1").WithLocation(19, 27),
......
......@@ -2218,13 +2218,11 @@ static int CalculateBMI(int weight, int height = weight)
CreateStandardCompilation(text).VerifyDiagnostics(
// (7,54): error CS0103: The name 'weight' does not exist in the current context
// static int CalculateBMI(int weight, int height = weight)
Diagnostic(ErrorCode.ERR_NameNotInContext, "weight").WithArguments("weight"),
// (7,45): error CS1750: A value of type '?' cannot be used as a default parameter because there are no standard conversions to type 'int'
// static int CalculateBMI(int weight, int height = weight)
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "height").WithArguments("?", "int"),
// (1,1): info CS8019: Unnecessary using directive.
Diagnostic(ErrorCode.ERR_NameNotInContext, "weight").WithArguments("weight").WithLocation(7, 54),
// (1,1): hidden CS8019: Unnecessary using directive.
// using System;
Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;"));
Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;").WithLocation(1, 1)
);
}
[Fact]
......
......@@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.Test.Utilities;
using System.Linq;
using Xunit;
using Roslyn.Test.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics
{
......@@ -31,6 +32,27 @@ static void Main()
);
}
[Fact]
[WorkItem(19013, "https://github.com/dotnet/roslyn/issues/19013")]
public void TestCSharp7Cascade()
{
string source = @"
using System.Threading;
using System.Threading.Tasks;
class C
{
async Task M(CancellationToken t = default) { await Task.Delay(0); }
}
";
var comp = CreateCompilationWithMscorlib46(source);
comp.VerifyDiagnostics(
// (7,40): error CS8107: Feature 'default literal' is not available in C# 7. Please use language version 7.1 or greater.
// async Task M(CancellationToken t = default) { await Task.Delay(0); }
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion7, "default").WithArguments("default literal", "7.1").WithLocation(7, 40)
);
}
[Fact]
public void AssignmentToInt()
{
......
......@@ -7560,26 +7560,23 @@ void M(int* p = stackalloc int[1])
CreateStandardCompilation(text, options: TestOptions.UnsafeReleaseDll).VerifyDiagnostics(
// (4,21): error CS1525: Invalid expression term 'stackalloc'
// void M(int* p = stackalloc int[1])
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "stackalloc").WithArguments("stackalloc"),
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "stackalloc").WithArguments("stackalloc").WithLocation(4, 21),
// (4,21): error CS1003: Syntax error, ',' expected
// void M(int* p = stackalloc int[1])
Diagnostic(ErrorCode.ERR_SyntaxError, "stackalloc").WithArguments(",", "stackalloc"),
Diagnostic(ErrorCode.ERR_SyntaxError, "stackalloc").WithArguments(",", "stackalloc").WithLocation(4, 21),
// (4,32): error CS1003: Syntax error, ',' expected
// void M(int* p = stackalloc int[1])
Diagnostic(ErrorCode.ERR_SyntaxError, "int").WithArguments(",", "int"),
Diagnostic(ErrorCode.ERR_SyntaxError, "int").WithArguments(",", "int").WithLocation(4, 32),
// (4,36): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
// void M(int* p = stackalloc int[1])
Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "1"),
Diagnostic(ErrorCode.ERR_ArraySizeInDeclaration, "1").WithLocation(4, 36),
// (4,38): error CS1001: Identifier expected
// void M(int* p = stackalloc int[1])
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")"),
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(4, 38),
// (4,38): error CS1737: Optional parameters must appear after all required parameters
// void M(int* p = stackalloc int[1])
Diagnostic(ErrorCode.ERR_DefaultValueBeforeRequiredValue, ")"),
// (4,17): error CS1750: A value of type '?' cannot be used as a default parameter
// because there are no standard conversions to type 'int*' void M(int* p =
// stackalloc int[1])
Diagnostic(ErrorCode.ERR_NoConversionForDefaultParam, "p").WithArguments("?", "int*"));
Diagnostic(ErrorCode.ERR_DefaultValueBeforeRequiredValue, ")").WithLocation(4, 38)
);
}
[Fact]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册