From c95761385ea72929c1901b8a2c74506960a9c220 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 15 Apr 2024 21:45:47 -0400 Subject: [PATCH] Make array suggestions slightly more accurate --- .../src/traits/error_reporting/suggestions.rs | 13 ++++++++++--- tests/ui/moves/move-into-dead-array-1.stderr | 4 ++-- 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 @@ fn ty_kind_suggestion(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> Opt 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 @@ fn ty_kind_suggestion(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> Opt 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