Skip to content

Commit 4d62545

Browse files
committed
Move def_id out add substsref
1 parent f13faf5 commit 4d62545

File tree

19 files changed

+134
-96
lines changed

19 files changed

+134
-96
lines changed

src/librustc/mir/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,20 +1732,22 @@ pub enum PlaceBase<'tcx> {
17321732
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
17331733
pub struct Static<'tcx> {
17341734
pub ty: Ty<'tcx>,
1735-
pub kind: StaticKind,
1735+
pub kind: StaticKind<'tcx>,
1736+
pub def_id: DefId,
17361737
}
17371738

17381739
#[derive(
17391740
Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, RustcEncodable, RustcDecodable,
17401741
)]
1741-
pub enum StaticKind {
1742-
Promoted(Promoted),
1743-
Static(DefId),
1742+
pub enum StaticKind<'tcx> {
1743+
Promoted(Promoted, SubstsRef<'tcx>),
1744+
Static,
17441745
}
17451746

17461747
impl_stable_hash_for!(struct Static<'tcx> {
17471748
ty,
1748-
kind
1749+
kind,
1750+
def_id
17491751
});
17501752

17511753
/// The `Projection` data structure defines things of the form `base.x`, `*b` or `b[index]`.
@@ -2106,10 +2108,12 @@ impl Debug for PlaceBase<'_> {
21062108
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
21072109
match *self {
21082110
PlaceBase::Local(id) => write!(fmt, "{:?}", id),
2109-
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) }) => {
2111+
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static, def_id }) => {
21102112
write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.def_path_str(def_id)), ty)
21112113
}
2112-
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Promoted(promoted) }) => {
2114+
PlaceBase::Static(box self::Static {
2115+
ty, kind: StaticKind::Promoted(promoted, _), def_id: _
2116+
}) => {
21132117
write!(fmt, "({:?}: {:?})", promoted, ty)
21142118
}
21152119
}

src/librustc/mir/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ macro_rules! make_mir_visitor {
708708
PlaceBase::Local(local) => {
709709
self.visit_local(local, context, location);
710710
}
711-
PlaceBase::Static(box Static { kind: _, ty }) => {
711+
PlaceBase::Static(box Static { kind: _, ty, def_id: _ }) => {
712712
self.visit_ty(& $($mutability)? *ty, TyContext::Location(location));
713713
}
714714
}

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,17 +609,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
609609
mir::Operand::Copy(
610610
Place {
611611
base: PlaceBase::Static(box Static {
612-
kind: StaticKind::Promoted(promoted),
612+
kind: StaticKind::Promoted(promoted, _),
613613
ty,
614+
def_id: _,
614615
}),
615616
projection: None,
616617
}
617618
) |
618619
mir::Operand::Move(
619620
Place {
620621
base: PlaceBase::Static(box Static {
621-
kind: StaticKind::Promoted(promoted),
622+
kind: StaticKind::Promoted(promoted, _),
622623
ty,
624+
def_id: _,
623625
}),
624626
projection: None,
625627
}

src/librustc_codegen_ssa/mir/place.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use rustc::ty::{self, Ty};
1+
use rustc::ty::{self, Instance, Ty};
2+
use rustc::ty::subst::Subst;
23
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx, HasTyCtxt};
34
use rustc::mir;
45
use rustc::mir::tcx::PlaceTy;
@@ -454,16 +455,25 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
454455
mir::PlaceRef {
455456
base: mir::PlaceBase::Static(box mir::Static {
456457
ty,
457-
kind: mir::StaticKind::Promoted(promoted),
458+
kind: mir::StaticKind::Promoted(promoted, substs),
459+
def_id,
458460
}),
459461
projection: None,
460462
} => {
463+
debug!("promoted={:?}, def_id={:?}, substs={:?}, self_substs={:?}", promoted, def_id, substs, self.instance.substs);
461464
let param_env = ty::ParamEnv::reveal_all();
465+
let instance = Instance::new(*def_id, substs.subst(bx.tcx(), self.instance.substs));
466+
debug!("instance: {:?}", instance);
462467
let cid = mir::interpret::GlobalId {
463-
instance: self.instance,
468+
instance: instance,
464469
promoted: Some(*promoted),
465470
};
466-
let layout = cx.layout_of(self.monomorphize(&ty));
471+
let mono_ty = tcx.subst_and_normalize_erasing_regions(
472+
instance.substs,
473+
param_env,
474+
ty,
475+
);
476+
let layout = cx.layout_of(mono_ty);
467477
match bx.tcx().const_eval(param_env.and(cid)) {
468478
Ok(val) => match val.val {
469479
mir::interpret::ConstValue::ByRef { alloc, offset } => {
@@ -487,7 +497,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
487497
mir::PlaceRef {
488498
base: mir::PlaceBase::Static(box mir::Static {
489499
ty,
490-
kind: mir::StaticKind::Static(def_id),
500+
kind: mir::StaticKind::Static,
501+
def_id,
491502
}),
492503
projection: None,
493504
} => {

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
159159
PlaceRef {
160160
base:
161161
PlaceBase::Static(box Static {
162-
kind: StaticKind::Promoted(_),
162+
kind: StaticKind::Promoted(..),
163163
..
164164
}),
165165
projection: None,
@@ -169,7 +169,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
169169
PlaceRef {
170170
base:
171171
PlaceBase::Static(box Static {
172-
kind: StaticKind::Static(def_id),
172+
kind: StaticKind::Static,
173+
def_id,
173174
..
174175
}),
175176
projection: None,
@@ -440,7 +441,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
440441
pub fn is_place_thread_local(&self, place_ref: PlaceRef<'cx, 'tcx>) -> bool {
441442
if let PlaceRef {
442443
base: PlaceBase::Static(box Static {
443-
kind: StaticKind::Static(def_id),
444+
kind: StaticKind::Static,
445+
def_id,
444446
..
445447
}),
446448
projection: None,

src/librustc_mir/borrow_check/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,13 +1467,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14671467
assert!(root_place.projection.is_none());
14681468
let (might_be_alive, will_be_dropped) = match root_place.base {
14691469
PlaceBase::Static(box Static {
1470-
kind: StaticKind::Promoted(_),
1470+
kind: StaticKind::Promoted(..),
14711471
..
14721472
}) => {
14731473
(true, false)
14741474
}
14751475
PlaceBase::Static(box Static {
1476-
kind: StaticKind::Static(_),
1476+
kind: StaticKind::Static,
14771477
..
14781478
}) => {
14791479
// Thread-locals might be dropped after the function exits, but
@@ -2155,7 +2155,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21552155
// `Place::Promoted` if the promotion weren't 100% legal. So we just forward this
21562156
PlaceRef {
21572157
base: PlaceBase::Static(box Static {
2158-
kind: StaticKind::Promoted(_),
2158+
kind: StaticKind::Promoted(..),
21592159
..
21602160
}),
21612161
projection: None,
@@ -2167,7 +2167,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21672167
}),
21682168
PlaceRef {
21692169
base: PlaceBase::Static(box Static {
2170-
kind: StaticKind::Static(def_id),
2170+
kind: StaticKind::Static,
2171+
def_id,
21712172
..
21722173
}),
21732174
projection: None,

src/librustc_mir/borrow_check/mutability_errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
149149
PlaceRef {
150150
base:
151151
PlaceBase::Static(box Static {
152-
kind: StaticKind::Promoted(_),
152+
kind: StaticKind::Promoted(..),
153153
..
154154
}),
155155
projection: None,
@@ -158,7 +158,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
158158
PlaceRef {
159159
base:
160160
PlaceBase::Static(box Static {
161-
kind: StaticKind::Static(def_id),
161+
kind: StaticKind::Static,
162+
def_id,
162163
..
163164
}),
164165
projection: None,

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
421421
let mut place_ty = match place_base {
422422
PlaceBase::Local(index) =>
423423
PlaceTy::from_ty(self.body.local_decls[*index].ty),
424-
PlaceBase::Static(box Static { kind, ty: sty }) => {
424+
PlaceBase::Static(box Static { kind, ty: sty, def_id }) => {
425425
let sty = self.sanitize_type(place, sty);
426426
let check_err =
427427
|verifier: &mut TypeVerifier<'a, 'b, 'tcx>,
@@ -445,7 +445,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
445445
};
446446
};
447447
match kind {
448-
StaticKind::Promoted(promoted) => {
448+
StaticKind::Promoted(promoted, _) => {
449449
if !self.errors_reported {
450450
let promoted_body = &self.promoted[*promoted];
451451
self.sanitize_promoted(promoted_body, location);
@@ -454,7 +454,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
454454
check_err(self, place, promoted_ty, sty);
455455
}
456456
}
457-
StaticKind::Static(def_id) => {
457+
StaticKind::Static => {
458458
let ty = self.tcx().type_of(*def_id);
459459
let ty = self.cx.normalize(ty, location);
460460

@@ -471,7 +471,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
471471
let is_promoted = match place {
472472
Place {
473473
base: PlaceBase::Static(box Static {
474-
kind: StaticKind::Promoted(_),
474+
kind: StaticKind::Promoted(..),
475475
..
476476
}),
477477
projection: None,

src/librustc_mir/borrow_check/place_ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
4646
}
4747
}
4848
}
49-
PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. }) =>
49+
PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_, _), .. }) =>
5050
false,
51-
PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. }) => {
51+
PlaceBase::Static(box Static{ kind: StaticKind::Static, def_id, .. }) => {
5252
tcx.is_mutable_static(*def_id)
5353
}
5454
};

src/librustc_mir/borrow_check/places_conflict.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ fn place_base_conflict<'tcx>(
329329
}
330330
(PlaceBase::Static(s1), PlaceBase::Static(s2)) => {
331331
match (&s1.kind, &s2.kind) {
332-
(StaticKind::Static(def_id_1), StaticKind::Static(def_id_2)) => {
333-
if def_id_1 != def_id_2 {
332+
(StaticKind::Static, StaticKind::Static) => {
333+
if s1.def_id != s2.def_id {
334334
debug!("place_element_conflict: DISJOINT-STATIC");
335335
Overlap::Disjoint
336-
} else if tcx.is_mutable_static(*def_id_1) {
336+
} else if tcx.is_mutable_static(s1.def_id) {
337337
// We ignore mutable statics - they can only be unsafe code.
338338
debug!("place_element_conflict: IGNORE-STATIC-MUT");
339339
Overlap::Disjoint
@@ -342,7 +342,7 @@ fn place_base_conflict<'tcx>(
342342
Overlap::EqualOrDisjoint
343343
}
344344
},
345-
(StaticKind::Promoted(promoted_1), StaticKind::Promoted(promoted_2)) => {
345+
(StaticKind::Promoted(promoted_1, _), StaticKind::Promoted(promoted_2, _)) => {
346346
if promoted_1 == promoted_2 {
347347
if let ty::Array(_, len) = s1.ty.sty {
348348
if let Some(0) = len.try_eval_usize(tcx, param_env) {

src/librustc_mir/build/expr/as_place.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
126126
ExprKind::StaticRef { id } => block.and(Place {
127127
base: PlaceBase::Static(Box::new(Static {
128128
ty: expr.ty,
129-
kind: StaticKind::Static(id),
129+
kind: StaticKind::Static,
130+
def_id: id,
130131
})),
131132
projection: None,
132133
}),

src/librustc_mir/interpret/place.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,19 +585,19 @@ where
585585
use rustc::mir::StaticKind;
586586

587587
Ok(match place_static.kind {
588-
StaticKind::Promoted(promoted) => {
588+
StaticKind::Promoted(promoted, _) => {
589589
let instance = self.frame().instance;
590590
self.const_eval_raw(GlobalId {
591591
instance,
592592
promoted: Some(promoted),
593593
})?
594594
}
595595

596-
StaticKind::Static(def_id) => {
596+
StaticKind::Static => {
597597
let ty = place_static.ty;
598598
assert!(!ty.needs_subst());
599599
let layout = self.layout_of(ty)?;
600-
let instance = ty::Instance::mono(*self.tcx, def_id);
600+
let instance = ty::Instance::mono(*self.tcx, place_static.def_id);
601601
let cid = GlobalId {
602602
instance,
603603
promoted: None

src/librustc_mir/monomorphize/collector.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ use rustc::hir::itemlikevisit::ItemLikeVisitor;
180180
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
181181
use rustc::mir::interpret::{AllocId, ConstValue};
182182
use rustc::middle::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem};
183-
use rustc::ty::subst::{InternalSubsts, SubstsRef};
183+
use rustc::ty::subst::{InternalSubsts, Subst, SubstsRef};
184184
use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind, Instance};
185185
use rustc::ty::print::obsolete::DefPathBasedNames;
186186
use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast};
@@ -661,7 +661,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
661661
_context: mir::visit::PlaceContext,
662662
location: Location) {
663663
match place_base {
664-
PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. }) => {
664+
PlaceBase::Static(box Static { kind: StaticKind::Static, def_id, .. }) => {
665665
debug!("visiting static {:?} @ {:?}", def_id, location);
666666

667667
let tcx = self.tcx;
@@ -670,8 +670,23 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
670670
self.output.push(MonoItem::Static(*def_id));
671671
}
672672
}
673-
PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. }) => {
674-
// FIXME: should we handle promoteds here instead of eagerly in collect_neighbours?
673+
PlaceBase::Static(box Static { kind: StaticKind::Promoted(promoted, substs), def_id, .. }) => {
674+
debug!("collecting promoted(def_id: {:?}, promoted: {:?}, substs: {:?})", def_id, promoted, substs);
675+
debug!("param_substs: {:?}", self.param_substs);
676+
let param_env = ty::ParamEnv::reveal_all();
677+
let cid = GlobalId {
678+
instance: Instance::new(*def_id, substs.subst(self.tcx, self.param_substs)),
679+
promoted: Some(*promoted),
680+
};
681+
debug!("cid: {:?}", cid);
682+
match self.tcx.const_eval(param_env.and(cid)) {
683+
Ok(val) => collect_const(self.tcx, val, substs, self.output),
684+
Err(ErrorHandled::Reported) => {},
685+
Err(ErrorHandled::TooGeneric) => {
686+
let span = self.tcx.promoted_mir(*def_id)[*promoted].span;
687+
span_bug!(span, "collection encountered polymorphic constant")
688+
},
689+
}
675690
}
676691
PlaceBase::Local(_) => {
677692
// Locals have no relevance for collector
@@ -1231,24 +1246,6 @@ fn collect_neighbours<'tcx>(
12311246
output,
12321247
param_substs: instance.substs,
12331248
}.visit_body(&body);
1234-
1235-
if let ty::InstanceDef::Item(def_id) = instance.def {
1236-
let param_env = ty::ParamEnv::reveal_all();
1237-
let promoted = tcx.promoted_mir(def_id);
1238-
for (promoted, promoted_body) in promoted.iter_enumerated() {
1239-
let cid = GlobalId {
1240-
instance,
1241-
promoted: Some(promoted),
1242-
};
1243-
match tcx.const_eval(param_env.and(cid)) {
1244-
Ok(val) => collect_const(tcx, val, instance.substs, output),
1245-
Err(ErrorHandled::Reported) => {},
1246-
Err(ErrorHandled::TooGeneric) => span_bug!(
1247-
promoted_body.span, "collection encountered polymorphic constant",
1248-
),
1249-
}
1250-
}
1251-
}
12521249
}
12531250

12541251
fn def_id_to_string(tcx: TyCtxt<'_>, def_id: DefId) -> String {

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
205205
PlaceBase::Local(..) => {
206206
// Locals are safe.
207207
}
208-
PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. }) => {
208+
PlaceBase::Static(box Static { kind: StaticKind::Promoted(_, _), .. }) => {
209209
bug!("unsafety checking should happen before promotion")
210210
}
211-
PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. }) => {
211+
PlaceBase::Static(box Static { kind: StaticKind::Static, def_id, .. }) => {
212212
if self.tcx.is_mutable_static(*def_id) {
213213
self.require_unsafe("use of mutable static",
214214
"mutable statics can be mutated by multiple threads: aliasing \

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
285285
place.iterate(|place_base, place_projection| {
286286
let mut eval = match place_base {
287287
PlaceBase::Local(loc) => self.get_const(*loc).clone()?,
288-
PlaceBase::Static(box Static {kind: StaticKind::Promoted(promoted), ..}) => {
288+
PlaceBase::Static(box Static {kind: StaticKind::Promoted(promoted, _), ..}) => {
289289
let generics = self.tcx.generics_of(self.source.def_id());
290290
if generics.requires_monomorphization(self.tcx) {
291291
// FIXME: can't handle code with generics

0 commit comments

Comments
 (0)