Skip to content

Commit 081a8a8

Browse files
committed
fix(hir_analysis/wfcheck): don't leak {type error}
avoid `{type error}` being leaked in user-facing messages, particularly when using the `adt_const_params` feature
1 parent 42ff2ee commit 081a8a8

File tree

68 files changed

+149
-324
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+149
-324
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -961,13 +961,22 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
961961
hir_ty.span,
962962
"using raw pointers as const generic parameters is forbidden",
963963
),
964-
_ => tcx.dcx().struct_span_err(
965-
hir_ty.span,
966-
format!("`{}` is forbidden as the type of a const generic parameter", ty),
967-
),
964+
_ => {
965+
// Avoid showing "{type error}" to users. See #118179.
966+
if ty.references_error() {
967+
return Ok(());
968+
}
969+
970+
tcx.dcx().struct_span_err(
971+
hir_ty.span,
972+
format!(
973+
"`{ty}` is forbidden as the type of a const generic parameter",
974+
),
975+
)
976+
}
968977
};
969978

970-
diag.note("the only supported types are integers, `bool` and `char`");
979+
diag.note("the only supported types are integers, `bool`, and `char`");
971980

972981
let cause = ObligationCause::misc(hir_ty.span, param.def_id);
973982
let adt_const_params_feature_string =

tests/ui/const-generics/adt_const_params/suggest_feature_only_when_possible.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error: `&'static mut ()` is forbidden as the type of a const generic parameter
44
LL | fn uwu_0<const N: &'static mut ()>() {}
55
| ^^^^^^^^^^^^^^^
66
|
7-
= note: the only supported types are integers, `bool` and `char`
7+
= note: the only supported types are integers, `bool`, and `char`
88

99
error: `&'static u32` is forbidden as the type of a const generic parameter
1010
--> $DIR/suggest_feature_only_when_possible.rs:15:19
1111
|
1212
LL | fn owo_0<const N: &'static u32>() {}
1313
| ^^^^^^^^^^^^
1414
|
15-
= note: the only supported types are integers, `bool` and `char`
15+
= note: the only supported types are integers, `bool`, and `char`
1616
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
1717
|
1818
LL + #![feature(adt_const_params)]
@@ -28,7 +28,7 @@ error: `Meow` is forbidden as the type of a const generic parameter
2828
LL | fn meow_0<const N: Meow>() {}
2929
| ^^^^
3030
|
31-
= note: the only supported types are integers, `bool` and `char`
31+
= note: the only supported types are integers, `bool`, and `char`
3232
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
3333
|
3434
LL + #![feature(adt_const_params)]
@@ -40,7 +40,7 @@ error: `&'static Meow` is forbidden as the type of a const generic parameter
4040
LL | fn meow_1<const N: &'static Meow>() {}
4141
| ^^^^^^^^^^^^^
4242
|
43-
= note: the only supported types are integers, `bool` and `char`
43+
= note: the only supported types are integers, `bool`, and `char`
4444
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
4545
|
4646
LL + #![feature(adt_const_params)]
@@ -56,39 +56,39 @@ error: `[Meow; 100]` is forbidden as the type of a const generic parameter
5656
LL | fn meow_2<const N: [Meow; 100]>() {}
5757
| ^^^^^^^^^^^
5858
|
59-
= note: the only supported types are integers, `bool` and `char`
59+
= note: the only supported types are integers, `bool`, and `char`
6060

6161
error: `(Meow, u8)` is forbidden as the type of a const generic parameter
6262
--> $DIR/suggest_feature_only_when_possible.rs:29:20
6363
|
6464
LL | fn meow_3<const N: (Meow, u8)>() {}
6565
| ^^^^^^^^^^
6666
|
67-
= note: the only supported types are integers, `bool` and `char`
67+
= note: the only supported types are integers, `bool`, and `char`
6868

6969
error: `(Meow, String)` is forbidden as the type of a const generic parameter
7070
--> $DIR/suggest_feature_only_when_possible.rs:34:20
7171
|
7272
LL | fn meow_4<const N: (Meow, String)>() {}
7373
| ^^^^^^^^^^^^^^
7474
|
75-
= note: the only supported types are integers, `bool` and `char`
75+
= note: the only supported types are integers, `bool`, and `char`
7676

7777
error: `String` is forbidden as the type of a const generic parameter
7878
--> $DIR/suggest_feature_only_when_possible.rs:38:19
7979
|
8080
LL | fn nya_0<const N: String>() {}
8181
| ^^^^^^
8282
|
83-
= note: the only supported types are integers, `bool` and `char`
83+
= note: the only supported types are integers, `bool`, and `char`
8484

8585
error: `Vec<u32>` is forbidden as the type of a const generic parameter
8686
--> $DIR/suggest_feature_only_when_possible.rs:40:19
8787
|
8888
LL | fn nya_1<const N: Vec<u32>>() {}
8989
| ^^^^^^^^
9090
|
91-
= note: the only supported types are integers, `bool` and `char`
91+
= note: the only supported types are integers, `bool`, and `char`
9292

9393
error: aborting due to 9 previous errors
9494

tests/ui/const-generics/const-param-elided-lifetime.full.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ LL | struct A<const N: &u8>;
55
| ^ explicit lifetime name needed here
66

77
error[E0637]: `&` without an explicit lifetime name cannot be used here
8-
--> $DIR/const-param-elided-lifetime.rs:14:15
8+
--> $DIR/const-param-elided-lifetime.rs:13:15
99
|
1010
LL | impl<const N: &u8> A<N> {
1111
| ^ explicit lifetime name needed here
1212

1313
error[E0637]: `&` without an explicit lifetime name cannot be used here
14-
--> $DIR/const-param-elided-lifetime.rs:17:21
14+
--> $DIR/const-param-elided-lifetime.rs:15:21
1515
|
1616
LL | fn foo<const M: &u8>(&self) {}
1717
| ^ explicit lifetime name needed here
1818

1919
error[E0637]: `&` without an explicit lifetime name cannot be used here
20-
--> $DIR/const-param-elided-lifetime.rs:22:15
20+
--> $DIR/const-param-elided-lifetime.rs:19:15
2121
|
2222
LL | impl<const N: &u8> B for A<N> {}
2323
| ^ explicit lifetime name needed here
2424

2525
error[E0637]: `&` without an explicit lifetime name cannot be used here
26-
--> $DIR/const-param-elided-lifetime.rs:26:17
26+
--> $DIR/const-param-elided-lifetime.rs:22:17
2727
|
2828
LL | fn bar<const N: &u8>() {}
2929
| ^ explicit lifetime name needed here

tests/ui/const-generics/const-param-elided-lifetime.min.stderr

+5-85
Original file line numberDiff line numberDiff line change
@@ -5,109 +5,29 @@ LL | struct A<const N: &u8>;
55
| ^ explicit lifetime name needed here
66

77
error[E0637]: `&` without an explicit lifetime name cannot be used here
8-
--> $DIR/const-param-elided-lifetime.rs:14:15
8+
--> $DIR/const-param-elided-lifetime.rs:13:15
99
|
1010
LL | impl<const N: &u8> A<N> {
1111
| ^ explicit lifetime name needed here
1212

1313
error[E0637]: `&` without an explicit lifetime name cannot be used here
14-
--> $DIR/const-param-elided-lifetime.rs:17:21
14+
--> $DIR/const-param-elided-lifetime.rs:15:21
1515
|
1616
LL | fn foo<const M: &u8>(&self) {}
1717
| ^ explicit lifetime name needed here
1818

1919
error[E0637]: `&` without an explicit lifetime name cannot be used here
20-
--> $DIR/const-param-elided-lifetime.rs:22:15
20+
--> $DIR/const-param-elided-lifetime.rs:19:15
2121
|
2222
LL | impl<const N: &u8> B for A<N> {}
2323
| ^ explicit lifetime name needed here
2424

2525
error[E0637]: `&` without an explicit lifetime name cannot be used here
26-
--> $DIR/const-param-elided-lifetime.rs:26:17
26+
--> $DIR/const-param-elided-lifetime.rs:22:17
2727
|
2828
LL | fn bar<const N: &u8>() {}
2929
| ^ explicit lifetime name needed here
3030

31-
error: `&u8` is forbidden as the type of a const generic parameter
32-
--> $DIR/const-param-elided-lifetime.rs:9:19
33-
|
34-
LL | struct A<const N: &u8>;
35-
| ^^^
36-
|
37-
= note: the only supported types are integers, `bool` and `char`
38-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
39-
|
40-
LL + #![feature(adt_const_params)]
41-
|
42-
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
43-
|
44-
LL + #![feature(unsized_const_params)]
45-
|
46-
47-
error: `&u8` is forbidden as the type of a const generic parameter
48-
--> $DIR/const-param-elided-lifetime.rs:14:15
49-
|
50-
LL | impl<const N: &u8> A<N> {
51-
| ^^^
52-
|
53-
= note: the only supported types are integers, `bool` and `char`
54-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
55-
|
56-
LL + #![feature(adt_const_params)]
57-
|
58-
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
59-
|
60-
LL + #![feature(unsized_const_params)]
61-
|
62-
63-
error: `&u8` is forbidden as the type of a const generic parameter
64-
--> $DIR/const-param-elided-lifetime.rs:22:15
65-
|
66-
LL | impl<const N: &u8> B for A<N> {}
67-
| ^^^
68-
|
69-
= note: the only supported types are integers, `bool` and `char`
70-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
71-
|
72-
LL + #![feature(adt_const_params)]
73-
|
74-
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
75-
|
76-
LL + #![feature(unsized_const_params)]
77-
|
78-
79-
error: `&u8` is forbidden as the type of a const generic parameter
80-
--> $DIR/const-param-elided-lifetime.rs:26:17
81-
|
82-
LL | fn bar<const N: &u8>() {}
83-
| ^^^
84-
|
85-
= note: the only supported types are integers, `bool` and `char`
86-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
87-
|
88-
LL + #![feature(adt_const_params)]
89-
|
90-
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
91-
|
92-
LL + #![feature(unsized_const_params)]
93-
|
94-
95-
error: `&u8` is forbidden as the type of a const generic parameter
96-
--> $DIR/const-param-elided-lifetime.rs:17:21
97-
|
98-
LL | fn foo<const M: &u8>(&self) {}
99-
| ^^^
100-
|
101-
= note: the only supported types are integers, `bool` and `char`
102-
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
103-
|
104-
LL + #![feature(adt_const_params)]
105-
|
106-
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
107-
|
108-
LL + #![feature(unsized_const_params)]
109-
|
110-
111-
error: aborting due to 10 previous errors
31+
error: aborting due to 5 previous errors
11232

11333
For more information about this error, try `rustc --explain E0637`.

tests/ui/const-generics/const-param-elided-lifetime.rs

-5
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,18 @@
88

99
struct A<const N: &u8>;
1010
//~^ ERROR `&` without an explicit lifetime name cannot be used here
11-
//[min]~^^ ERROR `&u8` is forbidden
1211
trait B {}
1312

1413
impl<const N: &u8> A<N> {
1514
//~^ ERROR `&` without an explicit lifetime name cannot be used here
16-
//[min]~^^ ERROR `&u8` is forbidden
1715
fn foo<const M: &u8>(&self) {}
1816
//~^ ERROR `&` without an explicit lifetime name cannot be used here
19-
//[min]~^^ ERROR `&u8` is forbidden
2017
}
2118

2219
impl<const N: &u8> B for A<N> {}
2320
//~^ ERROR `&` without an explicit lifetime name cannot be used here
24-
//[min]~^^ ERROR `&u8` is forbidden
2521

2622
fn bar<const N: &u8>() {}
2723
//~^ ERROR `&` without an explicit lifetime name cannot be used here
28-
//[min]~^^ ERROR `&u8` is forbidden
2924

3025
fn main() {}

tests/ui/const-generics/const-param-type-depends-on-const-param.min.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ error: `[u8; N]` is forbidden as the type of a const generic parameter
2020
LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
2121
| ^^^^^^^
2222
|
23-
= note: the only supported types are integers, `bool` and `char`
23+
= note: the only supported types are integers, `bool`, and `char`
2424
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
2525
|
2626
LL + #![feature(adt_const_params)]
@@ -32,7 +32,7 @@ error: `[u8; N]` is forbidden as the type of a const generic parameter
3232
LL | pub struct SelfDependent<const N: [u8; N]>;
3333
| ^^^^^^^
3434
|
35-
= note: the only supported types are integers, `bool` and `char`
35+
= note: the only supported types are integers, `bool`, and `char`
3636
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
3737
|
3838
LL + #![feature(adt_const_params)]

tests/ui/const-generics/default-ty-closure.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: using function pointers as const generic parameters is forbidden
44
LL | struct X<const FN: fn() = { || {} }>;
55
| ^^^^
66
|
7-
= note: the only supported types are integers, `bool` and `char`
7+
= note: the only supported types are integers, `bool`, and `char`
88

99
error: aborting due to 1 previous error
1010

tests/ui/const-generics/float-generic.simple.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: `f32` is forbidden as the type of a const generic parameter
44
LL | fn foo<const F: f32>() {}
55
| ^^^
66
|
7-
= note: the only supported types are integers, `bool` and `char`
7+
= note: the only supported types are integers, `bool`, and `char`
88

99
error: aborting due to 1 previous error
1010

tests/ui/const-generics/fn-const-param-call.min.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error: using function pointers as const generic parameters is forbidden
44
LL | struct Wrapper<const F: fn() -> u32>;
55
| ^^^^^^^^^^^
66
|
7-
= note: the only supported types are integers, `bool` and `char`
7+
= note: the only supported types are integers, `bool`, and `char`
88

99
error: using function pointers as const generic parameters is forbidden
1010
--> $DIR/fn-const-param-call.rs:15:15
1111
|
1212
LL | impl<const F: fn() -> u32> Wrapper<F> {
1313
| ^^^^^^^^^^^
1414
|
15-
= note: the only supported types are integers, `bool` and `char`
15+
= note: the only supported types are integers, `bool`, and `char`
1616

1717
error: aborting due to 2 previous errors
1818

tests/ui/const-generics/fn-const-param-infer.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: using function pointers as const generic parameters is forbidden
44
LL | struct Checked<const F: fn(usize) -> bool>;
55
| ^^^^^^^^^^^^^^^^^
66
|
7-
= note: the only supported types are integers, `bool` and `char`
7+
= note: the only supported types are integers, `bool`, and `char`
88

99
error[E0308]: mismatched types
1010
--> $DIR/fn-const-param-infer.rs:33:25

tests/ui/const-generics/generic_const_exprs/array-size-in-generic-struct-param.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ error: `Config` is forbidden as the type of a const generic parameter
2222
LL | struct B<const CFG: Config> {
2323
| ^^^^^^
2424
|
25-
= note: the only supported types are integers, `bool` and `char`
25+
= note: the only supported types are integers, `bool`, and `char`
2626
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
2727
|
2828
LL + #![feature(adt_const_params)]

tests/ui/const-generics/generic_const_exprs/error_in_ty.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error: `[usize; x]` is forbidden as the type of a const generic parameter
1212
LL | pub struct A<const z: [usize; x]> {}
1313
| ^^^^^^^^^^
1414
|
15-
= note: the only supported types are integers, `bool` and `char`
15+
= note: the only supported types are integers, `bool`, and `char`
1616
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
1717
|
1818
LL + #![feature(adt_const_params)]

tests/ui/const-generics/generic_const_exprs/unevaluated-const-ice-119731.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ error: `[[usize; v4]; v4]` is forbidden as the type of a const generic parameter
6666
LL | pub struct v17<const v10: usize, const v7: v11> {
6767
| ^^^
6868
|
69-
= note: the only supported types are integers, `bool` and `char`
69+
= note: the only supported types are integers, `bool`, and `char`
7070
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
7171
|
7272
LL + #![feature(adt_const_params)]

tests/ui/const-generics/ice-118285-fn-ptr-value.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: using function pointers as const generic parameters is forbidden
44
LL | struct Checked<const F: fn(usize) -> bool>;
55
| ^^^^^^^^^^^^^^^^^
66
|
7-
= note: the only supported types are integers, `bool` and `char`
7+
= note: the only supported types are integers, `bool`, and `char`
88

99
error: aborting due to 1 previous error
1010

tests/ui/const-generics/intrinsics-type_name-as-const-argument.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error: `&'static str` is forbidden as the type of a const generic parameter
1313
LL | trait Trait<const S: &'static str> {}
1414
| ^^^^^^^^^^^^
1515
|
16-
= note: the only supported types are integers, `bool` and `char`
16+
= note: the only supported types are integers, `bool`, and `char`
1717
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
1818
|
1919
LL + #![feature(adt_const_params)]

0 commit comments

Comments
 (0)