Skip to content

Commit 3c70d60

Browse files
committed
Gracefully handle AnonConst in diagnostic_hir_wf_check()
when it is the default value of a generic param
1 parent 21d94a3 commit 3c70d60

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

compiler/rustc_hir_analysis/src/hir_wf_check.rs

+10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ 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)
169+
&& let hir::Node::GenericParam(hir::GenericParam {
170+
kind: hir::GenericParamKind::Const { ty, .. },
171+
..
172+
}) = tcx.hir_node_by_def_id(const_param_id) =>
173+
{
174+
vec![*ty]
175+
}
166176
ref node => bug!("Unexpected node {:?}", node),
167177
},
168178
WellFormedLoc::Param { function: _, param_idx } => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait Trait<const N: Trait = bar> {
2+
//~^ ERROR cannot find value `bar` in this scope
3+
//~| ERROR cycle detected when computing type of `Trait::N`
4+
//~| ERROR cycle detected when computing type of `Trait::N`
5+
//~| ERROR `(dyn Trait<{const error}> + 'static)` is forbidden as the type of a const generic parameter
6+
//~| WARN trait objects without an explicit `dyn` are deprecated
7+
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
8+
fn fnc(&self) {
9+
}
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
error[E0425]: cannot find value `bar` in this scope
2+
--> $DIR/ice-hir-wf-check-anon-const-issue-122199.rs:1:30
3+
|
4+
LL | trait Trait<const N: Trait = bar> {
5+
| ^^^ not found in this scope
6+
7+
warning: trait objects without an explicit `dyn` are deprecated
8+
--> $DIR/ice-hir-wf-check-anon-const-issue-122199.rs:1:22
9+
|
10+
LL | trait Trait<const N: Trait = bar> {
11+
| ^^^^^
12+
|
13+
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
14+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
15+
= note: `#[warn(bare_trait_objects)]` on by default
16+
help: if this is an object-safe trait, use `dyn`
17+
|
18+
LL | trait Trait<const N: dyn Trait = bar> {
19+
| +++
20+
21+
error[E0391]: cycle detected when computing type of `Trait::N`
22+
--> $DIR/ice-hir-wf-check-anon-const-issue-122199.rs:1:22
23+
|
24+
LL | trait Trait<const N: Trait = bar> {
25+
| ^^^^^
26+
|
27+
= note: ...which immediately requires computing type of `Trait::N` again
28+
note: cycle used when computing explicit predicates of trait `Trait`
29+
--> $DIR/ice-hir-wf-check-anon-const-issue-122199.rs:1:1
30+
|
31+
LL | trait Trait<const N: Trait = bar> {
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
= 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
34+
35+
error[E0391]: cycle detected when computing type of `Trait::N`
36+
--> $DIR/ice-hir-wf-check-anon-const-issue-122199.rs:1:13
37+
|
38+
LL | trait Trait<const N: Trait = bar> {
39+
| ^^^^^^^^^^^^^^^^^^^^
40+
|
41+
= note: ...which immediately requires computing type of `Trait::N` again
42+
note: cycle used when computing explicit predicates of trait `Trait`
43+
--> $DIR/ice-hir-wf-check-anon-const-issue-122199.rs:1:1
44+
|
45+
LL | trait Trait<const N: Trait = bar> {
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
= 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
48+
49+
error: `(dyn Trait<{const error}> + 'static)` is forbidden as the type of a const generic parameter
50+
--> $DIR/ice-hir-wf-check-anon-const-issue-122199.rs:1:22
51+
|
52+
LL | trait Trait<const N: Trait = bar> {
53+
| ^^^^^
54+
|
55+
= note: the only supported types are integers, `bool` and `char`
56+
57+
error: aborting due to 4 previous errors; 1 warning emitted
58+
59+
Some errors have detailed explanations: E0391, E0425.
60+
For more information about an error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)