提交 f08f7c03 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #16422 from CyrusNajmabadi/parserDiagnostics36

Move syntax checking of ObjectCreationExpressions out of the parser.
......@@ -3565,7 +3565,7 @@ private BoundExpression BindClassCreationExpression(ObjectCreationExpressionSynt
}
else if (node.Type.Kind() == SyntaxKind.TupleType)
{
Debug.Assert(node.HasErrors, "new <tuple type> should be a syntax error");
diagnostics.Add(ErrorCode.ERR_NewWithTupleTypeSyntax, node.Type.GetLocation());
return MakeBadExpressionForObjectCreation(node, type, boundInitializerOpt, analyzedArguments);
}
......
......@@ -10414,18 +10414,12 @@ private ExpressionSyntax ParseArrayOrObjectCreationExpression()
if (argumentList == null && initializer == null)
{
argumentList = _syntaxFactory.ArgumentList(
this.AddError(SyntaxFactory.MissingToken(SyntaxKind.OpenParenToken), ErrorCode.ERR_BadNewExpr),
this.EatToken(SyntaxKind.OpenParenToken, ErrorCode.ERR_BadNewExpr),
default(SeparatedSyntaxList<ArgumentSyntax>),
SyntaxFactory.MissingToken(SyntaxKind.CloseParenToken));
}
var objectCreation = _syntaxFactory.ObjectCreationExpression(@new, type, argumentList, initializer);
if (type.Kind == SyntaxKind.TupleType)
{
objectCreation = this.AddError(objectCreation, type, ErrorCode.ERR_NewWithTupleTypeSyntax);
}
return objectCreation;
return _syntaxFactory.ObjectCreationExpression(@new, type, argumentList, initializer);
}
}
......
......@@ -2132,12 +2132,11 @@ public static void Main()
{
e = new base; // CS1031, not a type
e = new this; // CS1031, not a type
e = new (); // CS1031, too few tuple elements
}
}
}
";
// TODO: this appears to be a severe regression from Dev10, which neatly reported 3 errors.
ParseAndValidate(text, TestOptions.Regular,
// (7,21): error CS1031: Type expected
// e = new base; // CS1031, not a type
......@@ -2156,17 +2155,37 @@ public static void Main()
Diagnostic(ErrorCode.ERR_BadNewExpr, "this").WithLocation(8, 21),
// (8,21): error CS1002: ; expected
// e = new this; // CS1031, not a type
Diagnostic(ErrorCode.ERR_SemicolonExpected, "this").WithLocation(8, 21),
// (9,21): error CS8181: 'new' cannot be used with tuple type. Use a tuple literal expression instead.
// e = new (); // CS1031, too few tuple elements
Diagnostic(ErrorCode.ERR_NewWithTupleTypeSyntax, "()").WithLocation(9, 21),
// (9,22): error CS8124: Tuple must contain at least two elements.
// e = new (); // CS1031, too few tuple elements
Diagnostic(ErrorCode.ERR_TupleTooFewElements, ")").WithLocation(9, 22),
// (9,23): error CS1526: A new expression requires (), [], or {} after type
// e = new (); // CS1031, too few tuple elements
Diagnostic(ErrorCode.ERR_BadNewExpr, ";").WithLocation(9, 23)
);
Diagnostic(ErrorCode.ERR_SemicolonExpected, "this").WithLocation(8, 21));
}
[Fact]
public void CS1031ERR_TypeExpected02_Tuple()
{
var text = @"namespace x
{
public class a
{
public static void Main()
{
var e = new ();
}
}
}
";
CreateCompilationWithMscorlib(text).VerifyDiagnostics(
// (7,26): error CS8124: Tuple must contain at least two elements.
// var e = new ();
Diagnostic(ErrorCode.ERR_TupleTooFewElements, ")").WithLocation(7, 26),
// (7,27): error CS1526: A new expression requires (), [], or {} after type
// var e = new ();
Diagnostic(ErrorCode.ERR_BadNewExpr, ";").WithLocation(7, 27),
// (7,25): error CS8181: 'new' cannot be used with tuple type. Use a tuple literal expression instead.
// var e = new ();
Diagnostic(ErrorCode.ERR_NewWithTupleTypeSyntax, "()").WithLocation(7, 25),
// (7,25): error CS8179: Predefined type 'System.ValueTuple`2' is not defined or imported
// var e = new ();
Diagnostic(ErrorCode.ERR_PredefinedValueTupleTypeNotFound, "()").WithArguments("System.ValueTuple`2").WithLocation(7, 25));
}
[Fact]
......@@ -2180,7 +2199,6 @@ public static void Main()
{
e = new base; // CS1031, not a type
e = new this; // CS1031, not a type
e = new (); // CS1031, not a type
}
}
}
......@@ -2204,20 +2222,68 @@ public static void Main()
Diagnostic(ErrorCode.ERR_BadNewExpr, "this").WithLocation(8, 21),
// (8,21): error CS1002: ; expected
// e = new this; // CS1031, not a type
Diagnostic(ErrorCode.ERR_SemicolonExpected, "this").WithLocation(8, 21),
// (9,21): error CS8181: 'new' cannot be used with tuple type. Use a tuple literal expression instead.
// e = new (); // CS1031, not a type
Diagnostic(ErrorCode.ERR_NewWithTupleTypeSyntax, "()").WithLocation(9, 21),
// (9,21): error CS8059: Feature 'tuples' is not available in C# 6. Please use language version 7 or greater.
// e = new (); // CS1031, not a type
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion6, "()").WithArguments("tuples", "7").WithLocation(9, 21),
// (9,22): error CS8124: Tuple must contain at least two elements.
// e = new (); // CS1031, not a type
Diagnostic(ErrorCode.ERR_TupleTooFewElements, ")").WithLocation(9, 22),
// (9,23): error CS1526: A new expression requires (), [], or {} after type
// e = new (); // CS1031, not a type
Diagnostic(ErrorCode.ERR_BadNewExpr, ";").WithLocation(9, 23)
);
Diagnostic(ErrorCode.ERR_SemicolonExpected, "this").WithLocation(8, 21));
}
[Fact]
public void CS1031ERR_TypeExpected02WithCSharp6_Tuple()
{
var text = @"namespace x
{
public class a
{
public static void Main()
{
var e = new ();
}
}
}
";
CreateCompilationWithMscorlib(text, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.CSharp6)).VerifyDiagnostics(
// (7,25): error CS8059: Feature 'tuples' is not available in C# 6. Please use language version 7 or greater.
// var e = new ();
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion6, "()").WithArguments("tuples", "7").WithLocation(7, 25),
// (7,26): error CS8124: Tuple must contain at least two elements.
// var e = new ();
Diagnostic(ErrorCode.ERR_TupleTooFewElements, ")").WithLocation(7, 26),
// (7,27): error CS1526: A new expression requires (), [], or {} after type
// var e = new ();
Diagnostic(ErrorCode.ERR_BadNewExpr, ";").WithLocation(7, 27),
// (7,25): error CS8181: 'new' cannot be used with tuple type. Use a tuple literal expression instead.
// var e = new ();
Diagnostic(ErrorCode.ERR_NewWithTupleTypeSyntax, "()").WithLocation(7, 25),
// (7,25): error CS8179: Predefined type 'System.ValueTuple`2' is not defined or imported
// var e = new ();
Diagnostic(ErrorCode.ERR_PredefinedValueTupleTypeNotFound, "()").WithArguments("System.ValueTuple`2").WithLocation(7, 25));
}
[Fact]
public void CS1031ERR_TypeExpected02WithCSharp7_Tuple()
{
var text = @"namespace x
{
public class a
{
public static void Main()
{
var e = new ();
}
}
}
";
CreateCompilationWithMscorlib(text, parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.CSharp7)).VerifyDiagnostics(
// (7,26): error CS8124: Tuple must contain at least two elements.
// var e = new ();
Diagnostic(ErrorCode.ERR_TupleTooFewElements, ")"),
// (7,27): error CS1526: A new expression requires (), [], or {} after type
// var e = new ();
Diagnostic(ErrorCode.ERR_BadNewExpr, ";"),
// (7,25): error CS8179: Predefined type 'System.ValueTuple`2' is not defined or imported
// var e = new ();
Diagnostic(ErrorCode.ERR_PredefinedValueTupleTypeNotFound, "()").WithArguments("System.ValueTuple`2"),
// (7,25): error CS8181: 'new' cannot be used with tuple type. Use a tuple literal expression instead.
// var e = new ();
Diagnostic(ErrorCode.ERR_NewWithTupleTypeSyntax, "()"));
}
[WorkItem(541347, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/541347")]
......@@ -5633,4 +5699,4 @@ public static void Main(string[] args)
#endregion
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册