Allow to self reference associated types in where clauses
This commit is contained in:
parent
24dcf6f7a2
commit
2ca4964db5
8 changed files with 205 additions and 64 deletions
|
@ -6,6 +6,7 @@ mod errors;
|
|||
mod generics;
|
||||
|
||||
use crate::bounds::Bounds;
|
||||
use crate::collect::super_traits_of;
|
||||
use crate::collect::PlaceholderHirTyCollector;
|
||||
use crate::errors::{
|
||||
AmbiguousLifetimeBound, MultipleRelaxedDefaultBounds, TraitObjectDeclaredWithNoTraits,
|
||||
|
@ -768,7 +769,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
}
|
||||
|
||||
// Returns `true` if a bounds list includes `?Sized`.
|
||||
pub fn is_unsized(&self, ast_bounds: &[hir::GenericBound<'_>], span: Span) -> bool {
|
||||
pub fn is_unsized(&self, ast_bounds: &[&hir::GenericBound<'_>], span: Span) -> bool {
|
||||
let tcx = self.tcx();
|
||||
|
||||
// Try to find an unbound in bounds.
|
||||
|
@ -826,7 +827,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
fn add_bounds(
|
||||
&self,
|
||||
param_ty: Ty<'tcx>,
|
||||
ast_bounds: &[hir::GenericBound<'_>],
|
||||
ast_bounds: &[&hir::GenericBound<'_>],
|
||||
bounds: &mut Bounds<'tcx>,
|
||||
) {
|
||||
let mut trait_bounds = Vec::new();
|
||||
|
@ -844,7 +845,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
hir::GenericBound::Trait(_, hir::TraitBoundModifier::Maybe) => {}
|
||||
hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => self
|
||||
.instantiate_lang_item_trait_ref(
|
||||
lang_item, span, hir_id, args, param_ty, bounds,
|
||||
*lang_item, *span, *hir_id, args, param_ty, bounds,
|
||||
),
|
||||
hir::GenericBound::Outlives(ref l) => region_bounds.push(l),
|
||||
}
|
||||
|
@ -878,7 +879,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
pub fn compute_bounds(
|
||||
&self,
|
||||
param_ty: Ty<'tcx>,
|
||||
ast_bounds: &[hir::GenericBound<'_>],
|
||||
ast_bounds: &[&hir::GenericBound<'_>],
|
||||
sized_by_default: SizedByDefault,
|
||||
span: Span,
|
||||
) -> Bounds<'tcx> {
|
||||
|
@ -896,6 +897,39 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
bounds
|
||||
}
|
||||
|
||||
pub fn compute_bounds_that_match_assoc_type(
|
||||
&self,
|
||||
param_ty: Ty<'tcx>,
|
||||
ast_bounds: &[hir::GenericBound<'_>],
|
||||
sized_by_default: SizedByDefault,
|
||||
span: Span,
|
||||
assoc_name: Ident,
|
||||
) -> Bounds<'tcx> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
for ast_bound in ast_bounds {
|
||||
if let Some(trait_ref) = ast_bound.trait_ref() {
|
||||
if let Some(trait_did) = trait_ref.trait_def_id() {
|
||||
if super_traits_of(self.tcx(), trait_did).any(|trait_did| {
|
||||
self.tcx()
|
||||
.associated_items(trait_did)
|
||||
.find_by_name_and_kind(
|
||||
self.tcx(),
|
||||
assoc_name,
|
||||
ty::AssocKind::Type,
|
||||
trait_did,
|
||||
)
|
||||
.is_some()
|
||||
}) {
|
||||
result.push(ast_bound);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.compute_bounds(param_ty, &result, sized_by_default, span)
|
||||
}
|
||||
|
||||
/// Given an HIR binding like `Item = Foo` or `Item: Foo`, pushes the corresponding predicates
|
||||
/// onto `bounds`.
|
||||
///
|
||||
|
@ -1050,7 +1084,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
|
||||
// parameter to have a skipped binder.
|
||||
let param_ty = tcx.mk_projection(assoc_ty.def_id, candidate.skip_binder().substs);
|
||||
self.add_bounds(param_ty, ast_bounds, bounds);
|
||||
let ast_bounds: Vec<_> = ast_bounds.iter().collect();
|
||||
self.add_bounds(param_ty, &ast_bounds, bounds);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
@ -1377,12 +1412,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
let param_name = tcx.hir().ty_param_name(param_hir_id);
|
||||
self.one_bound_for_assoc_type(
|
||||
|| {
|
||||
traits::transitive_bounds(
|
||||
traits::transitive_bounds_that_define_assoc_type(
|
||||
tcx,
|
||||
predicates.iter().filter_map(|(p, _)| {
|
||||
p.to_opt_poly_trait_ref().map(|trait_ref| trait_ref.value)
|
||||
}),
|
||||
assoc_name,
|
||||
)
|
||||
.into_iter()
|
||||
},
|
||||
|| param_name.to_string(),
|
||||
assoc_name,
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
// ignore-tidy-filelength
|
||||
//! "Collection" is the process of determining the type and other external
|
||||
//! details of each item in Rust. Collection is specifically concerned
|
||||
//! with *inter-procedural* things -- for example, for a function
|
||||
|
@ -79,6 +80,7 @@ pub fn provide(providers: &mut Providers) {
|
|||
projection_ty_from_predicates,
|
||||
explicit_predicates_of,
|
||||
super_predicates_of,
|
||||
super_predicates_that_define_assoc_type,
|
||||
trait_explicit_predicates_and_bounds,
|
||||
type_param_predicates,
|
||||
trait_def,
|
||||
|
@ -651,17 +653,10 @@ impl ItemCtxt<'tcx> {
|
|||
hir::GenericBound::Trait(poly_trait_ref, _) => {
|
||||
let trait_ref = &poly_trait_ref.trait_ref;
|
||||
let trait_did = trait_ref.trait_def_id().unwrap();
|
||||
let traits_did = super_traits_of(self.tcx, trait_did);
|
||||
|
||||
traits_did.iter().any(|trait_did| {
|
||||
super_traits_of(self.tcx, trait_did).any(|trait_did| {
|
||||
self.tcx
|
||||
.associated_items(*trait_did)
|
||||
.find_by_name_and_kind(
|
||||
self.tcx,
|
||||
assoc_name,
|
||||
ty::AssocKind::Type,
|
||||
*trait_did,
|
||||
)
|
||||
.associated_items(trait_did)
|
||||
.find_by_name_and_kind(self.tcx, assoc_name, ty::AssocKind::Type, trait_did)
|
||||
.is_some()
|
||||
})
|
||||
}
|
||||
|
@ -1035,55 +1030,91 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef {
|
|||
/// the transitive super-predicates are converted.
|
||||
fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredicates<'_> {
|
||||
debug!("super_predicates(trait_def_id={:?})", trait_def_id);
|
||||
let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local());
|
||||
tcx.super_predicates_that_define_assoc_type((trait_def_id, None))
|
||||
}
|
||||
|
||||
let item = match tcx.hir().get(trait_hir_id) {
|
||||
Node::Item(item) => item,
|
||||
_ => bug!("trait_node_id {} is not an item", trait_hir_id),
|
||||
};
|
||||
|
||||
let (generics, bounds) = match item.kind {
|
||||
hir::ItemKind::Trait(.., ref generics, ref supertraits, _) => (generics, supertraits),
|
||||
hir::ItemKind::TraitAlias(ref generics, ref supertraits) => (generics, supertraits),
|
||||
_ => span_bug!(item.span, "super_predicates invoked on non-trait"),
|
||||
};
|
||||
|
||||
let icx = ItemCtxt::new(tcx, trait_def_id);
|
||||
|
||||
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
|
||||
let self_param_ty = tcx.types.self_param;
|
||||
let superbounds1 =
|
||||
AstConv::compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No, item.span);
|
||||
|
||||
let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
|
||||
|
||||
// Convert any explicit superbounds in the where-clause,
|
||||
// e.g., `trait Foo where Self: Bar`.
|
||||
// In the case of trait aliases, however, we include all bounds in the where-clause,
|
||||
// so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
|
||||
// as one of its "superpredicates".
|
||||
let is_trait_alias = tcx.is_trait_alias(trait_def_id);
|
||||
let superbounds2 = icx.type_parameter_bounds_in_generics(
|
||||
generics,
|
||||
item.hir_id,
|
||||
self_param_ty,
|
||||
OnlySelfBounds(!is_trait_alias),
|
||||
None,
|
||||
/// Ensures that the super-predicates of the trait with a `DefId`
|
||||
/// of `trait_def_id` are converted and stored. This also ensures that
|
||||
/// the transitive super-predicates are converted.
|
||||
fn super_predicates_that_define_assoc_type(
|
||||
tcx: TyCtxt<'_>,
|
||||
(trait_def_id, assoc_name): (DefId, Option<Ident>),
|
||||
) -> ty::GenericPredicates<'_> {
|
||||
debug!(
|
||||
"super_predicates_that_define_assoc_type(trait_def_id={:?}, assoc_name={:?})",
|
||||
trait_def_id, assoc_name
|
||||
);
|
||||
if trait_def_id.is_local() {
|
||||
debug!("super_predicates_that_define_assoc_type: local trait_def_id={:?}", trait_def_id);
|
||||
let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local());
|
||||
|
||||
// Combine the two lists to form the complete set of superbounds:
|
||||
let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
|
||||
let item = match tcx.hir().get(trait_hir_id) {
|
||||
Node::Item(item) => item,
|
||||
_ => bug!("trait_node_id {} is not an item", trait_hir_id),
|
||||
};
|
||||
|
||||
// Now require that immediate supertraits are converted,
|
||||
// which will, in turn, reach indirect supertraits.
|
||||
for &(pred, span) in superbounds {
|
||||
debug!("superbound: {:?}", pred);
|
||||
if let ty::PredicateAtom::Trait(bound, _) = pred.skip_binders() {
|
||||
tcx.at(span).super_predicates_of(bound.def_id());
|
||||
let (generics, bounds) = match item.kind {
|
||||
hir::ItemKind::Trait(.., ref generics, ref supertraits, _) => (generics, supertraits),
|
||||
hir::ItemKind::TraitAlias(ref generics, ref supertraits) => (generics, supertraits),
|
||||
_ => span_bug!(item.span, "super_predicates invoked on non-trait"),
|
||||
};
|
||||
|
||||
let icx = ItemCtxt::new(tcx, trait_def_id);
|
||||
|
||||
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
|
||||
let self_param_ty = tcx.types.self_param;
|
||||
let superbounds1 = if let Some(assoc_name) = assoc_name {
|
||||
AstConv::compute_bounds_that_match_assoc_type(
|
||||
&icx,
|
||||
self_param_ty,
|
||||
&bounds,
|
||||
SizedByDefault::No,
|
||||
item.span,
|
||||
assoc_name,
|
||||
)
|
||||
} else {
|
||||
let bounds: Vec<_> = bounds.iter().collect();
|
||||
AstConv::compute_bounds(&icx, self_param_ty, &bounds, SizedByDefault::No, item.span)
|
||||
};
|
||||
|
||||
let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
|
||||
|
||||
// Convert any explicit superbounds in the where-clause,
|
||||
// e.g., `trait Foo where Self: Bar`.
|
||||
// In the case of trait aliases, however, we include all bounds in the where-clause,
|
||||
// so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
|
||||
// as one of its "superpredicates".
|
||||
let is_trait_alias = tcx.is_trait_alias(trait_def_id);
|
||||
let superbounds2 = icx.type_parameter_bounds_in_generics(
|
||||
generics,
|
||||
item.hir_id,
|
||||
self_param_ty,
|
||||
OnlySelfBounds(!is_trait_alias),
|
||||
assoc_name,
|
||||
);
|
||||
|
||||
// Combine the two lists to form the complete set of superbounds:
|
||||
let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
|
||||
|
||||
// Now require that immediate supertraits are converted,
|
||||
// which will, in turn, reach indirect supertraits.
|
||||
if assoc_name.is_none() {
|
||||
// FIXME: move this into the `super_predicates_of` query
|
||||
for &(pred, span) in superbounds {
|
||||
debug!("superbound: {:?}", pred);
|
||||
if let ty::PredicateAtom::Trait(bound, _) = pred.skip_binders() {
|
||||
tcx.at(span).super_predicates_of(bound.def_id());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ty::GenericPredicates { parent: None, predicates: superbounds }
|
||||
ty::GenericPredicates { parent: None, predicates: superbounds }
|
||||
} else {
|
||||
// if `assoc_name` is None, then the query should've been redirected to an
|
||||
// external provider
|
||||
assert!(assoc_name.is_some());
|
||||
tcx.super_predicates_of(trait_def_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn super_traits_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> impl Iterator<Item = DefId> {
|
||||
|
@ -1123,6 +1154,8 @@ pub fn super_traits_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> impl Iterator<It
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
set.into_iter()
|
||||
}
|
||||
|
||||
fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
|
||||
|
@ -1976,8 +2009,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
|
|||
index += 1;
|
||||
|
||||
let sized = SizedByDefault::Yes;
|
||||
let bounds =
|
||||
AstConv::compute_bounds(&icx, param_ty, ¶m.bounds, sized, param.span);
|
||||
let bounds: Vec<_> = param.bounds.iter().collect();
|
||||
let bounds = AstConv::compute_bounds(&icx, param_ty, &bounds, sized, param.span);
|
||||
predicates.extend(bounds.predicates(tcx, param_ty));
|
||||
}
|
||||
GenericParamKind::Const { .. } => {
|
||||
|
|
|
@ -25,10 +25,11 @@ fn associated_type_bounds<'tcx>(
|
|||
InternalSubsts::identity_for_item(tcx, assoc_item_def_id),
|
||||
);
|
||||
|
||||
let bounds: Vec<_> = bounds.iter().collect();
|
||||
let bounds = AstConv::compute_bounds(
|
||||
&ItemCtxt::new(tcx, assoc_item_def_id),
|
||||
item_ty,
|
||||
bounds,
|
||||
&bounds,
|
||||
SizedByDefault::Yes,
|
||||
span,
|
||||
);
|
||||
|
@ -65,10 +66,11 @@ fn opaque_type_bounds<'tcx>(
|
|||
let item_ty =
|
||||
tcx.mk_opaque(opaque_def_id, InternalSubsts::identity_for_item(tcx, opaque_def_id));
|
||||
|
||||
let bounds: Vec<_> = bounds.iter().collect();
|
||||
let bounds = AstConv::compute_bounds(
|
||||
&ItemCtxt::new(tcx, opaque_def_id),
|
||||
item_ty,
|
||||
bounds,
|
||||
&bounds,
|
||||
SizedByDefault::Yes,
|
||||
span,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue