Skip to content

Commit 6aeead3

Browse files
committed
unified: Fix scoping for capture declarations
1 parent 91612f2 commit 6aeead3

3 files changed

Lines changed: 62 additions & 10 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
9999
index = 2 and result = decl.getValue()
100100
)
101101
or
102+
exists(FunctionExpr expr | n = expr |
103+
result = expr.getCaptureDeclaration(index)
104+
or
105+
// Put other children after the capture declarations.
106+
// They act as sibling-shadowing declarations, so they are in scope with in the other children.
107+
result = expr.getAFieldOrChild() and
108+
not result = expr.getACaptureDeclaration() and
109+
index = count(expr.getACaptureDeclaration())
110+
)
111+
or
102112
index = 0 and
103113
relocatedClassMember(n, result)
104114
}
@@ -174,11 +184,6 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
174184

175185
private class LocalVariableDeclarationSiblingShadowingDecl extends SiblingShadowingDecl instanceof LocalVariableDeclaration
176186
{
177-
LocalVariableDeclarationSiblingShadowingDecl() {
178-
// Capture-declarations act as local variables, but are not sibling-shadowing
179-
not this = any(FunctionExpr e).getACaptureDeclaration()
180-
}
181-
182187
override Expr getPattern() { result = LocalVariableDeclaration.super.getPattern() }
183188

184189
override AstNode getRhs() { result = LocalVariableDeclaration.super.getValue() }

‎unified/ql/test/library-tests/dataflow/capture.swift‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func t10() {
106106
sink(x1) // $ hasValueFlow=t10.1
107107
}
108108
let closure2 = { [x] in
109-
sink(x) // $ MISSING: hasValueFlow=t10.1
109+
sink(x) // $ hasValueFlow=t10.1
110110
}
111111
closure1()
112112
closure2()
@@ -115,8 +115,8 @@ func t10() {
115115
func t11() {
116116
let x = source("t11.1")
117117
let closure = { [x = x, blah = x] in
118-
sink(x) // $ MISSING: hasValueFlow=t11.1
119-
sink(blah) // $ MISSING: hasValueFlow=t11.1
118+
sink(x) // $ hasValueFlow=t11.1
119+
sink(blah) // $ hasValueFlow=t11.1
120120
}
121121
closure()
122122
}
@@ -163,8 +163,8 @@ class C {
163163
func capture_self() {
164164
x = source("C.2")
165165
let closure = { [self] in
166-
sink(self.x) // $ MISSING: hasValueFlow=C.2
167-
sink(x) // $ MISSING: hasValueFlow=C.2
166+
sink(self.x) // $ hasValueFlow=C.2
167+
sink(x) // $ hasValueFlow=C.2
168168
}
169169
closure()
170170
}

‎unified/ql/test/library-tests/dataflow/test.expected‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,37 @@ edges
8181
| capture.swift:94:13:94:17 | local [x] | capture.swift:99:16:99:16 | x | provenance | |
8282
| capture.swift:94:13:94:19 | local(...) | capture.swift:94:9:94:9 | y | provenance | |
8383
| capture.swift:104:9:104:9 | x | capture.swift:105:28:105:28 | x | provenance | |
84+
| capture.swift:104:9:104:9 | x | capture.swift:108:23:108:23 | x | provenance | |
8485
| capture.swift:104:13:104:27 | source(...) | capture.swift:104:9:104:9 | x | provenance | |
8586
| capture.swift:105:23:105:24 | x1 | capture.swift:111:5:111:12 | closure1 [x1] | provenance | |
8687
| capture.swift:105:28:105:28 | x | capture.swift:105:23:105:24 | x1 | provenance | |
88+
| capture.swift:108:23:108:23 | x | capture.swift:108:23:108:23 | x | provenance | |
89+
| capture.swift:108:23:108:23 | x | capture.swift:112:5:112:12 | closure2 [x] | provenance | |
8790
| capture.swift:111:5:111:12 | closure1 [x1] | capture.swift:106:14:106:15 | x1 | provenance | |
91+
| capture.swift:112:5:112:12 | closure2 [x] | capture.swift:109:14:109:14 | x | provenance | |
92+
| capture.swift:116:9:116:9 | x | capture.swift:117:26:117:26 | x | provenance | |
93+
| capture.swift:116:13:116:27 | source(...) | capture.swift:116:9:116:9 | x | provenance | |
94+
| capture.swift:117:22:117:22 | x | capture.swift:117:36:117:36 | x | provenance | |
95+
| capture.swift:117:22:117:22 | x | capture.swift:121:5:121:11 | closure [x] | provenance | |
96+
| capture.swift:117:26:117:26 | x | capture.swift:117:22:117:22 | x | provenance | |
97+
| capture.swift:117:29:117:32 | blah | capture.swift:121:5:121:11 | closure [blah] | provenance | |
98+
| capture.swift:117:36:117:36 | x | capture.swift:117:29:117:32 | blah | provenance | |
99+
| capture.swift:121:5:121:11 | closure [blah] | capture.swift:119:14:119:17 | blah | provenance | |
100+
| capture.swift:121:5:121:11 | closure [x] | capture.swift:118:14:118:14 | x | provenance | |
88101
| capture.swift:147:5:147:5 | x | capture.swift:149:5:149:12 | closure2 [x] | provenance | |
89102
| capture.swift:147:9:147:23 | source(...) | capture.swift:147:5:147:5 | x | provenance | |
90103
| capture.swift:149:5:149:12 | closure2 [x] | capture.swift:143:14:143:14 | x | provenance | |
91104
| capture.swift:156:9:156:9 | x | capture.swift:160:9:160:15 | closure [self, x] | provenance | |
92105
| capture.swift:156:13:156:25 | source(...) | capture.swift:156:9:156:9 | x | provenance | |
93106
| capture.swift:158:18:158:21 | self [x] | capture.swift:158:18:158:23 | ... .x | provenance | |
94107
| capture.swift:160:9:160:15 | closure [self, x] | capture.swift:158:18:158:21 | self [x] | provenance | |
108+
| capture.swift:164:9:164:9 | x | capture.swift:165:26:165:29 | self [x] | provenance | |
109+
| capture.swift:164:13:164:25 | source(...) | capture.swift:164:9:164:9 | x | provenance | |
110+
| capture.swift:165:26:165:29 | self [x] | capture.swift:165:26:165:29 | self [x] | provenance | |
111+
| capture.swift:165:26:165:29 | self [x] | capture.swift:169:9:169:15 | closure [self, x] | provenance | |
112+
| capture.swift:166:18:166:21 | self [x] | capture.swift:166:18:166:23 | ... .x | provenance | |
113+
| capture.swift:169:9:169:15 | closure [self, x] | capture.swift:166:18:166:21 | self [x] | provenance | |
114+
| capture.swift:169:9:169:15 | closure [self, x] | capture.swift:167:18:167:18 | x | provenance | |
95115
| implicit-self.swift:16:9:16:12 | [post] self [x] | implicit-self.swift:17:14:17:17 | self [x] | provenance | |
96116
| implicit-self.swift:16:9:16:14 | ... .x | implicit-self.swift:16:9:16:12 | [post] self [x] | provenance | |
97117
| implicit-self.swift:16:18:16:31 | source(...) | implicit-self.swift:16:9:16:14 | ... .x | provenance | |
@@ -341,7 +361,21 @@ nodes
341361
| capture.swift:105:23:105:24 | x1 | semmle.label | x1 |
342362
| capture.swift:105:28:105:28 | x | semmle.label | x |
343363
| capture.swift:106:14:106:15 | x1 | semmle.label | x1 |
364+
| capture.swift:108:23:108:23 | x | semmle.label | x |
365+
| capture.swift:108:23:108:23 | x | semmle.label | x |
366+
| capture.swift:109:14:109:14 | x | semmle.label | x |
344367
| capture.swift:111:5:111:12 | closure1 [x1] | semmle.label | closure1 [x1] |
368+
| capture.swift:112:5:112:12 | closure2 [x] | semmle.label | closure2 [x] |
369+
| capture.swift:116:9:116:9 | x | semmle.label | x |
370+
| capture.swift:116:13:116:27 | source(...) | semmle.label | source(...) |
371+
| capture.swift:117:22:117:22 | x | semmle.label | x |
372+
| capture.swift:117:26:117:26 | x | semmle.label | x |
373+
| capture.swift:117:29:117:32 | blah | semmle.label | blah |
374+
| capture.swift:117:36:117:36 | x | semmle.label | x |
375+
| capture.swift:118:14:118:14 | x | semmle.label | x |
376+
| capture.swift:119:14:119:17 | blah | semmle.label | blah |
377+
| capture.swift:121:5:121:11 | closure [blah] | semmle.label | closure [blah] |
378+
| capture.swift:121:5:121:11 | closure [x] | semmle.label | closure [x] |
345379
| capture.swift:143:14:143:14 | x | semmle.label | x |
346380
| capture.swift:147:5:147:5 | x | semmle.label | x |
347381
| capture.swift:147:9:147:23 | source(...) | semmle.label | source(...) |
@@ -351,6 +385,14 @@ nodes
351385
| capture.swift:158:18:158:21 | self [x] | semmle.label | self [x] |
352386
| capture.swift:158:18:158:23 | ... .x | semmle.label | ... .x |
353387
| capture.swift:160:9:160:15 | closure [self, x] | semmle.label | closure [self, x] |
388+
| capture.swift:164:9:164:9 | x | semmle.label | x |
389+
| capture.swift:164:13:164:25 | source(...) | semmle.label | source(...) |
390+
| capture.swift:165:26:165:29 | self [x] | semmle.label | self [x] |
391+
| capture.swift:165:26:165:29 | self [x] | semmle.label | self [x] |
392+
| capture.swift:166:18:166:21 | self [x] | semmle.label | self [x] |
393+
| capture.swift:166:18:166:23 | ... .x | semmle.label | ... .x |
394+
| capture.swift:167:18:167:18 | x | semmle.label | x |
395+
| capture.swift:169:9:169:15 | closure [self, x] | semmle.label | closure [self, x] |
354396
| implicit-self.swift:16:9:16:12 | [post] self [x] | semmle.label | [post] self [x] |
355397
| implicit-self.swift:16:9:16:14 | ... .x | semmle.label | ... .x |
356398
| implicit-self.swift:16:18:16:31 | source(...) | semmle.label | source(...) |
@@ -577,8 +619,13 @@ testFailures
577619
| capture.swift:95:10:95:10 | y | capture.swift:93:13:93:26 | source(...) | capture.swift:95:10:95:10 | y | $@ | capture.swift:93:13:93:26 | source(...) | source(...) |
578620
| capture.swift:98:14:98:14 | x | capture.swift:93:13:93:26 | source(...) | capture.swift:98:14:98:14 | x | $@ | capture.swift:93:13:93:26 | source(...) | source(...) |
579621
| capture.swift:106:14:106:15 | x1 | capture.swift:104:13:104:27 | source(...) | capture.swift:106:14:106:15 | x1 | $@ | capture.swift:104:13:104:27 | source(...) | source(...) |
622+
| capture.swift:109:14:109:14 | x | capture.swift:104:13:104:27 | source(...) | capture.swift:109:14:109:14 | x | $@ | capture.swift:104:13:104:27 | source(...) | source(...) |
623+
| capture.swift:118:14:118:14 | x | capture.swift:116:13:116:27 | source(...) | capture.swift:118:14:118:14 | x | $@ | capture.swift:116:13:116:27 | source(...) | source(...) |
624+
| capture.swift:119:14:119:17 | blah | capture.swift:116:13:116:27 | source(...) | capture.swift:119:14:119:17 | blah | $@ | capture.swift:116:13:116:27 | source(...) | source(...) |
580625
| capture.swift:143:14:143:14 | x | capture.swift:147:9:147:23 | source(...) | capture.swift:143:14:143:14 | x | $@ | capture.swift:147:9:147:23 | source(...) | source(...) |
581626
| capture.swift:158:18:158:23 | ... .x | capture.swift:156:13:156:25 | source(...) | capture.swift:158:18:158:23 | ... .x | $@ | capture.swift:156:13:156:25 | source(...) | source(...) |
627+
| capture.swift:166:18:166:23 | ... .x | capture.swift:164:13:164:25 | source(...) | capture.swift:166:18:166:23 | ... .x | $@ | capture.swift:164:13:164:25 | source(...) | source(...) |
628+
| capture.swift:167:18:167:18 | x | capture.swift:164:13:164:25 | source(...) | capture.swift:167:18:167:18 | x | $@ | capture.swift:164:13:164:25 | source(...) | source(...) |
582629
| implicit-self.swift:17:14:17:19 | ... .x | implicit-self.swift:16:18:16:31 | source(...) | implicit-self.swift:17:14:17:19 | ... .x | $@ | implicit-self.swift:16:18:16:31 | source(...) | source(...) |
583630
| implicit-self.swift:23:14:23:14 | x | implicit-self.swift:22:13:22:26 | source(...) | implicit-self.swift:23:14:23:14 | x | $@ | implicit-self.swift:22:13:22:26 | source(...) | source(...) |
584631
| implicit-self.swift:29:14:29:19 | ... .x | implicit-self.swift:28:13:28:26 | source(...) | implicit-self.swift:29:14:29:19 | ... .x | $@ | implicit-self.swift:28:13:28:26 | source(...) | source(...) |

0 commit comments

Comments
 (0)