Rust: Add more SSA tests

This commit is contained in:
Tom Hvitved
2025-06-30 15:26:30 +02:00
parent 785e0273f2
commit 2b2bd17d10
6 changed files with 2932 additions and 2796 deletions

View File

@@ -1,3 +1,3 @@
multipleCallTargets
| main.rs:85:19:85:40 | ...::from(...) |
| main.rs:102:19:102:40 | ...::from(...) |
| main.rs:87:19:87:40 | ...::from(...) |
| main.rs:106:19:106:40 | ...::from(...) |

View File

@@ -0,0 +1,4 @@
uselessPhiNode
| main.rs:629:17:631:9 | SSA phi(x) | 1 |
phiWithoutTwoPriorRefs
| 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 |

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,12 @@
use std::ops::AddAssign;
fn print_str(s: &str) { // s
fn print_str(s: &str) // s
{
println!("{}", s); // $ read_access=s
}
fn print_i64(i: i64) { // i
fn print_i64(i: i64) // i
{
println!("{}", i); // $ read_access=i
}
@@ -91,10 +93,12 @@ fn let_pattern3() {
}
fn let_pattern4() {
let Some(x5): Option<&str> = Some("x5") // x5
else {
todo!()
};
let Some(x5): Option<&str> // x5
= Some("x5")
else {
todo!()
};
print_str(x5); // $ read_access=x5
}
@@ -127,11 +131,14 @@ fn match_pattern1() {
fn match_pattern2() {
let numbers = (2, 4, 8, 16, 32); // numbers
match numbers { // $ read_access=numbers
match numbers // $ read_access=numbers
{
(
first, _, // first
third, _, // third
fifth // fifth
first, // first
_,
third, // third
_,
fifth, // fifth
) => {
print_i64(first); // $ read_access=first
print_i64(third); // $ read_access=third
@@ -139,11 +146,12 @@ fn match_pattern2() {
}
}
match numbers { // $ read_access=numbers
match numbers // $ read_access=numbers
{
(
first, // first
..,
last // last
last, // last
) => {
print_i64(first); // $ read_access=first
print_i64(last); // $ read_access=last
@@ -168,15 +176,19 @@ enum Message {
fn match_pattern4() {
let msg = Message::Hello { id: 0 }; // msg
match msg { // $ read_access=msg
match msg // $ read_access=msg
{
Message::Hello {
id: id_variable @ 3..=7, // id_variable
} => print_i64(id_variable), // $ read_access=id_variable
Message::Hello { id: 10..=12 } => {
println!("Found an id in another range")
}
Message::Hello { id } => // id
print_i64(id), // $ read_access=id
Message::Hello { id } // id
=>
{
print_i64(id) // $ read_access=id
}
}
}
@@ -262,14 +274,14 @@ fn param_pattern1(
(
b3, // b3
c1, // c1
): (&str, &str)) -> () {
): (&str, &str),
) -> () {
print_str(a8); // $ read_access=a8
print_str(b3); // $ read_access=b3
print_str(c1); // $ read_access=c1
}
fn param_pattern2(
(Either::Left(a9) | Either::Right(a9)): Either // a9
fn param_pattern2((Either::Left(a9) | Either::Right(a9)): Either, // a9
) -> () {
print_i64(a9); // $ read_access=a9
}
@@ -277,21 +289,21 @@ fn param_pattern2(
fn destruct_assignment() {
let (
mut a10, // a10
mut b4, // b4
mut c2 // c2
mut b4, // b4
mut c2, // c2
) = (1, 2, 3);
print_i64(a10); // $ read_access=a10
print_i64(b4); // $ read_access=b4
print_i64(c2); // $ read_access=c2
(
c2, // $ write_access=c2
b4, // $ write_access=b4
a10 // $ write_access=a10
c2, // $ write_access=c2
b4, // $ write_access=b4
a10, // $ write_access=a10
) = (
a10, // $ read_access=a10
b4, // $ read_access=b4
c2 // $ read_access=c2
b4, // $ read_access=b4
c2, // $ read_access=c2
);
print_i64(a10); // $ read_access=a10
print_i64(b4); // $ read_access=b4
@@ -300,7 +312,7 @@ fn destruct_assignment() {
match (4, 5) {
(
a10, // a10_2
b4 // b4
b4, // b4
) => {
print_i64(a10); // $ read_access=a10_2
print_i64(b4); // $ read_access=b4
@@ -320,8 +332,8 @@ fn closure_variable() {
print_i64(n1); // $ read_access=n1
immutable_variable();
let immutable_variable =
|x: i64| // x_2
let immutable_variable = // immutable_variable
|x: i64| // x_2
x; // $ read_access=x_2
let n2 = // n2
immutable_variable(6); // $ read_access=immutable_variable
@@ -335,7 +347,8 @@ fn nested_function() {
x; // $ read_access=x_1
print_i64(f(1)); // $ read_access=f1
fn f(x: i64) -> i64 { // x_2
fn f(x: i64) -> i64 // x_2
{
x + 1 // $ read_access=x_2
}
@@ -343,7 +356,8 @@ fn nested_function() {
{
print_i64(f(3));
fn f(x: i64) -> i64 { // x_3
fn f(x: i64) -> i64 // x_3
{
2 * x // $ read_access=x_3
}
@@ -383,14 +397,14 @@ fn mutate() {
print_i64(i); // $ read_access=i
}
fn mutate_param(x : &mut i64) -> &mut i64 {
fn mutate_param(x: &mut i64) -> &mut i64 {
*x = // $ read_access=x
*x + // $ read_access=x
*x; // $ read_access=x
return x; // $ read_access=x
}
fn mutate_param2<'a>(x : &'a mut i64, y :&mut &'a mut i64) {
fn mutate_param2<'a>(x: &'a mut i64, y: &mut &'a mut i64) {
*x = // $ read_access=x
*x + // $ read_access=x
*x; // $ read_access=x
@@ -411,7 +425,7 @@ fn mutate_arg() {
&mut &mut x; // $ access=x
mutate_param2(
&mut z, // $ access=z
w // $ read_access=w
w, // $ read_access=w
);
**w = 11; // $ read_access=w
// prints 11, not 8
@@ -472,11 +486,13 @@ async fn async_block_capture() {
print_i64(i); // $ read_access=i
}
fn phi(b : bool) {
fn phi(b: bool) {
let mut x = 1; // x
print_i64(x); // $ read_access=x
print_i64(x + 1); // $ read_access=x
if b { // $ read_access=b
#[rustfmt::skip]
let _ = if b // $ read_access=b
{
x = 2; // $ write_access=x
print_i64(x); // $ read_access=x
print_i64(x + 1); // $ read_access=x
@@ -484,26 +500,29 @@ fn phi(b : bool) {
x = 3; // $ write_access=x
print_i64(x); // $ read_access=x
print_i64(x + 1); // $ read_access=x
}
};
print_i64(x); // $ read_access=x
}
fn phi_read(b1 : bool, b2 : bool) {
fn phi_read(b1: bool, b2: bool) {
let x = 1; // x
if b1 { // $ read_access=b1
#[rustfmt::skip]
let _ = if b1 // $ read_access=b1
{
print_i64(x); // $ read_access=x
} else {
print_i64(x); // $ read_access=x
}
};
if b2 { // $ read_access=b2
#[rustfmt::skip]
let _ = if b2 // $ read_access=b2
{
print_i64(x); // $ read_access=x
} else {
print_i64(x); // $ read_access=x
}
};
}
struct MyStruct {
val: i64,
}
@@ -555,38 +574,34 @@ fn ref_arg() {
}
trait Bar {
fn bar(&self);
fn bar(&self);
}
impl MyStruct {
fn bar(&mut self) {
*self = MyStruct { val: 3 }; // $ read_access=self
}
fn bar(&mut self) {
*self = MyStruct { val: 3 }; // $ read_access=self
}
}
fn ref_methodcall_receiver() {
let mut a = MyStruct { val: 1 }; // a
a.bar(); // $ read_access=a
// prints 3, not 1
print_i64(a.val); // $ read_access=a
let mut a = MyStruct { val: 1 }; // a
a.bar(); // $ read_access=a
// prints 3, not 1
print_i64(a.val); // $ read_access=a
}
macro_rules! let_in_macro {
($e:expr) => {
{
let var_in_macro = $e;
var_in_macro
}
};
($e:expr) => {{
let var_in_macro = $e;
var_in_macro
}};
}
macro_rules! let_in_macro2 {
($e:expr) => {
{
let var_in_macro = 0;
$e
}
};
($e:expr) => {{
let var_in_macro = 0;
$e
}};
}
fn macro_invocation() {
@@ -601,6 +616,24 @@ fn macro_invocation() {
print_i64(var_in_macro); // $ read_access=var_in_macro1
}
fn let_without_initializer() {
let x; // x
x = 1; // $ write_access=x
print_i64(x); // $ read_access=x
}
fn capture_phi() {
let mut x = 100; // x
let mut cap = |b: bool| {
#[rustfmt::skip]
let _ = if b { // $ read_access=b
x = 200; // $ write_access=x
};
};
cap(true); // $ read_access=cap
print_i64(x); // $ read_access=x
}
fn main() {
immutable_variable();
mutable_variable();
@@ -637,4 +670,5 @@ fn main() {
ref_arg();
ref_methodcall_receiver();
macro_invocation();
capture_phi();
}

File diff suppressed because it is too large Load Diff