Skip to content

Commit

Permalink
Report diagnostic for field and value in property accessors when used…
Browse files Browse the repository at this point in the history
… as primary expressions only (#74164)
  • Loading branch information
cston authored Jun 27, 2024
1 parent 4ec795f commit 5d8b9b8
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ internal RangeVariableSymbol AddRangeVariable(Binder binder, SyntaxToken identif
bool error = false;

Debug.Assert(identifier.Parent is { });
binder.ReportFieldOrValueContextualKeywordConflictIfAny(identifier.Parent, identifier, diagnostics);

foreach (var existingRangeVariable in allRangeVariables.Keys)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Deconstruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,6 @@ private BoundExpression BindDeconstructionVariable(
{
SourceLocalSymbol localSymbol = LookupLocal(designation.Identifier);

ReportFieldOrValueContextualKeywordConflictIfAny(designation, designation.Identifier, diagnostics);

// is this a local?
if ((object)localSymbol != null)
{
Expand Down
4 changes: 0 additions & 4 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3116,8 +3116,6 @@ private BoundExpression BindOutVariableDeclarationArgument(
var designation = (SingleVariableDesignationSyntax)declarationExpression.Designation;
TypeSyntax typeSyntax = declarationExpression.Type;

ReportFieldOrValueContextualKeywordConflictIfAny(designation, designation.Identifier, diagnostics);

// Is this a local?
SourceLocalSymbol localSymbol = this.LookupLocal(designation.Identifier);
if ((object)localSymbol != null)
Expand Down Expand Up @@ -7510,8 +7508,6 @@ private BoundExpression BindMemberAccessWithBoundLeft(

boundLeft = MakeMemberAccessValue(boundLeft, diagnostics);

ReportFieldOrValueContextualKeywordConflictIfAny(right, right.Identifier, diagnostics);

TypeSymbol leftType = boundLeft.Type;

if ((object)leftType != null && leftType.IsDynamic())
Expand Down
3 changes: 0 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder_Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ private UnboundLambda AnalyzeAnonymousFunction(
// x => ...
hasSignature = true;
var simple = (SimpleLambdaExpressionSyntax)syntax;
ReportFieldOrValueContextualKeywordConflictIfAny(simple.Parameter, simple.Parameter.Identifier, diagnostics);
namesBuilder.Add(simple.Parameter.Identifier.ValueText);
break;
case SyntaxKind.ParenthesizedLambdaExpression:
Expand Down Expand Up @@ -193,8 +192,6 @@ private UnboundLambda AnalyzeAnonymousFunction(
}
}

ReportFieldOrValueContextualKeywordConflictIfAny(p, p.Identifier, diagnostics);

namesBuilder.Add(p.Identifier.ValueText);
typesBuilder.Add(type);
refKindsBuilder.Add(refKind);
Expand Down
12 changes: 2 additions & 10 deletions src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,6 @@ private BoundLabeledStatement BindLabeled(LabeledStatementSyntax node, BindingDi
CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics);
var binder = this.LookupSymbolsWithFallback(result, node.Identifier.ValueText, arity: 0, useSiteInfo: ref useSiteInfo, options: LookupOptions.LabelsOnly);

ReportFieldOrValueContextualKeywordConflictIfAny(node, node.Identifier, diagnostics);

// result.Symbols can be empty in some malformed code, e.g. when a labeled statement is used an embedded statement in an if or foreach statement
// In this case we create new label symbol on the fly, and an error is reported by parser
var symbol = result.Symbols.Count > 0 && result.IsMultiViable ?
Expand Down Expand Up @@ -556,8 +554,6 @@ private BoundStatement BindLocalFunctionStatement(LocalFunctionStatementSyntax n
{
MessageID.IDS_FeatureLocalFunctions.CheckFeatureAvailability(diagnostics, node.Identifier);

ReportFieldOrValueContextualKeywordConflictIfAny(node, node.Identifier, diagnostics);

// already defined symbol in containing block
var localSymbol = this.LookupLocalFunction(node.Identifier);

Expand Down Expand Up @@ -960,7 +956,7 @@ protected BoundLocalDeclaration BindVariableDeclaration(
{
Debug.Assert(declarator != null);

return BindVariableDeclaration(LocateDeclaredVariableSymbol(declarator, typeSyntax, kind, diagnostics),
return BindVariableDeclaration(LocateDeclaredVariableSymbol(declarator, typeSyntax, kind),
kind,
isVar,
declarator,
Expand Down Expand Up @@ -1194,10 +1190,8 @@ internal ImmutableArray<BoundExpression> BindDeclaratorArguments(VariableDeclara
return arguments;
}

private SourceLocalSymbol LocateDeclaredVariableSymbol(VariableDeclaratorSyntax declarator, TypeSyntax typeSyntax, LocalDeclarationKind outerKind, BindingDiagnosticBag diagnostics)
private SourceLocalSymbol LocateDeclaredVariableSymbol(VariableDeclaratorSyntax declarator, TypeSyntax typeSyntax, LocalDeclarationKind outerKind)
{
ReportFieldOrValueContextualKeywordConflictIfAny(declarator, declarator.Identifier, diagnostics);

LocalDeclarationKind kind = outerKind == LocalDeclarationKind.UsingVariable ? LocalDeclarationKind.UsingVariable : LocalDeclarationKind.RegularVariable;
return LocateDeclaredVariableSymbol(declarator.Identifier, typeSyntax, declarator.Initializer, kind);
}
Expand Down Expand Up @@ -3152,8 +3146,6 @@ private BoundCatchBlock BindCatchBlock(CatchClauseSyntax node, ArrayBuilder<Boun
var declaration = node.Declaration;
if (declaration != null)
{
ReportFieldOrValueContextualKeywordConflictIfAny(declaration, declaration.Identifier, diagnostics);

// Note: The type is being bound twice: here and in LocalSymbol.Type. Currently,
// LocalSymbol.Type ignores diagnostics so it seems cleaner to bind the type here
// as well. However, if LocalSymbol.Type is changed to report diagnostics, we'll
Expand Down
5 changes: 0 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/Binder_Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,6 @@ private TypeSymbol BindTupleType(TupleTypeSyntax syntax, BindingDiagnosticBag di
hasExplicitNames = true;
CheckTupleMemberName(name, i, nameToken, diagnostics, uniqueFieldNames);
locations.Add(nameToken.GetLocation());
ReportFieldOrValueContextualKeywordConflictIfAny(argumentSyntax, nameToken, diagnostics);
}
else
{
Expand Down Expand Up @@ -872,8 +871,6 @@ protected NamespaceOrTypeOrAliasSymbolWithAnnotations BindNonGenericSimpleNamesp
var result = LookupResult.GetInstance();
LookupOptions options = GetSimpleNameLookupOptions(node, node.Identifier.IsVerbatimIdentifier());

ReportFieldOrValueContextualKeywordConflictIfAny(node, node.Identifier, diagnostics);

CompoundUseSiteInfo<AssemblySymbol> useSiteInfo = GetNewCompoundUseSiteInfo(diagnostics);
this.LookupSymbolsSimpleName(result, qualifierOpt, identifierValueText, 0, basesBeingResolved, options, diagnose: true, useSiteInfo: ref useSiteInfo);
diagnostics.Add(node, useSiteInfo);
Expand Down Expand Up @@ -1213,8 +1210,6 @@ private TypeWithAnnotations BindGenericSimpleNamespaceOrTypeOrAliasSymbol(
diagnostics, basesBeingResolved, qualifierOpt, node, plainName, node.Arity, options);
NamedTypeSymbol resultType;

ReportFieldOrValueContextualKeywordConflictIfAny(node, node.Identifier, diagnostics);

if (isUnboundTypeExpr)
{
if (!IsUnboundTypeAllowed(node))
Expand Down
5 changes: 0 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/ForEachLoopBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,6 @@ private BoundForEachStatement BindForEachPartsWorker(BindingDiagnosticBag diagno
CheckFeatureAvailability(_syntax.AwaitKeyword, MessageID.IDS_FeatureAsyncStreams, diagnostics);
}

if (_syntax is ForEachStatementSyntax forEachStatement)
{
ReportFieldOrValueContextualKeywordConflictIfAny(forEachStatement, forEachStatement.Identifier, diagnostics);
}

// Use the right binder to avoid seeing iteration variable
BoundExpression collectionExpr = originalBinder.GetBinder(_syntax.Expression).BindRValueWithoutTargetType(_syntax.Expression, diagnostics);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,6 @@ private void ComputeParameters()
addRefReadOnlyModifier: false,
diagnostics: diagnostics).Cast<SourceParameterSymbol, ParameterSymbol>();

foreach (var parameter in this.Syntax.ParameterList.Parameters)
{
WithTypeParametersBinder.ReportFieldOrValueContextualKeywordConflictIfAny(parameter, parameter.Identifier, diagnostics);
}

// Note: we don't need to warn on annotations used in #nullable disable context for local functions, as this is handled in binding already

var isVararg = arglistToken.Kind() == SyntaxKind.ArgListKeyword;
Expand Down Expand Up @@ -437,7 +432,6 @@ private ImmutableArray<SourceMethodTypeParameterSymbol> MakeTypeParameters(Bindi
}

SourceMemberContainerTypeSymbol.ReportReservedTypeName(identifier.Text, this.DeclaringCompilation, diagnostics.DiagnosticBag, location);
_binder.ReportFieldOrValueContextualKeywordConflictIfAny(parameter, identifier, diagnostics);

var tpEnclosing = ContainingSymbol.FindEnclosingTypeParameter(name);
if ((object?)tpEnclosing != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4010,9 +4010,6 @@ static void N(int q)
// (24,17): error CS0136: A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
// int value = 0; // CS0136
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "value").WithArguments("value").WithLocation(24, 17),
// (24,17): info CS9258: 'value' is a contextual keyword in property accessors starting in language version preview. Use '@value' instead.
// int value = 0; // CS0136
Diagnostic(ErrorCode.INF_IdentifierConflictWithContextualKeyword, "value = 0").WithArguments("value", "preview").WithLocation(24, 17),
// (25,15): info CS9258: 'value' is a contextual keyword in property accessors starting in language version preview. Use '@value' instead.
// M(value);
Diagnostic(ErrorCode.INF_IdentifierConflictWithContextualKeyword, "value").WithArguments("value", "preview").WithLocation(25, 15),
Expand All @@ -4028,9 +4025,6 @@ static void N(int q)
// (9,17): error CS0136: A local or parameter named 'y' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
// int y = 0; // CS0136
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "y").WithArguments("y").WithLocation(9, 17),
// (24,17): info CS9258: 'value' is a contextual keyword in property accessors starting in language version preview. Use '@value' instead.
// int value = 0; // CS0136
Diagnostic(ErrorCode.INF_IdentifierConflictWithContextualKeyword, "value = 0").WithArguments("value", "preview").WithLocation(24, 17),
// (24,17): error CS0136: A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
// int value = 0; // CS0136
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "value").WithArguments("value").WithLocation(24, 17),
Expand Down
3 changes: 0 additions & 3 deletions src/Compilers/CSharp/Test/Symbol/Symbols/SymbolErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2495,9 +2495,6 @@ public byte MyProp
// (12,14): error CS0136: A local or parameter named 'x' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
// long x = 1; // 0136
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "x").WithArguments("x").WithLocation(12, 14),
// (20,17): info CS9258: 'value' is a contextual keyword in property accessors starting in language version preview. Use '@value' instead.
// int value; // 0136
Diagnostic(ErrorCode.INF_IdentifierConflictWithContextualKeyword, "value").WithArguments("value", "preview").WithLocation(20, 17),
// (20,17): error CS0136: A local or parameter named 'value' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
// int value; // 0136
Diagnostic(ErrorCode.ERR_LocalIllegallyOverrides, "value").WithArguments("value").WithLocation(20, 17),
Expand Down
Loading

0 comments on commit 5d8b9b8

Please sign in to comment.