Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fc4aa4e

Browse files
committed
add test for self-assignment on call through reference
1 parent a04b7a3 commit fc4aa4e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/tools/miri/tests/pass/calls.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,26 @@ fn const_fn_call() -> i64 {
3434
x
3535
}
3636

37+
fn call_return_into_passed_reference() {
38+
pub fn func<T>(v: &mut T, f: fn(&T) -> T) {
39+
// MIR building will introduce a temporary, so this becomes
40+
// `let temp = f(v); *v = temp;`.
41+
// If this got optimized to `*v = f(v)` on the MIR level we'd have UB
42+
// since the return place may not be observed while the function runs!
43+
*v = f(v);
44+
}
45+
46+
let mut x = 0;
47+
func(&mut x, |v| v + 1);
48+
assert_eq!(x, 1);
49+
}
50+
3751
fn main() {
3852
assert_eq!(call(), 2);
3953
assert_eq!(factorial_recursive(), 3628800);
4054
assert_eq!(call_generic(), (42, true));
4155
assert_eq!(cross_crate_fn_call(), 1);
4256
assert_eq!(const_fn_call(), 11);
57+
58+
call_return_into_passed_reference();
4359
}

0 commit comments

Comments
 (0)