Skip to content

Commit 122e4a2

Browse files
committed
Don't copy const data to do an autoderef+autoref.
1 parent c929ddd commit 122e4a2

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/librustc/middle/trans/consts.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
182182
}
183183
Some(@ty::AutoDerefRef(ref adj)) => {
184184
let mut ty = ety;
185+
let mut maybe_ptr = None;
185186
for adj.autoderefs.times {
186187
let (dv, dt) = const_deref(cx, llconst, ty, false);
188+
maybe_ptr = Some(llconst);
187189
llconst = dv;
188190
ty = dt;
189191
}
@@ -193,17 +195,21 @@ pub fn const_expr(cx: @CrateContext, e: @ast::expr) -> ValueRef {
193195
Some(ref autoref) => {
194196
fail_unless!(autoref.region == ty::re_static);
195197
fail_unless!(autoref.mutbl != ast::m_mutbl);
198+
// Don't copy data to do a deref+ref.
199+
let llptr = match maybe_ptr {
200+
Some(ptr) => ptr,
201+
None => const_addr_of(cx, llconst)
202+
};
196203
match autoref.kind {
197204
ty::AutoPtr => {
198-
llconst = const_addr_of(cx, llconst);
205+
llconst = llptr;
199206
}
200207
ty::AutoBorrowVec => {
201-
let base = const_addr_of(cx, llconst);
202208
let size = machine::llsize_of(cx,
203209
val_ty(llconst));
204210
fail_unless!(abi::slice_elt_base == 0);
205211
fail_unless!(abi::slice_elt_len == 1);
206-
llconst = C_struct(~[base, size]);
212+
llconst = C_struct(~[llptr, size]);
207213
}
208214
_ => {
209215
cx.sess.span_bug(e.span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 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+
type Big = [u64 * 8];
12+
struct Pair { a: int, b: &'self Big }
13+
const x: &'static Big = &([13, 14, 10, 13, 11, 14, 14, 15]);
14+
const y: &'static Pair<'static> = &Pair {a: 15, b: x};
15+
16+
pub fn main() {
17+
fail_unless!(ptr::addr_of(x) == ptr::addr_of(y.b));
18+
}

0 commit comments

Comments
 (0)