Skip to content

Commit 2b2bd17

Browse files
committed
Rust: Add more SSA tests
1 parent 785e027 commit 2b2bd17

File tree

6 files changed

+2932
-2796
lines changed

6 files changed

+2932
-2796
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
multipleCallTargets
2-
| main.rs:85:19:85:40 | ...::from(...) |
3-
| main.rs:102:19:102:40 | ...::from(...) |
2+
| main.rs:87:19:87:40 | ...::from(...) |
3+
| main.rs:106:19:106:40 | ...::from(...) |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
uselessPhiNode
2+
| main.rs:629:17:631:9 | SSA phi(x) | 1 |
3+
phiWithoutTwoPriorRefs
4+
| main.rs:629:17:631:9 | SSA phi(x) | main.rs:629:17:631:9 | if b {...} | main.rs:626:13:626:13 | x | 1 |

rust/ql/test/library-tests/variables/Cfg.expected

Lines changed: 1625 additions & 1568 deletions
Large diffs are not rendered by default.

rust/ql/test/library-tests/variables/Ssa.expected

Lines changed: 653 additions & 632 deletions
Large diffs are not rendered by default.

rust/ql/test/library-tests/variables/main.rs

Lines changed: 96 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::ops::AddAssign;
22

3-
fn print_str(s: &str) { // s
3+
fn print_str(s: &str) // s
4+
{
45
println!("{}", s); // $ read_access=s
56
}
67

7-
fn print_i64(i: i64) { // i
8+
fn print_i64(i: i64) // i
9+
{
810
println!("{}", i); // $ read_access=i
911
}
1012

@@ -91,10 +93,12 @@ fn let_pattern3() {
9193
}
9294

9395
fn let_pattern4() {
94-
let Some(x5): Option<&str> = Some("x5") // x5
95-
else {
96-
todo!()
97-
};
96+
let Some(x5): Option<&str> // x5
97+
= Some("x5")
98+
99+
else {
100+
todo!()
101+
};
98102
print_str(x5); // $ read_access=x5
99103
}
100104

@@ -127,23 +131,27 @@ fn match_pattern1() {
127131
fn match_pattern2() {
128132
let numbers = (2, 4, 8, 16, 32); // numbers
129133

130-
match numbers { // $ read_access=numbers
134+
match numbers // $ read_access=numbers
135+
{
131136
(
132-
first, _, // first
133-
third, _, // third
134-
fifth // fifth
137+
first, // first
138+
_,
139+
third, // third
140+
_,
141+
fifth, // fifth
135142
) => {
136143
print_i64(first); // $ read_access=first
137144
print_i64(third); // $ read_access=third
138145
print_i64(fifth); // $ read_access=fifth
139146
}
140147
}
141148

142-
match numbers { // $ read_access=numbers
149+
match numbers // $ read_access=numbers
150+
{
143151
(
144152
first, // first
145153
..,
146-
last // last
154+
last, // last
147155
) => {
148156
print_i64(first); // $ read_access=first
149157
print_i64(last); // $ read_access=last
@@ -168,15 +176,19 @@ enum Message {
168176
fn match_pattern4() {
169177
let msg = Message::Hello { id: 0 }; // msg
170178

171-
match msg { // $ read_access=msg
179+
match msg // $ read_access=msg
180+
{
172181
Message::Hello {
173182
id: id_variable @ 3..=7, // id_variable
174183
} => print_i64(id_variable), // $ read_access=id_variable
175184
Message::Hello { id: 10..=12 } => {
176185
println!("Found an id in another range")
177186
}
178-
Message::Hello { id } => // id
179-
print_i64(id), // $ read_access=id
187+
Message::Hello { id } // id
188+
=>
189+
{
190+
print_i64(id) // $ read_access=id
191+
}
180192
}
181193
}
182194

@@ -262,36 +274,36 @@ fn param_pattern1(
262274
(
263275
b3, // b3
264276
c1, // c1
265-
): (&str, &str)) -> () {
277+
): (&str, &str),
278+
) -> () {
266279
print_str(a8); // $ read_access=a8
267280
print_str(b3); // $ read_access=b3
268281
print_str(c1); // $ read_access=c1
269282
}
270283

271-
fn param_pattern2(
272-
(Either::Left(a9) | Either::Right(a9)): Either // a9
284+
fn param_pattern2((Either::Left(a9) | Either::Right(a9)): Either, // a9
273285
) -> () {
274286
print_i64(a9); // $ read_access=a9
275287
}
276288

277289
fn destruct_assignment() {
278290
let (
279291
mut a10, // a10
280-
mut b4, // b4
281-
mut c2 // c2
292+
mut b4, // b4
293+
mut c2, // c2
282294
) = (1, 2, 3);
283295
print_i64(a10); // $ read_access=a10
284296
print_i64(b4); // $ read_access=b4
285297
print_i64(c2); // $ read_access=c2
286298

287299
(
288-
c2, // $ write_access=c2
289-
b4, // $ write_access=b4
290-
a10 // $ write_access=a10
300+
c2, // $ write_access=c2
301+
b4, // $ write_access=b4
302+
a10, // $ write_access=a10
291303
) = (
292304
a10, // $ read_access=a10
293-
b4, // $ read_access=b4
294-
c2 // $ read_access=c2
305+
b4, // $ read_access=b4
306+
c2, // $ read_access=c2
295307
);
296308
print_i64(a10); // $ read_access=a10
297309
print_i64(b4); // $ read_access=b4
@@ -300,7 +312,7 @@ fn destruct_assignment() {
300312
match (4, 5) {
301313
(
302314
a10, // a10_2
303-
b4 // b4
315+
b4, // b4
304316
) => {
305317
print_i64(a10); // $ read_access=a10_2
306318
print_i64(b4); // $ read_access=b4
@@ -320,8 +332,8 @@ fn closure_variable() {
320332
print_i64(n1); // $ read_access=n1
321333

322334
immutable_variable();
323-
let immutable_variable =
324-
|x: i64| // x_2
335+
let immutable_variable = // immutable_variable
336+
|x: i64| // x_2
325337
x; // $ read_access=x_2
326338
let n2 = // n2
327339
immutable_variable(6); // $ read_access=immutable_variable
@@ -335,15 +347,17 @@ fn nested_function() {
335347
x; // $ read_access=x_1
336348
print_i64(f(1)); // $ read_access=f1
337349

338-
fn f(x: i64) -> i64 { // x_2
350+
fn f(x: i64) -> i64 // x_2
351+
{
339352
x + 1 // $ read_access=x_2
340353
}
341354

342355
print_i64(f(2)); // $ read_access=f1
343356

344357
{
345358
print_i64(f(3));
346-
fn f(x: i64) -> i64 { // x_3
359+
fn f(x: i64) -> i64 // x_3
360+
{
347361
2 * x // $ read_access=x_3
348362
}
349363

@@ -383,14 +397,14 @@ fn mutate() {
383397
print_i64(i); // $ read_access=i
384398
}
385399

386-
fn mutate_param(x : &mut i64) -> &mut i64 {
400+
fn mutate_param(x: &mut i64) -> &mut i64 {
387401
*x = // $ read_access=x
388402
*x + // $ read_access=x
389403
*x; // $ read_access=x
390404
return x; // $ read_access=x
391405
}
392406

393-
fn mutate_param2<'a>(x : &'a mut i64, y :&mut &'a mut i64) {
407+
fn mutate_param2<'a>(x: &'a mut i64, y: &mut &'a mut i64) {
394408
*x = // $ read_access=x
395409
*x + // $ read_access=x
396410
*x; // $ read_access=x
@@ -411,7 +425,7 @@ fn mutate_arg() {
411425
&mut &mut x; // $ access=x
412426
mutate_param2(
413427
&mut z, // $ access=z
414-
w // $ read_access=w
428+
w, // $ read_access=w
415429
);
416430
**w = 11; // $ read_access=w
417431
// prints 11, not 8
@@ -472,38 +486,43 @@ async fn async_block_capture() {
472486
print_i64(i); // $ read_access=i
473487
}
474488

475-
fn phi(b : bool) {
489+
fn phi(b: bool) {
476490
let mut x = 1; // x
477491
print_i64(x); // $ read_access=x
478492
print_i64(x + 1); // $ read_access=x
479-
if b { // $ read_access=b
493+
#[rustfmt::skip]
494+
let _ = if b // $ read_access=b
495+
{
480496
x = 2; // $ write_access=x
481497
print_i64(x); // $ read_access=x
482498
print_i64(x + 1); // $ read_access=x
483499
} else {
484500
x = 3; // $ write_access=x
485501
print_i64(x); // $ read_access=x
486502
print_i64(x + 1); // $ read_access=x
487-
}
503+
};
488504
print_i64(x); // $ read_access=x
489505
}
490506

491-
fn phi_read(b1 : bool, b2 : bool) {
507+
fn phi_read(b1: bool, b2: bool) {
492508
let x = 1; // x
493-
if b1 { // $ read_access=b1
509+
#[rustfmt::skip]
510+
let _ = if b1 // $ read_access=b1
511+
{
494512
print_i64(x); // $ read_access=x
495513
} else {
496514
print_i64(x); // $ read_access=x
497-
}
515+
};
498516

499-
if b2 { // $ read_access=b2
517+
#[rustfmt::skip]
518+
let _ = if b2 // $ read_access=b2
519+
{
500520
print_i64(x); // $ read_access=x
501521
} else {
502522
print_i64(x); // $ read_access=x
503-
}
523+
};
504524
}
505525

506-
507526
struct MyStruct {
508527
val: i64,
509528
}
@@ -555,38 +574,34 @@ fn ref_arg() {
555574
}
556575

557576
trait Bar {
558-
fn bar(&self);
577+
fn bar(&self);
559578
}
560579

561580
impl MyStruct {
562-
fn bar(&mut self) {
563-
*self = MyStruct { val: 3 }; // $ read_access=self
564-
}
581+
fn bar(&mut self) {
582+
*self = MyStruct { val: 3 }; // $ read_access=self
583+
}
565584
}
566585

567586
fn ref_methodcall_receiver() {
568-
let mut a = MyStruct { val: 1 }; // a
569-
a.bar(); // $ read_access=a
570-
// prints 3, not 1
571-
print_i64(a.val); // $ read_access=a
587+
let mut a = MyStruct { val: 1 }; // a
588+
a.bar(); // $ read_access=a
589+
// prints 3, not 1
590+
print_i64(a.val); // $ read_access=a
572591
}
573592

574593
macro_rules! let_in_macro {
575-
($e:expr) => {
576-
{
577-
let var_in_macro = $e;
578-
var_in_macro
579-
}
580-
};
594+
($e:expr) => {{
595+
let var_in_macro = $e;
596+
var_in_macro
597+
}};
581598
}
582599

583600
macro_rules! let_in_macro2 {
584-
($e:expr) => {
585-
{
586-
let var_in_macro = 0;
587-
$e
588-
}
589-
};
601+
($e:expr) => {{
602+
let var_in_macro = 0;
603+
$e
604+
}};
590605
}
591606

592607
fn macro_invocation() {
@@ -601,6 +616,24 @@ fn macro_invocation() {
601616
print_i64(var_in_macro); // $ read_access=var_in_macro1
602617
}
603618

619+
fn let_without_initializer() {
620+
let x; // x
621+
x = 1; // $ write_access=x
622+
print_i64(x); // $ read_access=x
623+
}
624+
625+
fn capture_phi() {
626+
let mut x = 100; // x
627+
let mut cap = |b: bool| {
628+
#[rustfmt::skip]
629+
let _ = if b { // $ read_access=b
630+
x = 200; // $ write_access=x
631+
};
632+
};
633+
cap(true); // $ read_access=cap
634+
print_i64(x); // $ read_access=x
635+
}
636+
604637
fn main() {
605638
immutable_variable();
606639
mutable_variable();
@@ -637,4 +670,5 @@ fn main() {
637670
ref_arg();
638671
ref_methodcall_receiver();
639672
macro_invocation();
673+
capture_phi();
640674
}

0 commit comments

Comments
 (0)