Skip to content

Commit 2f48bfa

Browse files
committed
Improve errors for recursive type aliases
1 parent d3e2578 commit 2f48bfa

14 files changed

+38
-23
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,18 @@ rustc_queries! {
116116

117117
/// Records the type of every item.
118118
query type_of(key: DefId) -> Ty<'tcx> {
119-
desc { |tcx| "computing type of `{}`", tcx.def_path_str(key) }
119+
desc { |tcx|
120+
"{action} `{path}`",
121+
action = {
122+
use rustc_hir::def::DefKind;
123+
match tcx.def_kind(key) {
124+
DefKind::TyAlias => "expanding type alias",
125+
DefKind::TraitAlias => "expanding trait alias",
126+
_ => "computing type of",
127+
}
128+
},
129+
path = tcx.def_path_str(key),
130+
}
120131
cache_on_disk_if { key.is_local() }
121132
}
122133

compiler/rustc_query_system/src/query/job.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,14 @@ pub(crate) fn report_cycle<'a>(
591591
err.span_note(span, &format!("...which requires {}...", query.description));
592592
}
593593

594-
err.note(&format!(
595-
"...which again requires {}, completing the cycle",
596-
stack[0].query.description
597-
));
594+
if stack.len() == 1 {
595+
err.note(&format!("...which immediately requires {} again", stack[0].query.description));
596+
} else {
597+
err.note(&format!(
598+
"...which again requires {}, completing the cycle",
599+
stack[0].query.description
600+
));
601+
}
598602

599603
if let Some((span, query)) = usage {
600604
err.span_note(fix_span(span, &query), &format!("cycle used when {}", query.description));

src/test/ui/associated-type-bounds/ambiguous-associated-type2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0391]: cycle detected when computing the super traits of `Baz` with assoc
44
LL | trait Baz: Foo + Bar<Self::Item> {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: ...which again requires computing the super traits of `Baz` with associated type name `Item`, completing the cycle
7+
= note: ...which immediately requires computing the super traits of `Baz` with associated type name `Item` again
88
note: cycle used when computing the super traits of `Baz`
99
--> $DIR/ambiguous-associated-type2.rs:7:1
1010
|

src/test/ui/coherence/coherence-inherited-assoc-ty-cycle-err.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0391]: cycle detected when building specialization graph of trait `Trait`
1414
LL | trait Trait<T> { type Assoc; }
1515
| ^^^^^^^^^^^^^^
1616
|
17-
= note: ...which again requires building specialization graph of trait `Trait`, completing the cycle
17+
= note: ...which immediately requires building specialization graph of trait `Trait` again
1818
note: cycle used when coherence checking all impls of trait `Trait`
1919
--> $DIR/coherence-inherited-assoc-ty-cycle-err.rs:9:1
2020
|

src/test/ui/cycle-trait/cycle-trait-default-type-trait.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0391]: cycle detected when computing type of `Foo::X`
44
LL | trait Foo<X = Box<dyn Foo>> {
55
| ^^^
66
|
7-
= note: ...which again requires computing type of `Foo::X`, completing the cycle
7+
= note: ...which immediately requires computing type of `Foo::X` again
88
note: cycle used when collecting item types in top-level module
99
--> $DIR/cycle-trait-default-type-trait.rs:4:1
1010
|
@@ -17,7 +17,7 @@ error[E0391]: cycle detected when computing type of `Foo::X`
1717
LL | trait Foo<X = Box<dyn Foo>> {
1818
| ^^^
1919
|
20-
= note: ...which again requires computing type of `Foo::X`, completing the cycle
20+
= note: ...which immediately requires computing type of `Foo::X` again
2121
note: cycle used when collecting item types in top-level module
2222
--> $DIR/cycle-trait-default-type-trait.rs:4:1
2323
|

src/test/ui/infinite/infinite-struct.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ error[E0391]: cycle detected when computing drop-check constraints for `Take`
1818
LL | struct Take(Take);
1919
| ^^^^^^^^^^^^^^^^^^
2020
|
21-
= note: ...which again requires computing drop-check constraints for `Take`, completing the cycle
21+
= note: ...which immediately requires computing drop-check constraints for `Take` again
2222
= note: cycle used when computing dropck types for `Canonical { max_universe: U0, variables: [], value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing }, value: Take } }`
2323

2424
error: aborting due to 2 previous errors

src/test/ui/infinite/infinite-tag-type-recursion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ error[E0391]: cycle detected when computing drop-check constraints for `MList`
1717
LL | enum MList { Cons(isize, MList), Nil }
1818
| ^^^^^^^^^^
1919
|
20-
= note: ...which again requires computing drop-check constraints for `MList`, completing the cycle
20+
= note: ...which immediately requires computing drop-check constraints for `MList` again
2121
= note: cycle used when computing dropck types for `Canonical { max_universe: U0, variables: [], value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing }, value: MList } }`
2222

2323
error: aborting due to 2 previous errors

src/test/ui/infinite/infinite-vec-type-recursion.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0391]: cycle detected when computing type of `X`
1+
error[E0391]: cycle detected when expanding type alias `X`
22
--> $DIR/infinite-vec-type-recursion.rs:1:14
33
|
44
LL | type X = Vec<X>;
55
| ^
66
|
7-
= note: ...which again requires computing type of `X`, completing the cycle
7+
= note: ...which immediately requires expanding type alias `X` again
88
note: cycle used when collecting item types in top-level module
99
--> $DIR/infinite-vec-type-recursion.rs:1:1
1010
|

src/test/ui/issues/issue-20772.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | |
66
LL | | {}
77
| |__^
88
|
9-
= note: ...which again requires computing the super traits of `T` with associated type name `Item`, completing the cycle
9+
= note: ...which immediately requires computing the super traits of `T` with associated type name `Item` again
1010
note: cycle used when computing the super traits of `T`
1111
--> $DIR/issue-20772.rs:1:1
1212
|

src/test/ui/issues/issue-20825.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0391]: cycle detected when computing the super traits of `Processor` with
44
LL | pub trait Processor: Subscriber<Input = Self::Input> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: ...which again requires computing the super traits of `Processor` with associated type name `Input`, completing the cycle
7+
= note: ...which immediately requires computing the super traits of `Processor` with associated type name `Input` again
88
note: cycle used when computing the super traits of `Processor`
99
--> $DIR/issue-20825.rs:5:1
1010
|

src/test/ui/issues/issue-21177.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0391]: cycle detected when computing the bounds for type parameter `T`
44
LL | fn foo<T: Trait<A = T::B>>() { }
55
| ^^^^
66
|
7-
= note: ...which again requires computing the bounds for type parameter `T`, completing the cycle
7+
= note: ...which immediately requires computing the bounds for type parameter `T` again
88
note: cycle used when computing explicit predicates of `foo`
99
--> $DIR/issue-21177.rs:6:21
1010
|

src/test/ui/issues/issue-34373.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0391]: cycle detected when computing type of `Foo::T`
44
LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
55
| ^^^^^^^^^^
66
|
7-
note: ...which requires computing type of `DefaultFoo`...
7+
note: ...which requires expanding type alias `DefaultFoo`...
88
--> $DIR/issue-34373.rs:8:19
99
|
1010
LL | type DefaultFoo = Foo;

src/test/ui/resolve/issue-23305.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0391]: cycle detected when computing type of `<impl at $DIR/issue-23305.r
44
LL | impl dyn ToNbt<Self> {}
55
| ^^^^
66
|
7-
= note: ...which again requires computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:24>`, completing the cycle
7+
= note: ...which immediately requires computing type of `<impl at $DIR/issue-23305.rs:5:1: 5:24>` again
88
note: cycle used when collecting item types in top-level module
99
--> $DIR/issue-23305.rs:1:1
1010
|

src/test/ui/resolve/resolve-self-in-impl.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-
44
LL | impl Tr for Self {}
55
| ^^^^
66
|
7-
= note: ...which again requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:14:1: 14:20>`, completing the cycle
7+
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:14:1: 14:20>` again
88
note: cycle used when collecting item types in top-level module
99
--> $DIR/resolve-self-in-impl.rs:1:1
1010
|
@@ -23,7 +23,7 @@ error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-
2323
LL | impl Tr for S<Self> {}
2424
| ^^^^
2525
|
26-
= note: ...which again requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:15:1: 15:23>`, completing the cycle
26+
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:15:1: 15:23>` again
2727
note: cycle used when collecting item types in top-level module
2828
--> $DIR/resolve-self-in-impl.rs:1:1
2929
|
@@ -42,7 +42,7 @@ error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-
4242
LL | impl Self {}
4343
| ^^^^
4444
|
45-
= note: ...which again requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:16:1: 16:13>`, completing the cycle
45+
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:16:1: 16:13>` again
4646
note: cycle used when collecting item types in top-level module
4747
--> $DIR/resolve-self-in-impl.rs:1:1
4848
|
@@ -61,7 +61,7 @@ error[E0391]: cycle detected when computing type of `<impl at $DIR/resolve-self-
6161
LL | impl S<Self> {}
6262
| ^^^^
6363
|
64-
= note: ...which again requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:17:1: 17:16>`, completing the cycle
64+
= note: ...which immediately requires computing type of `<impl at $DIR/resolve-self-in-impl.rs:17:1: 17:16>` again
6565
note: cycle used when collecting item types in top-level module
6666
--> $DIR/resolve-self-in-impl.rs:1:1
6767
|
@@ -80,7 +80,7 @@ error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/
8080
LL | impl Tr<Self::A> for S {}
8181
| ^^^^^^^^^^^^^^^^^^^^^^
8282
|
83-
= note: ...which again requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:26>`, completing the cycle
83+
= note: ...which immediately requires computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:18:1: 18:26>` again
8484
note: cycle used when collecting item types in top-level module
8585
--> $DIR/resolve-self-in-impl.rs:1:1
8686
|

0 commit comments

Comments
 (0)