Skip to content

Commit 6de40ab

Browse files
committed
return unfixed len if pat has reported error
1 parent 8501f1c commit 6de40ab

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator;
1212
use rustc_hir::{HirId, Pat, PatKind};
1313
use rustc_infer::infer;
1414
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
15+
use rustc_middle::mir::interpret::ErrorHandled;
1516
use rustc_middle::ty::{self, Adt, BindingMode, Ty, TypeVisitableExt};
1617
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
1718
use rustc_span::edit_distance::find_best_match_for_name;
@@ -2164,7 +2165,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21642165
len: ty::Const<'tcx>,
21652166
min_len: u64,
21662167
) -> (Option<Ty<'tcx>>, Ty<'tcx>) {
2167-
let guar = if let Some(len) = len.try_eval_target_usize(self.tcx, self.param_env) {
2168+
let len = match len.eval(self.tcx, self.param_env, None) {
2169+
Ok(val) => val
2170+
.try_to_scalar()
2171+
.and_then(|scalar| scalar.try_to_int().ok())
2172+
.and_then(|int| int.try_to_target_usize(self.tcx).ok()),
2173+
Err(ErrorHandled::Reported(..)) => {
2174+
let guar = self.error_scrutinee_unfixed_length(span);
2175+
return (Some(Ty::new_error(self.tcx, guar)), arr_ty);
2176+
}
2177+
Err(ErrorHandled::TooGeneric(..)) => None,
2178+
};
2179+
2180+
let guar = if let Some(len) = len {
21682181
// Now we know the length...
21692182
if slice.is_none() {
21702183
// ...and since there is no variable-length pattern,

tests/ui/consts/issue-116186.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![allow(incomplete_features)]
2+
#![feature(generic_const_exprs)]
3+
4+
fn something(path: [usize; N]) -> impl Clone {
5+
//~^ ERROR cannot find value `N` in this scope
6+
match path {
7+
[] => 0, //~ ERROR cannot pattern-match on an array without a fixed length
8+
_ => 1,
9+
};
10+
}
11+
12+
fn main() {}

tests/ui/consts/issue-116186.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0425]: cannot find value `N` in this scope
2+
--> $DIR/issue-116186.rs:4:28
3+
|
4+
LL | fn something(path: [usize; N]) -> impl Clone {
5+
| ^ not found in this scope
6+
|
7+
help: you might be missing a const parameter
8+
|
9+
LL | fn something<const N: /* Type */>(path: [usize; N]) -> impl Clone {
10+
| +++++++++++++++++++++
11+
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
19+
20+
Some errors have detailed explanations: E0425, E0730.
21+
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)