File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed
src/tools/miri/tests/pass Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -34,10 +34,26 @@ fn const_fn_call() -> i64 {
34
34
x
35
35
}
36
36
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
+
37
51
fn main ( ) {
38
52
assert_eq ! ( call( ) , 2 ) ;
39
53
assert_eq ! ( factorial_recursive( ) , 3628800 ) ;
40
54
assert_eq ! ( call_generic( ) , ( 42 , true ) ) ;
41
55
assert_eq ! ( cross_crate_fn_call( ) , 1 ) ;
42
56
assert_eq ! ( const_fn_call( ) , 11 ) ;
57
+
58
+ call_return_into_passed_reference ( ) ;
43
59
}
You can’t perform that action at this time.
0 commit comments