提交 1e13b4e9 编写于 作者: C CyrusNajmabadi

Move diagnostic from parser to later phase.

上级 45273896
......@@ -3371,11 +3371,20 @@ private ImmutableArray<BoundCatchBlock> BindCatchBlocks(SyntaxList<CatchClauseSy
}
var catchBlocks = ArrayBuilder<BoundCatchBlock>.GetInstance(n);
var hasCatchAll = false;
foreach (var catchSyntax in catchClauses)
{
if (hasCatchAll)
{
diagnostics.Add(ErrorCode.ERR_TooManyCatches, catchSyntax.CatchKeyword.GetLocation());
}
var catchBinder = this.GetBinder(catchSyntax);
var catchBlock = catchBinder.BindCatchBlock(catchSyntax, catchBlocks, diagnostics);
catchBlocks.Add(catchBlock);
hasCatchAll |= catchSyntax.Declaration == null && catchSyntax.Filter == null;
}
return catchBlocks.ToImmutableAndFree();
}
......
......@@ -7649,7 +7649,6 @@ private TryStatementSyntax ParseTryStatement()
try
{
bool hasEnd = false;
bool hasCatchAll = false;
if (this.CurrentToken.Kind == SyntaxKind.CatchKeyword)
{
......@@ -7657,9 +7656,7 @@ private TryStatementSyntax ParseTryStatement()
catches = _pool.Allocate<CatchClauseSyntax>();
while (this.CurrentToken.Kind == SyntaxKind.CatchKeyword)
{
var clause = this.ParseCatchClause(hasCatchAll);
hasCatchAll |= clause.Declaration == null && clause.Filter == null;
catches.Add(clause);
catches.Add(this.ParseCatchClause());
}
}
......@@ -7704,18 +7701,12 @@ private bool IsEndOfTryBlock()
|| this.CurrentToken.Kind == SyntaxKind.FinallyKeyword;
}
private CatchClauseSyntax ParseCatchClause(bool hasCatchAll)
private CatchClauseSyntax ParseCatchClause()
{
Debug.Assert(this.CurrentToken.Kind == SyntaxKind.CatchKeyword);
var @catch = this.EatToken();
// Check for the error of catch clause following empty catch here.
if (hasCatchAll)
{
@catch = this.AddError(@catch, ErrorCode.ERR_TooManyCatches);
}
CatchDeclarationSyntax decl = null;
var saveTerm = _termState;
......
......@@ -1711,10 +1711,19 @@ public static int Main()
}
";
ParseAndValidate(test,
Diagnostic(ErrorCode.ERR_TooManyCatches, "catch"),
Diagnostic(ErrorCode.ERR_TooManyCatches, "catch"),
Diagnostic(ErrorCode.ERR_TooManyCatches, "catch"));
CreateCompilationWithMscorlib(test).VerifyDiagnostics(
// (16,9): error CS1017: Catch clauses cannot follow the general catch clause of a try statement
// catch (S1) {}
Diagnostic(ErrorCode.ERR_TooManyCatches, "catch").WithLocation(16, 9),
// (17,9): error CS1017: Catch clauses cannot follow the general catch clause of a try statement
// catch (S) {}
Diagnostic(ErrorCode.ERR_TooManyCatches, "catch").WithLocation(17, 9),
// (18,9): error CS1017: Catch clauses cannot follow the general catch clause of a try statement
// catch when (false) {}
Diagnostic(ErrorCode.ERR_TooManyCatches, "catch").WithLocation(18, 9),
// (18,21): warning CS7095: Filter expression is a constant, consider removing the filter
// catch when (false) {}
Diagnostic(ErrorCode.WRN_FilterIsConstant, "false").WithLocation(18, 21));
}
[Fact]
......
......@@ -3979,10 +3979,9 @@ public void TestCatchAfterCatchStart()
Assert.NotNull(ms.Body);
Assert.Equal(1, ms.Body.Statements.Count);
Assert.Equal(SyntaxKind.TryStatement, ms.Body.Statements[0].Kind());
Assert.Equal(3, file.Errors().Length);
Assert.Equal(2, file.Errors().Length);
Assert.Equal((int)ErrorCode.ERR_LbraceExpected, file.Errors()[0].Code);
Assert.Equal((int)ErrorCode.ERR_RbraceExpected, file.Errors()[1].Code);
Assert.Equal((int)ErrorCode.ERR_TooManyCatches, file.Errors()[2].Code);
}
[Fact]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册