Skip to content

Commit 0077d12

Browse files
committed
Auto merge of #45047 - durka:trait-alias, r=petrochenkov
trait alias infrastructure This will be an implementation of trait aliases (RFC 1733, #41517). Progress so far: - [x] Feature gate - [x] Add to parser - [x] `where` clauses - [x] prohibit LHS type parameter bounds via AST validation #45047 (comment) - [x] Add to AST and HIR - [x] make a separate PathSource for trait alias contexts #45047 (comment) - [x] Stub out enough of typeck and resolve to just barely not ICE Postponed: - [ ] Actually implement the alias part - [ ] #21903 - [ ] #24010 I need some pointers on where to start with that last one. The test currently does this: ``` error[E0283]: type annotations required: cannot resolve `_: CD` --> src/test/run-pass/trait-alias.rs:34:16 | 34 | let both = foo(); | ^^^ | = note: required by `foo` ```
2 parents 3fc7f85 + 834674f commit 0077d12

File tree

35 files changed

+323
-35
lines changed

35 files changed

+323
-35
lines changed

src/librustc/hir/def.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub enum Def {
3737
Trait(DefId),
3838
TyAlias(DefId),
3939
TyForeign(DefId),
40+
TraitAlias(DefId),
4041
AssociatedTy(DefId),
4142
PrimTy(hir::PrimTy),
4243
TyParam(DefId),
@@ -155,7 +156,8 @@ impl Def {
155156
pub fn def_id(&self) -> DefId {
156157
match *self {
157158
Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
158-
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
159+
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) |
160+
Def::TyAlias(id) | Def::TraitAlias(id) |
159161
Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
160162
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
161163
Def::AssociatedConst(id) | Def::Macro(id, ..) |
@@ -186,6 +188,7 @@ impl Def {
186188
Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
187189
Def::Enum(..) => "enum",
188190
Def::TyAlias(..) => "type alias",
191+
Def::TraitAlias(..) => "trait alias",
189192
Def::AssociatedTy(..) => "associated type",
190193
Def::Struct(..) => "struct",
191194
Def::StructCtor(.., CtorKind::Fn) => "tuple struct",

src/librustc/hir/intravisit.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
526526
walk_list!(visitor, visit_ty_param_bound, bounds);
527527
walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
528528
}
529+
ItemTraitAlias(ref generics, ref bounds) => {
530+
visitor.visit_id(item.id);
531+
visitor.visit_generics(generics);
532+
walk_list!(visitor, visit_ty_param_bound, bounds);
533+
}
529534
}
530535
walk_list!(visitor, visit_attribute, &item.attrs);
531536
}

src/librustc/hir/lowering.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,9 +1924,11 @@ impl<'a> LoweringContext<'a> {
19241924
bounds,
19251925
items)
19261926
}
1927-
ItemKind::MacroDef(..) | ItemKind::Mac(..) => {
1928-
panic!("Shouldn't still be around")
1927+
ItemKind::TraitAlias(ref generics, ref bounds) => {
1928+
hir::ItemTraitAlias(self.lower_generics(generics),
1929+
self.lower_bounds(bounds, ImplTraitContext::Disallowed))
19291930
}
1931+
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
19301932
}
19311933

19321934
// [1] `defaultness.has_value()` is never called for an `impl`, always `true` in order to

src/librustc/hir/map/def_collector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
106106
let def_data = match i.node {
107107
ItemKind::AutoImpl(..) | ItemKind::Impl(..) =>
108108
DefPathData::Impl,
109-
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) | ItemKind::Trait(..) |
109+
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
110+
ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
110111
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>
111112
DefPathData::TypeNs(i.ident.name.as_str()),
112113
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
11851185
ItemStruct(..) => "struct",
11861186
ItemUnion(..) => "union",
11871187
ItemTrait(..) => "trait",
1188+
ItemTraitAlias(..) => "trait alias",
11881189
ItemImpl(..) => "impl",
11891190
ItemAutoImpl(..) => "default impl",
11901191
};

src/librustc/hir/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,8 @@ pub enum Item_ {
18881888
ItemUnion(VariantData, Generics),
18891889
/// Represents a Trait Declaration
18901890
ItemTrait(IsAuto, Unsafety, Generics, TyParamBounds, HirVec<TraitItemRef>),
1891+
/// Represents a Trait Alias Declaration
1892+
ItemTraitAlias(Generics, TyParamBounds),
18911893

18921894
/// Auto trait implementations
18931895
///
@@ -1919,6 +1921,7 @@ impl Item_ {
19191921
ItemStruct(..) => "struct",
19201922
ItemUnion(..) => "union",
19211923
ItemTrait(..) => "trait",
1924+
ItemTraitAlias(..) => "trait alias",
19221925
ItemImpl(..) |
19231926
ItemAutoImpl(..) => "item",
19241927
}

src/librustc/hir/print.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,27 @@ impl<'a> State<'a> {
747747
}
748748
self.bclose(item.span)?;
749749
}
750+
hir::ItemTraitAlias(ref generics, ref bounds) => {
751+
self.head("")?;
752+
self.print_visibility(&item.vis)?;
753+
self.word_nbsp("trait")?;
754+
self.print_name(item.name)?;
755+
self.print_generics(generics)?;
756+
let mut real_bounds = Vec::with_capacity(bounds.len());
757+
// FIXME(durka) this seems to be some quite outdated syntax
758+
for b in bounds.iter() {
759+
if let TraitTyParamBound(ref ptr, hir::TraitBoundModifier::Maybe) = *b {
760+
self.s.space()?;
761+
self.word_space("for ?")?;
762+
self.print_trait_ref(&ptr.trait_ref)?;
763+
} else {
764+
real_bounds.push(b.clone());
765+
}
766+
}
767+
self.print_bounds(" = ", &real_bounds[..])?;
768+
self.print_where_clause(&generics.where_clause)?;
769+
self.s.word(";")?;
770+
}
750771
}
751772
self.ann.post(self, NodeItem(item))
752773
}

src/librustc/ich/impls_hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ impl_stable_hash_for!(enum hir::Item_ {
848848
ItemStruct(variant_data, generics),
849849
ItemUnion(variant_data, generics),
850850
ItemTrait(is_auto, unsafety, generics, bounds, item_refs),
851+
ItemTraitAlias(generics, bounds),
851852
ItemAutoImpl(unsafety, trait_ref),
852853
ItemImpl(unsafety, impl_polarity, impl_defaultness, generics, trait_ref, ty, impl_item_refs)
853854
});
@@ -1004,6 +1005,7 @@ impl_stable_hash_for!(enum hir::def::Def {
10041005
Variant(def_id),
10051006
Trait(def_id),
10061007
TyAlias(def_id),
1008+
TraitAlias(def_id),
10071009
AssociatedTy(def_id),
10081010
PrimTy(prim_ty),
10091011
TyParam(def_id),

src/librustc/middle/reachable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
268268
hir::ItemExternCrate(_) | hir::ItemUse(..) |
269269
hir::ItemTy(..) | hir::ItemStatic(..) |
270270
hir::ItemMod(..) | hir::ItemForeignMod(..) |
271-
hir::ItemImpl(..) | hir::ItemTrait(..) |
271+
hir::ItemImpl(..) | hir::ItemTrait(..) | hir::ItemTraitAlias(..) |
272272
hir::ItemStruct(..) | hir::ItemEnum(..) |
273273
hir::ItemUnion(..) | hir::ItemAutoImpl(..) |
274274
hir::ItemGlobalAsm(..) => {}

src/librustc/middle/resolve_lifetime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
469469
| hir::ItemStruct(_, ref generics)
470470
| hir::ItemUnion(_, ref generics)
471471
| hir::ItemTrait(_, _, ref generics, ..)
472+
| hir::ItemTraitAlias(ref generics, ..)
472473
| hir::ItemImpl(_, _, _, ref generics, ..) => {
473474
// These kinds of items have only early bound lifetime parameters.
474475
let mut index = if let hir::ItemTrait(..) = item.node {

0 commit comments

Comments
 (0)