about summary refs log tree commit diff
path: root/compiler/rustc_trait_selection
diff options
context:
space:
mode:
authorHenry Boisdequin <65845077+henryboisdequin@users.noreply.github.com>2021-01-29 11:54:19 +0530
committerHenry Boisdequin <65845077+henryboisdequin@users.noreply.github.com>2021-02-03 10:18:08 +0530
commitc2e849c0221bbaf1106884cd776e375168a95323 (patch)
tree2817749464cf18a0471f6333e0457576889aa433 /compiler/rustc_trait_selection
parent368275062fb655c1f36e0398f88b15379a1f3c93 (diff)
added a suggestion to create a `const` item if the `fn` in the array repeat expression is a `const fn`
Diffstat (limited to 'compiler/rustc_trait_selection')
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs18
1 files changed, 17 insertions, 1 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 21828006164..859e2fb7162 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
@@ -1881,10 +1881,26 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
             ObligationCauseCode::Coercion { source: _, target } => {
                 err.note(&format!("required by cast to type `{}`", self.ty_to_string(target)));
             }
-            ObligationCauseCode::RepeatVec => {
+            ObligationCauseCode::RepeatVec(is_const_fn) => {
                 err.note(
                     "the `Copy` trait is required because the repeated element will be copied",
                 );
+                if is_const_fn && !self.tcx.sess.is_nightly_build() {
+                    err.help(
+                        "consider creating a new `const` item and initializing with the result \
+                        of the function call to be used in the repeat position, like \
+                        `const VAL: Type = const_fn();` and `let x = [VAL; 42];`",
+                    );
+                } else if self.tcx.sess.is_nightly_build() && is_const_fn {
+                    err.help(
+                        "create an inline `const` block, see PR \
+                        #2920 <https://github.com/rust-lang/rfcs/pull/2920> \
+                        for more information",
+                    );
+                } else {
+                    // Don't suggest anything to the user as suggesting the user to make the function `const`
+                    // could lead them down the wrong path.
+                }
             }
             ObligationCauseCode::VariableType(hir_id) => {
                 let parent_node = self.tcx.hir().get_parent_node(hir_id);