Skip to content

Commit 99db273

Browse files
committed
Auto merge of #134504 - oli-obk:push-rltsvnyttwll, r=compiler-errors
Use trait definition cycle detection for trait alias definitions, too fixes #133901 In general doing this for `All` is not right, but this code path is specifically for traits and trait aliases, and there we only ever use `All` for trait aliases.
2 parents 8a6878a + 37f2998 commit 99db273

File tree

6 files changed

+72
-8
lines changed

6 files changed

+72
-8
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
653653
}
654654
}
655655
}
656-
PredicateFilter::SelfAndAssociatedTypeBounds => {
656+
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
657657
for &(pred, span) in implied_bounds {
658658
debug!("superbound: {:?}", pred);
659659
if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()

compiler/rustc_middle/src/ty/sty.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<'tcx> Ty<'tcx> {
401401
/// The more specific methods will often optimize their creation.
402402
#[allow(rustc::usage_of_ty_tykind)]
403403
#[inline]
404-
pub fn new(tcx: TyCtxt<'tcx>, st: TyKind<'tcx>) -> Ty<'tcx> {
404+
fn new(tcx: TyCtxt<'tcx>, st: TyKind<'tcx>) -> Ty<'tcx> {
405405
tcx.mk_ty_from_kind(st)
406406
}
407407

@@ -613,6 +613,41 @@ impl<'tcx> Ty<'tcx> {
613613
#[inline]
614614
pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
615615
tcx.debug_assert_args_compatible(def.did(), args);
616+
if cfg!(debug_assertions) {
617+
match tcx.def_kind(def.did()) {
618+
DefKind::Struct | DefKind::Union | DefKind::Enum => {}
619+
DefKind::Mod
620+
| DefKind::Variant
621+
| DefKind::Trait
622+
| DefKind::TyAlias
623+
| DefKind::ForeignTy
624+
| DefKind::TraitAlias
625+
| DefKind::AssocTy
626+
| DefKind::TyParam
627+
| DefKind::Fn
628+
| DefKind::Const
629+
| DefKind::ConstParam
630+
| DefKind::Static { .. }
631+
| DefKind::Ctor(..)
632+
| DefKind::AssocFn
633+
| DefKind::AssocConst
634+
| DefKind::Macro(..)
635+
| DefKind::ExternCrate
636+
| DefKind::Use
637+
| DefKind::ForeignMod
638+
| DefKind::AnonConst
639+
| DefKind::InlineConst
640+
| DefKind::OpaqueTy
641+
| DefKind::Field
642+
| DefKind::LifetimeParam
643+
| DefKind::GlobalAsm
644+
| DefKind::Impl { .. }
645+
| DefKind::Closure
646+
| DefKind::SyntheticCoroutineBody => {
647+
bug!("not an adt: {def:?} ({:?})", tcx.def_kind(def.did()))
648+
}
649+
}
650+
}
616651
Ty::new(tcx, Adt(def, args))
617652
}
618653

@@ -772,7 +807,7 @@ impl<'tcx> Ty<'tcx> {
772807
}
773808
}
774809
});
775-
Ty::new(tcx, Adt(adt_def, args))
810+
Ty::new_adt(tcx, adt_def, args)
776811
}
777812

778813
#[inline]

tests/ui/infinite/infinite-trait-alias-recursion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(trait_alias)]
22

33
trait T1 = T2;
4-
//~^ ERROR cycle detected when computing the super predicates of `T1`
4+
//~^ ERROR cycle detected when computing the implied predicates of `T1`
55

66
trait T2 = T3;
77

tests/ui/infinite/infinite-trait-alias-recursion.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error[E0391]: cycle detected when computing the super predicates of `T1`
1+
error[E0391]: cycle detected when computing the implied predicates of `T1`
22
--> $DIR/infinite-trait-alias-recursion.rs:3:12
33
|
44
LL | trait T1 = T2;
55
| ^^
66
|
7-
note: ...which requires computing the super predicates of `T2`...
7+
note: ...which requires computing the implied predicates of `T2`...
88
--> $DIR/infinite-trait-alias-recursion.rs:6:12
99
|
1010
LL | trait T2 = T3;
1111
| ^^
12-
note: ...which requires computing the super predicates of `T3`...
12+
note: ...which requires computing the implied predicates of `T3`...
1313
--> $DIR/infinite-trait-alias-recursion.rs:8:12
1414
|
1515
LL | trait T3 = T1 + T3;
1616
| ^^
17-
= note: ...which again requires computing the super predicates of `T1`, completing the cycle
17+
= note: ...which again requires computing the implied predicates of `T1`, completing the cycle
1818
= note: trait aliases cannot be recursive
1919
note: cycle used when checking that `T1` is well-formed
2020
--> $DIR/infinite-trait-alias-recursion.rs:3:1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! This test used to get stuck in an infinite
2+
//! recursion during normalization.
3+
//!
4+
//! issue: https://github.com/rust-lang/rust/issues/133901
5+
6+
#![feature(trait_alias)]
7+
fn foo<T: Baz<i32>>() {}
8+
trait Baz<A> = Baz<Option<A>>;
9+
//~^ ERROR: cycle detected when computing the implied predicates of `Baz`
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0391]: cycle detected when computing the implied predicates of `Baz`
2+
--> $DIR/infinite_normalization.rs:8:16
3+
|
4+
LL | trait Baz<A> = Baz<Option<A>>;
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: ...which immediately requires computing the implied predicates of `Baz` again
8+
= note: trait aliases cannot be recursive
9+
note: cycle used when computing normalized predicates of `foo`
10+
--> $DIR/infinite_normalization.rs:7:1
11+
|
12+
LL | fn foo<T: Baz<i32>>() {}
13+
| ^^^^^^^^^^^^^^^^^^^^^
14+
= 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
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)