Skip to content

Commit df19281

Browse files
committed
Rust: Introduce AstNode.getEnclosingCallable
1 parent 086e0c6 commit df19281

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private module CfgInput implements InputSig<Location> {
2121
/** An AST node with an associated control-flow graph. */
2222
class CfgScope = Scope::CfgScope;
2323

24-
CfgScope getCfgScope(AstNode n) { result = Scope::scopeOfAst(n) }
24+
CfgScope getCfgScope(AstNode n) { result = n.getEnclosingCallable() }
2525

2626
class SuccessorType = Cfg::SuccessorType;
2727

rust/ql/lib/codeql/rust/controlflow/internal/Scope.qll

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,3 @@ class CfgScope extends Callable {
2424
result = this.(ClosureExpr).getBody()
2525
}
2626
}
27-
28-
/**
29-
* Gets the immediate parent of a non-`AstNode` element `e`.
30-
*
31-
* We restrict `e` to be a non-`AstNode` to skip past non-`AstNode` in
32-
* the transitive closure computation in `getParentOfAst`. This is
33-
* necessary because the parent of an `AstNode` is not necessarily an `AstNode`.
34-
*/
35-
private Element getParentOfAstStep(Element e) {
36-
not e instanceof AstNode and
37-
result = getImmediateParent(e)
38-
}
39-
40-
/** Gets the nearest enclosing parent of `ast` that is an `AstNode`. */
41-
private AstNode getParentOfAst(AstNode ast) {
42-
result = getParentOfAstStep*(getImmediateParent(ast))
43-
}
44-
45-
/** Gets the enclosing scope of a node */
46-
cached
47-
AstNode scopeOfAst(AstNode n) {
48-
exists(AstNode p | p = getParentOfAst(n) |
49-
if p instanceof CfgScope then p = result else result = scopeOfAst(p)
50-
)
51-
}

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

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,6 @@ module Impl {
421421
)
422422
}
423423

424-
private import codeql.rust.controlflow.internal.Scope
425-
426424
/** A variable access. */
427425
class VariableAccess extends PathExprImpl::PathExpr instanceof VariableAccessCand {
428426
private string name;
@@ -434,7 +432,7 @@ module Impl {
434432
Variable getVariable() { result = v }
435433

436434
/** Holds if this access is a capture. */
437-
predicate isCapture() { scopeOfAst(this) != scopeOfAst(v.getPat()) }
435+
predicate isCapture() { this.getEnclosingCallable() != v.getPat().getEnclosingCallable() }
438436

439437
override string toString() { result = name }
440438

0 commit comments

Comments
 (0)