diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-06-06 23:53:17 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-06 23:53:17 +0200 |
| commit | 4ce2db5b31fcf7a40aa6f7cbb466c8387905cf47 (patch) | |
| tree | 007bd0b0d6920817550de0b13370f123d9702725 | |
| parent | 93ca0af08ca056e9e38d172e9b224d1e607a8e99 (diff) | |
| parent | 35cb28b7cfd2659bb1ac5ebed026694770199d0d (diff) | |
| download | rust-4ce2db5b31fcf7a40aa6f7cbb466c8387905cf47.tar.gz rust-4ce2db5b31fcf7a40aa6f7cbb466c8387905cf47.zip | |
Rollup merge of #142043 - estebank:const-suggestion, r=wesleywiser
Verbose suggestion to make param `const`
```
error[E0747]: type provided when a constant was expected
--> $DIR/invalid-const-arguments.rs:10:19
|
LL | impl<N> Foo for B<N> {}
| ^
|
help: consider changing this type parameter to a const parameter
|
LL - impl<N> Foo for B<N> {}
LL + impl<const N: u8> Foo for B<N> {}
|
```
Part of rust-lang/rust#141973.
3 files changed, 22 insertions, 10 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs index 21f0f9648ea..3a26b8331f8 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs @@ -73,7 +73,7 @@ fn generic_arg_mismatch_err( let param_name = tcx.hir_ty_param_name(param_local_id); let param_type = tcx.type_of(param.def_id).instantiate_identity(); if param_type.is_suggestable(tcx, false) { - err.span_suggestion( + err.span_suggestion_verbose( tcx.def_span(src_def_id), "consider changing this type parameter to a const parameter", format!("const {param_name}: {param_type}"), diff --git a/tests/ui/const-generics/early/invalid-const-arguments.stderr b/tests/ui/const-generics/early/invalid-const-arguments.stderr index 86b4d006454..3a9868836ec 100644 --- a/tests/ui/const-generics/early/invalid-const-arguments.stderr +++ b/tests/ui/const-generics/early/invalid-const-arguments.stderr @@ -51,9 +51,13 @@ error[E0747]: type provided when a constant was expected --> $DIR/invalid-const-arguments.rs:10:19 | LL | impl<N> Foo for B<N> {} - | - ^ - | | - | help: consider changing this type parameter to a const parameter: `const N: u8` + | ^ + | +help: consider changing this type parameter to a const parameter + | +LL - impl<N> Foo for B<N> {} +LL + impl<const N: u8> Foo for B<N> {} + | error[E0747]: unresolved item provided when a constant was expected --> $DIR/invalid-const-arguments.rs:14:32 diff --git a/tests/ui/const-generics/kind_mismatch.stderr b/tests/ui/const-generics/kind_mismatch.stderr index 1487b189619..12a2ab141a6 100644 --- a/tests/ui/const-generics/kind_mismatch.stderr +++ b/tests/ui/const-generics/kind_mismatch.stderr @@ -2,17 +2,25 @@ error[E0747]: type provided when a constant was expected --> $DIR/kind_mismatch.rs:11:38 | LL | impl<K> ContainsKey<K> for KeyHolder<K> {} - | - ^ - | | - | help: consider changing this type parameter to a const parameter: `const K: u8` + | ^ + | +help: consider changing this type parameter to a const parameter + | +LL - impl<K> ContainsKey<K> for KeyHolder<K> {} +LL + impl<const K: u8> ContainsKey<K> for KeyHolder<K> {} + | error[E0747]: type provided when a constant was expected --> $DIR/kind_mismatch.rs:11:21 | LL | impl<K> ContainsKey<K> for KeyHolder<K> {} - | - ^ - | | - | help: consider changing this type parameter to a const parameter: `const K: u8` + | ^ + | +help: consider changing this type parameter to a const parameter + | +LL - impl<K> ContainsKey<K> for KeyHolder<K> {} +LL + impl<const K: u8> ContainsKey<K> for KeyHolder<K> {} + | error: aborting due to 2 previous errors |
