1
Fork 0

Note that trait aliases cannot be recursive

This commit is contained in:
Noah Lev 2021-08-21 14:19:59 -07:00
parent bf360dc272
commit c861964735
3 changed files with 21 additions and 9 deletions

View file

@ -178,6 +178,10 @@ impl Key for (DefId, Option<Ident>) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span { fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.0) tcx.def_span(self.0)
} }
#[inline(always)]
fn key_as_def_id(&self) -> Option<DefId> {
Some(self.0)
}
} }
impl Key for (DefId, LocalDefId, Ident) { impl Key for (DefId, LocalDefId, Ident) {

View file

@ -600,16 +600,23 @@ pub(crate) fn report_cycle<'a>(
)); ));
} }
if !stack.is_empty() if stack.iter().all(|entry| {
&& stack.iter().all(|entry| { entry.query.def_kind.map_or(false, |def_kind| {
entry.query.def_kind.map_or(false, |def_kind| { matches!(def_kind, SimpleDefKind::TyAlias | SimpleDefKind::TraitAlias)
matches!(def_kind, SimpleDefKind::TyAlias | SimpleDefKind::TraitAlias)
})
}) })
{ }) {
err.note("type aliases cannot be recursive"); if stack.iter().all(|entry| {
err.help("consider using a struct, enum, or union instead to break the cycle"); entry
err.help("see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information"); .query
.def_kind
.map_or(false, |def_kind| matches!(def_kind, SimpleDefKind::TyAlias))
}) {
err.note("type aliases cannot be recursive");
err.help("consider using a struct, enum, or union instead to break the cycle");
err.help("see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information");
} else {
err.note("trait aliases cannot be recursive");
}
} }
if let Some((span, query)) = usage { if let Some((span, query)) = usage {

View file

@ -30,6 +30,7 @@ note: ...which requires computing the super traits of `T3`...
LL | trait T3 = T1 + T3; LL | trait T3 = T1 + T3;
| ^^ | ^^
= note: ...which again requires computing the super predicates of `T1`, completing the cycle = note: ...which again requires computing the super predicates of `T1`, completing the cycle
= note: trait aliases cannot be recursive
note: cycle used when collecting item types in top-level module note: cycle used when collecting item types in top-level module
--> $DIR/infinite-trait-alias-recursion.rs:3:1 --> $DIR/infinite-trait-alias-recursion.rs:3:1
| |