Skip to content

Commit

Permalink
Don't emit spurious error for pattern matched array with erroneous le…
Browse files Browse the repository at this point in the history
…n const
  • Loading branch information
compiler-errors committed Sep 21, 2024
1 parent a9a8f79 commit 920a973
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 22 deletions.
13 changes: 1 addition & 12 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
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;
Expand Down Expand Up @@ -2413,17 +2412,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
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...
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/issue-116186.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down
11 changes: 2 additions & 9 deletions tests/ui/consts/issue-116186.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 2 previous errors
error: aborting due to 1 previous error

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`.

0 comments on commit 920a973

Please sign in to comment.