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

Merge pull request #16351 from CyrusNajmabadi/parserDiagnostics32

Move errors about params/__arglist being last out of the parser.
......@@ -3816,11 +3816,6 @@ private static bool CanReuseBracketedParameterList(CSharp.Syntax.BracketedParame
if (this.CurrentToken.Kind != closeKind)
{
tryAgain:
int mustBeLastIndex = -1;
bool mustBeLastHadParams = false;
bool hasParams = false;
bool hasArgList = false;
if (this.IsPossibleParameter() || this.CurrentToken.Kind == SyntaxKind.CommaToken)
{
// first parameter
......@@ -3828,14 +3823,6 @@ private static bool CanReuseBracketedParameterList(CSharp.Syntax.BracketedParame
modifiers.Clear();
var parameter = this.ParseParameter(attributes, modifiers);
nodes.Add(parameter);
hasParams = modifiers.Any((int)SyntaxKind.ParamsKeyword);
hasArgList = parameter.Identifier.Kind == SyntaxKind.ArgListKeyword;
bool mustBeLast = hasParams || hasArgList;
if (mustBeLast && mustBeLastIndex == -1)
{
mustBeLastIndex = nodes.Count - 1;
mustBeLastHadParams = hasParams;
}
// additional parameters
while (true)
......@@ -3857,15 +3844,6 @@ private static bool CanReuseBracketedParameterList(CSharp.Syntax.BracketedParame
}
nodes.Add(parameter);
hasParams = modifiers.Any((int)SyntaxKind.ParamsKeyword);
hasArgList = parameter.Identifier.Kind == SyntaxKind.ArgListKeyword;
mustBeLast = hasParams || hasArgList;
if (mustBeLast && mustBeLastIndex == -1)
{
mustBeLastIndex = nodes.Count - 1;
mustBeLastHadParams = hasParams;
}
continue;
}
else if (this.SkipBadParameterListTokens(ref open, nodes, SyntaxKind.CommaToken, closeKind) == PostSkipAction.Abort)
......@@ -3878,11 +3856,6 @@ private static bool CanReuseBracketedParameterList(CSharp.Syntax.BracketedParame
{
goto tryAgain;
}
if (mustBeLastIndex >= 0 && mustBeLastIndex < nodes.Count - 1)
{
nodes[mustBeLastIndex] = this.AddError(nodes[mustBeLastIndex], mustBeLastHadParams ? ErrorCode.ERR_ParamsLast : ErrorCode.ERR_VarargsLast);
}
}
_termState = saveTerm;
......
......@@ -28,10 +28,19 @@ internal static class ParameterHelpers
int firstDefault = -1;
var builder = ArrayBuilder<ParameterSymbol>.GetInstance();
ImmutableArray<ParameterSymbol> parameters;
var mustBeLastParameter = (ParameterSyntax)null;
foreach (var parameterSyntax in syntax.Parameters)
{
if (mustBeLastParameter == null)
{
if (parameterSyntax.Modifiers.Any(SyntaxKind.ParamsKeyword) ||
parameterSyntax.Identifier.Kind() == SyntaxKind.ArgListKeyword)
{
mustBeLastParameter = parameterSyntax;
}
}
CheckParameterModifiers(parameterSyntax, diagnostics);
var refKind = GetModifiers(parameterSyntax.Modifiers,
......@@ -92,7 +101,16 @@ internal static class ParameterHelpers
++parameterIndex;
}
parameters = builder.ToImmutableAndFree();
if (mustBeLastParameter != null && mustBeLastParameter != syntax.Parameters.Last())
{
diagnostics.Add(
mustBeLastParameter.Identifier.Kind() == SyntaxKind.ArgListKeyword
? ErrorCode.ERR_VarargsLast
: ErrorCode.ERR_ParamsLast,
mustBeLastParameter.GetLocation());
}
ImmutableArray<ParameterSymbol> parameters = builder.ToImmutableAndFree();
var methodOwner = owner as MethodSymbol;
var typeParameters = (object)methodOwner != null ?
......
......@@ -5397,8 +5397,14 @@ class C
void M(__arglist, int j) {}
}";
TestError(text1, ErrorCode.ERR_ParamsLast);
TestError(text2, ErrorCode.ERR_VarargsLast);
CreateCompilationWithMscorlib(text1).VerifyDiagnostics(
// (4,11): error CS0231: A params parameter must be the last parameter in a formal parameter list
// void M(params int[] i, int j) {}
Diagnostic(ErrorCode.ERR_ParamsLast, "params int[] i").WithLocation(4, 11));
CreateCompilationWithMscorlib(text2).VerifyDiagnostics(
// (4,11): error CS0257: An __arglist parameter must be the last parameter in a formal parameter list
// void M(__arglist, int j) {}
Diagnostic(ErrorCode.ERR_VarargsLast, "__arglist").WithLocation(4, 11));
}
[Fact]
......
......@@ -317,7 +317,6 @@ public struct st { }
public void CS0231ERR_ParamsLast()
{
var test = @"
using System;
public class MyClass {
public void MyMeth(params int[] values, int i) {}
public static int Main() {
......@@ -326,7 +325,10 @@ public class MyClass {
}
";
ParseAndValidate(test, Diagnostic(ErrorCode.ERR_ParamsLast, "params int[] values"));
CreateCompilationWithMscorlib45(test).VerifyDiagnostics(
// (3,24): error CS0231: A params parameter must be the last parameter in a formal parameter list
// public void MyMeth(params int[] values, int i) {}
Diagnostic(ErrorCode.ERR_ParamsLast, "params int[] values").WithLocation(3, 24));
}
[Fact]
......@@ -341,10 +343,10 @@ public void Bar(__arglist, int b)
}
";
ParseAndValidate(test,
// (4,19): error CS0257: An __arglist parameter must be the last parameter in a formal parameter list
// public void Bar(__arglist, int b)
Diagnostic(ErrorCode.ERR_VarargsLast, "__arglist"));
CreateCompilationWithMscorlib(test).VerifyDiagnostics(
// (4,19): error CS0257: An __arglist parameter must be the last parameter in a formal parameter list
// public void Bar(__arglist, int b)
Diagnostic(ErrorCode.ERR_VarargsLast, "__arglist"));
}
[WorkItem(536668, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/536668")]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册