Skip to content

Commit 7542615

Browse files
committed
change const param ty warning message
1 parent 6ad01e9 commit 7542615

27 files changed

+78
-86
lines changed

src/librustc_typeck/check/wfcheck.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,14 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
289289
let ty = tcx.type_of(tcx.hir().local_def_id(param.hir_id));
290290

291291
let err_ty_str;
292+
let mut is_ptr = true;
292293
let err = if tcx.features().min_const_generics {
293294
match ty.kind {
294295
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Error(_) => None,
295296
ty::FnPtr(_) => Some("function pointers"),
296297
ty::RawPtr(_) => Some("raw pointers"),
297298
_ => {
299+
is_ptr = false;
298300
err_ty_str = format!("`{}`", ty);
299301
Some(err_ty_str.as_str())
300302
}
@@ -307,19 +309,29 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
307309
}
308310
};
309311
if let Some(unsupported_type) = err {
310-
let mut err = tcx.sess.struct_span_err(
311-
hir_ty.span,
312-
&format!("using {} as const generic parameters is forbidden", unsupported_type),
313-
);
314-
315-
if tcx.features().min_const_generics {
316-
err.note("the only supported types are integers, `bool` and `char`")
312+
if is_ptr {
313+
tcx.sess.span_err(
314+
hir_ty.span,
315+
&format!(
316+
"using {} as const generic parameters is forbidden",
317+
unsupported_type
318+
),
319+
)
320+
} else {
321+
tcx.sess
322+
.struct_span_err(
323+
hir_ty.span,
324+
&format!(
325+
"{} is forbidden as the type of a const generic parameter",
326+
unsupported_type
327+
),
328+
)
329+
.note("the only supported types are integers, `bool` and `char`")
317330
.note("more complex types are supported with `#[feature(const_generics)]`")
318331
.emit()
319-
} else {
320-
err.emit();
321332
}
322333
};
334+
323335
if traits::search_for_structural_match_violation(param.hir_id, param.span, tcx, ty)
324336
.is_some()
325337
{

src/test/ui/const-generics/array-size-in-generic-struct-param.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | arr: [u8; CFG.arr_size],
1414
|
1515
= help: it is currently only allowed to use either `CFG` or `{ CFG }` as generic constants
1616

17-
error: using `Config` as const generic parameters is forbidden
17+
error: `Config` is forbidden as the type of a const generic parameter
1818
--> $DIR/array-size-in-generic-struct-param.rs:18:21
1919
|
2020
LL | struct B<const CFG: Config> {

src/test/ui/const-generics/array-size-in-generic-struct-param.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct Config {
1616
}
1717

1818
struct B<const CFG: Config> {
19-
//[min]~^ ERROR using `Config` as const generic parameters is forbidden
19+
//[min]~^ ERROR `Config` is forbidden
2020
arr: [u8; CFG.arr_size],
2121
//[full]~^ ERROR constant expression depends on a generic parameter
2222
//[min]~^^ ERROR generic parameters must not be used inside of non trivial

src/test/ui/const-generics/const-param-elided-lifetime.min.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here
2828
LL | fn bar<const N: &u8>() {}
2929
| ^ explicit lifetime name needed here
3030

31-
error: using `&'static u8` as const generic parameters is forbidden
31+
error: `&'static u8` is forbidden as the type of a const generic parameter
3232
--> $DIR/const-param-elided-lifetime.rs:11:19
3333
|
3434
LL | struct A<const N: &u8>;
@@ -37,7 +37,7 @@ LL | struct A<const N: &u8>;
3737
= note: the only supported types are integers, `bool` and `char`
3838
= note: more complex types are supported with `#[feature(const_generics)]`
3939

40-
error: using `&'static u8` as const generic parameters is forbidden
40+
error: `&'static u8` is forbidden as the type of a const generic parameter
4141
--> $DIR/const-param-elided-lifetime.rs:16:15
4242
|
4343
LL | impl<const N: &u8> A<N> {
@@ -46,7 +46,7 @@ LL | impl<const N: &u8> A<N> {
4646
= note: the only supported types are integers, `bool` and `char`
4747
= note: more complex types are supported with `#[feature(const_generics)]`
4848

49-
error: using `&'static u8` as const generic parameters is forbidden
49+
error: `&'static u8` is forbidden as the type of a const generic parameter
5050
--> $DIR/const-param-elided-lifetime.rs:24:15
5151
|
5252
LL | impl<const N: &u8> B for A<N> {}
@@ -55,7 +55,7 @@ LL | impl<const N: &u8> B for A<N> {}
5555
= note: the only supported types are integers, `bool` and `char`
5656
= note: more complex types are supported with `#[feature(const_generics)]`
5757

58-
error: using `&'static u8` as const generic parameters is forbidden
58+
error: `&'static u8` is forbidden as the type of a const generic parameter
5959
--> $DIR/const-param-elided-lifetime.rs:28:17
6060
|
6161
LL | fn bar<const N: &u8>() {}
@@ -64,7 +64,7 @@ LL | fn bar<const N: &u8>() {}
6464
= note: the only supported types are integers, `bool` and `char`
6565
= note: more complex types are supported with `#[feature(const_generics)]`
6666

67-
error: using `&'static u8` as const generic parameters is forbidden
67+
error: `&'static u8` is forbidden as the type of a const generic parameter
6868
--> $DIR/const-param-elided-lifetime.rs:19:21
6969
|
7070
LL | fn foo<const M: &u8>(&self) {}

src/test/ui/const-generics/const-param-elided-lifetime.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010

1111
struct A<const N: &u8>;
1212
//~^ ERROR `&` without an explicit lifetime name cannot be used here
13-
//[min]~^^ ERROR using `&'static u8` as const generic parameters is forbidden
13+
//[min]~^^ ERROR `&'static u8` is forbidden
1414
trait B {}
1515

1616
impl<const N: &u8> A<N> {
1717
//~^ ERROR `&` without an explicit lifetime name cannot be used here
18-
//[min]~^^ ERROR using `&'static u8` as const generic parameters is forbidden
18+
//[min]~^^ ERROR `&'static u8` is forbidden
1919
fn foo<const M: &u8>(&self) {}
2020
//~^ ERROR `&` without an explicit lifetime name cannot be used here
21-
//[min]~^^ ERROR using `&'static u8` as const generic parameters is forbidden
21+
//[min]~^^ ERROR `&'static u8` is forbidden
2222
}
2323

2424
impl<const N: &u8> B for A<N> {}
2525
//~^ ERROR `&` without an explicit lifetime name cannot be used here
26-
//[min]~^^ ERROR using `&'static u8` as const generic parameters is forbidden
26+
//[min]~^^ ERROR `&'static u8` is forbidden
2727

2828
fn bar<const N: &u8>() {}
2929
//~^ ERROR `&` without an explicit lifetime name cannot be used here
30-
//[min]~^^ ERROR using `&'static u8` as const generic parameters is forbidden
30+
//[min]~^^ ERROR `&'static u8` is forbidden
3131

3232
fn main() {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0770]: the type of const parameters must not depend on other generic para
1010
LL | pub struct SelfDependent<const N: [u8; N]>;
1111
| ^ the type must not depend on the parameter `N`
1212

13-
error: using `[u8; _]` as const generic parameters is forbidden
13+
error: `[u8; _]` is forbidden as the type of a const generic parameter
1414
--> $DIR/const-param-type-depends-on-const-param.rs:12:47
1515
|
1616
LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
@@ -19,7 +19,7 @@ LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
1919
= note: the only supported types are integers, `bool` and `char`
2020
= note: more complex types are supported with `#[feature(const_generics)]`
2121

22-
error: using `[u8; _]` as const generic parameters is forbidden
22+
error: `[u8; _]` is forbidden as the type of a const generic parameter
2323
--> $DIR/const-param-type-depends-on-const-param.rs:16:35
2424
|
2525
LL | pub struct SelfDependent<const N: [u8; N]>;

src/test/ui/const-generics/const-param-type-depends-on-const-param.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
1313
//~^ ERROR: the type of const parameters must not depend on other generic parameters
14-
//[min]~^^ ERROR using `[u8; _]` as const generic parameters is forbidden
14+
//[min]~^^ ERROR `[u8; _]` is forbidden
1515

1616
pub struct SelfDependent<const N: [u8; N]>;
1717
//~^ ERROR: the type of const parameters must not depend on other generic parameters
18-
//[min]~^^ ERROR using `[u8; _]` as const generic parameters is forbidden
18+
//[min]~^^ ERROR `[u8; _]` is forbidden
1919

2020
fn main() {}

src/test/ui/const-generics/different_byref.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: using `[usize; 1]` as const generic parameters is forbidden
1+
error: `[usize; 1]` is forbidden as the type of a const generic parameter
22
--> $DIR/different_byref.rs:8:23
33
|
44
LL | struct Const<const V: [usize; 1]> {}

src/test/ui/const-generics/different_byref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![cfg_attr(min, feature(min_const_generics))]
77

88
struct Const<const V: [usize; 1]> {}
9-
//[min]~^ using `[usize; 1]` as const generic parameters is forbidden
9+
//[min]~^ ERROR `[usize; 1]` is forbidden
1010

1111
fn main() {
1212
let mut x = Const::<{ [3] }> {};

src/test/ui/const-generics/fn-const-param-call.min.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ error: using function pointers as const generic parameters is forbidden
33
|
44
LL | struct Wrapper<const F: fn() -> u32>;
55
| ^^^^^^^^^^^
6-
|
7-
= note: the only supported types are integers, `bool` and `char`
8-
= note: more complex types are supported with `#[feature(const_generics)]`
96

107
error: using function pointers as const generic parameters is forbidden
118
--> $DIR/fn-const-param-call.rs:14:15
129
|
1310
LL | impl<const F: fn() -> u32> Wrapper<F> {
1411
| ^^^^^^^^^^^
15-
|
16-
= note: the only supported types are integers, `bool` and `char`
17-
= note: more complex types are supported with `#[feature(const_generics)]`
1812

1913
error: aborting due to 2 previous errors
2014

src/test/ui/const-generics/fn-const-param-infer.min.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ error: using function pointers as const generic parameters is forbidden
33
|
44
LL | struct Checked<const F: fn(usize) -> bool>;
55
| ^^^^^^^^^^^^^^^^^
6-
|
7-
= note: the only supported types are integers, `bool` and `char`
8-
= note: more complex types are supported with `#[feature(const_generics)]`
96

107
error: aborting due to previous error
118

src/test/ui/const-generics/forbid-non-structural_match-types.min.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: using `A` as const generic parameters is forbidden
1+
error: `A` is forbidden as the type of a const generic parameter
22
--> $DIR/forbid-non-structural_match-types.rs:10:19
33
|
44
LL | struct B<const X: A>; // ok
@@ -7,7 +7,7 @@ LL | struct B<const X: A>; // ok
77
= note: the only supported types are integers, `bool` and `char`
88
= note: more complex types are supported with `#[feature(const_generics)]`
99

10-
error: using `C` as const generic parameters is forbidden
10+
error: `C` is forbidden as the type of a const generic parameter
1111
--> $DIR/forbid-non-structural_match-types.rs:15:19
1212
|
1313
LL | struct D<const X: C>;

src/test/ui/const-generics/forbid-non-structural_match-types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
struct A;
99

1010
struct B<const X: A>; // ok
11-
//[min]~^ ERROR using `A` as const generic parameters is forbidden
11+
//[min]~^ ERROR `A` is forbidden
1212

1313
struct C;
1414

1515
struct D<const X: C>; //~ ERROR `C` must be annotated with `#[derive(PartialEq, Eq)]`
16-
//[min]~^ ERROR using `C` as const generic parameters is forbidden
16+
//[min]~^ ERROR `C` is forbidden
1717

1818
fn main() {}

src/test/ui/const-generics/issue-66596-impl-trait-for-str-const-arg.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: using `&'static str` as const generic parameters is forbidden
1+
error: `&'static str` is forbidden as the type of a const generic parameter
22
--> $DIR/issue-66596-impl-trait-for-str-const-arg.rs:9:25
33
|
44
LL | trait Trait<const NAME: &'static str> {

src/test/ui/const-generics/issue-66596-impl-trait-for-str-const-arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
trait Trait<const NAME: &'static str> {
10-
//[min]~^ ERROR using `&'static str` as const generic parameters is forbidden
10+
//[min]~^ ERROR `&'static str` is forbidden
1111
type Assoc;
1212
}
1313

src/test/ui/const-generics/issues/issue-74950.min.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: using `Inner` as const generic parameters is forbidden
1+
error: `Inner` is forbidden as the type of a const generic parameter
22
--> $DIR/issue-74950.rs:18:23
33
|
44
LL | struct Outer<const I: Inner>;
@@ -7,7 +7,7 @@ LL | struct Outer<const I: Inner>;
77
= note: the only supported types are integers, `bool` and `char`
88
= note: more complex types are supported with `#[feature(const_generics)]`
99

10-
error: using `Inner` as const generic parameters is forbidden
10+
error: `Inner` is forbidden as the type of a const generic parameter
1111
--> $DIR/issue-74950.rs:18:23
1212
|
1313
LL | struct Outer<const I: Inner>;
@@ -16,7 +16,7 @@ LL | struct Outer<const I: Inner>;
1616
= note: the only supported types are integers, `bool` and `char`
1717
= note: more complex types are supported with `#[feature(const_generics)]`
1818

19-
error: using `Inner` as const generic parameters is forbidden
19+
error: `Inner` is forbidden as the type of a const generic parameter
2020
--> $DIR/issue-74950.rs:18:23
2121
|
2222
LL | struct Outer<const I: Inner>;
@@ -25,7 +25,7 @@ LL | struct Outer<const I: Inner>;
2525
= note: the only supported types are integers, `bool` and `char`
2626
= note: more complex types are supported with `#[feature(const_generics)]`
2727

28-
error: using `Inner` as const generic parameters is forbidden
28+
error: `Inner` is forbidden as the type of a const generic parameter
2929
--> $DIR/issue-74950.rs:18:23
3030
|
3131
LL | struct Outer<const I: Inner>;
@@ -34,7 +34,7 @@ LL | struct Outer<const I: Inner>;
3434
= note: the only supported types are integers, `bool` and `char`
3535
= note: more complex types are supported with `#[feature(const_generics)]`
3636

37-
error: using `Inner` as const generic parameters is forbidden
37+
error: `Inner` is forbidden as the type of a const generic parameter
3838
--> $DIR/issue-74950.rs:18:23
3939
|
4040
LL | struct Outer<const I: Inner>;

src/test/ui/const-generics/issues/issue-74950.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ struct Inner;
1616
// - impl StructuralEq
1717
#[derive(PartialEq, Eq)]
1818
struct Outer<const I: Inner>;
19-
//[min]~^ using `Inner` as const generic parameters is forbidden
20-
//[min]~| using `Inner` as const generic parameters is forbidden
21-
//[min]~| using `Inner` as const generic parameters is forbidden
22-
//[min]~| using `Inner` as const generic parameters is forbidden
23-
//[min]~| using `Inner` as const generic parameters is forbidden
19+
//[min]~^ `Inner` is forbidden
20+
//[min]~| `Inner` is forbidden
21+
//[min]~| `Inner` is forbidden
22+
//[min]~| `Inner` is forbidden
23+
//[min]~| `Inner` is forbidden
2424

2525
fn main() {}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#![feature(min_const_generics)]
22

33
struct Foo<const N: [u8; 0]>;
4-
//~^ ERROR using `[u8; 0]` as const generic parameters is forbidden
4+
//~^ ERROR `[u8; 0]` is forbidden
55

66
struct Bar<const N: ()>;
7-
//~^ ERROR using `()` as const generic parameters is forbidden
8-
7+
//~^ ERROR `()` is forbidden
98
#[derive(PartialEq, Eq)]
109
struct No;
1110

1211
struct Fez<const N: No>;
13-
//~^ ERROR using `No` as const generic parameters is forbidden
12+
//~^ ERROR `No` is forbidden
1413

1514
struct Faz<const N: &'static u8>;
16-
//~^ ERROR using `&'static u8` as const generic parameters is forbidden
15+
//~^ ERROR `&'static u8` is forbidden
1716

1817
fn main() {}

src/test/ui/const-generics/min_const_generics/complex-types.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: using `[u8; 0]` as const generic parameters is forbidden
1+
error: `[u8; 0]` is forbidden as the type of a const generic parameter
22
--> $DIR/complex-types.rs:3:21
33
|
44
LL | struct Foo<const N: [u8; 0]>;
@@ -7,7 +7,7 @@ LL | struct Foo<const N: [u8; 0]>;
77
= note: the only supported types are integers, `bool` and `char`
88
= note: more complex types are supported with `#[feature(const_generics)]`
99

10-
error: using `()` as const generic parameters is forbidden
10+
error: `()` is forbidden as the type of a const generic parameter
1111
--> $DIR/complex-types.rs:6:21
1212
|
1313
LL | struct Bar<const N: ()>;
@@ -16,17 +16,17 @@ LL | struct Bar<const N: ()>;
1616
= note: the only supported types are integers, `bool` and `char`
1717
= note: more complex types are supported with `#[feature(const_generics)]`
1818

19-
error: using `No` as const generic parameters is forbidden
20-
--> $DIR/complex-types.rs:12:21
19+
error: `No` is forbidden as the type of a const generic parameter
20+
--> $DIR/complex-types.rs:11:21
2121
|
2222
LL | struct Fez<const N: No>;
2323
| ^^
2424
|
2525
= note: the only supported types are integers, `bool` and `char`
2626
= note: more complex types are supported with `#[feature(const_generics)]`
2727

28-
error: using `&'static u8` as const generic parameters is forbidden
29-
--> $DIR/complex-types.rs:15:21
28+
error: `&'static u8` is forbidden as the type of a const generic parameter
29+
--> $DIR/complex-types.rs:14:21
3030
|
3131
LL | struct Faz<const N: &'static u8>;
3232
| ^^^^^^^^^^^

src/test/ui/const-generics/nested-type.full.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
2-
--> $DIR/nested-type.rs:17:5
2+
--> $DIR/nested-type.rs:16:5
33
|
44
LL | Foo::<17>::value()
55
| ^^^^^^^^^^^^^^^^^^
66

77
error[E0080]: evaluation of constant value failed
8-
--> $DIR/nested-type.rs:17:5
8+
--> $DIR/nested-type.rs:16:5
99
|
1010
LL | Foo::<17>::value()
1111
| ^^^^^^^^^^^^^^^^^^ calling non-const function `Foo::{{constant}}#0::Foo::<17_usize>::value`

0 commit comments

Comments
 (0)