Skip to content

Commit

Permalink
Auto merge of #38053 - eddyb:lazy-9, r=nikomatsakis
Browse files Browse the repository at this point in the history
[9/n] rustc: move type information out of AdtDef and TraitDef.

_This is part of a series ([prev](#37688) | [next]()) of patches designed to rework rustc into an out-of-order on-demand pipeline model for both better feature support (e.g. [MIR-based](https://github.com/solson/miri) early constant evaluation) and incremental execution of compiler passes (e.g. type-checking), with beneficial consequences to IDE support as well.
If any motivation is unclear, please ask for additional PR description clarifications or code comments._

<hr>

Both `AdtDef` and `TraitDef` contained type information (field types, generics and predicates) which was required to create them, preventing their use before that type information exists, or in the case of field types, *mutation* was required, leading to a variance-magicking implementation of `ivar`s.

This PR takes that information out and the resulting cleaner setup could even eventually end up merged with HIR, because, just like `AssociatedItem` before it, there's no dependency on types anymore.
(With one exception, variant discriminants should probably be moved into their own map later.)
  • Loading branch information
bors committed Dec 2, 2016
2 parents 2f8fd53 + 07ff914 commit c80c31a
Show file tree
Hide file tree
Showing 49 changed files with 322 additions and 564 deletions.
3 changes: 0 additions & 3 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ pub enum DepNode<D: Clone + Debug> {
// predicates for an item wind up in `ItemSignature`).
AssociatedItems(D),
ItemSignature(D),
FieldTy(D),
SizedConstraint(D),
AssociatedItemDefIds(D),
InherentImpls(D),
Expand Down Expand Up @@ -161,7 +160,6 @@ impl<D: Clone + Debug> DepNode<D> {
TypeckItemBody,
AssociatedItems,
ItemSignature,
FieldTy,
AssociatedItemDefIds,
InherentImpls,
TraitImpls,
Expand Down Expand Up @@ -229,7 +227,6 @@ impl<D: Clone + Debug> DepNode<D> {
TransInlinedItem(ref d) => op(d).map(TransInlinedItem),
AssociatedItems(ref d) => op(d).map(AssociatedItems),
ItemSignature(ref d) => op(d).map(ItemSignature),
FieldTy(ref d) => op(d).map(FieldTy),
SizedConstraint(ref d) => op(d).map(SizedConstraint),
AssociatedItemDefIds(ref d) => op(d).map(AssociatedItemDefIds),
InherentImpls(ref d) => op(d).map(InherentImpls),
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ pub trait CrateStore<'tcx> {
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> ty::Generics<'tcx>;
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef;
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> &'tcx ty::AdtDef;
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;

Expand Down Expand Up @@ -425,9 +425,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn item_generics<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> ty::Generics<'tcx> { bug!("item_generics") }
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef
{ bug!("trait_def") }
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> &'tcx ty::AdtDef
{ bug!("adt_def") }
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name> { bug!("fn_arg_names") }
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> { vec![] }
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
// are properly handled.
self.walk_expr(with_expr);

fn contains_field_named(field: ty::FieldDef,
fn contains_field_named(field: &ty::FieldDef,
fields: &[hir::Field])
-> bool
{
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ pub enum TerminatorKind<'tcx> {
/// lvalue evaluates to some enum; jump depending on the branch
Switch {
discr: Lvalue<'tcx>,
adt_def: AdtDef<'tcx>,
adt_def: &'tcx AdtDef,
targets: Vec<BasicBlock>,
},

Expand Down Expand Up @@ -866,7 +866,7 @@ pub enum ProjectionElem<'tcx, V> {
/// "Downcast" to a variant of an ADT. Currently, we only introduce
/// this for ADTs with more than one variant. It may be better to
/// just introduce it always, or always for enums.
Downcast(AdtDef<'tcx>, usize),
Downcast(&'tcx AdtDef, usize),
}

/// Alias for projections as they appear in lvalues, where the base is an lvalue
Expand Down Expand Up @@ -1035,7 +1035,7 @@ pub enum AggregateKind<'tcx> {
/// The second field is variant number (discriminant), it's equal to 0
/// for struct and union expressions. The fourth field is active field
/// number and is present only for union expressions.
Adt(AdtDef<'tcx>, usize, &'tcx Substs<'tcx>, Option<usize>),
Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
Closure(DefId, ClosureSubsts<'tcx>),
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/tcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum LvalueTy<'tcx> {
Ty { ty: Ty<'tcx> },

/// Downcast to a particular variant of an enum.
Downcast { adt_def: AdtDef<'tcx>,
Downcast { adt_def: &'tcx AdtDef,
substs: &'tcx Substs<'tcx>,
variant_index: usize },
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
for item in self.tcx.get_attrs(def_id).iter() {
if item.check_name("rustc_on_unimplemented") {
let err_sp = item.meta().span.substitute_dummy(span);
let def = self.tcx.lookup_trait_def(trait_ref.def_id);
let trait_str = def.trait_ref.to_string();
let trait_str = self.tcx.item_path_str(trait_ref.def_id);
if let Some(istring) = item.value_str() {
let istring = &*istring.as_str();
let generic_map = def.generics.types.iter().map(|param| {
let generics = self.tcx.item_generics(trait_ref.def_id);
let generic_map = generics.types.iter().map(|param| {
(param.name.as_str().to_string(),
trait_ref.substs.type_for_def(param).to_string())
}).collect::<FxHashMap<String, String>>();
Expand Down
16 changes: 10 additions & 6 deletions src/librustc/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use super::elaborate_predicates;

use hir::def_id::DefId;
use traits;
use ty::{self, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use ty::{self, Ty, TyCtxt, TypeFoldable};
use ty::subst::Substs;
use syntax::ast;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -126,9 +127,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

fn supertraits_reference_self(self, trait_def_id: DefId) -> bool {
let trait_def = self.lookup_trait_def(trait_def_id);
let trait_ref = trait_def.trait_ref.clone();
let trait_ref = trait_ref.to_poly_trait_ref();
let trait_ref = ty::Binder(ty::TraitRef {
def_id: trait_def_id,
substs: Substs::identity_for_item(self, trait_def_id)
});
let predicates = self.item_super_predicates(trait_def_id);
predicates
.predicates
Expand Down Expand Up @@ -317,8 +319,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

// Compute supertraits of current trait lazily.
if supertraits.is_none() {
let trait_def = self.lookup_trait_def(trait_def_id);
let trait_ref = ty::Binder(trait_def.trait_ref.clone());
let trait_ref = ty::Binder(ty::TraitRef {
def_id: trait_def_id,
substs: Substs::identity_for_item(self, trait_def_id)
});
supertraits = Some(traits::supertraits(self, trait_ref).collect());
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
(&ty::TyAdt(def, substs_a), &ty::TyAdt(_, substs_b)) => {
let fields = def
.all_fields()
.map(|f| f.unsubst_ty())
.map(|f| tcx.item_type(f.did))
.collect::<Vec<_>>();

// The last field of the structure has to exist and contain type parameters.
Expand Down
14 changes: 6 additions & 8 deletions src/librustc/traits/specialize/specialization_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,18 @@ impl<'a, 'gcx, 'tcx> Node {
}
}

pub struct Ancestors<'a, 'tcx: 'a> {
trait_def: &'a TraitDef<'tcx>,
pub struct Ancestors<'a> {
trait_def: &'a TraitDef,
current_source: Option<Node>,
}

impl<'a, 'tcx> Iterator for Ancestors<'a, 'tcx> {
impl<'a> Iterator for Ancestors<'a> {
type Item = Node;
fn next(&mut self) -> Option<Node> {
let cur = self.current_source.take();
if let Some(Node::Impl(cur_impl)) = cur {
let parent = self.trait_def.specialization_graph.borrow().parent(cur_impl);
if parent == self.trait_def.def_id() {
if parent == self.trait_def.def_id {
self.current_source = Some(Node::Trait(parent));
} else {
self.current_source = Some(Node::Impl(parent));
Expand All @@ -332,7 +332,7 @@ impl<T> NodeItem<T> {
}
}

impl<'a, 'gcx, 'tcx> Ancestors<'a, 'tcx> {
impl<'a, 'gcx, 'tcx> Ancestors<'a> {
/// Search the items from the given ancestors, returning each definition
/// with the given name and the given kind.
#[inline] // FIXME(#35870) Avoid closures being unexported due to impl Trait.
Expand All @@ -347,9 +347,7 @@ impl<'a, 'gcx, 'tcx> Ancestors<'a, 'tcx> {

/// Walk up the specialization ancestors of a given impl, starting with that
/// impl itself.
pub fn ancestors<'a, 'tcx>(trait_def: &'a TraitDef<'tcx>,
start_from_impl: DefId)
-> Ancestors<'a, 'tcx> {
pub fn ancestors<'a>(trait_def: &'a TraitDef, start_from_impl: DefId) -> Ancestors<'a> {
Ancestors {
trait_def: trait_def,
current_source: Some(Node::Impl(start_from_impl)),
Expand Down
45 changes: 13 additions & 32 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ pub struct CtxtArenas<'tcx> {

// references
generics: TypedArena<ty::Generics<'tcx>>,
trait_def: TypedArena<ty::TraitDef<'tcx>>,
adt_def: TypedArena<ty::AdtDefData<'tcx, 'tcx>>,
trait_def: TypedArena<ty::TraitDef>,
adt_def: TypedArena<ty::AdtDef>,
mir: TypedArena<RefCell<Mir<'tcx>>>,
}

Expand Down Expand Up @@ -425,6 +425,7 @@ pub struct GlobalCtxt<'tcx> {
pub impl_trait_refs: RefCell<DepTrackingMap<maps::ImplTraitRefs<'tcx>>>,
pub trait_defs: RefCell<DepTrackingMap<maps::TraitDefs<'tcx>>>,
pub adt_defs: RefCell<DepTrackingMap<maps::AdtDefs<'tcx>>>,
pub adt_sized_constraint: RefCell<DepTrackingMap<maps::AdtSizedConstraint<'tcx>>>,

/// Maps from the def-id of an item (trait/struct/enum/fn) to its
/// associated generics and predicates.
Expand Down Expand Up @@ -688,38 +689,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.global_interners.arenas.mir.alloc(RefCell::new(mir))
}

pub fn intern_trait_def(self, def: ty::TraitDef<'gcx>)
-> &'gcx ty::TraitDef<'gcx> {
let did = def.trait_ref.def_id;
let interned = self.alloc_trait_def(def);
if let Some(prev) = self.trait_defs.borrow_mut().insert(did, interned) {
bug!("Tried to overwrite interned TraitDef: {:?}", prev)
}
self.generics.borrow_mut().insert(did, interned.generics);
interned
}

pub fn alloc_trait_def(self, def: ty::TraitDef<'gcx>)
-> &'gcx ty::TraitDef<'gcx> {
pub fn alloc_trait_def(self, def: ty::TraitDef) -> &'gcx ty::TraitDef {
self.global_interners.arenas.trait_def.alloc(def)
}

pub fn insert_adt_def(self, did: DefId, adt_def: ty::AdtDefMaster<'gcx>) {
// this will need a transmute when reverse-variance is removed
if let Some(prev) = self.adt_defs.borrow_mut().insert(did, adt_def) {
bug!("Tried to overwrite interned AdtDef: {:?}", prev)
}
}

pub fn intern_adt_def(self,
did: DefId,
kind: AdtKind,
variants: Vec<ty::VariantDefData<'gcx, 'gcx>>)
-> ty::AdtDefMaster<'gcx> {
let def = ty::AdtDefData::new(self, did, kind, variants);
let interned = self.global_interners.arenas.adt_def.alloc(def);
self.insert_adt_def(did, interned);
interned
pub fn alloc_adt_def(self,
did: DefId,
kind: AdtKind,
variants: Vec<ty::VariantDef>)
-> &'gcx ty::AdtDef {
let def = ty::AdtDef::new(self, did, kind, variants);
self.global_interners.arenas.adt_def.alloc(def)
}

pub fn intern_stability(self, stab: attr::Stability) -> &'gcx attr::Stability {
Expand Down Expand Up @@ -815,6 +795,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
impl_trait_refs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
trait_defs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
adt_defs: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
adt_sized_constraint: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
generics: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
predicates: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
super_predicates: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
Expand Down Expand Up @@ -1392,7 +1373,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.mk_imm_ref(self.mk_region(ty::ReStatic), self.mk_str())
}

pub fn mk_adt(self, def: AdtDef<'tcx>, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
pub fn mk_adt(self, def: &'tcx AdtDef, substs: &'tcx Substs<'tcx>) -> Ty<'tcx> {
// take a copy of substs so that we own the vectors inside
self.mk_ty(TyAdt(def, substs))
}
Expand Down
90 changes: 0 additions & 90 deletions src/librustc/ty/ivar.rs

This file was deleted.

5 changes: 3 additions & 2 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ dep_map_ty! { Predicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
dep_map_ty! { SuperPredicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx> }
dep_map_ty! { AssociatedItemDefIds: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>> }
dep_map_ty! { ImplTraitRefs: ItemSignature(DefId) -> Option<ty::TraitRef<'tcx>> }
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef<'tcx> }
dep_map_ty! { AdtDefs: ItemSignature(DefId) -> ty::AdtDefMaster<'tcx> }
dep_map_ty! { TraitDefs: ItemSignature(DefId) -> &'tcx ty::TraitDef }
dep_map_ty! { AdtDefs: ItemSignature(DefId) -> &'tcx ty::AdtDef }
dep_map_ty! { AdtSizedConstraint: SizedConstraint(DefId) -> Ty<'tcx> }
dep_map_ty! { ItemVariances: ItemSignature(DefId) -> Rc<Vec<ty::Variance>> }
dep_map_ty! { InherentImpls: InherentImpls(DefId) -> Vec<DefId> }
dep_map_ty! { ReprHints: ReprHints(DefId) -> Rc<Vec<attr::ReprAttr>> }
Expand Down
Loading

0 comments on commit c80c31a

Please sign in to comment.