Skip to content

Commit a172063

Browse files
committed
Rust: Document VariableOrAccessCand
1 parent b0efffd commit a172063

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,53 @@ module Impl {
231231
TVariableOrAccessCandVariable(Variable v) or
232232
TVariableOrAccessCandVariableAccessCand(VariableAccessCand va)
233233

234+
/**
235+
* A variable declaration or variable access candidate.
236+
*
237+
* In order to determine whether a candidate is an actual variable access,
238+
* we rank declarations and candidates by their position in source code.
239+
*
240+
* The ranking must take variable names into account, but also variable scopes;
241+
* below a comment `rank(scope, name, i)` means that the declaration/access on
242+
* the given line has rank `i` amongst all declarations/accesses inside variable
243+
* scope `scope`, for variable name `name`:
244+
*
245+
* ```rust
246+
* fn f() { // scope0
247+
* let x = 0; // rank(scope0, "x", 0)
248+
* use(x); // rank(scope0, "x", 1)
249+
* let x = // rank(scope0, "x", 3)
250+
* x + 1; // rank(scope0, "x", 2)
251+
* let y = // rank(scope0, "y", 0)
252+
* x; // rank(scope0, "x", 4)
253+
*
254+
* { // scope1
255+
* use(x); // rank(scope1, "x", 0), rank(scope0, "x", 4)
256+
* use(y); // rank(scope1, "y", 0), rank(scope0, "y", 1)
257+
* let x = 2; // rank(scope1, "x", 1)
258+
* use(x); // rank(scope1, "x", 2), rank(scope0, "x", 4)
259+
* }
260+
* }
261+
* ```
262+
*
263+
* Variable declarations are only ranked in the scope that they bind into, while
264+
* accesses candidates propagate outwards through scopes, as they may access
265+
* declarations from outer scopes.
266+
*
267+
* For an access candidate with ranks `{ rank(scope_i, name, rnk_i) | i in I }` and
268+
* declarations `d in D` with ranks `rnk(scope_d, name, rnk_d)`, the target is
269+
* calculated as
270+
* ```
271+
* max_{i in I} (
272+
* max_{d in D | scope_d = scope_i and rnk_d < rnk_i} (
273+
* d
274+
* )
275+
* )
276+
* ```
277+
*
278+
* i.e., its the nearest declaration before the access in the same (or outer) scope
279+
* as the access.
280+
*/
234281
private class VariableOrAccessCand extends TVariableOrAccessCand {
235282
Variable asVariable() { this = TVariableOrAccessCandVariable(result) }
236283

rust/ql/test/utils/internal/InlineExpectationsTestImpl.qll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ private import R
33
private import codeql.util.test.InlineExpectationsTest
44

55
module Impl implements InlineExpectationsTestSig {
6-
/**
7-
* A class representing line comments in C# used by the InlineExpectations core code
8-
*/
96
class ExpectationComment extends R::Comment {
107
/** Gets the contents of the given comment, _without_ the preceding comment marker (`//`). */
118
string getContents() { result = this.getCommentText() }

0 commit comments

Comments
 (0)