Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adt_const_params leaking {type error} in error msg #131038

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,13 +961,20 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
hir_ty.span,
"using raw pointers as const generic parameters is forbidden",
),
_ => tcx.dcx().struct_span_err(
hir_ty.span,
format!("`{}` is forbidden as the type of a const generic parameter", ty),
),
_ => {
// Avoid showing "{type error}" to users. See #118179.
ty.error_reported()?;

tcx.dcx().struct_span_err(
hir_ty.span,
format!(
"`{ty}` is forbidden as the type of a const generic parameter",
),
)
}
};

diag.note("the only supported types are integers, `bool` and `char`");
diag.note("the only supported types are integers, `bool`, and `char`");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma before and looks wrong to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's called the oxford comma.

Copy link
Contributor

@klensy klensy Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, okay, looking at *.stderr there used both cases with and w\o comma with enumerations.

One more wtf styling case. Probably exist somewhere in rust style guide for errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found only 2 mentions: rust-lang/rustc-dev-guide#1087 (comment), rust-lang/rustc-dev-guide#1139 (comment); should be added to the guide?


let cause = ObligationCause::misc(hir_ty.span, param.def_id);
let adt_const_params_feature_string =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ error: `&'static mut ()` is forbidden as the type of a const generic parameter
LL | fn uwu_0<const N: &'static mut ()>() {}
| ^^^^^^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`

error: `&'static u32` is forbidden as the type of a const generic parameter
--> $DIR/suggest_feature_only_when_possible.rs:15:19
|
LL | fn owo_0<const N: &'static u32>() {}
| ^^^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
Expand All @@ -28,7 +28,7 @@ error: `Meow` is forbidden as the type of a const generic parameter
LL | fn meow_0<const N: Meow>() {}
| ^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
Expand All @@ -40,7 +40,7 @@ error: `&'static Meow` is forbidden as the type of a const generic parameter
LL | fn meow_1<const N: &'static Meow>() {}
| ^^^^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
Expand All @@ -56,39 +56,39 @@ error: `[Meow; 100]` is forbidden as the type of a const generic parameter
LL | fn meow_2<const N: [Meow; 100]>() {}
| ^^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`

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

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

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

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

error: aborting due to 9 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ LL | struct A<const N: &u8>;
| ^ explicit lifetime name needed here

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

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

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

error[E0637]: `&` without an explicit lifetime name cannot be used here
--> $DIR/const-param-elided-lifetime.rs:26:17
--> $DIR/const-param-elided-lifetime.rs:22:17
|
LL | fn bar<const N: &u8>() {}
| ^ explicit lifetime name needed here
Expand Down
90 changes: 5 additions & 85 deletions tests/ui/const-generics/const-param-elided-lifetime.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,29 @@ LL | struct A<const N: &u8>;
| ^ explicit lifetime name needed here

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

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

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

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

error: `&u8` is forbidden as the type of a const generic parameter
--> $DIR/const-param-elided-lifetime.rs:9:19
|
LL | struct A<const N: &u8>;
| ^^^
|
= note: the only supported types are integers, `bool` and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
|
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
|
LL + #![feature(unsized_const_params)]
|

error: `&u8` is forbidden as the type of a const generic parameter
--> $DIR/const-param-elided-lifetime.rs:14:15
|
LL | impl<const N: &u8> A<N> {
| ^^^
|
= note: the only supported types are integers, `bool` and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
|
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
|
LL + #![feature(unsized_const_params)]
|

error: `&u8` is forbidden as the type of a const generic parameter
--> $DIR/const-param-elided-lifetime.rs:22:15
|
LL | impl<const N: &u8> B for A<N> {}
| ^^^
|
= note: the only supported types are integers, `bool` and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
|
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
|
LL + #![feature(unsized_const_params)]
|

error: `&u8` is forbidden as the type of a const generic parameter
--> $DIR/const-param-elided-lifetime.rs:26:17
|
LL | fn bar<const N: &u8>() {}
| ^^^
|
= note: the only supported types are integers, `bool` and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
|
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
|
LL + #![feature(unsized_const_params)]
|

error: `&u8` is forbidden as the type of a const generic parameter
--> $DIR/const-param-elided-lifetime.rs:17:21
|
LL | fn foo<const M: &u8>(&self) {}
| ^^^
|
= note: the only supported types are integers, `bool` and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
|
help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait
|
LL + #![feature(unsized_const_params)]
|

error: aborting due to 10 previous errors
error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0637`.
5 changes: 0 additions & 5 deletions tests/ui/const-generics/const-param-elided-lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@

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

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

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

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

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ error: `[u8; N]` is forbidden as the type of a const generic parameter
LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
| ^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
Expand All @@ -32,7 +32,7 @@ error: `[u8; N]` is forbidden as the type of a const generic parameter
LL | pub struct SelfDependent<const N: [u8; N]>;
| ^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/default-ty-closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: using function pointers as const generic parameters is forbidden
LL | struct X<const FN: fn() = { || {} }>;
| ^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`

error: aborting due to 1 previous error

2 changes: 1 addition & 1 deletion tests/ui/const-generics/float-generic.simple.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: `f32` is forbidden as the type of a const generic parameter
LL | fn foo<const F: f32>() {}
| ^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`

error: aborting due to 1 previous error

4 changes: 2 additions & 2 deletions tests/ui/const-generics/fn-const-param-call.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ error: using function pointers as const generic parameters is forbidden
LL | struct Wrapper<const F: fn() -> u32>;
| ^^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`

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

error: aborting due to 2 previous errors

2 changes: 1 addition & 1 deletion tests/ui/const-generics/fn-const-param-infer.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: using function pointers as const generic parameters is forbidden
LL | struct Checked<const F: fn(usize) -> bool>;
| ^^^^^^^^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`

error[E0308]: mismatched types
--> $DIR/fn-const-param-infer.rs:33:25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ error: `Config` is forbidden as the type of a const generic parameter
LL | struct B<const CFG: Config> {
| ^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ error: `[usize; x]` is forbidden as the type of a const generic parameter
LL | pub struct A<const z: [usize; x]> {}
| ^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ error: `[[usize; v4]; v4]` is forbidden as the type of a const generic parameter
LL | pub struct v17<const v10: usize, const v7: v11> {
| ^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`
help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types
|
LL + #![feature(adt_const_params)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/ice-118285-fn-ptr-value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: using function pointers as const generic parameters is forbidden
LL | struct Checked<const F: fn(usize) -> bool>;
| ^^^^^^^^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`
= note: the only supported types are integers, `bool`, and `char`

error: aborting due to 1 previous error

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