Skip to content

Disallow native ints inside nameof#47497

Merged
jcouv merged 18 commits into
dotnet:masterfrom
Youssef1313:patch-27
Sep 11, 2020
Merged

Disallow native ints inside nameof#47497
jcouv merged 18 commits into
dotnet:masterfrom
Youssef1313:patch-27

Conversation

@Youssef1313
Copy link
Copy Markdown
Member

@Youssef1313 Youssef1313 commented Sep 6, 2020

Related to #47128

cc: @cston @333fred @gafter for review.

@Youssef1313 Youssef1313 requested a review from a team as a code owner September 6, 2020 08:48
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs Outdated
if (node is IdentifierNameSyntax identifier)
{
var type = BindNativeIntegerSymbolIfAny(identifier, diagnostics);
var type = IsInsideNameof ? null : BindNativeIntegerSymbolIfAny(identifier, diagnostics);
Copy link
Copy Markdown
Member Author

@Youssef1313 Youssef1313 Sep 6, 2020

Choose a reason for hiding this comment

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

int isn't allowed in nameof. However, something like nameof(int.MaxValue) is allowed.

Is there similar thing for nint? e.g. nameof(nint.Something) which should be allowed?

If there is something like that, my approach will probably fail.

Edit:

Probably nameof(nint.Equals) should be allowed. Let me add a test for it.

Edit 2:

Added a check for parent node to fix this issue.

var type = BindNativeIntegerSymbolIfAny(identifier, diagnostics);
// Don't bind nameof(nint) or nameof(nuint) so that ERR_NameNotInContext is reported.
// The check for ArgumentSyntax is to allow the binding for something like nameof(nint.Equals).
var type = IsInsideNameof && identifier.Parent is ArgumentSyntax
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.

Do I need to check that the parent of this ArgumentSyntax is an ArgumentListSyntax of an InvocationExpressionSyntax whose Expression is an IdentifierNameSyntax with IdentifierToken.Text equals to nameof?
That would be a large check that I don't know it's necessary or not.

For the above to make some sense, refer to the following visualization while reading it 😄

image

Copy link
Copy Markdown
Member Author

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

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

image

image

In the current master, nameof(dynamic) generates an invalid operation. While nameof(nint) generates "none" operation. I'm not sure if my change will fix that, or if that even needs to be fixed. (Given that nameof(int) generates "none")

@jcouv jcouv added Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Sep 6, 2020
Comment thread src/Compilers/CSharp/Test/Semantic/Semantics/NameOfTests.cs Outdated
Comment thread src/Compilers/CSharp/Test/Semantic/Semantics/NameOfTests.cs
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs Outdated
Comment thread src/Compilers/CSharp/Test/Semantic/Semantics/NameOfTests.cs
Copy link
Copy Markdown
Contributor

@cston cston left a comment

Choose a reason for hiding this comment

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

Thanks @Youssef1313.

Comment thread src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs Outdated
identifier.Parent?.Parent?.Parent is InvocationExpressionSyntax invocation &&
(invocation.Expression as IdentifierNameSyntax)?.Identifier.ContextualKind() == SyntaxKind.NameOfKeyword
? null
: BindNativeIntegerSymbolIfAny(identifier, diagnostics);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: could we move all the extra logic into BindNativeIntegerSymbolIfAny? It would help encapsulate the complexity away from sight a bit.

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.

@jcouv There is another call for BindNativeIntegerSymbolIfAny:

bindingResult = BindNativeIntegerSymbolIfAny(node, diagnostics);

I don't know if there would be unintended effects of changing the method itself. That's why I kept the change outside it to only affect this code path.

If you can confirm that both method calls need this change, I'll do that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The second caller doesn't need this change, but won't be hurt (the IsInsideNameof check is fast).

@cston Just to confirm, what do you think of my proposal (move the new logic inside BindNativeIntegerSymbolIfAny)?

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.

what do you think of my proposal (move the new logic inside BindNativeIntegerSymbolIfAny)?

Sounds reasonable. And it would mean we could avoid the extra check unless the identifier is "nint" or "nuint".


In reply to: 485950715 [](ancestors = 485950715)

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

Done with review pass (iteration 14). Minor comment

@jcouv jcouv self-assigned this Sep 9, 2020
Comment thread src/Compilers/CSharp/Test/Semantic/Semantics/NameOfTests.cs Outdated
@Youssef1313
Copy link
Copy Markdown
Member Author

@cston Moved the logic to BindNativeIntegerSymbolIfAny and updated its comment to be clear about that.

Do you want me to squash all the commits to avoid any noise in master branch?

@cston
Copy link
Copy Markdown
Contributor

cston commented Sep 10, 2020

Do you want me to squash all the commits to avoid any noise in master branch?

No, please avoid squashing commits while the PR is being reviewed. We'll Squash and Merge when merging the change.

@Youssef1313
Copy link
Copy Markdown
Member Author

@cston An unrelated test has failed. Can you re-run the failed job?

@jaredpar
Copy link
Copy Markdown
Member

Hit the re-run on it.

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks! (iteration 18)

@jcouv jcouv merged commit af4fe0d into dotnet:master Sep 11, 2020
@ghost ghost added this to the Next milestone Sep 11, 2020
@jcouv
Copy link
Copy Markdown
Member

jcouv commented Sep 11, 2020

Merged/squashed. Thanks for the fix @Youssef1313 !

@Youssef1313 Youssef1313 deleted the patch-27 branch September 11, 2020 18:17
@dibarbet dibarbet modified the milestones: Next, 16.8.P4 Sep 22, 2020
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.

6 participants