Skip to content

Commit 449e8bf

Browse files
committed
Auto merge of #31286 - oli-obk:fix/mir_box, r=nagisa
the previous code generated a temporary of the inner type and assigned the box-memory to it. So if you did `let x: Box<usize> = box 5;` you got a ```rust let var0: Box<usize>; // x let mut tmp0: Box<usize>; let mut tmp1: usize; ... tmp1 = Box(usize); (*tmp1) = const 5; tmp0 = tmp1; var0 = tmp0; ``` r? @nagisa
2 parents 9bda7ea + 9b81d03 commit 449e8bf

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/librustc_mir/build/expr/as_rvalue.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,15 @@ impl<'a,'tcx> Builder<'a,'tcx> {
6161
}
6262
ExprKind::Box { value } => {
6363
let value = this.hir.mirror(value);
64-
let value_ty = value.ty.clone();
65-
let result = this.temp(value_ty.clone());
64+
let result = this.temp(expr.ty);
6665

6766
// to start, malloc some memory of suitable type (thus far, uninitialized):
68-
let rvalue = Rvalue::Box(value.ty.clone());
67+
let rvalue = Rvalue::Box(value.ty);
6968
this.cfg.push_assign(block, expr_span, &result, rvalue);
7069

7170
// schedule a shallow free of that memory, lest we unwind:
7271
let extent = this.extent_of_innermost_scope();
73-
this.schedule_drop(expr_span, extent, DropKind::Free, &result, value_ty);
72+
this.schedule_drop(expr_span, extent, DropKind::Free, &result, value.ty);
7473

7574
// initialize the box contents:
7675
let contents = result.clone().deref();

src/test/run-pass/mir_boxing.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 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+
#![feature(rustc_attrs, box_syntax)]
12+
13+
#[rustc_mir]
14+
fn test() -> Box<i32> {
15+
box 42
16+
}
17+
18+
fn main() {
19+
assert_eq!(*test(), 42);
20+
}

0 commit comments

Comments
 (0)