Disallow native ints inside nameof#47497
Conversation
| if (node is IdentifierNameSyntax identifier) | ||
| { | ||
| var type = BindNativeIntegerSymbolIfAny(identifier, diagnostics); | ||
| var type = IsInsideNameof ? null : BindNativeIntegerSymbolIfAny(identifier, diagnostics); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 😄
| identifier.Parent?.Parent?.Parent is InvocationExpressionSyntax invocation && | ||
| (invocation.Expression as IdentifierNameSyntax)?.Identifier.ContextualKind() == SyntaxKind.NameOfKeyword | ||
| ? null | ||
| : BindNativeIntegerSymbolIfAny(identifier, diagnostics); |
There was a problem hiding this comment.
nit: could we move all the extra logic into BindNativeIntegerSymbolIfAny? It would help encapsulate the complexity away from sight a bit.
There was a problem hiding this comment.
@jcouv There is another call for BindNativeIntegerSymbolIfAny:
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.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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)
jcouv
left a comment
There was a problem hiding this comment.
Done with review pass (iteration 14). Minor comment
|
@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? |
No, please avoid squashing commits while the PR is being reviewed. We'll Squash and Merge when merging the change. |
|
@cston An unrelated test has failed. Can you re-run the failed job? |
|
Hit the re-run on it. |
|
Merged/squashed. Thanks for the fix @Youssef1313 ! |



Related to #47128
cc: @cston @333fred @gafter for review.