Skip to content

Commit

Permalink
Create trait_def table.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 31, 2022
1 parent 618138b commit e62f483
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 72 deletions.
37 changes: 3 additions & 34 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,37 +909,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
)
}

fn get_trait_def(self, item_id: DefIndex, sess: &Session) -> ty::TraitDef {
match self.kind(item_id) {
EntryKind::Trait(data) => {
let data = data.decode((self, sess));
ty::TraitDef::new(
self.local_def_id(item_id),
data.unsafety,
data.paren_sugar,
data.has_auto_impl,
data.is_marker,
data.skip_array_during_method_dispatch,
data.specialization_kind,
self.def_path_hash(item_id),
data.must_implement_one_of,
)
}
EntryKind::TraitAlias => ty::TraitDef::new(
self.local_def_id(item_id),
hir::Unsafety::Normal,
false,
false,
false,
false,
ty::trait_def::TraitSpecializationKind::None,
self.def_path_hash(item_id),
None,
),
_ => bug!("def-index does not refer to trait or trait alias"),
}
}

fn get_variant(self, kind: &EntryKind, index: DefIndex, parent_did: DefId) -> ty::VariantDef {
let data = match kind {
EntryKind::Variant(data) | EntryKind::Struct(data) | EntryKind::Union(data) => {
Expand Down Expand Up @@ -1172,7 +1141,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
callback(exp);
}
}
EntryKind::Enum | EntryKind::Trait(..) => {}
EntryKind::Enum | EntryKind::Trait => {}
_ => bug!("`for_each_module_child` is called on a non-module: {:?}", self.def_kind(id)),
}
}
Expand All @@ -1187,7 +1156,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn module_expansion(self, id: DefIndex, sess: &Session) -> ExpnId {
match self.kind(id) {
EntryKind::Mod(_) | EntryKind::Enum | EntryKind::Trait(_) => {
EntryKind::Mod(_) | EntryKind::Enum | EntryKind::Trait => {
self.get_expn_that_defined(id, sess)
}
_ => panic!("Expected module, found {:?}", self.local_def_id(id)),
Expand Down Expand Up @@ -1396,7 +1365,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
_ => return None,
}
def_key.parent.and_then(|parent_index| match self.kind(parent_index) {
EntryKind::Trait(_) | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
EntryKind::Trait | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
_ => None,
})
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,
asyncness => { table }
fn_arg_names => { table }
generator_kind => { table }
trait_def => { table }

trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) }
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
adt_destructor => {
let _ = cdata;
Expand Down
19 changes: 8 additions & 11 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,19 +1474,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
hir::ItemKind::Trait(..) => {
let trait_def = self.tcx.trait_def(def_id);
let data = TraitData {
unsafety: trait_def.unsafety,
paren_sugar: trait_def.paren_sugar,
has_auto_impl: self.tcx.trait_is_auto(def_id),
is_marker: trait_def.is_marker,
skip_array_during_method_dispatch: trait_def.skip_array_during_method_dispatch,
specialization_kind: trait_def.specialization_kind,
must_implement_one_of: trait_def.must_implement_one_of.clone(),
};
record!(self.tables.trait_def[def_id] <- trait_def);

EntryKind::Trait
}
hir::ItemKind::TraitAlias(..) => {
let trait_def = self.tcx.trait_def(def_id);
record!(self.tables.trait_def[def_id] <- trait_def);

EntryKind::Trait(self.lazy(data))
EntryKind::TraitAlias
}
hir::ItemKind::TraitAlias(..) => EntryKind::TraitAlias,
hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => {
bug!("cannot encode info for item {:?}", item)
}
Expand Down
14 changes: 2 additions & 12 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ define_tables! {
asyncness: Table<DefIndex, Lazy!(hir::IsAsync)>,
fn_arg_names: Table<DefIndex, Lazy!([Ident])>,
generator_kind: Table<DefIndex, Lazy!(hir::GeneratorKind)>,
trait_def: Table<DefIndex, Lazy!(ty::TraitDef)>,

trait_item_def_id: Table<DefIndex, Lazy<DefId>>,
inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
Expand Down Expand Up @@ -360,7 +361,7 @@ enum EntryKind {
ProcMacro(MacroKind),
Closure,
Generator,
Trait(Lazy<TraitData>),
Trait,
Impl,
AssocFn(Lazy<AssocFnData>),
AssocType(AssocContainer),
Expand All @@ -377,17 +378,6 @@ struct VariantData {
is_non_exhaustive: bool,
}

#[derive(TyEncodable, TyDecodable)]
struct TraitData {
unsafety: hir::Unsafety,
paren_sugar: bool,
has_auto_impl: bool,
is_marker: bool,
skip_array_during_method_dispatch: bool,
specialization_kind: ty::trait_def::TraitSpecializationKind,
must_implement_one_of: Option<Box<[Ident]>>,
}

/// Describes whether the container of an associated item
/// is a trait or an impl and whether, in a trait, it has
/// a default, or an in impl, whether it's marked "default".
Expand Down
13 changes: 2 additions & 11 deletions compiler/rustc_middle/src/ty/trait_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ use crate::ty::{Ident, Ty, TyCtxt};
use hir::def_id::LOCAL_CRATE;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::DefPathHash;
use std::iter;

use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::ErrorGuaranteed;
use rustc_macros::HashStable;

/// A trait's definition with type information.
#[derive(HashStable)]
#[derive(HashStable, Encodable, Decodable)]
pub struct TraitDef {
// We already have the def_path_hash below, no need to hash it twice
#[stable_hasher(ignore)]
pub def_id: DefId,

pub unsafety: hir::Unsafety,
Expand All @@ -43,18 +40,14 @@ pub struct TraitDef {
/// on this trait.
pub specialization_kind: TraitSpecializationKind,

/// The ICH of this trait's DefPath, cached here so it doesn't have to be
/// recomputed all the time.
pub def_path_hash: DefPathHash,

/// List of functions from `#[rustc_must_implement_one_of]` attribute one of which
/// must be implemented.
pub must_implement_one_of: Option<Box<[Ident]>>,
}

/// Whether this trait is treated specially by the standard library
/// specialization lint.
#[derive(HashStable, PartialEq, Clone, Copy, TyEncodable, TyDecodable)]
#[derive(HashStable, PartialEq, Clone, Copy, Encodable, Decodable)]
pub enum TraitSpecializationKind {
/// The default. Specializing on this trait is not allowed.
None,
Expand Down Expand Up @@ -92,7 +85,6 @@ impl<'tcx> TraitDef {
is_marker: bool,
skip_array_during_method_dispatch: bool,
specialization_kind: TraitSpecializationKind,
def_path_hash: DefPathHash,
must_implement_one_of: Option<Box<[Ident]>>,
) -> TraitDef {
TraitDef {
Expand All @@ -103,7 +95,6 @@ impl<'tcx> TraitDef {
is_marker,
skip_array_during_method_dispatch,
specialization_kind,
def_path_hash,
must_implement_one_of,
}
}
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,6 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
} else {
ty::trait_def::TraitSpecializationKind::None
};
let def_path_hash = tcx.def_path_hash(def_id);

let must_implement_one_of = tcx
.get_attrs(def_id)
.iter()
Expand Down Expand Up @@ -1326,7 +1324,6 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
is_marker,
skip_array_during_method_dispatch,
spec_kind,
def_path_hash,
must_implement_one_of,
)
}
Expand Down

0 comments on commit e62f483

Please sign in to comment.