Skip to content

Commit 1336b8e

Browse files
nikomatsakisAlexander Regueiro
and
Alexander Regueiro
committed
integrate trait aliases into def-paths / metadata
Co-authored-by: Alexander Regueiro <[email protected]>
1 parent 430553b commit 1336b8e

File tree

13 files changed

+82
-39
lines changed

13 files changed

+82
-39
lines changed

src/librustc/hir/map/def_collector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
120120
let def_data = match i.node {
121121
ItemKind::Impl(..) => DefPathData::Impl,
122122
ItemKind::Trait(..) => DefPathData::Trait(i.ident.as_interned_str()),
123+
ItemKind::TraitAlias(..) => DefPathData::TraitAlias(i.ident.as_interned_str()),
123124
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
124-
ItemKind::TraitAlias(..) | ItemKind::Existential(..) |
125-
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>
126-
DefPathData::TypeNs(i.ident.as_interned_str()),
125+
ItemKind::Existential(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
126+
ItemKind::Ty(..) => DefPathData::TypeNs(i.ident.as_interned_str()),
127127
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
128128
return visit::walk_item(self, i);
129129
}

src/librustc/hir/map/definitions.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ pub enum DefPathData {
373373
/// GlobalMetaData identifies a piece of crate metadata that is global to
374374
/// a whole crate (as opposed to just one item). GlobalMetaData components
375375
/// are only supposed to show up right below the crate root.
376-
GlobalMetaData(InternedString)
376+
GlobalMetaData(InternedString),
377+
/// A trait alias.
378+
TraitAlias(InternedString),
377379
}
378380

379381
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
@@ -615,6 +617,7 @@ impl DefPathData {
615617
match *self {
616618
TypeNs(name) |
617619
Trait(name) |
620+
TraitAlias(name) |
618621
AssocTypeInTrait(name) |
619622
AssocTypeInImpl(name) |
620623
AssocExistentialInImpl(name) |
@@ -642,6 +645,7 @@ impl DefPathData {
642645
let s = match *self {
643646
TypeNs(name) |
644647
Trait(name) |
648+
TraitAlias(name) |
645649
AssocTypeInTrait(name) |
646650
AssocTypeInImpl(name) |
647651
AssocExistentialInImpl(name) |

src/librustc/traits/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21542154

21552155
let def_id = obligation.predicate.def_id();
21562156

2157-
if ty::is_trait_alias(self.tcx(), def_id) {
2157+
if self.tcx().is_trait_alias(def_id) {
21582158
candidates.vec.push(TraitAliasCandidate(def_id.clone()));
21592159
}
21602160

src/librustc/ty/item_path.rs

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
311311
data @ DefPathData::Misc |
312312
data @ DefPathData::TypeNs(..) |
313313
data @ DefPathData::Trait(..) |
314+
data @ DefPathData::TraitAlias(..) |
314315
data @ DefPathData::AssocTypeInTrait(..) |
315316
data @ DefPathData::AssocTypeInImpl(..) |
316317
data @ DefPathData::AssocExistentialInImpl(..) |

src/librustc/ty/mod.rs

-12
Original file line numberDiff line numberDiff line change
@@ -3178,18 +3178,6 @@ pub fn is_impl_trait_defn(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<DefI
31783178
None
31793179
}
31803180

3181-
/// Returns `true` if `def_id` is a trait alias.
3182-
pub fn is_trait_alias(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> bool {
3183-
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
3184-
if let Node::Item(item) = tcx.hir().get(node_id) {
3185-
if let hir::ItemKind::TraitAlias(..) = item.node {
3186-
return true;
3187-
}
3188-
}
3189-
}
3190-
false
3191-
}
3192-
31933181
/// See `ParamEnv` struct definition for details.
31943182
fn param_env<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
31953183
def_id: DefId)

src/librustc/ty/util.rs

+9
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
526526
}
527527
}
528528

529+
/// True if `def_id` refers to a trait alias (i.e., `trait Foo = ...;`).
530+
pub fn is_trait_alias(self, def_id: DefId) -> bool {
531+
if let DefPathData::TraitAlias(_) = self.def_key(def_id).disambiguated_data.data {
532+
true
533+
} else {
534+
false
535+
}
536+
}
537+
529538
/// True if this def-id refers to the implicit constructor for
530539
/// a tuple struct like `struct Foo(u32)`.
531540
pub fn is_struct_constructor(self, def_id: DefId) -> bool {

src/librustc/util/ppaux.rs

+1
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ impl PrintContext {
409409
DefPathData::AssocTypeInImpl(_) |
410410
DefPathData::AssocExistentialInImpl(_) |
411411
DefPathData::Trait(_) |
412+
DefPathData::TraitAlias(_) |
412413
DefPathData::Impl |
413414
DefPathData::TypeNs(_) => {
414415
break;

src/librustc_metadata/decoder.rs

+30-16
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ impl<'tcx> EntryKind<'tcx> {
418418
EntryKind::Mod(_) => Def::Mod(did),
419419
EntryKind::Variant(_) => Def::Variant(did),
420420
EntryKind::Trait(_) => Def::Trait(did),
421+
EntryKind::TraitAlias(_) => Def::TraitAlias(did),
421422
EntryKind::Enum(..) => Def::Enum(did),
422423
EntryKind::MacroDef(_) => Def::Macro(did, MacroKind::Bang),
423424
EntryKind::ForeignType => Def::ForeignTy(did),
@@ -520,17 +521,26 @@ impl<'a, 'tcx> CrateMetadata {
520521
}
521522

522523
pub fn get_trait_def(&self, item_id: DefIndex, sess: &Session) -> ty::TraitDef {
523-
let data = match self.entry(item_id).kind {
524-
EntryKind::Trait(data) => data.decode((self, sess)),
525-
_ => bug!(),
526-
};
527-
528-
ty::TraitDef::new(self.local_def_id(item_id),
529-
data.unsafety,
530-
data.paren_sugar,
531-
data.has_auto_impl,
532-
data.is_marker,
533-
self.def_path_table.def_path_hash(item_id))
524+
match self.entry(item_id).kind {
525+
EntryKind::Trait(data) => {
526+
let data = data.decode((self, sess));
527+
ty::TraitDef::new(self.local_def_id(item_id),
528+
data.unsafety,
529+
data.paren_sugar,
530+
data.has_auto_impl,
531+
data.is_marker,
532+
self.def_path_table.def_path_hash(item_id))
533+
},
534+
EntryKind::TraitAlias(_) => {
535+
ty::TraitDef::new(self.local_def_id(item_id),
536+
hir::Unsafety::Normal,
537+
false,
538+
false,
539+
false,
540+
self.def_path_table.def_path_hash(item_id))
541+
},
542+
_ => bug!("def-index does not refer to trait or trait alias"),
543+
}
534544
}
535545

536546
fn get_variant(&self,
@@ -615,10 +625,13 @@ impl<'a, 'tcx> CrateMetadata {
615625
item_id: DefIndex,
616626
tcx: TyCtxt<'a, 'tcx, 'tcx>)
617627
-> ty::GenericPredicates<'tcx> {
618-
match self.entry(item_id).kind {
619-
EntryKind::Trait(data) => data.decode(self).super_predicates.decode((self, tcx)),
620-
_ => bug!(),
621-
}
628+
let super_predicates = match self.entry(item_id).kind {
629+
EntryKind::Trait(data) => data.decode(self).super_predicates,
630+
EntryKind::TraitAlias(data) => data.decode(self).super_predicates,
631+
_ => bug!("def-index does not refer to trait or trait alias"),
632+
};
633+
634+
super_predicates.decode((self, tcx))
622635
}
623636

624637
pub fn get_generics(&self,
@@ -1014,7 +1027,8 @@ impl<'a, 'tcx> CrateMetadata {
10141027
}
10151028
def_key.parent.and_then(|parent_index| {
10161029
match self.entry(parent_index).kind {
1017-
EntryKind::Trait(_) => Some(self.local_def_id(parent_index)),
1030+
EntryKind::Trait(_) |
1031+
EntryKind::TraitAlias(_) => Some(self.local_def_id(parent_index)),
10181032
_ => None,
10191033
}
10201034
})

src/librustc_metadata/encoder.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
11311131

11321132
EntryKind::Impl(self.lazy(&data))
11331133
}
1134-
hir::ItemKind::Trait(..) |
1135-
hir::ItemKind::TraitAlias(..) => {
1134+
hir::ItemKind::Trait(..) => {
11361135
let trait_def = tcx.trait_def(def_id);
11371136
let data = TraitData {
11381137
unsafety: trait_def.unsafety,
@@ -1144,6 +1143,13 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
11441143

11451144
EntryKind::Trait(self.lazy(&data))
11461145
}
1146+
hir::ItemKind::TraitAlias(..) => {
1147+
let data = TraitAliasData {
1148+
super_predicates: self.lazy(&tcx.super_predicates_of(def_id)),
1149+
};
1150+
1151+
EntryKind::TraitAlias(self.lazy(&data))
1152+
}
11471153
hir::ItemKind::ExternCrate(_) |
11481154
hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
11491155
};
@@ -1217,6 +1223,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12171223
hir::ItemKind::Impl(..) |
12181224
hir::ItemKind::Existential(..) |
12191225
hir::ItemKind::Trait(..) => Some(self.encode_generics(def_id)),
1226+
hir::ItemKind::TraitAlias(..) => Some(self.encode_generics(def_id)),
12201227
_ => None,
12211228
},
12221229
predicates: match item.node {
@@ -1229,7 +1236,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12291236
hir::ItemKind::Union(..) |
12301237
hir::ItemKind::Impl(..) |
12311238
hir::ItemKind::Existential(..) |
1232-
hir::ItemKind::Trait(..) => Some(self.encode_predicates(def_id)),
1239+
hir::ItemKind::Trait(..) |
1240+
hir::ItemKind::TraitAlias(..) => Some(self.encode_predicates(def_id)),
12331241
_ => None,
12341242
},
12351243

@@ -1239,7 +1247,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12391247
// hack. (No reason not to expand it in the future if
12401248
// necessary.)
12411249
predicates_defined_on: match item.node {
1242-
hir::ItemKind::Trait(..) => Some(self.encode_predicates_defined_on(def_id)),
1250+
hir::ItemKind::Trait(..) |
1251+
hir::ItemKind::TraitAlias(..) => Some(self.encode_predicates_defined_on(def_id)),
12431252
_ => None, // not *wrong* for other kinds of items, but not needed
12441253
},
12451254

src/librustc_metadata/schema.rs

+13
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ pub enum EntryKind<'tcx> {
316316
AssociatedType(AssociatedContainer),
317317
AssociatedExistential(AssociatedContainer),
318318
AssociatedConst(AssociatedContainer, ConstQualif, Lazy<RenderedConst>),
319+
TraitAlias(Lazy<TraitAliasData<'tcx>>),
319320
}
320321

321322
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for EntryKind<'gcx> {
@@ -370,6 +371,9 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for EntryKind<'gcx> {
370371
EntryKind::Trait(ref trait_data) => {
371372
trait_data.hash_stable(hcx, hasher);
372373
}
374+
EntryKind::TraitAlias(ref trait_alias_data) => {
375+
trait_alias_data.hash_stable(hcx, hasher);
376+
}
373377
EntryKind::Impl(ref impl_data) => {
374378
impl_data.hash_stable(hcx, hasher);
375379
}
@@ -474,6 +478,15 @@ impl_stable_hash_for!(struct TraitData<'tcx> {
474478
super_predicates
475479
});
476480

481+
#[derive(RustcEncodable, RustcDecodable)]
482+
pub struct TraitAliasData<'tcx> {
483+
pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
484+
}
485+
486+
impl_stable_hash_for!(struct TraitAliasData<'tcx> {
487+
super_predicates
488+
});
489+
477490
#[derive(RustcEncodable, RustcDecodable)]
478491
pub struct ImplData<'tcx> {
479492
pub polarity: hir::ImplPolarity,

src/librustc_resolve/build_reduced_graph.rs

+3
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ impl<'a> Resolver<'a> {
673673
}
674674
module.populated.set(true);
675675
}
676+
Def::TraitAlias(..) => {
677+
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
678+
}
676679
Def::Struct(..) | Def::Union(..) => {
677680
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
678681

src/librustc_traits/lowering/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ crate fn program_clauses_for<'a, 'tcx>(
158158
def_id: DefId,
159159
) -> Clauses<'tcx> {
160160
match tcx.def_key(def_id).disambiguated_data.data {
161-
DefPathData::Trait(_) => program_clauses_for_trait(tcx, def_id),
161+
DefPathData::Trait(_) |
162+
DefPathData::TraitAlias(_) => program_clauses_for_trait(tcx, def_id),
162163
DefPathData::Impl => program_clauses_for_impl(tcx, def_id),
163164
DefPathData::AssocTypeInImpl(..) => program_clauses_for_associated_type_value(tcx, def_id),
164165
DefPathData::AssocTypeInTrait(..) => program_clauses_for_associated_type_def(tcx, def_id),

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ fn super_predicates_of<'a, 'tcx>(
706706
// In the case of trait aliases, however, we include all bounds in the where clause,
707707
// so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
708708
// as one of its "superpredicates".
709-
let is_trait_alias = ty::is_trait_alias(tcx, trait_def_id);
709+
let is_trait_alias = tcx.is_trait_alias(trait_def_id);
710710
let superbounds2 = icx.type_parameter_bounds_in_generics(
711711
generics, item.id, self_param_ty, OnlySelfBounds(!is_trait_alias));
712712

0 commit comments

Comments
 (0)