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

0 commit comments

Comments
 (0)