Skip to content

Commit 3b0e27c

Browse files
committedMay 8, 2016
trans: handle string literal reborrows.
1 parent 3001626 commit 3b0e27c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

‎src/librustc_trans/mir/constant.rs

+10
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ enum Base {
140140
/// A constant value without an unique address.
141141
Value(ValueRef),
142142

143+
/// String literal base pointer (cast from array).
144+
Str(ValueRef),
145+
143146
/// The address of a static.
144147
Static(ValueRef)
145148
}
@@ -156,6 +159,10 @@ impl<'tcx> ConstLvalue<'tcx> {
156159
fn to_const(&self, span: Span) -> Const<'tcx> {
157160
match self.base {
158161
Base::Value(val) => Const::new(val, self.ty),
162+
Base::Str(ptr) => {
163+
span_bug!(span, "loading from `str` ({:?}) in constant",
164+
Value(ptr))
165+
}
159166
Base::Static(val) => {
160167
span_bug!(span, "loading from `static` ({:?}) in constant",
161168
Value(val))
@@ -375,6 +382,8 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
375382
};
376383
if self.ccx.statics().borrow().contains_key(&base) {
377384
(Base::Static(base), extra)
385+
} else if let ty::TyStr = projected_ty.sty {
386+
(Base::Str(base), extra)
378387
} else {
379388
let val = consts::load_const(self.ccx, base, projected_ty);
380389
if val.is_null() {
@@ -669,6 +678,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
669678
consts::addr_of(self.ccx, llval, align, "ref")
670679
}
671680
}
681+
Base::Str(llval) |
672682
Base::Static(llval) => llval
673683
};
674684

‎src/test/run-pass/mir_constval_adts.rs

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct Point {
1414
_x: i32,
1515
_y: i32,
1616
}
17+
1718
const STRUCT: Point = Point { _x: 42, _y: 42 };
1819
const TUPLE1: (i32, i32) = (42, 42);
1920
const TUPLE2: (&'static str, &'static str) = ("hello","world");
@@ -26,7 +27,19 @@ fn mir() -> (Point, (i32, i32), (&'static str, &'static str)){
2627
(struct1, tuple1, tuple2)
2728
}
2829

30+
#[derive(PartialEq, Eq, Debug)]
31+
struct Newtype<T>(T);
32+
33+
const NEWTYPE: Newtype<&'static str> = Newtype("foobar");
34+
35+
#[rustc_mir]
36+
fn test_promoted_newtype_str_ref() {
37+
let x = &NEWTYPE;
38+
assert_eq!(x, &Newtype("foobar"));
39+
}
40+
2941
fn main(){
3042
assert_eq!(mir(), (STRUCT, TUPLE1, TUPLE2));
43+
test_promoted_newtype_str_ref();
3144
}
3245

0 commit comments

Comments
 (0)
Please sign in to comment.