@@ -76,28 +76,21 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
7676 or
7777 result = n .( LogicalAndRoot ) .getNthLeaf ( index )
7878 or
79- index = 0 and
80- relocatedClassMember ( n , result )
81- }
82-
83- /**
84- * Holds if `member` is moved onto a child of `className` instead of the class itself,
85- * so the member name is not in scope in the base types and type parameter constraints.
86- */
87- private predicate relocatedClassMember ( Identifier className , Member member ) {
79+ // Move members under the name node of its class, so the member name is not in scope
80+ // in the base types and type parameter constraints.
8881 exists ( ClassLikeDeclaration cls |
89- className = cls .getNameNode ( ) and
90- member = cls .getAMember ( )
82+ n = cls .getNameNode ( ) and
83+ result = cls .getAMember ( ) and
84+ index = 0
9185 )
9286 }
9387
9488 AstNode getChild ( AstNode n , int index ) {
9589 result = getChild1 ( n , index )
9690 or
97- not exists ( getChild1 ( n , _) ) and
91+ not result = getChild1 ( _ , _) and
9892 not n instanceof LogicalAndExpr and // also ignore intermediate nodes within a 'logical and' tree
9993 not n instanceof GuardIfStmt and
100- not relocatedClassMember ( _, result ) and
10194 index = result .getParentIndex ( ) and
10295 result = n .getAFieldOrChild ( )
10396 }
@@ -205,8 +198,17 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
205198 declaration = func
206199 )
207200 or
208- exists ( Parameter param |
209- scope = param .getParent ( ) and // TODO: add SourceCallable and use .getParameter() instead
201+ exists ( Parameter param , Callable callable |
202+ callable = param .getDeclaringCallable ( ) and
203+ (
204+ // Scope to the body so parameter defaults cannot see the parameter list
205+ // TODO: Use a plugin for languages where parameter defaults can reference earlier parameters
206+ scope = callable .getBody ( )
207+ or
208+ // For bodyless functions, choose an arbitrary scope
209+ not exists ( callable .getBody ( ) ) and
210+ scope = param
211+ ) and
210212 pattern = param .getPattern ( ) and
211213 declaration = param
212214 )
@@ -332,7 +334,9 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
332334
333335 predicate implicitDeclInScope ( string name , AstNode scope ) { implicitDeclInScope ( name , scope , _) }
334336
335- predicate accessCand ( AstNode n , string name ) { n .( PotentialLocalNameAccess ) .getName ( ) = name }
337+ predicate accessCand ( AstNode n , string name ) {
338+ n .( PotentialLocalNameAccess ) .getName ( ) = name and not n instanceof NameBinding
339+ }
336340
337341 predicate uncertainScope ( AstNode scope ) {
338342 // Classes can have uncertain members due to unqualified access to inherited members.
@@ -349,6 +353,17 @@ predicate bindingContext = LocalNameBindingInput::bindingContext/3;
349353
350354module LocalNameBindingOutput = LocalNameBinding< Location , LocalNameBindingInput > ;
351355
356+ private LocalNameBindingOutput:: Local resolveId ( AstNode n ) {
357+ // For names in binding position, always resolve to the scope set by the bindingContext predicate
358+ exists ( AstNode scope |
359+ bindingContext ( n , scope , _) and
360+ result .hasNameAndScope ( n .( Identifier ) .getValue ( ) , scope )
361+ )
362+ or
363+ not bindingContext ( n , _, _) and
364+ result = n .( LocalNameBindingOutput:: LocalAccess ) .getLocal ( )
365+ }
366+
352367module Public {
353368 /**
354369 * A representative for a lexically scoped entity, such as a local variable, type name, or module name.
@@ -381,7 +396,7 @@ module Public {
381396 string getName ( ) { result = this .getValue ( ) }
382397
383398 /** Gets the representative for the local name introduced by this declaration. */
384- LocalName getLocalName ( ) { result = this . ( LocalNameBindingOutput :: LocalAccess ) . getLocal ( ) }
399+ LocalName getLocalName ( ) { result = resolveId ( this ) }
385400 }
386401
387402 final class LocalVariable = LocalVariableImpl ;
@@ -499,7 +514,7 @@ final class PotentialLocalNameAccess = PotentialLocalNameAccessImpl;
499514 */
500515abstract private class PotentialLocalNameAccessImpl extends AstNode {
501516 /** Gets the representative for the local name being accessed. */
502- LocalName getLocalName ( ) { result = this . ( LocalNameBindingOutput :: LocalAccess ) . getLocal ( ) }
517+ LocalName getLocalName ( ) { result = resolveId ( this ) }
503518
504519 /** Gets the name being accessed. */
505520 abstract string getName ( ) ;
0 commit comments