Skip to content

Commit 58a1b5a

Browse files
author
Alexander Regueiro
committed
Added support for trait aliases as bounds.
1 parent 451987d commit 58a1b5a

File tree

12 files changed

+248
-82
lines changed

12 files changed

+248
-82
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,7 @@ impl<'hir> Map<'hir> {
301301
ItemKind::Struct(..) => Some(Def::Struct(def_id())),
302302
ItemKind::Union(..) => Some(Def::Union(def_id())),
303303
ItemKind::Trait(..) => Some(Def::Trait(def_id())),
304-
ItemKind::TraitAlias(..) => {
305-
bug!("trait aliases are not yet implemented (see issue #41517)")
306-
},
304+
ItemKind::TraitAlias(..) => Some(Def::TraitAlias(def_id())),
307305
ItemKind::ExternCrate(_) |
308306
ItemKind::Use(..) |
309307
ItemKind::ForeignMod(..) |

src/librustc/ich/impls_ty.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@ for traits::Vtable<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
11191119
&VtableClosure(ref table_closure) => table_closure.hash_stable(hcx, hasher),
11201120
&VtableFnPointer(ref table_fn_pointer) => table_fn_pointer.hash_stable(hcx, hasher),
11211121
&VtableGenerator(ref table_generator) => table_generator.hash_stable(hcx, hasher),
1122+
&VtableTraitAlias(ref table_alias) => table_alias.hash_stable(hcx, hasher),
11221123
}
11231124
}
11241125
}
@@ -1227,6 +1228,22 @@ for traits::VtableGeneratorData<'gcx, N> where N: HashStable<StableHashingContex
12271228
}
12281229
}
12291230

1231+
impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
1232+
for traits::VtableTraitAliasData<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
1233+
fn hash_stable<W: StableHasherResult>(&self,
1234+
hcx: &mut StableHashingContext<'a>,
1235+
hasher: &mut StableHasher<W>) {
1236+
let traits::VtableTraitAliasData {
1237+
alias_def_id,
1238+
substs,
1239+
ref nested,
1240+
} = *self;
1241+
alias_def_id.hash_stable(hcx, hasher);
1242+
substs.hash_stable(hcx, hasher);
1243+
nested.hash_stable(hcx, hasher);
1244+
}
1245+
}
1246+
12301247
impl_stable_hash_for!(
12311248
impl<'tcx, V> for struct infer::canonical::Canonical<'tcx, V> {
12321249
variables, value

src/librustc/traits/mod.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,11 @@ pub enum Vtable<'tcx, N> {
534534
/// Same as above, but for a fn pointer type with the given signature.
535535
VtableFnPointer(VtableFnPointerData<'tcx, N>),
536536

537-
/// Vtable automatically generated for a generator
537+
/// Vtable automatically generated for a generator.
538538
VtableGenerator(VtableGeneratorData<'tcx, N>),
539+
540+
/// Vtable for a trait alias.
541+
VtableTraitAlias(VtableTraitAliasData<'tcx, N>),
539542
}
540543

541544
/// Identifies a particular impl in the source, along with a set of
@@ -605,6 +608,13 @@ pub struct VtableFnPointerData<'tcx, N> {
605608
pub nested: Vec<N>
606609
}
607610

611+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)]
612+
pub struct VtableTraitAliasData<'tcx, N> {
613+
pub alias_def_id: DefId,
614+
pub substs: &'tcx Substs<'tcx>,
615+
pub nested: Vec<N>,
616+
}
617+
608618
/// Creates predicate obligations from the generic bounds.
609619
pub fn predicates_for_generics<'tcx>(cause: ObligationCause<'tcx>,
610620
param_env: ty::ParamEnv<'tcx>,
@@ -1067,6 +1077,7 @@ impl<'tcx, N> Vtable<'tcx, N> {
10671077
VtableGenerator(c) => c.nested,
10681078
VtableObject(d) => d.nested,
10691079
VtableFnPointer(d) => d.nested,
1080+
VtableTraitAlias(d) => d.nested,
10701081
}
10711082
}
10721083

@@ -1090,20 +1101,25 @@ impl<'tcx, N> Vtable<'tcx, N> {
10901101
trait_def_id: d.trait_def_id,
10911102
nested: d.nested.into_iter().map(f).collect(),
10921103
}),
1093-
VtableFnPointer(p) => VtableFnPointer(VtableFnPointerData {
1094-
fn_ty: p.fn_ty,
1095-
nested: p.nested.into_iter().map(f).collect(),
1104+
VtableClosure(c) => VtableClosure(VtableClosureData {
1105+
closure_def_id: c.closure_def_id,
1106+
substs: c.substs,
1107+
nested: c.nested.into_iter().map(f).collect(),
10961108
}),
10971109
VtableGenerator(c) => VtableGenerator(VtableGeneratorData {
10981110
generator_def_id: c.generator_def_id,
10991111
substs: c.substs,
11001112
nested: c.nested.into_iter().map(f).collect(),
11011113
}),
1102-
VtableClosure(c) => VtableClosure(VtableClosureData {
1103-
closure_def_id: c.closure_def_id,
1104-
substs: c.substs,
1105-
nested: c.nested.into_iter().map(f).collect(),
1106-
})
1114+
VtableFnPointer(p) => VtableFnPointer(VtableFnPointerData {
1115+
fn_ty: p.fn_ty,
1116+
nested: p.nested.into_iter().map(f).collect(),
1117+
}),
1118+
VtableTraitAlias(d) => VtableTraitAlias(VtableTraitAliasData {
1119+
alias_def_id: d.alias_def_id,
1120+
substs: d.substs,
1121+
nested: d.nested.into_iter().map(f).collect(),
1122+
}),
11071123
}
11081124
}
11091125
}

src/librustc/traits/project.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ use super::PredicateObligation;
1919
use super::Selection;
2020
use super::SelectionContext;
2121
use super::SelectionError;
22-
use super::VtableClosureData;
23-
use super::VtableGeneratorData;
24-
use super::VtableFnPointerData;
25-
use super::VtableImplData;
22+
use super::{VtableImplData, VtableClosureData, VtableGeneratorData, VtableFnPointerData};
2623
use super::util;
2724

2825
use hir::def_id::DefId;
@@ -1073,7 +1070,8 @@ fn assemble_candidates_from_impls<'cx, 'gcx, 'tcx>(
10731070
super::VtableClosure(_) |
10741071
super::VtableGenerator(_) |
10751072
super::VtableFnPointer(_) |
1076-
super::VtableObject(_) => {
1073+
super::VtableObject(_) |
1074+
super::VtableTraitAlias(_) => {
10771075
debug!("assemble_candidates_from_impls: vtable={:?}",
10781076
vtable);
10791077
true
@@ -1235,7 +1233,8 @@ fn confirm_select_candidate<'cx, 'gcx, 'tcx>(
12351233
confirm_object_candidate(selcx, obligation, obligation_trait_ref),
12361234
super::VtableAutoImpl(..) |
12371235
super::VtableParam(..) |
1238-
super::VtableBuiltin(..) =>
1236+
super::VtableBuiltin(..) |
1237+
super::VtableTraitAlias(..) =>
12391238
// we don't create Select candidates with this kind of resolution
12401239
span_bug!(
12411240
obligation.cause.span,
@@ -1486,7 +1485,7 @@ fn confirm_impl_candidate<'cx, 'gcx, 'tcx>(
14861485
impl_vtable: VtableImplData<'tcx, PredicateObligation<'tcx>>)
14871486
-> Progress<'tcx>
14881487
{
1489-
let VtableImplData { substs, nested, impl_def_id } = impl_vtable;
1488+
let VtableImplData { impl_def_id, substs, nested } = impl_vtable;
14901489

14911490
let tcx = selcx.tcx();
14921491
let param_env = obligation.param_env;

0 commit comments

Comments
 (0)