-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Improve StmtList #20512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rust: Improve StmtList #20512
Changes from all commits
ed2a14a
ed3a6fd
ab30c78
7459eed
85945db
8debce0
b12b36f
9bdac9d
93a0198
7e8e855
27b6f12
4570d7e
1236e2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// generated by codegen, remove this comment if you wish to edit this file | ||
/** | ||
* This module provides a hand-modifiable wrapper around the generated class `StmtList`. | ||
* | ||
|
@@ -12,17 +11,48 @@ private import codeql.rust.elements.internal.generated.StmtList | |
* be referenced directly. | ||
*/ | ||
module Impl { | ||
// the following QLdoc is generated: if you need to edit it, do it in the schema file | ||
/** | ||
* A list of statements in a block. | ||
* A list of statements in a block, with an optional tail expression at the | ||
* end that determines the block's value. | ||
* | ||
* For example: | ||
* ```rust | ||
* { | ||
* let x = 1; | ||
* let y = 2; | ||
* x + y | ||
* } | ||
* // ^^^^^^^^^ | ||
* ``` | ||
*/ | ||
class StmtList extends Generated::StmtList { } | ||
class StmtList extends Generated::StmtList { | ||
/** | ||
* Gets the `index`th statement or expression of this statement list (0-based). | ||
* | ||
* This includes both the statements and any tail expression in the statement list. To access | ||
* just the statements, use `getStatement`. To access just the tail expression, if any, | ||
* use `getTailExpr`. | ||
*/ | ||
AstNode getStmtOrExpr(int index) { | ||
result = this.getStatement(index) | ||
or | ||
index = this.getNumberOfStatements() and | ||
result = this.getTailExpr() | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the above comment I'm not entirely convinced that we want to expose this predicate. But, if we do, then there is in fact already an implementation here. Given that I think we should:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's interesting, I will make some changes... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done the first two. I could be persuaded to delete the test, but I'm not convinced its redundant - it seems to me that each is testing slightly different areas of code in slightly different ways (there is a lot of overlap). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! Let's keep the tests. |
||
|
||
/** | ||
* Gets any of the statements or expressions of this statement list. | ||
* | ||
* This includes both the statements and any tail expression in the statement list. To access | ||
* just the statements, use `getAStatement`. To access just the tail expression, if any, | ||
* use `getTailExpr`. | ||
*/ | ||
final AstNode getAStmtOrExpr() { result = this.getStmtOrExpr(_) } | ||
|
||
/** | ||
* Gets the number of statements or expressions of this statement list. | ||
*/ | ||
final int getNumberOfStmtOrExpr() { result = count(int i | exists(this.getStmtOrExpr(i))) } | ||
paldepind marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
instances | ||
| gen_stmt_list.rs:3:27:12:1 | StmtList | | ||
| gen_stmt_list.rs:7:5:10:5 | StmtList | | ||
| gen_stmt_list.rs:3:27:14:1 | StmtList | | ||
| gen_stmt_list.rs:8:5:12:5 | StmtList | | ||
getAttr | ||
getStatement | ||
| gen_stmt_list.rs:7:5:10:5 | StmtList | 0 | gen_stmt_list.rs:8:9:8:18 | let ... = 1 | | ||
| gen_stmt_list.rs:7:5:10:5 | StmtList | 1 | gen_stmt_list.rs:9:9:9:18 | let ... = 2 | | ||
| gen_stmt_list.rs:8:5:12:5 | StmtList | 0 | gen_stmt_list.rs:9:9:9:18 | let ... = 1 | | ||
| gen_stmt_list.rs:8:5:12:5 | StmtList | 1 | gen_stmt_list.rs:10:9:10:18 | let ... = 2 | | ||
getTailExpr | ||
| gen_stmt_list.rs:3:27:12:1 | StmtList | gen_stmt_list.rs:7:5:10:5 | { ... } | | ||
| gen_stmt_list.rs:3:27:14:1 | StmtList | gen_stmt_list.rs:8:5:12:5 | { ... } | | ||
| gen_stmt_list.rs:8:5:12:5 | StmtList | gen_stmt_list.rs:11:9:11:13 | ... + ... | |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
| StmtList.rs:4:19:9:1 | StmtList | 2 | hasTailExpr | 0:let ... = 1, 1:let ... = 2, 2:... + ... | | ||
| StmtList.rs:11:18:15:1 | StmtList | 2 | | 0:let ... = 1, 1:let ... = 2 | | ||
| StmtList.rs:17:19:20:1 | StmtList | 0 | hasTailExpr | 0:... + ... | | ||
| StmtList.rs:22:18:25:1 | StmtList | 1 | | 0:ExprStmt | | ||
| StmtList.rs:27:18:29:1 | StmtList | 0 | | | | ||
| StmtList.rs:31:18:34:1 | StmtList | 0 | | | | ||
| StmtList.rs:36:29:43:1 | StmtList | 0 | hasTailExpr | 0:if cond {...} else {...} | | ||
| StmtList.rs:38:10:40:2 | StmtList | 0 | hasTailExpr | 0:1 | | ||
| StmtList.rs:40:9:42:2 | StmtList | 0 | hasTailExpr | 0:2 | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import rust | ||
import TestUtils | ||
|
||
from StmtList sl, string tail | ||
where | ||
toBeTested(sl) and | ||
if sl.hasTailExpr() then tail = "hasTailExpr" else tail = "" | ||
select sl, sl.getNumberOfStatements(), tail, | ||
concat(int i, AstNode n | | ||
n = sl.getStmtOrExpr(i) | ||
| | ||
i.toString() + ":" + n.toString(), ", " order by i | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
// --- tests --- | ||
|
||
fn test1() -> i32 { | ||
// two statements + tail expression | ||
let a = 1; | ||
let b = 2; | ||
a + b | ||
} | ||
|
||
fn test2() -> () { | ||
// two statements only | ||
let a = 1; | ||
let b = 2; | ||
} | ||
|
||
fn test3() -> i32 { | ||
// tail expression only | ||
1 + 2 | ||
} | ||
|
||
fn test4() -> () { | ||
// one statement only | ||
1 + 2; | ||
} | ||
|
||
fn test5() -> () { | ||
// neither | ||
} | ||
|
||
fn test6() -> () { | ||
// neither | ||
; | ||
} | ||
|
||
fn test7(cond: bool) -> i32 { | ||
// nested blocks | ||
if cond { | ||
1 | ||
} else { | ||
2 | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.