Skip to content

Commit 7ded08c

Browse files
committed
unified: Add tests and handle capture-declaration scoping
1 parent aca4c93 commit 7ded08c

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
168168

169169
private class LocalVariableDeclarationSiblingShadowingDecl extends SiblingShadowingDecl instanceof LocalVariableDeclaration
170170
{
171+
LocalVariableDeclarationSiblingShadowingDecl() {
172+
// Capture-declarations act as local variables, but are not sibling-shadowing
173+
not this = any(FunctionExpr e).getACaptureDeclaration()
174+
}
175+
171176
override Expr getPattern() { result = LocalVariableDeclaration.super.getPattern() }
172177

173178
override AstNode getRhs() { result = LocalVariableDeclaration.super.getValue() }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class C {
2+
func t1() { // implicit-self=t1.self
3+
print(self) // $ access=t1.self
4+
}
5+
6+
var instanceField = 123;
7+
8+
func t2() { // implicit-self=t2.self
9+
print(instanceField) // $ access=instanceField implicit-qualifier=t2.self
10+
}
11+
12+
func t3() { // implicit-self=t3.self
13+
foo(123) { [self] in // name=closure.self
14+
print(self) // $ access=closure.self
15+
print(instanceField) // $ access=instanceField implicit-qualifier=closure.self
16+
}
17+
}
18+
19+
func t4() { // implicit-self=t4.self
20+
foo(123) { [weak self] in // name=weak.self
21+
// Here, 'self' is an Option<C> referring to .some(<outer self>) if it
22+
// has not been GC'ed yet. Swift does not allow unqualified self access here.
23+
24+
print(self) // $ access=weak.self
25+
26+
// Unwrap the 'self' optional to get a strong reference.
27+
guard let self else { return } // $ access=weak.self // name=guarded.self
28+
29+
print(self) // $ access=guarded.self
30+
print(instanceField) // $ access=instanceField implicit-qualifier=guarded.self
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)