11private import rust
22private 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
44private import codeql.rust.elements.internal.PathImpl:: Impl as PathImpl
55private import codeql.rust.elements.internal.PathExprBaseImpl:: Impl as PathExprBaseImpl
66private import codeql.rust.elements.internal.FormatTemplateVariableAccessImpl:: Impl as FormatTemplateVariableAccessImpl
@@ -36,9 +36,30 @@ module Impl {
3636 ClosureBodyScope ( ) { this = any ( ClosureExpr ce ) .getBody ( ) }
3737 }
3838
39- class IfExprScope extends VariableScope , IfExpr { }
39+ // class IfExprScope extends VariableScope, IfExpr { }
40+ // class WhileExprScope extends VariableScope, WhileExpr { }
41+ class ConditionScope extends VariableScope {
42+ private AstNode parent ;
43+ private AstNode body ;
4044
41- class WhileExprScope extends VariableScope , WhileExpr { }
45+ ConditionScope ( ) {
46+ parent =
47+ any ( IfExpr ie |
48+ this = ie .getCondition ( ) and
49+ body = ie .getThen ( )
50+ )
51+ or
52+ parent =
53+ any ( WhileExpr we |
54+ this = we .getCondition ( ) and
55+ body = we .getLoopBody ( )
56+ )
57+ }
58+
59+ AstNode getConditionParent ( ) { result = parent }
60+
61+ AstNode getBody ( ) { result = body }
62+ }
4263
4364 private Pat getAPatAncestor ( Pat p ) {
4465 ( p instanceof IdentPat or p instanceof OrPat ) and
@@ -206,39 +227,52 @@ module Impl {
206227 private AstNode getElseBranch (
207228 AstNode elseParentParent , int index , AstNode elseParent , int elseIndex
208229 ) {
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- )
230+ elseParent = ParentChild:: getImmediateChild ( elseParentParent , index ) and
231+ result = ParentChild:: getImmediateChild ( elseParent , elseIndex ) and
232+ result = elseParent .( LetStmt ) .getLetElse ( )
233+ // or
234+ // result = elseParent.(IfExpr).getElse()
216235 }
217236
218- private AstNode getLoopBody ( LoopingExpr loop ) { result = loop .getLoopBody ( ) }
219-
237+ // private AstNode getLoopBody(LoopingExpr loop) { result = loop.getLoopBody() }
220238 pragma [ nomagic]
221239 private Element getImmediateChildAdj ( Element e , int preOrd , int index , int postOrd ) {
222- result = getImmediateChild ( e , index ) and
240+ result = ParentChild :: getImmediateChild ( e , index ) and
223241 preOrd = 0 and
224242 postOrd = 0 and
225- not result = getElseBranch ( _, _, e , index ) and
226- not result = getLoopBody ( e )
243+ not exists ( ConditionScope cs |
244+ e = cs .getParentNode ( ) and
245+ result = cs .getBody ( )
246+ ) and
247+ not result = getElseBranch ( _, _, e , index )
248+ or
249+ // not result = getElseBranch(_, _, e, index) and
250+ // not result = getLoopBody(e)
251+ // e = getImmediateChild(e.(ConditionScope).getParentNode(), _) and
252+ result = e .( ConditionScope ) .getBody ( ) and
253+ preOrd = 1 and
254+ index = 0 and
255+ postOrd = 0
227256 or
228257 result = getElseBranch ( e , index , _, _) and
229258 preOrd = 0 and
230259 postOrd = - 1
231- or
232- result = getLoopBody ( e ) and
233- index = 0 and
234- preOrd = 1 and
235- postOrd = 0
236- }
237-
260+ // or
261+ // result = getLoopBody(e) and
262+ // index = 0 and
263+ // preOrd = 1 and
264+ // postOrd = 0
265+ }
266+
267+ // private AstNode testgetImmediateChildAdj(AstNode e, int index) {
268+ // result = getImmediateChildAdj(e, index) and
269+ // e.getLocation().getStartLine() = 91 and
270+ // e.getLocation().getFile().getBaseName() = "main.rs"
271+ // }
238272 pragma [ nomagic]
239273 private Element getImmediateChildAdj ( Element e , int index ) {
240274 result =
241- rank [ index + 1 ] ( Element res , int i , int preOrd , int postOrd |
275+ rank [ index + 1 ] ( Element res , int preOrd , int i , int postOrd |
242276 res = getImmediateChildAdj ( e , preOrd , i , postOrd )
243277 |
244278 res order by preOrd , i , postOrd
@@ -253,6 +287,7 @@ module Impl {
253287 n instanceof VariableAccessCand or
254288 n instanceof LetStmt or
255289 n instanceof LetExpr or
290+ n = any ( LetExpr le ) .getScrutinee ( ) or
256291 n instanceof VariableScope
257292 ) and
258293 exists ( AstNode n0 |
@@ -269,6 +304,10 @@ module Impl {
269304 /** Gets the immediately enclosing variable scope of `n`. */
270305 private VariableScope getEnclosingScope ( AstNode n ) { result = getAnAncestorInVariableScope ( n ) }
271306
307+ // private VariableScope testgetEnclosingScope(AstNode n) {
308+ // result = getAnAncestorInVariableScope(n) and
309+ // n.getLocation().getStartLine() = 91
310+ // }
272311 /**
273312 * Get all the pattern ancestors of this variable up to an including the
274313 * root of the pattern.
@@ -299,6 +338,7 @@ module Impl {
299338 this instanceof VariableAccessCand or
300339 this instanceof LetStmt or
301340 this instanceof LetExpr or
341+ this = any ( LetExpr le ) .getScrutinee ( ) or
302342 getImmediateChildAdj ( this , _) instanceof RelevantElement
303343 }
304344
@@ -384,6 +424,10 @@ module Impl {
384424 )
385425 }
386426
427+ // private predicate testvariableDeclInScope(Variable v, VariableScope scope, string name, int ord) {
428+ // variableDeclInScope(v, scope, name, ord) and
429+ // v.getLocation().getStartLine() = 91
430+ // }
387431 /**
388432 * Holds if `v` is named `name` and is declared inside variable scope
389433 * `scope`. The pre-order numbering of the binding site of `v`, amongst
@@ -412,10 +456,10 @@ module Impl {
412456 or
413457 exists ( LetExpr let |
414458 let .getPat ( ) = pat and
415- scope = getEnclosingScope ( let ) and
459+ scope = getEnclosingScope ( let . getScrutinee ( ) ) and
416460 // for `let` expressions, variables are bound _after_ the statement, i.e.
417461 // not in the RHS
418- ord = getLastPreOrderNumbering ( scope , let ) + 1
462+ ord = getLastPreOrderNumbering ( scope , let . getScrutinee ( ) ) + 1
419463 )
420464 or
421465 exists ( ForExpr fe |
@@ -427,6 +471,12 @@ module Impl {
427471 )
428472 }
429473
474+ // private predicate testvariableAccessCandInScope(
475+ // VariableAccessCand cand, VariableScope scope, string name, int nestLevel, int ord
476+ // ) {
477+ // variableAccessCandInScope(cand, scope, name, nestLevel, ord) and
478+ // cand.getLocation().getStartLine() = 93
479+ // }
430480 /**
431481 * Holds if `cand` may access a variable named `name` at pre-order number `ord`
432482 * in the variable scope `scope`.
0 commit comments