Skip to content

Commit faefc61

Browse files
committed
Auto merge of #124219 - gurry:122989-ice-unexpected-anon-const, r=compiler-errors
Do not ICE on `AnonConst`s in `diagnostic_hir_wf_check` Fixes #122989 Below is the snippet from #122989 that ICEs: ```rust trait Traitor<const N: N<2> = 1, const N: N<2> = N> { fn N(&N) -> N<2> { M } } trait N<const N: Traitor<2> = 12> {} ``` The `AnonConst` that triggers the ICE is the `2` in the param `const N: N<2> = 1`. The currently existing code in `diagnostic_hir_wf_check` deals only with `AnonConst`s that are default values of some param, but the `2` is not a default value. It is just an `AnonConst` HIR node inside a `TraitRef` HIR node corresponding to `N<2>`. Therefore the existing code cannot handle it and this PR ensures that it does.
2 parents b923ea4 + 446f78d commit faefc61

File tree

4 files changed

+208
-14
lines changed

4 files changed

+208
-14
lines changed

Diff for: compiler/rustc_hir_analysis/src/hir_wf_check.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,17 @@ fn diagnostic_hir_wf_check<'tcx>(
163163
kind: hir::GenericParamKind::Type { default: Some(ty), .. },
164164
..
165165
}) => vec![*ty],
166-
hir::Node::AnonConst(_)
167-
if let Some(const_param_id) =
168-
tcx.hir().opt_const_param_default_param_def_id(hir_id)
166+
hir::Node::AnonConst(_) => {
167+
if let Some(const_param_id) = tcx.hir().opt_const_param_default_param_def_id(hir_id)
169168
&& let hir::Node::GenericParam(hir::GenericParam {
170169
kind: hir::GenericParamKind::Const { ty, .. },
171170
..
172-
}) = tcx.hir_node_by_def_id(const_param_id) =>
173-
{
174-
vec![*ty]
171+
}) = tcx.hir_node_by_def_id(const_param_id)
172+
{
173+
vec![*ty]
174+
} else {
175+
vec![]
176+
}
175177
}
176178
ref node => bug!("Unexpected node {:?}", node),
177179
},

Diff for: tests/crashes/122989.rs

-8
This file was deleted.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for ICE #122989
2+
trait Foo<const N: Bar<2>> {
3+
//~^ WARN trait objects without an explicit `dyn` are deprecated
4+
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
5+
//~| ERROR cycle detected when computing type of `Foo::N`
6+
//~| ERROR cycle detected when computing type of `Foo::N`
7+
//~| ERROR the trait `Foo` cannot be made into an object
8+
//~| ERROR the trait `Foo` cannot be made into an object
9+
//~| ERROR the trait `Foo` cannot be made into an object
10+
//~| ERROR `(dyn Bar<2> + 'static)` is forbidden as the type of a const generic parameter
11+
fn func() {
12+
}
13+
}
14+
15+
trait Bar<const M: Foo<2>> {}
16+
//~^ WARN trait objects without an explicit `dyn` are deprecated
17+
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
18+
//~| ERROR the trait `Foo` cannot be made into an object
19+
//~| ERROR `(dyn Foo<2> + 'static)` is forbidden as the type of a const generic parameter
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
warning: trait objects without an explicit `dyn` are deprecated
2+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:20
3+
|
4+
LL | trait Foo<const N: Bar<2>> {
5+
| ^^^^^^
6+
|
7+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
8+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
9+
= note: `#[warn(bare_trait_objects)]` on by default
10+
help: if this is an object-safe trait, use `dyn`
11+
|
12+
LL | trait Foo<const N: dyn Bar<2>> {
13+
| +++
14+
15+
warning: trait objects without an explicit `dyn` are deprecated
16+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:20
17+
|
18+
LL | trait Bar<const M: Foo<2>> {}
19+
| ^^^^^^
20+
|
21+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
22+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
23+
help: if this is an object-safe trait, use `dyn`
24+
|
25+
LL | trait Bar<const M: dyn Foo<2>> {}
26+
| +++
27+
28+
error[E0391]: cycle detected when computing type of `Foo::N`
29+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:11
30+
|
31+
LL | trait Foo<const N: Bar<2>> {
32+
| ^^^^^^^^^^^^^^^
33+
|
34+
note: ...which requires computing type of `Bar::M`...
35+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:11
36+
|
37+
LL | trait Bar<const M: Foo<2>> {}
38+
| ^^^^^^^^^^^^^^^
39+
= note: ...which again requires computing type of `Foo::N`, completing the cycle
40+
note: cycle used when computing explicit predicates of trait `Foo`
41+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:1
42+
|
43+
LL | trait Foo<const N: Bar<2>> {
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
45+
= 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
46+
47+
error[E0391]: cycle detected when computing type of `Foo::N`
48+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:11
49+
|
50+
LL | trait Foo<const N: Bar<2>> {
51+
| ^^^^^^^^^^^^^^^
52+
|
53+
note: ...which requires computing type of `Bar::M`...
54+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:11
55+
|
56+
LL | trait Bar<const M: Foo<2>> {}
57+
| ^^^^^^^^^^^^^^^
58+
= note: ...which again requires computing type of `Foo::N`, completing the cycle
59+
note: cycle used when computing explicit predicates of trait `Foo`
60+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:1
61+
|
62+
LL | trait Foo<const N: Bar<2>> {
63+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
64+
= 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
65+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
66+
67+
error[E0038]: the trait `Foo` cannot be made into an object
68+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:24
69+
|
70+
LL | trait Foo<const N: Bar<2>> {
71+
| ^ `Foo` cannot be made into an object
72+
|
73+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
74+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:11:8
75+
|
76+
LL | trait Foo<const N: Bar<2>> {
77+
| --- this trait cannot be made into an object...
78+
...
79+
LL | fn func() {
80+
| ^^^^ ...because associated function `func` has no `self` parameter
81+
help: consider turning `func` into a method by giving it a `&self` argument
82+
|
83+
LL | fn func(&self) {
84+
| +++++
85+
help: alternatively, consider constraining `func` so it does not apply to trait objects
86+
|
87+
LL | fn func() where Self: Sized {
88+
| +++++++++++++++++
89+
90+
error[E0038]: the trait `Foo` cannot be made into an object
91+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:11
92+
|
93+
LL | trait Foo<const N: Bar<2>> {
94+
| ^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
95+
|
96+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
97+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:11:8
98+
|
99+
LL | trait Foo<const N: Bar<2>> {
100+
| --- this trait cannot be made into an object...
101+
...
102+
LL | fn func() {
103+
| ^^^^ ...because associated function `func` has no `self` parameter
104+
help: consider turning `func` into a method by giving it a `&self` argument
105+
|
106+
LL | fn func(&self) {
107+
| +++++
108+
help: alternatively, consider constraining `func` so it does not apply to trait objects
109+
|
110+
LL | fn func() where Self: Sized {
111+
| +++++++++++++++++
112+
113+
error: `(dyn Bar<2> + 'static)` is forbidden as the type of a const generic parameter
114+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:20
115+
|
116+
LL | trait Foo<const N: Bar<2>> {
117+
| ^^^^^^
118+
|
119+
= note: the only supported types are integers, `bool` and `char`
120+
121+
error[E0038]: the trait `Foo` cannot be made into an object
122+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:11
123+
|
124+
LL | trait Bar<const M: Foo<2>> {}
125+
| ^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
126+
|
127+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
128+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:11:8
129+
|
130+
LL | trait Foo<const N: Bar<2>> {
131+
| --- this trait cannot be made into an object...
132+
...
133+
LL | fn func() {
134+
| ^^^^ ...because associated function `func` has no `self` parameter
135+
help: consider turning `func` into a method by giving it a `&self` argument
136+
|
137+
LL | fn func(&self) {
138+
| +++++
139+
help: alternatively, consider constraining `func` so it does not apply to trait objects
140+
|
141+
LL | fn func() where Self: Sized {
142+
| +++++++++++++++++
143+
144+
error: `(dyn Foo<2> + 'static)` is forbidden as the type of a const generic parameter
145+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:20
146+
|
147+
LL | trait Bar<const M: Foo<2>> {}
148+
| ^^^^^^
149+
|
150+
= note: the only supported types are integers, `bool` and `char`
151+
152+
error[E0038]: the trait `Foo` cannot be made into an object
153+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:11
154+
|
155+
LL | trait Foo<const N: Bar<2>> {
156+
| ^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
157+
|
158+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
159+
--> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:11:8
160+
|
161+
LL | trait Foo<const N: Bar<2>> {
162+
| --- this trait cannot be made into an object...
163+
...
164+
LL | fn func() {
165+
| ^^^^ ...because associated function `func` has no `self` parameter
166+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
167+
help: consider turning `func` into a method by giving it a `&self` argument
168+
|
169+
LL | fn func(&self) {
170+
| +++++
171+
help: alternatively, consider constraining `func` so it does not apply to trait objects
172+
|
173+
LL | fn func() where Self: Sized {
174+
| +++++++++++++++++
175+
176+
error: aborting due to 8 previous errors; 2 warnings emitted
177+
178+
Some errors have detailed explanations: E0038, E0391.
179+
For more information about an error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)