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

Merge pull request #16091 from CyrusNajmabadi/parserDiagnostics2

Do not restrict attributes on functions parameters when parsing.
......@@ -94,6 +94,11 @@ internal partial class Binder
foreach (var p in parameterSyntaxList.Value)
{
foreach (var attributeList in p.AttributeLists)
{
Error(diagnostics, ErrorCode.ERR_AttributesNotAllowed, attributeList);
}
if (p.IsArgList)
{
Error(diagnostics, ErrorCode.ERR_IllegalVarArgs, p);
......
......@@ -956,19 +956,14 @@ private bool IsPossibleAttributeDeclaration()
return this.CurrentToken.Kind == SyntaxKind.OpenBracketToken;
}
private void ParseAttributeDeclarations(SyntaxListBuilder list, bool allowAttributes = true)
private void ParseAttributeDeclarations(SyntaxListBuilder list)
{
var saveTerm = _termState;
_termState |= TerminatorState.IsAttributeDeclarationTerminator;
while (this.IsPossibleAttributeDeclaration())
{
var section = this.ParseAttributeDeclaration();
if (!allowAttributes)
{
section = this.AddError(section, ErrorCode.ERR_AttributesNotAllowed);
}
list.Add(section);
list.Add(this.ParseAttributeDeclaration());
}
_termState = saveTerm;
......@@ -2656,7 +2651,8 @@ public static bool IsComplete(CSharpSyntaxNode node)
return true;
}
private ConstructorDeclarationSyntax ParseConstructorDeclaration(string typeName, SyntaxListBuilder<AttributeListSyntax> attributes, SyntaxListBuilder modifiers)
private ConstructorDeclarationSyntax ParseConstructorDeclaration(
string typeName, SyntaxListBuilder<AttributeListSyntax> attributes, SyntaxListBuilder modifiers)
{
var name = this.ParseIdentifierToken();
Debug.Assert(name.ValueText == typeName);
......@@ -2665,7 +2661,7 @@ private ConstructorDeclarationSyntax ParseConstructorDeclaration(string typeName
_termState |= TerminatorState.IsEndOfMethodSignature;
try
{
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true, allowAttributes: true);
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true);
ConstructorInitializerSyntax initializer = this.CurrentToken.Kind == SyntaxKind.ColonToken
? this.ParseConstructorInitializer()
......@@ -2899,7 +2895,7 @@ private bool IsEndOfNameInExplicitInterface()
var saveTerm = _termState;
_termState |= TerminatorState.IsEndOfMethodSignature;
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true, allowAttributes: true);
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true);
var constraints = default(SyntaxListBuilder<TypeParameterConstraintClauseSyntax>);
try
......@@ -3014,7 +3010,7 @@ private ConversionOperatorDeclarationSyntax ParseConversionOperatorDeclaration(S
var type = this.ParseType();
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true, allowAttributes: true);
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true);
if (paramList.Parameters.Count != 1)
{
paramList = this.AddErrorToFirstToken(paramList, ErrorCode.ERR_OvlUnaryOperatorExpected);
......@@ -3102,7 +3098,7 @@ private ConversionOperatorDeclarationSyntax ParseConversionOperatorDeclaration(S
}
}
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true, allowAttributes: true);
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true);
switch (paramList.Parameters.Count)
{
......@@ -3779,7 +3775,7 @@ private bool CanReuseAccessorDeclaration(bool isEvent)
return false;
}
internal ParameterListSyntax ParseParenthesizedParameterList(bool allowThisKeyword, bool allowDefaults, bool allowAttributes)
internal ParameterListSyntax ParseParenthesizedParameterList(bool allowThisKeyword, bool allowDefaults)
{
if (this.IsIncrementalAndFactoryContextMatches && CanReuseParameterList(this.CurrentNode as CSharp.Syntax.ParameterListSyntax))
{
......@@ -3795,7 +3791,7 @@ internal ParameterListSyntax ParseParenthesizedParameterList(bool allowThisKeywo
SyntaxToken open;
SyntaxToken close;
this.ParseParameterList(out open, parameters, out close, openKind, closeKind, allowThisKeyword, allowDefaults, allowAttributes);
this.ParseParameterList(out open, parameters, out close, openKind, closeKind, allowThisKeyword, allowDefaults);
return _syntaxFactory.ParameterList(open, parameters, close);
}
finally
......@@ -3820,7 +3816,7 @@ internal BracketedParameterListSyntax ParseBracketedParameterList(bool allowDefa
SyntaxToken open;
SyntaxToken close;
this.ParseParameterList(out open, parameters, out close, openKind, closeKind, allowThisKeyword: false, allowDefaults: allowDefaults, allowAttributes: true);
this.ParseParameterList(out open, parameters, out close, openKind, closeKind, allowThisKeyword: false, allowDefaults: allowDefaults);
return _syntaxFactory.BracketedParameterList(open, parameters, close);
}
finally
......@@ -3892,8 +3888,7 @@ private static bool CanReuseBracketedParameterList(CSharp.Syntax.BracketedParame
SyntaxKind openKind,
SyntaxKind closeKind,
bool allowThisKeyword,
bool allowDefaults,
bool allowAttributes)
bool allowDefaults)
{
open = this.EatToken(openKind);
......@@ -3917,7 +3912,7 @@ private static bool CanReuseBracketedParameterList(CSharp.Syntax.BracketedParame
// first parameter
attributes.Clear();
modifiers.Clear();
var parameter = this.ParseParameter(attributes, modifiers, allowThisKeyword, allowDefaults, allowAttributes);
var parameter = this.ParseParameter(attributes, modifiers, allowThisKeyword, allowDefaults);
nodes.Add(parameter);
hasParams = modifiers.Any((int)SyntaxKind.ParamsKeyword);
hasArgList = parameter.Identifier.Kind == SyntaxKind.ArgListKeyword;
......@@ -3940,7 +3935,7 @@ private static bool CanReuseBracketedParameterList(CSharp.Syntax.BracketedParame
nodes.AddSeparator(this.EatToken(SyntaxKind.CommaToken));
attributes.Clear();
modifiers.Clear();
parameter = this.ParseParameter(attributes, modifiers, allowThisKeyword, allowDefaults, allowAttributes);
parameter = this.ParseParameter(attributes, modifiers, allowThisKeyword, allowDefaults);
if (parameter.IsMissing && this.IsPossibleParameter(allowThisKeyword))
{
// ensure we always consume tokens
......@@ -4080,15 +4075,14 @@ private static bool CanReuseParameter(CSharp.Syntax.ParameterSyntax parameter)
SyntaxListBuilder<AttributeListSyntax> attributes,
SyntaxListBuilder modifiers,
bool allowThisKeyword,
bool allowDefaults,
bool allowAttributes)
bool allowDefaults)
{
if (this.IsIncrementalAndFactoryContextMatches && CanReuseParameter(this.CurrentNode as CSharp.Syntax.ParameterSyntax, attributes, modifiers))
{
return (ParameterSyntax)this.EatNode();
}
this.ParseAttributeDeclarations(attributes, allowAttributes);
this.ParseAttributeDeclarations(attributes);
this.ParseParameterModifiers(modifiers, allowThisKeyword);
var hasArgList = this.CurrentToken.Kind == SyntaxKind.ArgListKeyword;
......@@ -4966,7 +4960,7 @@ private bool IsLocalFunctionAfterIdentifier()
{
var typeParameterListOpt = this.ParseTypeParameterList(allowVariance: false);
var paramList = ParseParenthesizedParameterList(
allowThisKeyword: true, allowDefaults: true, allowAttributes: true);
allowThisKeyword: true, allowDefaults: true);
if (!paramList.IsMissing &&
(this.CurrentToken.Kind == SyntaxKind.OpenBraceToken ||
......@@ -5058,7 +5052,7 @@ private DelegateDeclarationSyntax ParseDelegateDeclaration(SyntaxListBuilder<Att
_termState |= TerminatorState.IsEndOfMethodSignature;
var name = this.ParseIdentifierToken();
var typeParameters = this.ParseTypeParameterList(allowVariance: true);
var parameterList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true, allowAttributes: true);
var parameterList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: true);
var constraints = default(SyntaxListBuilder<TypeParameterConstraintClauseSyntax>);
try
{
......@@ -7141,7 +7135,7 @@ private bool IsPossibleMethodDeclarationFollowingNullableType()
var saveTerm = _termState;
_termState |= TerminatorState.IsEndOfMethodSignature;
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true, allowAttributes: true);
var paramList = this.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true);
_termState = saveTerm;
var separatedParameters = paramList.Parameters.GetWithSeparators();
......@@ -8829,7 +8823,7 @@ private static bool IsAccessibilityModifier(SyntaxKind kind)
TypeParameterListSyntax typeParameterListOpt = this.ParseTypeParameterList(allowVariance: false);
// "await f<T>()" still makes sense, so don't force accept a local function if there's a type parameter list.
ParameterListSyntax paramList = this.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true, allowAttributes: true);
ParameterListSyntax paramList = this.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true);
// "await x()" is ambiguous (see note at start of this method), but we assume "await x(await y)" is meant to be a function if it's in a non-async context.
if (!forceLocalFunc)
{
......@@ -11090,7 +11084,7 @@ private AnonymousMethodExpressionSyntax ParseAnonymousMethodExpression()
ParameterListSyntax parameterList = null;
if (this.CurrentToken.Kind == SyntaxKind.OpenParenToken)
{
parameterList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: false, allowAttributes: false);
parameterList = this.ParseParenthesizedParameterList(allowThisKeyword: false, allowDefaults: false);
}
// In mismatched braces cases (missing a }) it is possible for delegate declarations to be
......
......@@ -1816,7 +1816,7 @@ public static ParameterListSyntax ParseParameterList(string text, int offset = 0
using (var lexer = MakeLexer(text, offset, (CSharpParseOptions)options))
using (var parser = MakeParser(lexer))
{
var node = parser.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true, allowAttributes: true);
var node = parser.ParseParenthesizedParameterList(allowThisKeyword: true, allowDefaults: true);
if (consumeFullText) node = parser.ConsumeUnexpectedTokens(node);
return (ParameterListSyntax)node.CreateRed();
}
......
......@@ -2584,8 +2584,7 @@ class A
ParseAndValidate(test, Diagnostic(ErrorCode.ERR_DefaultValueNotAllowed, "="));
}
[WorkItem(540251, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/540251")]
[Fact]
[Fact, WorkItem(540251, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/540251")]
public void CS7014ERR_AttributesNotAllowed()
{
var test = @"
......@@ -2596,7 +2595,7 @@ class Program
static void Main()
{
const string message = ""the parameter is obsolete"";
Action<int> a = delegate (
Action<int, int> a = delegate (
[ObsoleteAttribute(message)] [ObsoleteAttribute(message)] int x,
[ObsoleteAttribute(message)] int y
) { };
......@@ -2604,16 +2603,19 @@ static void Main()
}
";
ParseAndValidate(test,
// (10,13): error CS7014: Attributes are not valid in this context.
// [ObsoleteAttribute(message)] [ObsoleteAttribute(message)] int x,
Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]"),
// (10,42): error CS7014: Attributes are not valid in this context.
// [ObsoleteAttribute(message)] [ObsoleteAttribute(message)] int x,
Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]"),
// (11,13): error CS7014: Attributes are not valid in this context.
// [ObsoleteAttribute(message)] int y
Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]"));
CreateCompilationWithMscorlib(test).VerifyDiagnostics(
// (10,13): error CS7014: Attributes are not valid in this context.
// [ObsoleteAttribute(message)] [ObsoleteAttribute(message)] int x,
Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]").WithLocation(10, 13),
// (10,42): error CS7014: Attributes are not valid in this context.
// [ObsoleteAttribute(message)] [ObsoleteAttribute(message)] int x,
Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]").WithLocation(10, 42),
// (11,13): error CS7014: Attributes are not valid in this context.
// [ObsoleteAttribute(message)] int y
Diagnostic(ErrorCode.ERR_AttributesNotAllowed, "[ObsoleteAttribute(message)]").WithLocation(11, 13),
// (8,22): warning CS0219: The variable 'message' is assigned but its value is never used
// const string message = "the parameter is obsolete";
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "message").WithArguments("message").WithLocation(8, 22));
}
[WorkItem(863401, "DevDiv/Personal")]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册