Allow independent methods to have the same parameter name#45501
Allow independent methods to have the same parameter name#45501sharwell merged 2 commits intodotnet:release/dev16.7from
Conversation
|
@sharwell What in case of local methods? In C#8 we can hide existing parameter, but in netcore2.1 suggested name can breake up build. Here's a testCase: |
|
@miqm I believe that's an existing bug, so I wasn't trying to fix it. Is there a specific case that worked in 16.6 but isn't working anymore? |
|
@sharwell - I think I didn't make myself clear enough. The full test cases, that IMHO should work, are as follows: I verified those test cases on 16.7-preview2 and they all work. I'm not sure if they will work with your code. Stopping search on ParameterList syntax will take into account only items on current parameter list (inner method's list) and will not take into account the outer method block. |
miqm
left a comment
There was a problem hiding this comment.
Instead adding additional case I'd revert to what it was before but use a new method with old IsExecutableBlock method content.
| public SyntaxNode GetParameterList(SyntaxNode node) | ||
| => node.GetParameterList(); | ||
|
|
||
| public bool IsParameterList(SyntaxNode node) |
There was a problem hiding this comment.
public bool IsParameterScope(SyntaxNode node)
=> node.IsKind(SyntaxKind.Block, SyntaxKind.SwitchSection);
| SyntaxToken? GetNameOfParameter(SyntaxNode node); | ||
| SyntaxNode GetDefaultOfParameter(SyntaxNode node); | ||
| SyntaxNode GetParameterList(SyntaxNode node); | ||
| bool IsParameterList(SyntaxNode node); |
| Return node.GetParameterList() | ||
| End Function | ||
|
|
||
| Public Function IsParameterList(node As SyntaxNode) As Boolean Implements ISyntaxFacts.IsParameterList |
There was a problem hiding this comment.
Public Function IsParameterScope(node As SyntaxNode) As Boolean Implements ISyntaxFacts.IsParameterScope
Return node.IsKind(SyntaxKind.Block, SyntaxKind.SwitchSection)
End Function
| { | ||
| var container = containerOpt ?? location.AncestorsAndSelf().FirstOrDefault( | ||
| a => SyntaxFacts.IsExecutableBlock(a) || SyntaxFacts.IsMethodBody(a)); | ||
| a => SyntaxFacts.IsExecutableBlock(a) || SyntaxFacts.IsParameterList(a) || SyntaxFacts.IsMethodBody(a)); |
There was a problem hiding this comment.
a => SyntaxFacts.IsParameterScope(a) || SyntaxFacts.IsMethodBody(a));
There was a problem hiding this comment.
IsParameterScope isn't something SyntaxFacts should know about. SyntaxFacts is just a way to structurally ask what type of node something is, in a way that is agnostic to VB/C#. i.e. if both VB/C# have an InvocationExpressionSyntax, then SyntaxFacts can be used with IsInvocationExpression to ask that question in a uniform way, without worrying about lang.
It's specifically for syntactic similarity questions, not for semantic questions (like scopes). Thanks!
There was a problem hiding this comment.
@CyrusNajmabadi Ok, understood. Although I still think this particular change will lead to check only current ParameterList and will not take into account i.e. method body for local method parameters. Perhaps it could be added some sort of container filter which can be used by Declaration Name provider to take into account only MethodBody (if there is one)?
@sharwell This is screenshot from 16.6.2: As you can see, context1 name is suggested here correctly - context is used in the method parameters list. In this solution I think "context" will be suggested, as the lookup for container will stop on first Parameters list - in this case - on the parameters list for the local method. |
|
@miqm I added all your tests locally and they worked without other changes. See my new commit! |
miqm
left a comment
There was a problem hiding this comment.
Oh, apparently inner method's parameterList is not ParameterList in SyntaxFacts understanding. Happy to approve :)

Fixes #45492