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

Merge pull request #16143 from CyrusNajmabadi/parserDiagnostics16

Allow parsing any types in catch clauses in the parser.
......@@ -7543,16 +7543,17 @@ private CatchClauseSyntax ParseCatchClause()
if (this.CurrentToken.Kind == SyntaxKind.OpenParenToken)
{
var openParen = this.EatToken();
_termState |= TerminatorState.IsEndOfCatchClause;
var type = this.ParseClassType();
var type = this.ParseType();
SyntaxToken name = null;
if (this.IsTrueIdentifier())
{
name = this.ParseIdentifierToken();
}
_termState = saveTerm;
var closeParen = this.EatToken(SyntaxKind.CloseParenToken);
decl = _syntaxFactory.CatchDeclaration(openParen, type, name, closeParen);
}
......@@ -7613,31 +7614,6 @@ private bool IsEndOfCatchBlock()
|| this.CurrentToken.Kind == SyntaxKind.FinallyKeyword;
}
private TypeSyntax ParseClassType()
{
var type = this.ParseType();
switch (type.Kind)
{
case SyntaxKind.PredefinedType:
var kt = ((PredefinedTypeSyntax)type).Keyword.Kind;
if (kt != SyntaxKind.ObjectKeyword && kt != SyntaxKind.StringKeyword)
{
goto default;
}
break;
default:
if (!SyntaxFacts.IsName(type.Kind))
{
type = this.AddError(type, ErrorCode.ERR_ClassTypeExpected);
}
break;
}
return type;
}
private StatementSyntax ParseCheckedStatement()
{
Debug.Assert(this.CurrentToken.Kind == SyntaxKind.CheckedKeyword || this.CurrentToken.Kind == SyntaxKind.UncheckedKeyword);
......
......@@ -46,6 +46,32 @@ static void Main()
Assert.Equal("string.operator !=(string, string)", filterExprInfo.Symbol.ToDisplayString());
}
[Fact]
public void CatchClauseValueType()
{
var source = @"
class C
{
static void Main()
{
try
{
}
catch (int e)
{
}
}
}
";
CreateCompilationWithMscorlib(source).VerifyDiagnostics(
// (9,16): error CS0155: The type caught or thrown must be derived from System.Exception
// catch (int e)
Diagnostic(ErrorCode.ERR_BadExceptionType, "int").WithLocation(9, 16),
// (9,20): warning CS0168: The variable 'e' is declared but never used
// catch (int e)
Diagnostic(ErrorCode.WRN_UnreferencedVar, "e").WithArguments("e").WithLocation(9, 20));
}
[ConditionalFact(typeof(x86))]
[WorkItem(7030, "https://github.com/dotnet/roslyn/issues/7030")]
public void Issue7030()
......
......@@ -1647,9 +1647,16 @@ public static void Main()
}
";
ParseAndValidate(test,
Diagnostic(ErrorCode.ERR_ClassTypeExpected, "int"),
Diagnostic(ErrorCode.ERR_ClassTypeExpected, "byte"));
CreateCompilationWithMscorlib(test).VerifyDiagnostics(
// (10,15): error CS0155: The type caught or thrown must be derived from System.Exception
// catch(int)
Diagnostic(ErrorCode.ERR_BadExceptionType, "int").WithLocation(10, 15),
// (13,15): error CS0155: The type caught or thrown must be derived from System.Exception
// catch(byte)
Diagnostic(ErrorCode.ERR_BadExceptionType, "byte").WithLocation(13, 15),
// (2,1): hidden CS8019: Unnecessary using directive.
// using System;
Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;").WithLocation(2, 1));
}
[WorkItem(863382, "DevDiv/Personal")]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册