Skip to content

Commit 70fe20a

Browse files
committed
Fix codegen breaking aliasing rules for functions with sret results
This reverts commit a0ec902 "Avoid unnecessary temporary on assignments". Leaving out the temporary for the functions return value can lead to a situation that conflicts with rust's aliasing rules. Given this: ````rust fn func(f: &mut Foo) -> Foo { /* ... */ } fn bar() { let mut foo = Foo { /* ... */ }; foo = func(&mut foo); } ```` We effectively get two mutable references to the same variable `foo` at the same time. One for the parameter `f`, and one for the hidden out-pointer. So we can't just `trans_into` the destination directly, but must use `trans` to get a new temporary slot from which the result can be copied.
1 parent 2130f22 commit 70fe20a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/librustc/middle/trans/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
940940
controlflow::trans_loop(bcx, expr.id, &**body)
941941
}
942942
ast::ExprAssign(ref dst, ref src) => {
943+
let src_datum = unpack_datum!(bcx, trans(bcx, &**src));
943944
let dst_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, &**dst, "assign"));
944945

945946
if ty::type_needs_drop(bcx.tcx(), dst_datum.ty) {
@@ -960,7 +961,6 @@ fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
960961
// We could avoid this intermediary with some analysis
961962
// to determine whether `dst` may possibly own `src`.
962963
debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
963-
let src_datum = unpack_datum!(bcx, trans(bcx, &**src));
964964
let src_datum = unpack_datum!(
965965
bcx, src_datum.to_rvalue_datum(bcx, "ExprAssign"));
966966
bcx = glue::drop_ty(bcx,
@@ -969,7 +969,7 @@ fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
969969
Some(NodeInfo { id: expr.id, span: expr.span }));
970970
src_datum.store_to(bcx, dst_datum.val)
971971
} else {
972-
trans_into(bcx, &**src, SaveIn(dst_datum.to_llref()))
972+
src_datum.store_to(bcx, dst_datum.val)
973973
}
974974
}
975975
ast::ExprAssignOp(op, ref dst, ref src) => {
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub struct Foo {
12+
f1: int,
13+
_f2: int,
14+
}
15+
16+
#[inline(never)]
17+
pub fn foo(f: &mut Foo) -> Foo {
18+
let ret = *f;
19+
f.f1 = 0;
20+
ret
21+
}
22+
23+
pub fn main() {
24+
let mut f = Foo {
25+
f1: 8,
26+
_f2: 9,
27+
};
28+
f = foo(&mut f);
29+
assert_eq!(f.f1, 8);
30+
}

0 commit comments

Comments
 (0)