Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure to get the name of nameof argument #52016

Merged
merged 4 commits into from
Mar 26, 2021
Merged

Conversation

Youssef1313
Copy link
Member

@Youssef1313 Youssef1313 commented Mar 20, 2021

Fixes #52013.

@Youssef1313 Youssef1313 marked this pull request as ready for review March 20, 2021 15:52
@Youssef1313 Youssef1313 requested a review from a team as a code owner March 20, 2021 15:52
@jcouv jcouv added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Mar 21, 2021
@@ -1874,7 +1874,7 @@ private void EnsureNameofExpressionSymbols(BoundMethodGroup methodGroup, Binding
/// <summary>
/// Returns true if syntax form is OK (so no errors were reported)
/// </summary>
private bool CheckSyntaxForNameofArgument(ExpressionSyntax argument, out string name, BindingDiagnosticBag diagnostics, bool top = true)
private bool CheckSyntaxForNameofArgument(ExpressionSyntax argument, out string name, BindingDiagnosticBag diagnostics, bool boundArgumentHasAnyErrors, bool top = true)
Copy link
Contributor

@AlekseyTs AlekseyTs Mar 23, 2021

Choose a reason for hiding this comment

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

bool boundArgumentHasAnyErrors [](start = 128, length = 30)

Instead of adding this parameter we should consider passing BindingDiagnosticBag.Discarded when we are trying to suppress cascading diagnostics. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

@AlekseyTs That simplifies it alot! 🎉

// We relax the instance-vs-static requirement for top-level member access expressions by creating a NameofBinder binder.
var nameofBinder = new NameofBinder(argument, this);
var boundArgument = nameofBinder.BindExpression(argument, diagnostics);
if (!boundArgument.HasAnyErrors && CheckSyntaxForNameofArgument(argument, out name, diagnostics) && boundArgument.Kind == BoundKind.MethodGroup)
var syntaxIsOk = CheckSyntaxForNameofArgument(argument, out string name, diagnostics, boundArgument.HasAnyErrors);
Copy link
Contributor

@AlekseyTs AlekseyTs Mar 23, 2021

Choose a reason for hiding this comment

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

var syntaxIsOk = CheckSyntaxForNameofArgument(argument, out string name, diagnostics, boundArgument.HasAnyErrors); [](start = 12, length = 114)

We should add tests that observe this behavior change via SemanticModel/IOperation and possibly other APIs. #Closed

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Mar 23, 2021

Done with review pass (commit 3) #Closed

// We relax the instance-vs-static requirement for top-level member access expressions by creating a NameofBinder binder.
var nameofBinder = new NameofBinder(argument, this);
var boundArgument = nameofBinder.BindExpression(argument, diagnostics);
if (!boundArgument.HasAnyErrors && CheckSyntaxForNameofArgument(argument, out name, diagnostics) && boundArgument.Kind == BoundKind.MethodGroup)

bool syntaxIsOk = CheckSyntaxForNameofArgument(argument, out string name, boundArgument.HasAnyErrors ? BindingDiagnosticBag.Discarded : diagnostics);
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't want to introduce a local for this conditional expression to prevent it from being used accidentally. (See few lines later which uses diagnostics regardless syntaxIsOk)

}
Block[B2] - Exit
Predecessors: [B1]
Statements (0)
";
VerifyFlowGraphAndDiagnosticsForTest<BlockSyntax>(source, expectedFlowGraph, expectedDiagnostics);
Copy link
Contributor

Choose a reason for hiding this comment

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

VerifyFlowGraphAndDiagnosticsForTest [](start = 12, length = 36)

Did you really mean to validate a flow graph or just copied "wrong" test?

Copy link
Member Author

@Youssef1313 Youssef1313 Mar 24, 2021

Choose a reason for hiding this comment

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

@AlekseyTs, Ah I think I wanted to validate the operation tree instead. But probably okay to keep it since the ILiteralOperation's constant value should be affected by this change. Let me know if this alone is sufficient, or if I should validate both flow graph and operation tree, or operation tree only.

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 4), modulo a question around IOperation test.

@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler Please review. Need a second sign-off for a small community PR.

3 similar comments
@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler Please review. Need a second sign-off for a small community PR.

@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler Please review. Need a second sign-off for a small community PR.

@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler Please review. Need a second sign-off for a small community PR.

@333fred 333fred merged commit 5ac580f into dotnet:main Mar 26, 2021
@ghost ghost added this to the Next milestone Mar 26, 2021
@333fred
Copy link
Member

333fred commented Mar 26, 2021

Thanks @Youssef1313!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nameof in attribute produces empty string
5 participants