Skip to content

Commit 8455650

Browse files
committed
Handle statics in Subst::subst() by implementing TypeFoldable
1 parent e63b992 commit 8455650

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

src/librustc/mir/mod.rs

+54-4
Original file line numberDiff line numberDiff line change
@@ -1729,15 +1729,15 @@ pub enum PlaceBase<'tcx> {
17291729
}
17301730

17311731
/// We store the normalized type to avoid requiring normalization when reading MIR
1732-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
1732+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
17331733
pub struct Static<'tcx> {
17341734
pub ty: Ty<'tcx>,
17351735
pub kind: StaticKind<'tcx>,
17361736
pub def_id: DefId,
17371737
}
17381738

17391739
#[derive(
1740-
Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, RustcEncodable, RustcDecodable,
1740+
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, RustcEncodable, RustcDecodable,
17411741
)]
17421742
pub enum StaticKind<'tcx> {
17431743
Promoted(Promoted, SubstsRef<'tcx>),
@@ -3221,13 +3221,63 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
32213221
impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
32223222
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
32233223
Place {
3224-
base: self.base.clone(),
3224+
base: self.base.fold_with(folder),
32253225
projection: self.projection.fold_with(folder),
32263226
}
32273227
}
32283228

32293229
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3230-
self.projection.visit_with(visitor)
3230+
self.base.visit_with(visitor) || self.projection.visit_with(visitor)
3231+
}
3232+
}
3233+
3234+
impl<'tcx> TypeFoldable<'tcx> for PlaceBase<'tcx> {
3235+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
3236+
match self {
3237+
PlaceBase::Local(local) => PlaceBase::Local(local.fold_with(folder)),
3238+
PlaceBase::Static(static_) => PlaceBase::Static(static_.fold_with(folder)),
3239+
}
3240+
}
3241+
3242+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3243+
match self {
3244+
PlaceBase::Local(local) => local.visit_with(visitor),
3245+
PlaceBase::Static(static_) => (**static_).visit_with(visitor),
3246+
}
3247+
}
3248+
}
3249+
3250+
impl<'tcx> TypeFoldable<'tcx> for Static<'tcx> {
3251+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
3252+
Static {
3253+
ty: self.ty.fold_with(folder),
3254+
kind: self.kind.fold_with(folder),
3255+
def_id: self.def_id,
3256+
}
3257+
}
3258+
3259+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3260+
let Static { ty, kind, def_id: _ } = self;
3261+
3262+
ty.visit_with(visitor) || kind.visit_with(visitor)
3263+
}
3264+
}
3265+
3266+
impl<'tcx> TypeFoldable<'tcx> for StaticKind<'tcx> {
3267+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
3268+
match self {
3269+
StaticKind::Promoted(promoted, substs) =>
3270+
StaticKind::Promoted(promoted.fold_with(folder), substs.fold_with(folder)),
3271+
StaticKind::Static => StaticKind::Static
3272+
}
3273+
}
3274+
3275+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
3276+
match self {
3277+
StaticKind::Promoted(promoted, substs) =>
3278+
promoted.visit_with(visitor) || substs.visit_with(visitor),
3279+
StaticKind::Static => { false }
3280+
}
32313281
}
32323282
}
32333283

src/librustc_codegen_ssa/mir/place.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc::ty::{self, Instance, Ty};
2-
use rustc::ty::subst::Subst;
32
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx, HasTyCtxt};
43
use rustc::mir;
54
use rustc::mir::tcx::PlaceTy;
@@ -461,18 +460,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
461460
projection: None,
462461
} => {
463462
let param_env = ty::ParamEnv::reveal_all();
464-
let instance = Instance::new(*def_id, substs.subst(bx.tcx(), self.instance.substs));
465-
debug!("instance: {:?}", instance);
463+
let instance = Instance::new(*def_id, self.monomorphize(substs));
466464
let cid = mir::interpret::GlobalId {
467465
instance: instance,
468466
promoted: Some(*promoted),
469467
};
470-
let mono_ty = tcx.subst_and_normalize_erasing_regions(
471-
instance.substs,
472-
param_env,
473-
ty,
474-
);
475-
let layout = cx.layout_of(mono_ty);
468+
let layout = cx.layout_of(self.monomorphize(&ty));
476469
match bx.tcx().const_eval(param_env.and(cid)) {
477470
Ok(val) => match val.val {
478471
mir::interpret::ConstValue::ByRef { alloc, offset } => {

src/librustc_mir/transform/inline.rs

-15
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,10 @@ impl Inliner<'tcx> {
479479
args: &args,
480480
local_map,
481481
scope_map,
482-
callsite,
483482
destination: dest,
484483
return_block,
485484
cleanup_block: cleanup,
486485
in_cleanup_block: false,
487-
tcx: self.tcx,
488486
};
489487

490488

@@ -639,12 +637,10 @@ struct Integrator<'a, 'tcx> {
639637
args: &'a [Local],
640638
local_map: IndexVec<Local, Local>,
641639
scope_map: IndexVec<SourceScope, SourceScope>,
642-
callsite: CallSite<'tcx>,
643640
destination: Place<'tcx>,
644641
return_block: BasicBlock,
645642
cleanup_block: Option<BasicBlock>,
646643
in_cleanup_block: bool,
647-
tcx: TyCtxt<'tcx>,
648644
}
649645

650646
impl<'a, 'tcx> Integrator<'a, 'tcx> {
@@ -693,17 +689,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
693689
// Return pointer; update the place itself
694690
*place = self.destination.clone();
695691
},
696-
Place {
697-
base: PlaceBase::Static(box Static {
698-
kind: StaticKind::Promoted(_, substs),
699-
..
700-
}),
701-
projection: None,
702-
} => {
703-
let adjusted_substs = substs.subst(self.tcx, self.callsite.substs);
704-
debug!("replacing substs {:?} with {:?}", substs, adjusted_substs);
705-
*substs = adjusted_substs;
706-
},
707692
_ => self.super_place(place, _ctxt, _location)
708693
}
709694
}

0 commit comments

Comments
 (0)