Skip to content

Commit a7925dc

Browse files
authored
Merge pull request #22960 from asgerf/unified/func-param-clash
Unified: Fix two bugs in parameter name scoping
2 parents 721743f + 9ef6569 commit a7925dc

3 files changed

Lines changed: 264 additions & 198 deletions

File tree

‎shared/namebinding/codeql/namebinding/LocalNameBinding.qll‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,12 @@ module LocalNameBinding<LocationSig Location, LocalNameBindingInputSig<Location>
589589

590590
/** Gets a textual representation of this local entity. */
591591
string toString() { result = this.getName() }
592+
593+
/** Holds if this variable has the given name and scope. */
594+
pragma[nomagic]
595+
predicate hasNameAndScope(string name, AstNode scope) {
596+
this = TExplicitLocal(_, name, scope) or this = TImplicitLocal(name, scope)
597+
}
592598
}
593599

594600
final class Local = LocalImpl;
@@ -624,10 +630,6 @@ module LocalNameBinding<LocationSig Location, LocalNameBindingInputSig<Location>
624630
override string getName() { result = name }
625631

626632
override Location getLocation() { result = scope.getLocation() }
627-
628-
/** Holds if this variable has the given name and scope. */
629-
pragma[nomagic]
630-
predicate hasNameAndScope(string name_, AstNode scope_) { name = name_ and scope = scope_ }
631633
}
632634

633635
/** A local access. */

‎unified/ql/lib/codeql/unified/internal/LocalNameBinding.qll‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

350354
module 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+
352367
module 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
*/
500515
abstract 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

Comments
 (0)