Skip to content

Commit c861964

Browse files
committed
Note that trait aliases cannot be recursive
1 parent bf360dc commit c861964

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

compiler/rustc_query_impl/src/keys.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ impl Key for (DefId, Option<Ident>) {
178178
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
179179
tcx.def_span(self.0)
180180
}
181+
#[inline(always)]
182+
fn key_as_def_id(&self) -> Option<DefId> {
183+
Some(self.0)
184+
}
181185
}
182186

183187
impl Key for (DefId, LocalDefId, Ident) {

compiler/rustc_query_system/src/query/job.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -600,16 +600,23 @@ pub(crate) fn report_cycle<'a>(
600600
));
601601
}
602602

603-
if !stack.is_empty()
604-
&& stack.iter().all(|entry| {
605-
entry.query.def_kind.map_or(false, |def_kind| {
606-
matches!(def_kind, SimpleDefKind::TyAlias | SimpleDefKind::TraitAlias)
607-
})
603+
if stack.iter().all(|entry| {
604+
entry.query.def_kind.map_or(false, |def_kind| {
605+
matches!(def_kind, SimpleDefKind::TyAlias | SimpleDefKind::TraitAlias)
608606
})
609-
{
610-
err.note("type aliases cannot be recursive");
611-
err.help("consider using a struct, enum, or union instead to break the cycle");
612-
err.help("see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information");
607+
}) {
608+
if stack.iter().all(|entry| {
609+
entry
610+
.query
611+
.def_kind
612+
.map_or(false, |def_kind| matches!(def_kind, SimpleDefKind::TyAlias))
613+
}) {
614+
err.note("type aliases cannot be recursive");
615+
err.help("consider using a struct, enum, or union instead to break the cycle");
616+
err.help("see <https://doc.rust-lang.org/reference/types.html#recursive-types> for more information");
617+
} else {
618+
err.note("trait aliases cannot be recursive");
619+
}
613620
}
614621

615622
if let Some((span, query)) = usage {

src/test/ui/infinite/infinite-trait-alias-recursion.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ note: ...which requires computing the super traits of `T3`...
3030
LL | trait T3 = T1 + T3;
3131
| ^^
3232
= note: ...which again requires computing the super predicates of `T1`, completing the cycle
33+
= note: trait aliases cannot be recursive
3334
note: cycle used when collecting item types in top-level module
3435
--> $DIR/infinite-trait-alias-recursion.rs:3:1
3536
|

0 commit comments

Comments
 (0)