Don't emit spurious error for pattern matched array with erroneous len const

This commit is contained in:
Michael Goulet 2024-09-20 20:16:49 -04:00
parent a9a8f79f86
commit 920a973bdb
3 changed files with 4 additions and 22 deletions

View File

@ -11,7 +11,6 @@
use rustc_hir::pat_util::EnumerateAndAdjustIterator;
use rustc_hir::{self as hir, BindingMode, ByRef, HirId, LangItem, Mutability, Pat, PatKind};
use rustc_infer::infer;
use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
use rustc_middle::{bug, span_bug};
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
@ -2413,17 +2412,7 @@ fn check_array_pat_len(
len: ty::Const<'tcx>,
min_len: u64,
) -> (Option<Ty<'tcx>>, Ty<'tcx>) {
let len = match len.eval(self.tcx, self.param_env, span) {
Ok((_, val)) => val
.try_to_scalar()
.and_then(|scalar| scalar.try_to_scalar_int().ok())
.map(|int| int.to_target_usize(self.tcx)),
Err(ErrorHandled::Reported(..)) => {
let guar = self.error_scrutinee_unfixed_length(span);
return (Some(Ty::new_error(self.tcx, guar)), arr_ty);
}
Err(ErrorHandled::TooGeneric(..)) => None,
};
let len = len.try_eval_target_usize(self.tcx, self.param_env);
let guar = if let Some(len) = len {
// Now we know the length...

View File

@ -4,7 +4,7 @@
fn something(path: [usize; N]) -> impl Clone {
//~^ ERROR cannot find value `N` in this scope
match path {
[] => 0, //~ ERROR cannot pattern-match on an array without a fixed length
[] => 0,
_ => 1,
};
}

View File

@ -9,13 +9,6 @@ help: you might be missing a const parameter
LL | fn something<const N: /* Type */>(path: [usize; N]) -> impl Clone {
| +++++++++++++++++++++
error[E0730]: cannot pattern-match on an array without a fixed length
--> $DIR/issue-116186.rs:7:9
|
LL | [] => 0,
| ^^
error: aborting due to 1 previous error
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0425, E0730.
For more information about an error, try `rustc --explain E0425`.
For more information about this error, try `rustc --explain E0425`.