Skip to content

Commit 0a0af0d

Browse files
committed
Rust: add tests for type extraction
1 parent 6da8853 commit 0a0af0d

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
fn main() {
2+
let who = "world";
3+
println!("Hello {who}!")
4+
}
5+
6+
fn empty_block() {}
7+
8+
fn single_empty_block() {
9+
{}
10+
}
11+
12+
macro_rules! block {
13+
(Empty) => {};
14+
(Stmt) => {
15+
main();
16+
};
17+
(Nested) => {
18+
block!(Stmt)
19+
};
20+
}
21+
22+
fn macro_stmts() {
23+
block!(Empty);
24+
block!(Stmt);
25+
block!(Nested);
26+
}
27+
fn format_template_variables() {
28+
let width = 4;
29+
let precision = 2;
30+
let value = 10;
31+
println!("Value {value:#width$.precision$}");
32+
}
33+
34+
fn chained_if(x: i32) {
35+
if x < 5 {
36+
println!("small");
37+
} else if x > 10 {
38+
println!("large");
39+
} else {
40+
println!("medium");
41+
}
42+
}
43+
44+
fn literal_in_pat(pair: (bool, bool)) -> bool {
45+
match pair {
46+
(x, true) => x,
47+
_ => false,
48+
}
49+
}
50+
51+
fn box_new() {
52+
vec![1];
53+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
exprTypes
2+
| main.rs:2:15:2:21 | "world" | &str |
3+
| main.rs:3:5:3:28 | ...::_print | fn _print(Arguments<'_>) |
4+
| main.rs:3:5:3:28 | MacroExpr | () |
5+
| main.rs:3:14:3:27 | ...::_print(...) | () |
6+
| main.rs:3:14:3:27 | FormatArgsExpr | Arguments<'_> |
7+
| main.rs:3:14:3:27 | MacroExpr | Arguments<'_> |
8+
| main.rs:3:14:3:27 | { ... } | () |
9+
| main.rs:9:5:9:6 | { ... } | () |
10+
| main.rs:24:5:24:16 | main | fn main() |
11+
| main.rs:24:5:24:16 | main(...) | () |
12+
| main.rs:25:5:25:18 | main | fn main() |
13+
| main.rs:25:5:25:18 | main(...) | () |
14+
| main.rs:28:17:28:17 | 4 | usize |
15+
| main.rs:29:21:29:21 | 2 | usize |
16+
| main.rs:30:17:30:18 | 10 | i32 |
17+
| main.rs:31:5:31:48 | ...::_print | fn _print(Arguments<'_>) |
18+
| main.rs:31:5:31:48 | MacroExpr | () |
19+
| main.rs:31:14:31:47 | ...::_print(...) | () |
20+
| main.rs:31:14:31:47 | FormatArgsExpr | Arguments<'_> |
21+
| main.rs:31:14:31:47 | MacroExpr | Arguments<'_> |
22+
| main.rs:31:14:31:47 | { ... } | () |
23+
| main.rs:35:5:41:5 | if ... {...} else {...} | () |
24+
| main.rs:35:8:35:8 | x | i32 |
25+
| main.rs:35:8:35:12 | ... < ... | bool |
26+
| main.rs:35:12:35:12 | 5 | i32 |
27+
| main.rs:36:9:36:25 | ...::_print | fn _print(Arguments<'_>) |
28+
| main.rs:36:9:36:25 | MacroExpr | () |
29+
| main.rs:36:18:36:24 | ...::_print(...) | () |
30+
| main.rs:36:18:36:24 | FormatArgsExpr | Arguments<'_> |
31+
| main.rs:36:18:36:24 | MacroExpr | Arguments<'_> |
32+
| main.rs:36:18:36:24 | { ... } | () |
33+
| main.rs:37:15:37:15 | x | i32 |
34+
| main.rs:37:15:37:20 | ... > ... | bool |
35+
| main.rs:37:19:37:20 | 10 | i32 |
36+
| main.rs:38:9:38:25 | ...::_print | fn _print(Arguments<'_>) |
37+
| main.rs:38:9:38:25 | MacroExpr | () |
38+
| main.rs:38:18:38:24 | ...::_print(...) | () |
39+
| main.rs:38:18:38:24 | FormatArgsExpr | Arguments<'_> |
40+
| main.rs:38:18:38:24 | MacroExpr | Arguments<'_> |
41+
| main.rs:38:18:38:24 | { ... } | () |
42+
| main.rs:40:9:40:26 | ...::_print | fn _print(Arguments<'_>) |
43+
| main.rs:40:9:40:26 | MacroExpr | () |
44+
| main.rs:40:18:40:25 | ...::_print(...) | () |
45+
| main.rs:40:18:40:25 | FormatArgsExpr | Arguments<'_> |
46+
| main.rs:40:18:40:25 | MacroExpr | Arguments<'_> |
47+
| main.rs:40:18:40:25 | { ... } | () |
48+
| main.rs:45:5:48:5 | match pair { ... } | bool |
49+
| main.rs:45:11:45:14 | pair | (bool, bool) |
50+
| main.rs:46:22:46:22 | x | bool |
51+
| main.rs:47:14:47:18 | false | bool |
52+
| main.rs:52:5:52:11 | ...::into_vec | fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global> |
53+
| main.rs:52:5:52:11 | MacroExpr | Vec<i32, Global> |
54+
| main.rs:52:10:52:10 | 1 | i32 |
55+
| main.rs:52:10:52:10 | ...::into_vec(...) | Vec<i32, Global> |
56+
| main.rs:52:10:52:10 | ...::new(...) | Box<[i32; 1], Global> |
57+
| main.rs:52:10:52:10 | [...] | [i32; 1] |
58+
patTypes
59+
| main.rs:2:9:2:11 | who | &str |
60+
| main.rs:28:9:28:13 | width | usize |
61+
| main.rs:29:9:29:17 | precision | usize |
62+
| main.rs:30:9:30:13 | value | i32 |
63+
| main.rs:34:15:34:15 | x | i32 |
64+
| main.rs:44:19:44:22 | pair | (bool, bool) |
65+
| main.rs:46:9:46:17 | TuplePat | (bool, bool) |
66+
| main.rs:46:10:46:10 | x | bool |
67+
| main.rs:46:13:46:16 | true | bool |
68+
| main.rs:47:9:47:9 | _ | (bool, bool) |
69+
missingExprType
70+
| main.rs:1:11:4:1 | { ... } |
71+
| main.rs:3:14:3:27 | "Hello {who}!\\n" |
72+
| main.rs:3:22:3:24 | who |
73+
| main.rs:6:18:6:19 | { ... } |
74+
| main.rs:8:25:10:1 | { ... } |
75+
| main.rs:22:18:26:1 | { ... } |
76+
| main.rs:23:5:23:17 | MacroExpr |
77+
| main.rs:24:5:24:16 | MacroExpr |
78+
| main.rs:25:5:25:18 | MacroExpr |
79+
| main.rs:25:5:25:18 | MacroExpr |
80+
| main.rs:27:32:32:1 | { ... } |
81+
| main.rs:31:14:31:47 | "Value {value:#width$.precisio... |
82+
| main.rs:31:22:31:26 | value |
83+
| main.rs:31:29:31:33 | width |
84+
| main.rs:31:36:31:44 | precision |
85+
| main.rs:34:23:42:1 | { ... } |
86+
| main.rs:35:14:37:5 | { ... } |
87+
| main.rs:36:18:36:24 | "small\\n" |
88+
| main.rs:37:12:41:5 | if ... {...} else {...} |
89+
| main.rs:37:22:39:5 | { ... } |
90+
| main.rs:38:18:38:24 | "large\\n" |
91+
| main.rs:39:12:41:5 | { ... } |
92+
| main.rs:40:18:40:25 | "medium\\n" |
93+
| main.rs:44:47:49:1 | { ... } |
94+
| main.rs:46:13:46:16 | true |
95+
| main.rs:51:14:53:1 | { ... } |
96+
| main.rs:52:5:52:11 | ...::new |
97+
missingPatType
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import rust
2+
3+
query predicate exprTypes(Expr e, string type) { type = e.getType() }
4+
5+
query predicate patTypes(Pat e, string type) { type = e.getType() }
6+
7+
query predicate missingExprType(Expr e) { not exists(e.getType()) }
8+
9+
query predicate missingPatType(Pat e) { not exists(e.getType()) }

0 commit comments

Comments
 (0)