about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-04-15 21:45:47 -0400
committerMichael Goulet <michael@errs.io>2024-04-15 21:45:47 -0400
commitc95761385ea72929c1901b8a2c74506960a9c220 (patch)
treea3331edbd21c04a699488b7ae23380a72bc4b520
parent8a981b6fee5bb52af9311bd317dd8a01ec60dd83 (diff)
downloadrust-c95761385ea72929c1901b8a2c74506960a9c220.tar.gz
rust-c95761385ea72929c1901b8a2c74506960a9c220.zip
Make array suggestions slightly more accurate
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs13
-rw-r--r--tests/ui/moves/move-into-dead-array-1.stderr4
2 files changed, 12 insertions, 5 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 f05e91a2995..8f167711bdf 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
@@ -4550,7 +4550,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
             self.type_implements_trait(default_trait, [ty], param_env).must_apply_modulo_regions()
         };
 
-        Some(match ty.kind() {
+        Some(match *ty.kind() {
             ty::Never | ty::Error(_) => return None,
             ty::Bool => "false".to_string(),
             ty::Char => "\'x\'".to_string(),
@@ -4577,12 +4577,19 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
                 if let (ty::Str, hir::Mutability::Not) = (ty.kind(), mutability) {
                     "\"\"".to_string()
                 } else {
-                    let ty = self.ty_kind_suggestion(param_env, *ty)?;
+                    let ty = self.ty_kind_suggestion(param_env, ty)?;
                     format!("&{}{ty}", mutability.prefix_str())
                 }
             }
             ty::Array(ty, len) if let Some(len) = len.try_eval_target_usize(tcx, param_env) => {
-                format!("[{}; {}]", self.ty_kind_suggestion(param_env, *ty)?, len)
+                if len == 0 {
+                    "[]".to_string()
+                } else if self.type_is_copy_modulo_regions(param_env, ty) || len == 1 {
+                    // Can only suggest `[ty; 0]` if sz == 1 or copy
+                    format!("[{}; {}]", self.ty_kind_suggestion(param_env, ty)?, len)
+                } else {
+                    "/* value */".to_string()
+                }
             }
             ty::Tuple(tys) => format!(
                 "({}{})",
diff --git a/tests/ui/moves/move-into-dead-array-1.stderr b/tests/ui/moves/move-into-dead-array-1.stderr
index 6fb3fd83fc0..14208f5e7aa 100644
--- a/tests/ui/moves/move-into-dead-array-1.stderr
+++ b/tests/ui/moves/move-into-dead-array-1.stderr
@@ -8,8 +8,8 @@ LL |     a[i] = d();
    |
 help: consider assigning a value
    |
-LL |     let mut a: [D; 4] = [/* value */; 4];
-   |                       ++++++++++++++++++
+LL |     let mut a: [D; 4] = /* value */;
+   |                       +++++++++++++
 
 error: aborting due to 1 previous error