Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ internal bool IsEarlyAttributeBinder
}
}

// Is the given node being bound as a nameof(...) operator?
protected virtual bool IsNameofArgument(SyntaxNode node)
{
return false;
}
// Return the nearest enclosing node being bound as a nameof(...) argument, if any, or null if none.
protected virtual SyntaxNode EnclosingNameofArgument => null;

/// <summary>
/// Get the next binder in which to look up a name, if not found by this binder.
Expand Down
11 changes: 6 additions & 5 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,8 @@ private BoundExpression BindNonMethod(SimpleNameSyntax node, Symbol symbol, Diag
case SymbolKind.Local:
{
var localSymbol = (LocalSymbol)symbol;
var constantValueOpt = localSymbol.IsConst ? localSymbol.GetConstantValue(node, this.LocalInProgress, diagnostics) : null;
var constantValueOpt = localSymbol.IsConst && this.EnclosingNameofArgument == null
? localSymbol.GetConstantValue(node, this.LocalInProgress, diagnostics) : null;
TypeSymbol type;

Location localSymbolLocation = localSymbol.Locations[0];
Expand Down Expand Up @@ -1277,7 +1278,7 @@ private BoundExpression SynthesizeReceiver(SimpleNameSyntax node, Symbol member,
if (currentType.IsEqualToOrDerivedFrom(member.ContainingType, ignoreDynamic: false, useSiteDiagnostics: ref useSiteDiagnostics))
{
bool hasErrors = false;
if (!IsNameofArgument(node))
if (EnclosingNameofArgument != node)
{
if (InFieldInitializer && !currentType.IsScriptClass)
{
Expand Down Expand Up @@ -4594,7 +4595,7 @@ private BoundExpression BindMemberAccessWithBoundLeft(
Error(diagnostics, ErrorCode.ERR_BadSKunknown, boundLeft.Syntax, leftType, MessageID.IDS_SK_TYVAR.Localize());
return BadExpression(node, LookupResultKind.NotAValue, boundLeft);
}
else if (this.IsNameofArgument(node))
else if (this.EnclosingNameofArgument == node)
{
// Support selecing an extension method from a type name in nameof(.)
return BindInstanceMemberAccess(node, right, boundLeft, rightName, rightArity, typeArgumentsSyntax, typeArguments, invoked, diagnostics);
Expand Down Expand Up @@ -5244,7 +5245,7 @@ protected BoundExpression BindFieldAccess(

ConstantValue constantValueOpt = null;

if (fieldSymbol.IsConst)
if (fieldSymbol.IsConst && this.EnclosingNameofArgument == null)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning "there is no enclosing nameof expression"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding this as a comment

{
constantValueOpt = fieldSymbol.GetConstantValue(this.ConstantFieldsInProgress, this.IsEarlyAttributeBinder);
if (constantValueOpt == ConstantValue.Unset)
Expand Down Expand Up @@ -5386,7 +5387,7 @@ private bool CheckInstanceOrStatic(
}
else
{
if (instanceReceiver == false && !IsNameofArgument(node))
if (instanceReceiver == false && EnclosingNameofArgument != node)
{
Error(diagnostics, ErrorCode.ERR_ObjectRequired, node, symbol);
resultKind = LookupResultKind.StaticInstanceMismatch;
Expand Down
5 changes: 1 addition & 4 deletions src/Compilers/CSharp/Portable/Binder/NameofBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public NameofBinder(SyntaxNode nameofArgument, Binder next) : base(next)
_nameofArgument = nameofArgument;
}

protected override bool IsNameofArgument(SyntaxNode possibleNameofArgument)
{
return possibleNameofArgument == _nameofArgument;
}
protected override SyntaxNode EnclosingNameofArgument => _nameofArgument;
}
}
Loading