From e4c71f1fd8caf703dde7edb75b3cd33585aadddc Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 14 Apr 2024 09:21:38 -0400 Subject: [PATCH] Fix value suggestion for array in generic context --- compiler/rustc_trait_selection/src/lib.rs | 1 + .../src/traits/error_reporting/suggestions.rs | 8 +++----- tests/ui/consts/value-suggestion-ice-123906.rs | 8 ++++++++ .../consts/value-suggestion-ice-123906.stderr | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 tests/ui/consts/value-suggestion-ice-123906.rs create mode 100644 tests/ui/consts/value-suggestion-ice-123906.stderr diff --git a/compiler/rustc_trait_selection/src/lib.rs b/compiler/rustc_trait_selection/src/lib.rs index b5fb710e4cc..057d00aeae8 100644 --- a/compiler/rustc_trait_selection/src/lib.rs +++ b/compiler/rustc_trait_selection/src/lib.rs @@ -22,6 +22,7 @@ #![feature(box_patterns)] #![feature(control_flow_enum)] #![feature(extract_if)] +#![feature(if_let_guard)] #![feature(let_chains)] #![feature(option_take_if)] #![feature(never_type)] 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 6ae7cbf717b..36fb5c8a6f7 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -4583,11 +4583,9 @@ fn ty_kind_suggestion(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> Opt format!("&{}{ty}", mutability.prefix_str()) } } - ty::Array(ty, len) => format!( - "[{}; {}]", - self.ty_kind_suggestion(param_env, *ty)?, - len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()), - ), + 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) + } ty::Tuple(tys) => format!( "({})", tys.iter() diff --git a/tests/ui/consts/value-suggestion-ice-123906.rs b/tests/ui/consts/value-suggestion-ice-123906.rs new file mode 100644 index 00000000000..531bbc15852 --- /dev/null +++ b/tests/ui/consts/value-suggestion-ice-123906.rs @@ -0,0 +1,8 @@ +fn as_chunks() -> [u8; N] { + loop { + break; + //~^ ERROR mismatched types + } +} + +fn main() {} diff --git a/tests/ui/consts/value-suggestion-ice-123906.stderr b/tests/ui/consts/value-suggestion-ice-123906.stderr new file mode 100644 index 00000000000..8e2c316400d --- /dev/null +++ b/tests/ui/consts/value-suggestion-ice-123906.stderr @@ -0,0 +1,18 @@ +error[E0308]: mismatched types + --> $DIR/value-suggestion-ice-123906.rs:3:9 + | +LL | fn as_chunks() -> [u8; N] { + | ------- expected `[u8; ]` because of this return type +LL | loop { + | ---- this loop is expected to be of type `[u8; N]` +LL | break; + | ^^^^^ expected `[u8; N]`, found `()` + | +help: give the `break` a value of the expected type + | +LL | break value; + | +++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`.