Skip to content

Commit 3659ff5

Browse files
committed
debug
1 parent 094a86e commit 3659ff5

File tree

3 files changed

+64
-42
lines changed

3 files changed

+64
-42
lines changed

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
private import rust
22
private import codeql.rust.controlflow.ControlFlowGraph
3-
private import codeql.rust.elements.internal.generated.ParentChild
3+
private import codeql.rust.elements.internal.generated.ParentChild as ParentChild
44
private import codeql.rust.elements.internal.PathImpl::Impl as PathImpl
55
private import codeql.rust.elements.internal.PathExprBaseImpl::Impl as PathExprBaseImpl
66
private import codeql.rust.elements.internal.FormatTemplateVariableAccessImpl::Impl as FormatTemplateVariableAccessImpl
@@ -36,9 +36,37 @@ module Impl {
3636
ClosureBodyScope() { this = any(ClosureExpr ce).getBody() }
3737
}
3838

39-
class IfExprScope extends VariableScope, IfExpr { }
39+
/**
40+
* A scope for conditions, which may introduce variables using `let`
41+
* expressions, which are available only in the `then` branch or
42+
* loop body.
43+
*/
44+
class ConditionScope extends VariableScope, Expr {
45+
private AstNode parent;
46+
private AstNode body;
47+
48+
ConditionScope() {
49+
parent =
50+
any(IfExpr ie |
51+
this = ie.getCondition() and
52+
body = ie.getThen()
53+
)
54+
or
55+
parent =
56+
any(WhileExpr we |
57+
this = we.getCondition() and
58+
body = we.getLoopBody()
59+
)
60+
}
4061

41-
class WhileExprScope extends VariableScope, WhileExpr { }
62+
/** Gets the parent of this condition. */
63+
AstNode getParent() { result = parent }
64+
65+
/**
66+
* Gets the body in which variables introduced in this scope are available.
67+
*/
68+
AstNode getBody() { result = body }
69+
}
4270

4371
private Pat getAPatAncestor(Pat p) {
4472
(p instanceof IdentPat or p instanceof OrPat) and
@@ -203,45 +231,38 @@ module Impl {
203231
string getName() { result = name_ }
204232
}
205233

206-
private AstNode getElseBranch(
207-
AstNode elseParentParent, int index, AstNode elseParent, int elseIndex
208-
) {
209-
elseParent = getImmediateChild(elseParentParent, index) and
210-
result = getImmediateChild(elseParent, elseIndex) and
211-
(
212-
result = elseParent.(LetStmt).getLetElse()
213-
or
214-
result = elseParent.(IfExpr).getElse()
215-
)
216-
}
217-
218-
private AstNode getLoopBody(LoopingExpr loop) { result = loop.getLoopBody() }
219-
220234
pragma[nomagic]
221-
private Element getImmediateChildAdj(Element e, int preOrd, int index, int postOrd) {
222-
result = getImmediateChild(e, index) and
235+
private Element getImmediateChildAdj(Element e, int preOrd, int index) {
236+
result = ParentChild::getImmediateChild(e, index) and
223237
preOrd = 0 and
224-
postOrd = 0 and
225-
not result = getElseBranch(_, _, e, index) and
226-
not result = getLoopBody(e)
227-
or
228-
result = getElseBranch(e, index, _, _) and
229-
preOrd = 0 and
230-
postOrd = -1
238+
not exists(ConditionScope cs |
239+
e = cs.getParent() and
240+
result = cs.getBody()
241+
)
231242
or
232-
result = getLoopBody(e) and
233-
index = 0 and
243+
result = e.(ConditionScope).getBody() and
234244
preOrd = 1 and
235-
postOrd = 0
245+
index = 0
236246
}
237247

248+
/**
249+
* An adjusted version of `ParentChild::getImmediateChild`, which makes the following
250+
* two adjustments:
251+
*
252+
* 1. For conditions like `if cond body`, instead of letting `body` be the second child
253+
* of `if`, we make it the last child of `cond`. This ensures that variables
254+
* introduced in the `cond` scope are available in `body`.
255+
*
256+
* 2. A similar adjustment is made for `while` loops: the body of the loop is made a
257+
* child of the loop condition instead of the loop itself.
258+
*/
238259
pragma[nomagic]
239260
private Element getImmediateChildAdj(Element e, int index) {
240261
result =
241-
rank[index + 1](Element res, int i, int preOrd, int postOrd |
242-
res = getImmediateChildAdj(e, preOrd, i, postOrd)
262+
rank[index + 1](Element res, int preOrd, int i |
263+
res = getImmediateChildAdj(e, preOrd, i)
243264
|
244-
res order by preOrd, i, postOrd
265+
res order by preOrd, i
245266
)
246267
}
247268

@@ -252,7 +273,7 @@ module Impl {
252273
n instanceof Pat or
253274
n instanceof VariableAccessCand or
254275
n instanceof LetStmt or
255-
n instanceof LetExpr or
276+
n = any(LetExpr le).getScrutinee() or
256277
n instanceof VariableScope
257278
) and
258279
exists(AstNode n0 |
@@ -298,7 +319,7 @@ module Impl {
298319
this instanceof VariableScope or
299320
this instanceof VariableAccessCand or
300321
this instanceof LetStmt or
301-
this instanceof LetExpr or
322+
this = any(LetExpr le).getScrutinee() or
302323
getImmediateChildAdj(this, _) instanceof RelevantElement
303324
}
304325

@@ -410,12 +431,13 @@ module Impl {
410431
ord = getLastPreOrderNumbering(scope, let) + 1
411432
)
412433
or
413-
exists(LetExpr let |
434+
exists(LetExpr let, Expr scrutinee |
414435
let.getPat() = pat and
415-
scope = getEnclosingScope(let) and
416-
// for `let` expressions, variables are bound _after_ the statement, i.e.
436+
scrutinee = let.getScrutinee() and
437+
scope = getEnclosingScope(scrutinee) and
438+
// for `let` expressions, variables are bound _after_ the expression, i.e.
417439
// not in the RHS
418-
ord = getLastPreOrderNumbering(scope, let) + 1
440+
ord = getLastPreOrderNumbering(scope, scrutinee) + 1
419441
)
420442
or
421443
exists(ForExpr fe |

rust/ql/test/library-tests/variables/Ssa.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ read
274274
| main.rs:335:20:335:20 | x | main.rs:335:20:335:20 | x | main.rs:337:19:337:19 | x |
275275
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:348:7:348:7 | x |
276276
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:353:7:353:7 | x |
277+
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:357:19:357:19 | x |
277278
| main.rs:347:16:347:16 | x | main.rs:347:16:347:16 | x | main.rs:350:19:350:19 | x |
278279
| main.rs:352:20:352:20 | x | main.rs:352:20:352:20 | x | main.rs:355:19:355:19 | x |
279280
| main.rs:362:5:362:6 | a8 | main.rs:362:5:362:6 | a8 | main.rs:368:15:368:16 | a8 |
@@ -567,6 +568,7 @@ adjacentReads
567568
| main.rs:318:14:318:14 | x | main.rs:318:14:318:14 | x | main.rs:321:5:321:5 | x | main.rs:323:19:323:19 | x |
568569
| main.rs:332:9:332:9 | x | main.rs:332:9:332:9 | x | main.rs:333:11:333:11 | x | main.rs:341:15:341:15 | x |
569570
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:348:7:348:7 | x | main.rs:353:7:353:7 | x |
571+
| main.rs:346:9:346:9 | x | main.rs:346:9:346:9 | x | main.rs:353:7:353:7 | x | main.rs:357:19:357:19 | x |
570572
| main.rs:389:9:389:10 | c2 | main.rs:382:13:382:14 | c2 | main.rs:395:9:395:10 | c2 | main.rs:399:15:399:16 | c2 |
571573
| main.rs:390:9:390:10 | b4 | main.rs:381:13:381:14 | b4 | main.rs:394:9:394:10 | b4 | main.rs:398:15:398:16 | b4 |
572574
| main.rs:390:9:390:10 | b4 | main.rs:381:13:381:14 | b4 | main.rs:398:15:398:16 | b4 | main.rs:412:15:412:16 | b4 |

rust/ql/test/library-tests/variables/variables.expected

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
testFailures
2-
| main.rs:357:19:357:19 | x | Unexpected result: read_access=x2 |
3-
| main.rs:357:32:357:50 | //... | Missing result: read_access=x1 |
42
variable
53
| main.rs:5:14:5:14 | s |
64
| main.rs:10:14:10:14 | i |
@@ -227,7 +225,7 @@ variableAccess
227225
| main.rs:350:19:350:19 | x | main.rs:347:16:347:16 | x |
228226
| main.rs:353:7:353:7 | x | main.rs:346:9:346:9 | x |
229227
| main.rs:355:19:355:19 | x | main.rs:352:20:352:20 | x |
230-
| main.rs:357:19:357:19 | x | main.rs:347:16:347:16 | x |
228+
| main.rs:357:19:357:19 | x | main.rs:346:9:346:9 | x |
231229
| main.rs:368:15:368:16 | a8 | main.rs:362:5:362:6 | a8 |
232230
| main.rs:369:15:369:16 | b3 | main.rs:364:9:364:10 | b3 |
233231
| main.rs:370:15:370:16 | c1 | main.rs:365:9:365:10 | c1 |
@@ -451,7 +449,7 @@ variableReadAccess
451449
| main.rs:350:19:350:19 | x | main.rs:347:16:347:16 | x |
452450
| main.rs:353:7:353:7 | x | main.rs:346:9:346:9 | x |
453451
| main.rs:355:19:355:19 | x | main.rs:352:20:352:20 | x |
454-
| main.rs:357:19:357:19 | x | main.rs:347:16:347:16 | x |
452+
| main.rs:357:19:357:19 | x | main.rs:346:9:346:9 | x |
455453
| main.rs:368:15:368:16 | a8 | main.rs:362:5:362:6 | a8 |
456454
| main.rs:369:15:369:16 | b3 | main.rs:364:9:364:10 | b3 |
457455
| main.rs:370:15:370:16 | c1 | main.rs:365:9:365:10 | c1 |

0 commit comments

Comments
 (0)