diff options
| author | Pavel Grigorenko <GrigorenkoPV@ya.ru> | 2024-06-24 15:30:24 +0300 |
|---|---|---|
| committer | Pavel Grigorenko <GrigorenkoPV@ya.ru> | 2024-06-24 15:30:24 +0300 |
| commit | ba5ec1fc5c68dc727e5f68cd804829c4f9eb9b81 (patch) | |
| tree | cde36e2352d3c620140b8d38434efd406dadb1be /compiler/rustc_trait_selection/src | |
| parent | 2c243d957008f5909f7a4af19e486ea8a3814be7 (diff) | |
| download | rust-ba5ec1fc5c68dc727e5f68cd804829c4f9eb9b81.tar.gz rust-ba5ec1fc5c68dc727e5f68cd804829c4f9eb9b81.zip | |
Suggest inline const blocks for array initialization
Diffstat (limited to 'compiler/rustc_trait_selection/src')
| -rw-r--r-- | compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs | 40 |
1 files changed, 7 insertions, 33 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index b2fa3489dda..f9cdca6360e 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -2915,38 +2915,21 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { } ObligationCauseCode::RepeatElementCopy { is_constable, - elt_type, + elt_type: _, elt_span, - elt_stmt_span, + elt_stmt_span: _, } => { err.note( "the `Copy` trait is required because this value will be copied for each element of the array", ); - let value_kind = match is_constable { - IsConstable::Fn => Some("the result of the function call"), - IsConstable::Ctor => Some("the result of the constructor"), - _ => None, - }; let sm = tcx.sess.source_map(); - if let Some(value_kind) = value_kind + if matches!(is_constable, IsConstable::Fn | IsConstable::Ctor) && let Ok(snip) = sm.span_to_snippet(elt_span) { - let help_msg = format!( - "consider creating a new `const` item and initializing it with {value_kind} \ - to be used in the repeat position" - ); - let indentation = sm.indentation_before(elt_stmt_span).unwrap_or_default(); - err.multipart_suggestion( - help_msg, - vec![ - ( - elt_stmt_span.shrink_to_lo(), - format!( - "const ARRAY_REPEAT_VALUE: {elt_type} = {snip};\n{indentation}" - ), - ), - (elt_span, "ARRAY_REPEAT_VALUE".to_string()), - ], + err.span_suggestion( + elt_span, + "create an inline `const` block", + format!("const {{ {snip} }}"), Applicability::MachineApplicable, ); } else { @@ -2954,15 +2937,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { err.help("consider using `core::array::from_fn` to initialize the array"); err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html for more information"); } - - if tcx.sess.is_nightly_build() - && matches!(is_constable, IsConstable::Fn | IsConstable::Ctor) - { - err.help( - "create an inline `const` block, see RFC #2920 \ - <https://github.com/rust-lang/rfcs/pull/2920> for more information", - ); - } } ObligationCauseCode::VariableType(hir_id) => { if let Some(typeck_results) = &self.typeck_results |
