提交 326d72a9 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #16350 from CyrusNajmabadi/parserDiagnostics31

Move indexer parameter count checking out of the parser.
......@@ -3118,7 +3118,7 @@ private ConversionOperatorDeclarationSyntax ParseConversionOperatorDeclaration(S
semicolon);
}
private MemberDeclarationSyntax ParseIndexerDeclaration(
private IndexerDeclarationSyntax ParseIndexerDeclaration(
SyntaxListBuilder<AttributeListSyntax> attributes,
SyntaxListBuilder modifiers,
TypeSyntax type,
......@@ -3136,11 +3136,6 @@ private ConversionOperatorDeclarationSyntax ParseConversionOperatorDeclaration(S
}
var parameterList = this.ParseBracketedParameterList();
// TODO: ReportExtensionMethods(parameters, retval);
if (parameterList.Parameters.Count == 0)
{
parameterList = this.AddErrorToLastToken(parameterList, ErrorCode.ERR_IndexerNeedsParam);
}
AccessorListSyntax accessorList = null;
ArrowExpressionClauseSyntax expressionBody = null;
......
......@@ -766,13 +766,19 @@ private DeclarationModifiers MakeModifiers(SyntaxTokenList modifiers, bool isExp
return mods;
}
private static ImmutableArray<ParameterSymbol> MakeParameters(Binder binder, SourcePropertySymbol owner, BaseParameterListSyntax parameterSyntaxOpt, DiagnosticBag diagnostics)
private static ImmutableArray<ParameterSymbol> MakeParameters(
Binder binder, SourcePropertySymbol owner, BaseParameterListSyntax parameterSyntaxOpt, DiagnosticBag diagnostics)
{
if (parameterSyntaxOpt == null)
{
return ImmutableArray<ParameterSymbol>.Empty;
}
if (parameterSyntaxOpt.Parameters.Count < 1)
{
diagnostics.Add(ErrorCode.ERR_IndexerNeedsParam, parameterSyntaxOpt.GetLastToken().GetLocation());
}
SyntaxToken arglistToken;
var parameters = ParameterHelpers.MakeParameters(
binder, owner, parameterSyntaxOpt, out arglistToken,
......
......@@ -3802,7 +3802,10 @@ public class MyClass {
}
";
ParseAndValidate(test, Diagnostic(ErrorCode.ERR_IndexerNeedsParam, "]"));
CreateCompilationWithMscorlib(test).VerifyDiagnostics(
// (4,14): error CS1551: Indexers must have at least one parameter
// int this[] {
Diagnostic(ErrorCode.ERR_IndexerNeedsParam, "]").WithLocation(4, 14));
}
[Fact]
......
......@@ -1870,11 +1870,27 @@ public void TestEndBraceAfterIndexerParameterStart()
var agg = (TypeDeclarationSyntax)file.Members[0];
Assert.Equal(1, agg.Members.Count);
Assert.Equal(SyntaxKind.IndexerDeclaration, agg.Members[0].Kind());
Assert.Equal(4, file.Errors().Length);
Assert.Equal((int)ErrorCode.ERR_IndexerNeedsParam, file.Errors()[0].Code);
Assert.Equal((int)ErrorCode.ERR_SyntaxError, file.Errors()[1].Code);
Assert.Equal((int)ErrorCode.ERR_LbraceExpected, file.Errors()[2].Code);
Assert.Equal((int)ErrorCode.ERR_RbraceExpected, file.Errors()[3].Code);
Assert.Equal(3, file.Errors().Length);
Assert.Equal((int)ErrorCode.ERR_SyntaxError, file.Errors()[0].Code);
Assert.Equal((int)ErrorCode.ERR_LbraceExpected, file.Errors()[1].Code);
Assert.Equal((int)ErrorCode.ERR_RbraceExpected, file.Errors()[2].Code);
CreateCompilationWithMscorlib(text).VerifyDiagnostics(
// (1,21): error CS1003: Syntax error, ']' expected
// class c { int this[ }
Diagnostic(ErrorCode.ERR_SyntaxError, "}").WithArguments("]", "}").WithLocation(1, 21),
// (1,21): error CS1514: { expected
// class c { int this[ }
Diagnostic(ErrorCode.ERR_LbraceExpected, "}").WithLocation(1, 21),
// (1,22): error CS1513: } expected
// class c { int this[ }
Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(1, 22),
// (1,15): error CS0548: 'c.this': property or indexer must have at least one accessor
// class c { int this[ }
Diagnostic(ErrorCode.ERR_PropertyWithNoAccessors, "this").WithArguments("c.this").WithLocation(1, 15),
// (1,19): error CS1551: Indexers must have at least one parameter
// class c { int this[ }
Diagnostic(ErrorCode.ERR_IndexerNeedsParam, "[").WithLocation(1, 19));
}
[Fact]
......@@ -1968,9 +1984,19 @@ public void TestGarbageAfterIndexerParameterStart()
var agg = (TypeDeclarationSyntax)file.Members[0];
Assert.Equal(1, agg.Members.Count);
Assert.Equal(SyntaxKind.IndexerDeclaration, agg.Members[0].Kind());
Assert.Equal(2, file.Errors().Length);
Assert.Equal((int)ErrorCode.ERR_IndexerNeedsParam, file.Errors()[0].Code);
Assert.Equal((int)ErrorCode.ERR_UnexpectedCharacter, file.Errors()[1].Code);
Assert.Equal(1, file.Errors().Length);
Assert.Equal((int)ErrorCode.ERR_UnexpectedCharacter, file.Errors()[0].Code);
CreateCompilationWithMscorlib(text).VerifyDiagnostics(
// (1,21): error CS1056: Unexpected character '$'
// class c { int this[ $ ] { } }
Diagnostic(ErrorCode.ERR_UnexpectedCharacter, "").WithArguments("$").WithLocation(1, 21),
// (1,15): error CS0548: 'c.this': property or indexer must have at least one accessor
// class c { int this[ $ ] { } }
Diagnostic(ErrorCode.ERR_PropertyWithNoAccessors, "this").WithArguments("c.this").WithLocation(1, 15),
// (1,23): error CS1551: Indexers must have at least one parameter
// class c { int this[ $ ] { } }
Diagnostic(ErrorCode.ERR_IndexerNeedsParam, "]").WithLocation(1, 23));
}
[Fact]
......@@ -2041,11 +2067,27 @@ public void TestMethodAfterIndexerParameterStart()
Assert.Equal(2, agg.Members.Count);
Assert.Equal(SyntaxKind.IndexerDeclaration, agg.Members[0].Kind());
Assert.Equal(SyntaxKind.MethodDeclaration, agg.Members[1].Kind());
Assert.Equal(4, file.Errors().Length);
Assert.Equal((int)ErrorCode.ERR_IndexerNeedsParam, file.Errors()[0].Code);
Assert.Equal((int)ErrorCode.ERR_SyntaxError, file.Errors()[1].Code);
Assert.Equal((int)ErrorCode.ERR_LbraceExpected, file.Errors()[2].Code);
Assert.Equal((int)ErrorCode.ERR_RbraceExpected, file.Errors()[3].Code);
Assert.Equal(3, file.Errors().Length);
Assert.Equal((int)ErrorCode.ERR_SyntaxError, file.Errors()[0].Code);
Assert.Equal((int)ErrorCode.ERR_LbraceExpected, file.Errors()[1].Code);
Assert.Equal((int)ErrorCode.ERR_RbraceExpected, file.Errors()[2].Code);
CreateCompilationWithMscorlib(text).VerifyDiagnostics(
// (1,21): error CS1003: Syntax error, ']' expected
// class c { int this[ public void m() { } }
Diagnostic(ErrorCode.ERR_SyntaxError, "public").WithArguments("]", "public").WithLocation(1, 21),
// (1,21): error CS1514: { expected
// class c { int this[ public void m() { } }
Diagnostic(ErrorCode.ERR_LbraceExpected, "public").WithLocation(1, 21),
// (1,21): error CS1513: } expected
// class c { int this[ public void m() { } }
Diagnostic(ErrorCode.ERR_RbraceExpected, "public").WithLocation(1, 21),
// (1,15): error CS0548: 'c.this': property or indexer must have at least one accessor
// class c { int this[ public void m() { } }
Diagnostic(ErrorCode.ERR_PropertyWithNoAccessors, "this").WithArguments("c.this").WithLocation(1, 15),
// (1,19): error CS1551: Indexers must have at least one parameter
// class c { int this[ public void m() { } }
Diagnostic(ErrorCode.ERR_IndexerNeedsParam, "[").WithLocation(1, 19));
}
[Fact]
......@@ -6679,9 +6721,6 @@ public void ColonColonInExplicitInterfaceMember()
// (2,4): error CS1003: Syntax error, '.' expected
// _ _::this
Diagnostic(ErrorCode.ERR_SyntaxError, "::").WithArguments(".", "::"),
// (3,1): error CS1551: Indexers must have at least one parameter
//
Diagnostic(ErrorCode.ERR_IndexerNeedsParam, ""),
// (2,10): error CS1003: Syntax error, '[' expected
// _ _::this
Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments("[", ""),
......@@ -6694,6 +6733,37 @@ public void ColonColonInExplicitInterfaceMember()
// (2,10): error CS1513: } expected
// _ _::this
Diagnostic(ErrorCode.ERR_RbraceExpected, ""));
CreateCompilationWithMscorlib(text).VerifyDiagnostics(
// (2,4): error CS1003: Syntax error, '.' expected
// _ _::this
Diagnostic(ErrorCode.ERR_SyntaxError, "::").WithArguments(".", "::").WithLocation(2, 4),
// (2,10): error CS1003: Syntax error, '[' expected
// _ _::this
Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments("[", "").WithLocation(2, 10),
// (2,10): error CS1003: Syntax error, ']' expected
// _ _::this
Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments("]", "").WithLocation(2, 10),
// (2,10): error CS1514: { expected
// _ _::this
Diagnostic(ErrorCode.ERR_LbraceExpected, "").WithLocation(2, 10),
// (2,10): error CS1513: } expected
// _ _::this
Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(2, 10),
// (2,3): error CS0246: The type or namespace name '_' could not be found (are you missing a using directive or an assembly reference?)
// _ _::this
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "_").WithArguments("_").WithLocation(2, 3),
// (2,1): error CS0246: The type or namespace name '_' could not be found (are you missing a using directive or an assembly reference?)
// _ _::this
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "_").WithArguments("_").WithLocation(2, 1),
// error CS1551: Indexers must have at least one parameter
Diagnostic(ErrorCode.ERR_IndexerNeedsParam).WithLocation(1, 1),
// (2,3): error CS0538: '_' in explicit interface declaration is not an interface
// _ _::this
Diagnostic(ErrorCode.ERR_ExplicitInterfaceImplementationNotInterface, "_").WithArguments("_").WithLocation(2, 3),
// (2,6): error CS0548: '<invalid-global-code>.this': property or indexer must have at least one accessor
// _ _::this
Diagnostic(ErrorCode.ERR_PropertyWithNoAccessors, "this").WithArguments("<invalid-global-code>.this").WithLocation(2, 6));
}
[WorkItem(649806, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/649806")]
......
......@@ -1571,9 +1571,30 @@ public void Error_IndexerDefinition()
new ErrorDescription { Code = 1001, Line = 1, Column = 13 },
new ErrorDescription { Code = 1003, Line = 1, Column = 13 },
new ErrorDescription { Code = 1003, Line = 1, Column = 17 },
new ErrorDescription { Code = 1551, Line = 1, Column = 17 },
new ErrorDescription { Code = 1514, Line = 1, Column = 17 },
new ErrorDescription { Code = 1513, Line = 1, Column = 17 });
CreateCompilationWithMscorlib(test).VerifyDiagnostics(
// (1,13): error CS1003: Syntax error, '[' expected
// string this ="";
Diagnostic(ErrorCode.ERR_SyntaxError, "=").WithArguments("[", "=").WithLocation(1, 13),
// (1,13): error CS1001: Identifier expected
// string this ="";
Diagnostic(ErrorCode.ERR_IdentifierExpected, "=").WithLocation(1, 13),
// (1,17): error CS1003: Syntax error, ']' expected
// string this ="";
Diagnostic(ErrorCode.ERR_SyntaxError, "").WithArguments("]", "").WithLocation(1, 17),
// (1,17): error CS1514: { expected
// string this ="";
Diagnostic(ErrorCode.ERR_LbraceExpected, "").WithLocation(1, 17),
// (1,17): error CS1513: } expected
// string this ="";
Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(1, 17),
// (1,8): error CS0548: '<invalid-global-code>.this': property or indexer must have at least one accessor
// string this ="";
Diagnostic(ErrorCode.ERR_PropertyWithNoAccessors, "this").WithArguments("<invalid-global-code>.this").WithLocation(1, 8),
// error CS1551: Indexers must have at least one parameter
Diagnostic(ErrorCode.ERR_IndexerNeedsParam).WithLocation(1, 1));
}
#endregion
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册