Skip to content

Commit

Permalink
Rust: add tests for type extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
aibaars committed Nov 25, 2024
1 parent 6da8853 commit 0a0af0d
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
53 changes: 53 additions & 0 deletions rust/ql/test/extractor-tests/types/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
fn main() {
let who = "world";
println!("Hello {who}!")
}

fn empty_block() {}

fn single_empty_block() {
{}
}

macro_rules! block {
(Empty) => {};
(Stmt) => {
main();
};
(Nested) => {
block!(Stmt)
};
}

fn macro_stmts() {
block!(Empty);
block!(Stmt);
block!(Nested);
}
fn format_template_variables() {
let width = 4;
let precision = 2;
let value = 10;
println!("Value {value:#width$.precision$}");
}

fn chained_if(x: i32) {
if x < 5 {
println!("small");
} else if x > 10 {
println!("large");
} else {
println!("medium");
}
}

fn literal_in_pat(pair: (bool, bool)) -> bool {
match pair {
(x, true) => x,
_ => false,
}
}

fn box_new() {
vec![1];
}
97 changes: 97 additions & 0 deletions rust/ql/test/extractor-tests/types/types.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
exprTypes
| main.rs:2:15:2:21 | "world" | &str |
| main.rs:3:5:3:28 | ...::_print | fn _print(Arguments<'_>) |
| main.rs:3:5:3:28 | MacroExpr | () |
| main.rs:3:14:3:27 | ...::_print(...) | () |
| main.rs:3:14:3:27 | FormatArgsExpr | Arguments<'_> |
| main.rs:3:14:3:27 | MacroExpr | Arguments<'_> |
| main.rs:3:14:3:27 | { ... } | () |
| main.rs:9:5:9:6 | { ... } | () |
| main.rs:24:5:24:16 | main | fn main() |
| main.rs:24:5:24:16 | main(...) | () |
| main.rs:25:5:25:18 | main | fn main() |
| main.rs:25:5:25:18 | main(...) | () |
| main.rs:28:17:28:17 | 4 | usize |
| main.rs:29:21:29:21 | 2 | usize |
| main.rs:30:17:30:18 | 10 | i32 |
| main.rs:31:5:31:48 | ...::_print | fn _print(Arguments<'_>) |
| main.rs:31:5:31:48 | MacroExpr | () |
| main.rs:31:14:31:47 | ...::_print(...) | () |
| main.rs:31:14:31:47 | FormatArgsExpr | Arguments<'_> |
| main.rs:31:14:31:47 | MacroExpr | Arguments<'_> |
| main.rs:31:14:31:47 | { ... } | () |
| main.rs:35:5:41:5 | if ... {...} else {...} | () |
| main.rs:35:8:35:8 | x | i32 |
| main.rs:35:8:35:12 | ... < ... | bool |
| main.rs:35:12:35:12 | 5 | i32 |
| main.rs:36:9:36:25 | ...::_print | fn _print(Arguments<'_>) |
| main.rs:36:9:36:25 | MacroExpr | () |
| main.rs:36:18:36:24 | ...::_print(...) | () |
| main.rs:36:18:36:24 | FormatArgsExpr | Arguments<'_> |
| main.rs:36:18:36:24 | MacroExpr | Arguments<'_> |
| main.rs:36:18:36:24 | { ... } | () |
| main.rs:37:15:37:15 | x | i32 |
| main.rs:37:15:37:20 | ... > ... | bool |
| main.rs:37:19:37:20 | 10 | i32 |
| main.rs:38:9:38:25 | ...::_print | fn _print(Arguments<'_>) |
| main.rs:38:9:38:25 | MacroExpr | () |
| main.rs:38:18:38:24 | ...::_print(...) | () |
| main.rs:38:18:38:24 | FormatArgsExpr | Arguments<'_> |
| main.rs:38:18:38:24 | MacroExpr | Arguments<'_> |
| main.rs:38:18:38:24 | { ... } | () |
| main.rs:40:9:40:26 | ...::_print | fn _print(Arguments<'_>) |
| main.rs:40:9:40:26 | MacroExpr | () |
| main.rs:40:18:40:25 | ...::_print(...) | () |
| main.rs:40:18:40:25 | FormatArgsExpr | Arguments<'_> |
| main.rs:40:18:40:25 | MacroExpr | Arguments<'_> |
| main.rs:40:18:40:25 | { ... } | () |
| main.rs:45:5:48:5 | match pair { ... } | bool |
| main.rs:45:11:45:14 | pair | (bool, bool) |
| main.rs:46:22:46:22 | x | bool |
| main.rs:47:14:47:18 | false | bool |
| main.rs:52:5:52:11 | ...::into_vec | fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global> |
| main.rs:52:5:52:11 | MacroExpr | Vec<i32, Global> |
| main.rs:52:10:52:10 | 1 | i32 |
| main.rs:52:10:52:10 | ...::into_vec(...) | Vec<i32, Global> |
| main.rs:52:10:52:10 | ...::new(...) | Box<[i32; 1], Global> |
| main.rs:52:10:52:10 | [...] | [i32; 1] |
patTypes
| main.rs:2:9:2:11 | who | &str |
| main.rs:28:9:28:13 | width | usize |
| main.rs:29:9:29:17 | precision | usize |
| main.rs:30:9:30:13 | value | i32 |
| main.rs:34:15:34:15 | x | i32 |
| main.rs:44:19:44:22 | pair | (bool, bool) |
| main.rs:46:9:46:17 | TuplePat | (bool, bool) |
| main.rs:46:10:46:10 | x | bool |
| main.rs:46:13:46:16 | true | bool |
| main.rs:47:9:47:9 | _ | (bool, bool) |
missingExprType
| main.rs:1:11:4:1 | { ... } |
| main.rs:3:14:3:27 | "Hello {who}!\\n" |
| main.rs:3:22:3:24 | who |
| main.rs:6:18:6:19 | { ... } |
| main.rs:8:25:10:1 | { ... } |
| main.rs:22:18:26:1 | { ... } |
| main.rs:23:5:23:17 | MacroExpr |
| main.rs:24:5:24:16 | MacroExpr |
| main.rs:25:5:25:18 | MacroExpr |
| main.rs:25:5:25:18 | MacroExpr |
| main.rs:27:32:32:1 | { ... } |
| main.rs:31:14:31:47 | "Value {value:#width$.precisio... |
| main.rs:31:22:31:26 | value |
| main.rs:31:29:31:33 | width |
| main.rs:31:36:31:44 | precision |
| main.rs:34:23:42:1 | { ... } |
| main.rs:35:14:37:5 | { ... } |
| main.rs:36:18:36:24 | "small\\n" |
| main.rs:37:12:41:5 | if ... {...} else {...} |
| main.rs:37:22:39:5 | { ... } |
| main.rs:38:18:38:24 | "large\\n" |
| main.rs:39:12:41:5 | { ... } |
| main.rs:40:18:40:25 | "medium\\n" |
| main.rs:44:47:49:1 | { ... } |
| main.rs:46:13:46:16 | true |
| main.rs:51:14:53:1 | { ... } |
| main.rs:52:5:52:11 | ...::new |
missingPatType
9 changes: 9 additions & 0 deletions rust/ql/test/extractor-tests/types/types.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import rust

query predicate exprTypes(Expr e, string type) { type = e.getType() }

query predicate patTypes(Pat e, string type) { type = e.getType() }

query predicate missingExprType(Expr e) { not exists(e.getType()) }

query predicate missingPatType(Pat e) { not exists(e.getType()) }

0 comments on commit 0a0af0d

Please sign in to comment.