about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-11-13 17:10:25 +0000
committerbors <bors@rust-lang.org>2015-11-13 17:10:25 +0000
commit3beb159809d2f1c9daf9c73d8fd62a4383742bc4 (patch)
tree5c1f285a9884685823e0e75cc2754214fcf13798
parentec8ae4b0eb4860f2ed30763d196a3fe0d7af72c2 (diff)
parentf1342ffb3ca3f5641b55ecdc6481f5cd80f338d6 (diff)
downloadrust-3beb159809d2f1c9daf9c73d8fd62a4383742bc4.tar.gz
rust-3beb159809d2f1c9daf9c73d8fd62a4383742bc4.zip
Auto merge of #29759 - nagisa:mir-static, r=nikomatsakis
Fixes #29578

r? @nikomatsakis

My own observations are posted inline as comments.
-rw-r--r--src/librustc_trans/trans/common.rs10
-rw-r--r--src/librustc_trans/trans/consts.rs16
-rw-r--r--src/librustc_trans/trans/expr.rs23
-rw-r--r--src/librustc_trans/trans/mir/lvalue.rs5
4 files changed, 17 insertions, 37 deletions
diff --git a/src/librustc_trans/trans/common.rs b/src/librustc_trans/trans/common.rs
index 8d6ba53dd22..ac3e3beeac8 100644
--- a/src/librustc_trans/trans/common.rs
+++ b/src/librustc_trans/trans/common.rs
@@ -1214,3 +1214,13 @@ pub fn shift_mask_val<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
     }
 }
 
+pub fn get_static_val<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
+                            did: DefId,
+                            ty: Ty<'tcx>)
+                            -> ValueRef {
+    if let Some(node_id) = ccx.tcx().map.as_local_node_id(did) {
+        base::get_item_val(ccx, node_id)
+    } else {
+        base::get_extern_const(ccx, did, ty)
+    }
+}
diff --git a/src/librustc_trans/trans/consts.rs b/src/librustc_trans/trans/consts.rs
index 90faef51c2c..b849888cf7c 100644
--- a/src/librustc_trans/trans/consts.rs
+++ b/src/librustc_trans/trans/consts.rs
@@ -29,8 +29,8 @@ use middle::const_eval::eval_const_expr_partial;
 use middle::def_id::DefId;
 use trans::{adt, closure, debuginfo, expr, inline, machine};
 use trans::base::{self, push_ctxt};
+use trans::common::{self, type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt};
 use trans::common::{CrateContext, C_integral, C_floating, C_bool, C_str_slice, C_bytes, val_ty};
-use trans::common::{type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt};
 use trans::common::{C_struct, C_undef, const_to_opt_int, const_to_opt_uint, VariantInfo, C_uint};
 use trans::common::{type_is_fat_ptr, Field, C_vector, C_array, C_null, ExprId, MethodCallKey};
 use trans::declare;
@@ -795,7 +795,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
             }
             let opt_def = cx.tcx().def_map.borrow().get(&cur.id).map(|d| d.full_def());
             if let Some(def::DefStatic(def_id, _)) = opt_def {
-                get_static_val(cx, def_id, ety)
+                common::get_static_val(cx, def_id, ety)
             } else {
                 // If this isn't the address of a static, then keep going through
                 // normal constant evaluation.
@@ -1075,15 +1075,3 @@ pub fn trans_static(ccx: &CrateContext,
         Ok(g)
     }
 }
-
-
-fn get_static_val<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
-                            did: DefId,
-                            ty: Ty<'tcx>)
-                            -> ValueRef {
-    if let Some(node_id) = ccx.tcx().map.as_local_node_id(did) {
-        base::get_item_val(ccx, node_id)
-    } else {
-        base::trans_external_path(ccx, did, ty)
-    }
-}
diff --git a/src/librustc_trans/trans/expr.rs b/src/librustc_trans/trans/expr.rs
index 7648587e352..f408bb595a2 100644
--- a/src/librustc_trans/trans/expr.rs
+++ b/src/librustc_trans/trans/expr.rs
@@ -941,29 +941,8 @@ fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
             DatumBlock::new(bcx, datum.to_expr_datum())
         }
         def::DefStatic(did, _) => {
-            // There are two things that may happen here:
-            //  1) If the static item is defined in this crate, it will be
-            //     translated using `get_item_val`, and we return a pointer to
-            //     the result.
-            //  2) If the static item is defined in another crate then we add
-            //     (or reuse) a declaration of an external global, and return a
-            //     pointer to that.
             let const_ty = expr_ty(bcx, ref_expr);
-
-            // For external constants, we don't inline.
-            let val = if let Some(node_id) = bcx.tcx().map.as_local_node_id(did) {
-                // Case 1.
-
-                // The LLVM global has the type of its initializer,
-                // which may not be equal to the enum's type for
-                // non-C-like enums.
-                let val = base::get_item_val(bcx.ccx(), node_id);
-                let pty = type_of::type_of(bcx.ccx(), const_ty).ptr_to();
-                PointerCast(bcx, val, pty)
-            } else {
-                // Case 2.
-                base::get_extern_const(bcx.ccx(), did, const_ty)
-            };
+            let val = get_static_val(bcx.ccx(), did, const_ty);
             let lval = Lvalue::new("expr::trans_def");
             DatumBlock::new(bcx, Datum::new(val, const_ty, LvalueExpr(lval)))
         }
diff --git a/src/librustc_trans/trans/mir/lvalue.rs b/src/librustc_trans/trans/mir/lvalue.rs
index 1ce7b55a9c6..98c15669976 100644
--- a/src/librustc_trans/trans/mir/lvalue.rs
+++ b/src/librustc_trans/trans/mir/lvalue.rs
@@ -65,7 +65,10 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
                     tcx.sess.bug(&format!("using operand temp {:?} as lvalue", lvalue)),
             },
             mir::Lvalue::Arg(index) => self.args[index as usize],
-            mir::Lvalue::Static(_def_id) => unimplemented!(),
+            mir::Lvalue::Static(def_id) => {
+                let const_ty = self.mir.lvalue_ty(tcx, lvalue);
+                LvalueRef::new(common::get_static_val(ccx, def_id, const_ty.to_ty(tcx)), const_ty)
+            },
             mir::Lvalue::ReturnPointer => {
                 let return_ty = bcx.monomorphize(&self.mir.return_ty);
                 let llval = fcx.get_ret_slot(bcx, return_ty, "return");