8000 Rollup merge of #126899 - GrigorenkoPV:suggest-const-block, r=davidtwco · rust-lang/rust@a7721a0 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a7721a0

Browse files
Rollup merge of #126899 - GrigorenkoPV:suggest-const-block, r=davidtwco
Suggest inline const blocks for array initialization #126894
2 parents 9ce2a07 + ba5ec1f commit a7721a0

File tree

4 files changed

+26
-66
lines changed

4 files changed

+26
-66
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,54 +2915,28 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
29152915
}
29162916
ObligationCauseCode::RepeatElementCopy {
29172917
is_constable,
2918-
elt_type,
2918+
elt_type: _,
29192919
elt_span,
2920-
elt_stmt_span,
2920+
elt_stmt_span: _,
29212921
} => {
29222922
err.note(
29232923
"the `Copy` trait is required because this value will be copied for each element of the array",
29242924
);
2925-
let value_kind = match is_constable {
2926-
IsConstable::Fn => Some("the result of the function call"),
2927-
IsConstable::Ctor => Some("the result of the constructor"),
2928-
_ => None,
2929-
};
29302925
let sm = tcx.sess.source_map();
2931-
if let Some(value_kind) = value_kind
2926+
if matches!(is_constable, IsConstable::Fn | IsConstable::Ctor)
29322927
&& let Ok(snip) = sm.span_to_snippet(elt_span)
29332928
{
2934-
let help_msg = format!(
2935-
"consider creating a new `const` item and initializing it with {value_kind} \
2936-
to be used in the repeat position"
2937-
);
2938-
let indentation = sm.indentation_before(elt_stmt_span).unwrap_or_default();
2939-
err.multipart_suggestion(
2940-
help_msg,
2941-
vec![
2942-
(
2943-
elt_stmt_span.shrink_to_lo(),
2944-
format!(
2945-
"const ARRAY_REPEAT_VALUE: {elt_type} = {snip};\n{indentation}"
2946-
),
2947-
),
2948-
(elt_span, "ARRAY_REPEAT_VALUE".to_string()),
2949-
],
2929+
err.span_suggestion(
2930+
elt_span,
2931+
"create an inline `const` block",
2932+
format!("const {{ {snip} }}"),
29502933
Applicability::MachineApplicable,
29512934
);
29522935
} else {
29532936
// FIXME: we may suggest array::repeat instead
29542937
err.help("consider using `core::array::from_fn` to initialize the array");
29552938
err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html for more information");
29562939
}
2957-
2958-
if tcx.sess.is_nightly_build()
2959-
&& matches!(is_constable, IsConstable::Fn | IsConstable::Ctor)
2960-
{
2961-
err.help(
2962-
"create an inline `const` block, see RFC #2920 \
2963-
<https://github.com/rust-lang/rfcs/pull/2920> for more information",
2964-
);
2965-
}
29662940
}
29672941
ObligationCauseCode::VariableType(hir_id) => {
29682942
if let Some(typeck_results) = &self.typeck_results

tests/ui/consts/const-blocks/fn-call-in-non-const.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ LL | let _: [Option<Bar>; 2] = [no_copy(); 2];
66
|
77
= note: required for `Option<Bar>` to implement `Copy`
88
= note: the `Copy` trait is required because this value will be copied for each element of the array
9-
= help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information
109
help: consider annotating `Bar` with `#[derive(Copy)]`
1110
|
1211
LL + #[derive(Copy)]
1312
LL | struct Bar;
1413
|
15-
help: consider creating a new `const` item and initializing it with the result of the function call to be used in the repeat position
16-
|
17-
LL ~ const ARRAY_REPEAT_VALUE: Option<Bar> = no_copy();
18-
LL ~ let _: [Option<Bar>; 2] = [ARRAY_REPEAT_VALUE; 2];
14+
help: create an inline `const` block
1915
|
16+
LL | let _: [Option<Bar>; 2] = [const { no_copy() }; 2];
17+
| ~~~~~~~~~~~~~~~~~~~
2018

2119
error: aborting due to 1 previous error
2220

tests/ui/consts/const-blocks/trait-error.stderr

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@ error[E0277]: the trait bound `String: Copy` is not satisfied
22
--> $DIR/trait-error.rs:5:6
33
|
44
LL | [Foo(String::new()); 4];
5-
| ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`, which is required by `Foo<String>: Copy`
5+
| ^^^^^^^^^^^^^^^^^^
6+
| |
7+
| the trait `Copy` is not implemented for `String`, which is required by `Foo<String>: Copy`
8+
| help: create an inline `const` block: `const { Foo(String::new()) }`
69
|
710
note: required for `Foo<String>` to implement `Copy`
811
--> $DIR/trait-error.rs:1:10
912
|
1013
LL | #[derive(Copy, Clone)]
1114
| ^^^^ unsatisfied trait bound introduced in this `derive` macro
1215
= note: the `Copy` trait is required because this value will be copied for each element of the array
13-
= help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information
1416
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
15-
help: consider creating a new `const` item and initializing it with the result of the function call to be used in the repeat position
16-
|
17-
LL ~ const ARRAY_REPEAT_VALUE: Foo<String> = Foo(String::new());
18-
LL ~ [ARRAY_REPEAT_VALUE; 4];
19-
|
2017

2118
error: aborting due to 1 previous error
2219

tests/ui/consts/const-fn-in-vec.stderr

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,36 @@ error[E0277]: the trait bound `String: Copy` is not satisfied
22
--> $DIR/const-fn-in-vec.rs:1:47
33
|
44
LL | static _MAYBE_STRINGS: [Option<String>; 5] = [None; 5];
5-
| ^^^^ the trait `Copy` is not implemented for `String`, which is required by `Option<String>: Copy`
5+
| ^^^^
6+
| |
7+
| the trait `Copy` is not implemented for `String`, which is required by `Option<String>: Copy`
8+
| help: create an inline `const` block: `const { None }`
69
|
710
= note: required for `Option<String>` to implement `Copy`
811
= note: the `Copy` trait is required because this value will be copied for each element of the array
9-
= help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information
10-
help: consider creating a new `const` item and initializing it with the result of the constructor to be used in the repeat position
11-
|
12-
LL + const ARRAY_REPEAT_VALUE: Option<String> = None;
13-
LL ~ static _MAYBE_STRINGS: [Option<String>; 5] = [ARRAY_REPEAT_VALUE; 5];
14-
|
1512

1613
error[E0277]: the trait bound `String: Copy` is not satisfied
1714
--> $DIR/const-fn-in-vec.rs:7:34
1815
|
1916
LL | let _strings: [String; 5] = [String::new(); 5];
20-
| ^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
17+
| ^^^^^^^^^^^^^
18+
| |
19+
| the trait `Copy` is not implemented for `String`
20+
| help: create an inline `const` block: `const { String::new() }`
2121
|
2222
= note: the `Copy` trait is required because this value will be copied for each element of the array
23-
= help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information
24-
help: consider creating a new `const` item and initializing it with the result of the function call to be used in the repeat position
25-
|
26-
LL ~ const ARRAY_REPEAT_VALUE: String = String::new();
27-
LL ~ let _strings: [String; 5] = [ARRAY_REPEAT_VALUE; 5];
28-
|
2923

3024
error[E0277]: the trait bound `String: Copy` is not satisfied
3125
--> $DIR/const-fn-in-vec.rs:9:48
3226
|
3327
LL | let _maybe_strings: [Option<String>; 5] = [None; 5];
34-
| ^^^^ the trait `Copy` is not implemented for `String`, which is required by `Option<String>: Copy`
28+
| ^^^^
29+
| |
30+
| the trait `Copy` is not implemented for `String`, which is required by `Option<String>: Copy`
31+
| help: create an inline `const` block: `const { None }`
3532
|
3633
= note: required for `Option<String>` to implement `Copy`
3734
= note: the `Copy` trait is required because this value will be copied for each element of the array
38-
= help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information
39-
help: consider creating a new `const` item and initializing it with the result of the constructor to be used in the repeat position
40-
|
41-
LL ~ const ARRAY_REPEAT_VALUE: Option<String> = None;
42-
LL ~ let _maybe_strings: [Option<String>; 5] = [ARRAY_REPEAT_VALUE; 5];
43-
|
4435

4536
error: aborting due to 3 previous errors
4637

0 commit comments

Comments
 (0)
0