Skip to content

Commit 2e6b52c

Browse files
authored
Unrolled build for rust-lang#121987
Rollup merge of rust-lang#121987 - Nadrieril:abort-on-arity-mismatch, r=compiler-errors pattern analysis: abort on arity mismatch This is one more PR replacing panics by `Err()` aborts. I recently audited all the `unwrap()` calls, but I had forgotten about array accesses. (Again [discovered by rust-analyzer](rust-lang/rust-analyzer#16746)). r? ```@compiler-errors```
2 parents 5a1e544 + 2af01a2 commit 2e6b52c

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

compiler/rustc_pattern_analysis/src/usefulness.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -1001,19 +1001,26 @@ impl<'p, Cx: TypeCx> PatStack<'p, Cx> {
10011001
/// Only call if `ctor.is_covered_by(self.head().ctor())` is true.
10021002
fn pop_head_constructor(
10031003
&self,
1004+
cx: &Cx,
10041005
ctor: &Constructor<Cx>,
10051006
ctor_arity: usize,
10061007
ctor_is_relevant: bool,
1007-
) -> PatStack<'p, Cx> {
1008+
) -> Result<PatStack<'p, Cx>, Cx::Error> {
10081009
// We pop the head pattern and push the new fields extracted from the arguments of
10091010
// `self.head()`.
10101011
let mut new_pats = self.head().specialize(ctor, ctor_arity);
1012+
if new_pats.len() != ctor_arity {
1013+
return Err(cx.bug(format_args!(
1014+
"uncaught type error: pattern {:?} has inconsistent arity (expected arity {ctor_arity})",
1015+
self.head().as_pat().unwrap()
1016+
)));
1017+
}
10111018
new_pats.extend_from_slice(&self.pats[1..]);
10121019
// `ctor` is relevant for this row if it is the actual constructor of this row, or if the
10131020
// row has a wildcard and `ctor` is relevant for wildcards.
10141021
let ctor_is_relevant =
10151022
!matches!(self.head().ctor(), Constructor::Wildcard) || ctor_is_relevant;
1016-
PatStack { pats: new_pats, relevant: self.relevant && ctor_is_relevant }
1023+
Ok(PatStack { pats: new_pats, relevant: self.relevant && ctor_is_relevant })
10171024
}
10181025
}
10191026

@@ -1083,18 +1090,19 @@ impl<'p, Cx: TypeCx> MatrixRow<'p, Cx> {
10831090
/// Only call if `ctor.is_covered_by(self.head().ctor())` is true.
10841091
fn pop_head_constructor(
10851092
&self,
1093+
cx: &Cx,
10861094
ctor: &Constructor<Cx>,
10871095
ctor_arity: usize,
10881096
ctor_is_relevant: bool,
10891097
parent_row: usize,
1090-
) -> MatrixRow<'p, Cx> {
1091-
MatrixRow {
1092-
pats: self.pats.pop_head_constructor(ctor, ctor_arity, ctor_is_relevant),
1098+
) -> Result<MatrixRow<'p, Cx>, Cx::Error> {
1099+
Ok(MatrixRow {
1100+
pats: self.pats.pop_head_constructor(cx, ctor, ctor_arity, ctor_is_relevant)?,
10931101
parent_row,
10941102
is_under_guard: self.is_under_guard,
10951103
useful: false,
10961104
intersects: BitSet::new_empty(0), // Initialized in `Matrix::expand_and_push`.
1097-
}
1105+
})
10981106
}
10991107
}
11001108

@@ -1217,7 +1225,7 @@ impl<'p, Cx: TypeCx> Matrix<'p, Cx> {
12171225
};
12181226
for (i, row) in self.rows().enumerate() {
12191227
if ctor.is_covered_by(pcx.cx, row.head().ctor())? {
1220-
let new_row = row.pop_head_constructor(ctor, arity, ctor_is_relevant, i);
1228+
let new_row = row.pop_head_constructor(pcx.cx, ctor, arity, ctor_is_relevant, i)?;
12211229
matrix.expand_and_push(new_row);
12221230
}
12231231
}

0 commit comments

Comments
 (0)