Skip to content

Commit 920a973

Browse files
Don't emit spurious error for pattern matched array with erroneous len const
1 parent a9a8f79 commit 920a973

File tree

3 files changed

+4
-22
lines changed

3 files changed

+4
-22
lines changed

Diff for: compiler/rustc_hir_typeck/src/pat.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
1111
use rustc_hir::pat_util::EnumerateAndAdjustIterator;
1212
use rustc_hir::{self as hir, BindingMode, ByRef, HirId, LangItem, Mutability, Pat, PatKind};
1313
use rustc_infer::infer;
14-
use rustc_middle::mir::interpret::ErrorHandled;
1514
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
1615
use rustc_middle::{bug, span_bug};
1716
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
@@ -2413,17 +2412,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24132412
len: ty::Const<'tcx>,
24142413
min_len: u64,
24152414
) -> (Option<Ty<'tcx>>, Ty<'tcx>) {
2416-
let len = match len.eval(self.tcx, self.param_env, span) {
2417-
Ok((_, val)) => val
2418-
.try_to_scalar()
2419-
.and_then(|scalar| scalar.try_to_scalar_int().ok())
2420-
.map(|int| int.to_target_usize(self.tcx)),
2421-
Err(ErrorHandled::Reported(..)) => {
2422-
let guar = self.error_scrutinee_unfixed_length(span);
2423-
return (Some(Ty::new_error(self.tcx, guar)), arr_ty);
2424-
}
2425-
Err(ErrorHandled::TooGeneric(..)) => None,
2426-
};
2415+
let len = len.try_eval_target_usize(self.tcx, self.param_env);
24272416

24282417
let guar = if let Some(len) = len {
24292418
// Now we know the length...

Diff for: tests/ui/consts/issue-116186.rs

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

Diff for: tests/ui/consts/issue-116186.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ help: you might be missing a const parameter
99
LL | fn something<const N: /* Type */>(path: [usize; N]) -> impl Clone {
1010
| +++++++++++++++++++++
1111

12-
error[E0730]: cannot pattern-match on an array without a fixed length
13-
--> $DIR/issue-116186.rs:7:9
14-
|
15-
LL | [] => 0,
16-
| ^^
17-
18-
error: aborting due to 2 previous errors
12+
error: aborting due to 1 previous error
1913

20-
Some errors have detailed explanations: E0425, E0730.
21-
For more information about an error, try `rustc --explain E0425`.
14+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)