提交 941a08f2 编写于 作者: C CyrusNajmabadi

Move diagnostic from parser to later phase.

上级 07dc0b23
......@@ -1604,7 +1604,6 @@ private TypeDeclarationSyntax ParseClassOrStructOrInterfaceDeclaration(SyntaxLis
var typeParameters = this.ParseTypeParameterList(allowVariance: classOrStructOrInterface.Kind == SyntaxKind.InterfaceKeyword);
_termState = saveTerm;
bool hasTypeParams = typeParameters != null;
var baseList = this.ParseBaseList();
// Parse class body
......@@ -1616,7 +1615,7 @@ private TypeDeclarationSyntax ParseClassOrStructOrInterfaceDeclaration(SyntaxLis
if (this.CurrentToken.ContextualKind == SyntaxKind.WhereKeyword)
{
constraints = _pool.Allocate<TypeParameterConstraintClauseSyntax>();
this.ParseTypeParameterConstraintClauses(hasTypeParams, constraints);
this.ParseTypeParameterConstraintClauses(constraints);
}
var openBrace = this.EatToken(SyntaxKind.OpenBraceToken);
......@@ -1916,18 +1915,11 @@ private bool IsPossibleTypeParameterConstraintClauseStart()
this.PeekToken(2).Kind == SyntaxKind.ColonToken;
}
private void ParseTypeParameterConstraintClauses(bool isAllowed, SyntaxListBuilder list)
private void ParseTypeParameterConstraintClauses(SyntaxListBuilder list)
{
while (this.CurrentToken.ContextualKind == SyntaxKind.WhereKeyword)
{
var constraint = this.ParseTypeParameterConstraintClause();
if (!isAllowed)
{
constraint = this.AddErrorToFirstToken(constraint, ErrorCode.ERR_ConstraintOnlyAllowedOnGenericDecl);
isAllowed = true; // silence any further errors
}
list.Add(constraint);
list.Add(this.ParseTypeParameterConstraintClause());
}
}
......@@ -2945,7 +2937,7 @@ private bool IsEndOfNameInExplicitInterface()
if (this.CurrentToken.ContextualKind == SyntaxKind.WhereKeyword)
{
constraints = _pool.Allocate<TypeParameterConstraintClauseSyntax>();
this.ParseTypeParameterConstraintClauses(typeParameterList != null, constraints);
this.ParseTypeParameterConstraintClauses(constraints);
}
else if (this.CurrentToken.Kind == SyntaxKind.ColonToken)
{
......@@ -5103,7 +5095,7 @@ private DelegateDeclarationSyntax ParseDelegateDeclaration(SyntaxListBuilder<Att
if (this.CurrentToken.ContextualKind == SyntaxKind.WhereKeyword)
{
constraints = _pool.Allocate<TypeParameterConstraintClauseSyntax>();
this.ParseTypeParameterConstraintClauses(typeParameters != null, constraints);
this.ParseTypeParameterConstraintClauses(constraints);
}
_termState = saveTerm;
......@@ -8885,7 +8877,7 @@ private static bool IsAccessibilityModifier(SyntaxKind kind)
if (this.CurrentToken.ContextualKind == SyntaxKind.WhereKeyword)
{
constraints = _pool.Allocate<TypeParameterConstraintClauseSyntax>();
this.ParseTypeParameterConstraintClauses(typeParameterListOpt != null, constraints);
this.ParseTypeParameterConstraintClauses(constraints);
forceLocalFunc = true;
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
......@@ -90,6 +91,7 @@ internal ReturnTypeAndDiagnostics(TypeSymbol returnType, ImmutableArray<Diagnost
else
{
_typeParameters = ImmutableArray<TypeParameterSymbol>.Empty;
ReportErrorIfHasConstraints(_syntax.ConstraintClauses, diagnostics);
}
if (IsExtensionMethod)
......
......@@ -95,9 +95,14 @@ internal sealed class SourceMemberMethodSymbol : SourceMethodSymbol
this.MakeFlags(methodKind, declarationModifiers, returnsVoid, isExtensionMethod, isMetadataVirtualIgnoringModifiers);
_typeParameters = (syntax.Arity == 0) ?
ImmutableArray<TypeParameterSymbol>.Empty :
MakeTypeParameters(syntax, diagnostics);
_typeParameters = syntax.Arity == 0
? ImmutableArray<TypeParameterSymbol>.Empty
: MakeTypeParameters(syntax, diagnostics);
if (syntax.Arity == 0)
{
ReportErrorIfHasConstraints(syntax.ConstraintClauses, diagnostics);
}
bool hasBlockBody = syntax.Body != null;
_isExpressionBodied = !hasBlockBody && syntax.ExpressionBody != null;
......
......@@ -118,6 +118,15 @@ private ImmutableArray<TypeParameterSymbol> MakeTypeParameters(DiagnosticBag dia
{
if (declaration.Arity == 0)
{
if (declaration.Kind != DeclarationKind.Enum)
{
foreach (var syntaxRef in this.SyntaxReferences)
{
var constraintClauses = GetConstraintClauses((CSharpSyntaxNode)syntaxRef.GetSyntax());
ReportErrorIfHasConstraints(constraintClauses, diagnostics);
}
}
return ImmutableArray<TypeParameterSymbol>.Empty;
}
......
......@@ -1089,6 +1089,17 @@ public ImmutableArray<SymbolDisplayPart> ToDisplayParts(SymbolDisplayFormat form
return SymbolDisplay.ToMinimalDisplayParts(this, semanticModel, position, format);
}
protected static void ReportErrorIfHasConstraints(
SyntaxList<TypeParameterConstraintClauseSyntax> constraintClauses, DiagnosticBag diagnostics)
{
if (constraintClauses.Count > 0)
{
diagnostics.Add(
ErrorCode.ERR_ConstraintOnlyAllowedOnGenericDecl,
constraintClauses[0].WhereKeyword.GetLocation());
}
}
#region ISymbol Members
SymbolKind ISymbol.Kind
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册