]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_mir/monomorphize/collector.rs
Move def_id out add substsref
[rust.git] / src / librustc_mir / monomorphize / collector.rs
index 9d80163f30f9d26276812828dc53ac52a8076769..512ace1a4728cc16dec51151688b2d75bf43ab9d 100644 (file)
 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
 use rustc::mir::interpret::{AllocId, ConstValue};
 use rustc::middle::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem};
-use rustc::ty::subst::{InternalSubsts, SubstsRef};
+use rustc::ty::subst::{InternalSubsts, Subst, SubstsRef};
 use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind, Instance};
 use rustc::ty::print::obsolete::DefPathBasedNames;
 use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast};
@@ -661,7 +661,7 @@ fn visit_place_base(&mut self,
                         _context: mir::visit::PlaceContext,
                         location: Location) {
         match place_base {
-            PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. }) => {
+            PlaceBase::Static(box Static { kind: StaticKind::Static, def_id, .. }) => {
                 debug!("visiting static {:?} @ {:?}", def_id, location);
 
                 let tcx = self.tcx;
@@ -670,8 +670,23 @@ fn visit_place_base(&mut self,
                     self.output.push(MonoItem::Static(*def_id));
                 }
             }
-            PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. }) => {
-                // FIXME: should we handle promoteds here instead of eagerly in collect_neighbours?
+            PlaceBase::Static(box Static { kind: StaticKind::Promoted(promoted, substs), def_id, .. }) => {
+                debug!("collecting promoted(def_id: {:?}, promoted: {:?}, substs: {:?})", def_id, promoted, substs);
+                debug!("param_substs: {:?}", self.param_substs);
+                let param_env = ty::ParamEnv::reveal_all();
+                let cid = GlobalId {
+                    instance: Instance::new(*def_id, substs.subst(self.tcx, self.param_substs)),
+                    promoted: Some(*promoted),
+                };
+                debug!("cid: {:?}", cid);
+                match self.tcx.const_eval(param_env.and(cid)) {
+                    Ok(val) => collect_const(self.tcx, val, substs, self.output),
+                    Err(ErrorHandled::Reported) => {},
+                    Err(ErrorHandled::TooGeneric) => {
+                        let span = self.tcx.promoted_mir(*def_id)[*promoted].span;
+                        span_bug!(span, "collection encountered polymorphic constant")
+                    },
+                }
             }
             PlaceBase::Local(_) => {
                 // Locals have no relevance for collector
@@ -1231,24 +1246,6 @@ fn collect_neighbours<'tcx>(
         output,
         param_substs: instance.substs,
     }.visit_body(&body);
-
-    if let ty::InstanceDef::Item(def_id) = instance.def {
-        let param_env = ty::ParamEnv::reveal_all();
-        let promoted = tcx.promoted_mir(def_id);
-        for (promoted, promoted_body) in promoted.iter_enumerated() {
-            let cid = GlobalId {
-                instance,
-                promoted: Some(promoted),
-            };
-            match tcx.const_eval(param_env.and(cid)) {
-                Ok(val) => collect_const(tcx, val, instance.substs, output),
-                Err(ErrorHandled::Reported) => {},
-                Err(ErrorHandled::TooGeneric) => span_bug!(
-                    promoted_body.span, "collection encountered polymorphic constant",
-                ),
-            }
-        }
-    }
 }
 
 fn def_id_to_string(tcx: TyCtxt<'_>, def_id: DefId) -> String {