Skip to content

Commit 714301a

Browse files
committed
Rollup merge of rust-lang#32032 - arielb1:load-const, r=eddyb
Fixes rust-lang#30891 r? @eddyb
2 parents a022de6 + cf29344 commit 714301a

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/librustc_trans/trans/consts.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,17 @@ pub fn addr_of(ccx: &CrateContext,
161161
gv
162162
}
163163

164-
fn const_deref_ptr(cx: &CrateContext, v: ValueRef) -> ValueRef {
164+
/// Deref a constant pointer
165+
fn load_const(cx: &CrateContext, v: ValueRef, t: Ty) -> ValueRef {
165166
let v = match cx.const_unsized().borrow().get(&v) {
166167
Some(&v) => v,
167168
None => v
168169
};
169-
unsafe {
170-
llvm::LLVMGetInitializer(v)
170+
let d = unsafe { llvm::LLVMGetInitializer(v) };
171+
if t.is_bool() {
172+
unsafe { llvm::LLVMConstTrunc(d, Type::i1(cx).to_ref()) }
173+
} else {
174+
d
171175
}
172176
}
173177

@@ -178,7 +182,7 @@ fn const_deref<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
178182
match ty.builtin_deref(true, ty::NoPreference) {
179183
Some(mt) => {
180184
if type_is_sized(cx.tcx(), mt.ty) {
181-
(const_deref_ptr(cx, v), mt.ty)
185+
(load_const(cx, v, mt.ty), mt.ty)
182186
} else {
183187
// Derefing a fat pointer does not change the representation,
184188
// just the type to the unsized contents.
@@ -588,7 +592,10 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
588592
let is_float = ty.is_fp();
589593
let signed = ty.is_signed();
590594

591-
let (te2, _) = try!(const_expr(cx, &e2, param_substs, fn_args, trueconst));
595+
let (te2, ty2) = try!(const_expr(cx, &e2, param_substs, fn_args, trueconst));
596+
debug!("const_expr_unadjusted: te2={}, ty={:?}",
597+
cx.tn().val_to_string(te2),
598+
ty2);
592599

593600
try!(check_binary_expr_validity(cx, e, ty, te1, te2, trueconst));
594601

@@ -671,13 +678,13 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
671678
};
672679
let (arr, len) = match bt.sty {
673680
ty::TyArray(_, u) => (bv, C_uint(cx, u)),
674-
ty::TySlice(_) | ty::TyStr => {
681+
ty::TySlice(..) | ty::TyStr => {
675682
let e1 = const_get_elt(cx, bv, &[0]);
676-
(const_deref_ptr(cx, e1), const_get_elt(cx, bv, &[1]))
683+
(load_const(cx, e1, bt), const_get_elt(cx, bv, &[1]))
677684
},
678685
ty::TyRef(_, mt) => match mt.ty.sty {
679686
ty::TyArray(_, u) => {
680-
(const_deref_ptr(cx, bv), C_uint(cx, u))
687+
(load_const(cx, bv, mt.ty), C_uint(cx, u))
681688
},
682689
_ => cx.sess().span_bug(base.span,
683690
&format!("index-expr base must be a vector \
@@ -891,7 +898,8 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
891898
expr::trans_def_fn_unadjusted(cx, e, def, param_substs).val
892899
}
893900
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
894-
const_deref_ptr(cx, try!(get_const_val(cx, def_id, e, param_substs)))
901+
load_const(cx, try!(get_const_val(cx, def_id, e, param_substs)),
902+
ety)
895903
}
896904
Def::Variant(enum_did, variant_did) => {
897905
let vinfo = cx.tcx().lookup_adt_def(enum_did).variant_with_id(variant_did);

src/test/run-pass/issue-30891.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
const ERROR_CONST: bool = true;
12+
13+
fn get() -> bool {
14+
false || ERROR_CONST
15+
}
16+
17+
pub fn main() {
18+
assert_eq!(get(), true);
19+
}

0 commit comments

Comments
 (0)