mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 23:25:03 +00:00
Fix index-out-of-bounds panic in match checking
This commit is contained in:
parent
5d7974e5fb
commit
9ecbadcedb
1 changed files with 26 additions and 2 deletions
|
@ -362,7 +362,12 @@ impl PatStack {
|
||||||
cx: &MatchCheckCtx,
|
cx: &MatchCheckCtx,
|
||||||
constructor: &Constructor,
|
constructor: &Constructor,
|
||||||
) -> MatchCheckResult<Option<PatStack>> {
|
) -> MatchCheckResult<Option<PatStack>> {
|
||||||
let result = match (self.head().as_pat(cx), constructor) {
|
if self.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let head_pat = self.head().as_pat(cx);
|
||||||
|
let result = match (head_pat, constructor) {
|
||||||
(Pat::Tuple { args: ref pat_ids, ellipsis }, Constructor::Tuple { arity: _ }) => {
|
(Pat::Tuple { args: ref pat_ids, ellipsis }, Constructor::Tuple { arity: _ }) => {
|
||||||
if ellipsis.is_some() {
|
if ellipsis.is_some() {
|
||||||
// If there are ellipsis here, we should add the correct number of
|
// If there are ellipsis here, we should add the correct number of
|
||||||
|
@ -531,7 +536,7 @@ impl Matrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn heads(&self) -> Vec<PatIdOrWild> {
|
fn heads(&self) -> Vec<PatIdOrWild> {
|
||||||
self.0.iter().map(|p| p.head()).collect()
|
self.0.iter().flat_map(|p| p.get_head()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes `D(self)` for each contained PatStack.
|
/// Computes `D(self)` for each contained PatStack.
|
||||||
|
@ -1992,6 +1997,25 @@ mod tests {
|
||||||
|
|
||||||
check_no_diagnostic(content);
|
check_no_diagnostic(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn or_pattern_panic() {
|
||||||
|
let content = r"
|
||||||
|
pub enum Category {
|
||||||
|
Infinity,
|
||||||
|
Zero,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn panic(a: Category, b: Category) {
|
||||||
|
match (a, b) {
|
||||||
|
(Category::Zero | Category::Infinity, _) => {}
|
||||||
|
(_, Category::Zero | Category::Infinity) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
check_no_diagnostic(content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue