Skip to content

Commit a8307a2

Browse files
committed
---
yaml --- r: 276351 b: refs/heads/master c: 3b0e27c h: refs/heads/master i: 276349: 6998406 276347: 84aa9ab 276343: 292a7d8 276335: 59eb413 276319: 66677d7 276287: 0da0315 276223: 6479ec1
1 parent 0f503b7 commit a8307a2

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 300162659504fed4798e535791023bf0174fc81c
2+
refs/heads/master: 3b0e27cc74d61e229aeaf0a710d3a018f7104ffc
33
refs/heads/snap-stage3: 235d77457d80b549dad3ac36d94f235208a1eafb
44
refs/heads/try: 49312a405e14a449b98fe0056b12a40ac128be4a
55
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

trunk/src/librustc_trans/mir/constant.rs

Lines changed: 10 additions & 0 deletions
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

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

Lines changed: 13 additions & 0 deletions
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)