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 {
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+
3751fn 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}
You can’t perform that action at this time.
0 commit comments