Skip to content

Commit 4d442f5

Browse files
authored
Rollup merge of rust-lang#121409 - compiler-errors:atb-cycle, r=cjgillot
Prevent cycle in implied predicates computation Makes rust-lang#65913 from hang -> fail. I believe fail is the correct state for this test to remain for the long term.
2 parents 86a35c0 + 6edbc8d commit 4d442f5

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -640,16 +640,30 @@ pub(super) fn implied_predicates_with_filter(
640640

641641
// Now require that immediate supertraits are converted, which will, in
642642
// turn, reach indirect supertraits, so we detect cycles now instead of
643-
// overflowing during elaboration.
644-
if matches!(filter, PredicateFilter::SelfOnly) {
645-
for &(pred, span) in implied_bounds {
646-
debug!("superbound: {:?}", pred);
647-
if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
648-
&& bound.polarity == ty::ImplPolarity::Positive
649-
{
650-
tcx.at(span).super_predicates_of(bound.def_id());
643+
// overflowing during elaboration. Same for implied predicates, which
644+
// make sure we walk into associated type bounds.
645+
match filter {
646+
PredicateFilter::SelfOnly => {
647+
for &(pred, span) in implied_bounds {
648+
debug!("superbound: {:?}", pred);
649+
if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
650+
&& bound.polarity == ty::ImplPolarity::Positive
651+
{
652+
tcx.at(span).super_predicates_of(bound.def_id());
653+
}
654+
}
655+
}
656+
PredicateFilter::SelfAndAssociatedTypeBounds => {
657+
for &(pred, span) in implied_bounds {
658+
debug!("superbound: {:?}", pred);
659+
if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
660+
&& bound.polarity == ty::ImplPolarity::Positive
661+
{
662+
tcx.at(span).implied_predicates_of(bound.def_id());
663+
}
651664
}
652665
}
666+
_ => {}
653667
}
654668

655669
ty::GenericPredicates { parent: None, predicates: implied_bounds }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(associated_type_bounds)]
2+
3+
trait A {
4+
type T;
5+
}
6+
7+
trait B: A<T: B> {}
8+
//~^ ERROR cycle detected when computing the implied predicates of `B`
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0391]: cycle detected when computing the implied predicates of `B`
2+
--> $DIR/implied-bounds-cycle.rs:7:15
3+
|
4+
LL | trait B: A<T: B> {}
5+
| ^
6+
|
7+
= note: ...which immediately requires computing the implied predicates of `B` again
8+
note: cycle used when computing normalized predicates of `B`
9+
--> $DIR/implied-bounds-cycle.rs:7:1
10+
|
11+
LL | trait B: A<T: B> {}
12+
| ^^^^^^^^^^^^^^^^
13+
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)