Skip to content

Commit

Permalink
Collect relevant item bounds from trait clauses for nested rigid proj…
Browse files Browse the repository at this point in the history
…ections
  • Loading branch information
compiler-errors committed Feb 7, 2024
1 parent 0af2783 commit fa551a1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
21 changes: 16 additions & 5 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,22 @@ fn associated_type_bounds<'tcx>(
let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id);

let bounds_from_parent = trait_predicates.predicates.iter().copied().filter(|(pred, _)| {
match pred.kind().skip_binder() {
ty::ClauseKind::Trait(tr) => tr.self_ty() == item_ty,
ty::ClauseKind::Projection(proj) => proj.projection_ty.self_ty() == item_ty,
ty::ClauseKind::TypeOutlives(outlives) => outlives.0 == item_ty,
_ => false,
let mut clause_ty = match pred.kind().skip_binder() {
ty::ClauseKind::Trait(tr) => tr.self_ty(),
ty::ClauseKind::Projection(proj) => proj.projection_ty.self_ty(),
ty::ClauseKind::TypeOutlives(outlives) => outlives.0,
_ => return false,
};

loop {
if clause_ty == item_ty {
return true;
} else if let ty::Alias(ty::Projection, alias_ty) = *clause_ty.kind() {
clause_ty = alias_ty.self_ty();
continue;
}

return false;
}
});

Expand Down
28 changes: 28 additions & 0 deletions tests/ui/associated-types/imply-relevant-nested-item-bounds-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// check-pass
// revisions: current next
//[next] compile-flags: -Znext-solver

trait Trait
where
Self::Assoc: Clone,
{
type Assoc;
}

fn foo<T: Trait>(x: &T::Assoc) -> T::Assoc {
x.clone()
}

trait Trait2
where
Self::Assoc: Iterator,
<Self::Assoc as Iterator>::Item: Clone,
{
type Assoc;
}

fn foo2<T: Trait2>(x: &<T::Assoc as Iterator>::Item) -> <T::Assoc as Iterator>::Item {
x.clone()
}

fn main() {}
23 changes: 23 additions & 0 deletions tests/ui/associated-types/imply-relevant-nested-item-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// check-pass
// revisions: current next
//[next] compile-flags: -Znext-solver

trait Foo
where
Self::Iterator: Iterator,
<Self::Iterator as Iterator>::Item: Bar,
{
type Iterator;

fn iter() -> Self::Iterator;
}

trait Bar {
fn bar(&self);
}

fn x<T: Foo>() {
T::iter().next().unwrap().bar();
}

fn main() {}

0 comments on commit fa551a1

Please sign in to comment.