Skip to content

Commit 2adb618

Browse files
committed
Auto merge of #29759 - nagisa:mir-static, r=<try>
Fixes #29578 r? @nikomatsakis My own observations are posted inline as comments.
2 parents 8c9c951 + f1342ff commit 2adb618

File tree

4 files changed

+17
-37
lines changed

4 files changed

+17
-37
lines changed

src/librustc_trans/trans/common.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1214,3 +1214,13 @@ pub fn shift_mask_val<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
12141214
}
12151215
}
12161216

1217+
pub fn get_static_val<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1218+
did: DefId,
1219+
ty: Ty<'tcx>)
1220+
-> ValueRef {
1221+
if let Some(node_id) = ccx.tcx().map.as_local_node_id(did) {
1222+
base::get_item_val(ccx, node_id)
1223+
} else {
1224+
base::get_extern_const(ccx, did, ty)
1225+
}
1226+
}

src/librustc_trans/trans/consts.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use middle::const_eval::eval_const_expr_partial;
2929
use middle::def_id::DefId;
3030
use trans::{adt, closure, debuginfo, expr, inline, machine};
3131
use trans::base::{self, push_ctxt};
32+
use trans::common::{self, type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt};
3233
use trans::common::{CrateContext, C_integral, C_floating, C_bool, C_str_slice, C_bytes, val_ty};
33-
use trans::common::{type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt};
3434
use trans::common::{C_struct, C_undef, const_to_opt_int, const_to_opt_uint, VariantInfo, C_uint};
3535
use trans::common::{type_is_fat_ptr, Field, C_vector, C_array, C_null, ExprId, MethodCallKey};
3636
use trans::declare;
@@ -795,7 +795,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
795795
}
796796
let opt_def = cx.tcx().def_map.borrow().get(&cur.id).map(|d| d.full_def());
797797
if let Some(def::DefStatic(def_id, _)) = opt_def {
798-
get_static_val(cx, def_id, ety)
798+
common::get_static_val(cx, def_id, ety)
799799
} else {
800800
// If this isn't the address of a static, then keep going through
801801
// normal constant evaluation.
@@ -1075,15 +1075,3 @@ pub fn trans_static(ccx: &CrateContext,
10751075
Ok(g)
10761076
}
10771077
}
1078-
1079-
1080-
fn get_static_val<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1081-
did: DefId,
1082-
ty: Ty<'tcx>)
1083-
-> ValueRef {
1084-
if let Some(node_id) = ccx.tcx().map.as_local_node_id(did) {
1085-
base::get_item_val(ccx, node_id)
1086-
} else {
1087-
base::trans_external_path(ccx, did, ty)
1088-
}
1089-
}

src/librustc_trans/trans/expr.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -941,29 +941,8 @@ fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
941941
DatumBlock::new(bcx, datum.to_expr_datum())
942942
}
943943
def::DefStatic(did, _) => {
944-
// There are two things that may happen here:
945-
// 1) If the static item is defined in this crate, it will be
946-
// translated using `get_item_val`, and we return a pointer to
947-
// the result.
948-
// 2) If the static item is defined in another crate then we add
949-
// (or reuse) a declaration of an external global, and return a
950-
// pointer to that.
951944
let const_ty = expr_ty(bcx, ref_expr);
952-
953-
// For external constants, we don't inline.
954-
let val = if let Some(node_id) = bcx.tcx().map.as_local_node_id(did) {
955-
// Case 1.
956-
957-
// The LLVM global has the type of its initializer,
958-
// which may not be equal to the enum's type for
959-
// non-C-like enums.
960-
let val = base::get_item_val(bcx.ccx(), node_id);
961-
let pty = type_of::type_of(bcx.ccx(), const_ty).ptr_to();
962-
PointerCast(bcx, val, pty)
963-
} else {
964-
// Case 2.
965-
base::get_extern_const(bcx.ccx(), did, const_ty)
966-
};
945+
let val = get_static_val(bcx.ccx(), did, const_ty);
967946
let lval = Lvalue::new("expr::trans_def");
968947
DatumBlock::new(bcx, Datum::new(val, const_ty, LvalueExpr(lval)))
969948
}

src/librustc_trans/trans/mir/lvalue.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
6565
tcx.sess.bug(&format!("using operand temp {:?} as lvalue", lvalue)),
6666
},
6767
mir::Lvalue::Arg(index) => self.args[index as usize],
68-
mir::Lvalue::Static(_def_id) => unimplemented!(),
68+
mir::Lvalue::Static(def_id) => {
69+
let const_ty = self.mir.lvalue_ty(tcx, lvalue);
70+
LvalueRef::new(common::get_static_val(ccx, def_id, const_ty.to_ty(tcx)), const_ty)
71+
},
6972
mir::Lvalue::ReturnPointer => {
7073
let return_ty = bcx.monomorphize(&self.mir.return_ty);
7174
let llval = fcx.get_ret_slot(bcx, return_ty, "return");

0 commit comments

Comments
 (0)