mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 19:58:32 +00:00
Fix a #14731 regression in missing_constructor() for vector patterns
Fixes #15080.
This commit is contained in:
parent
f556c8cbd8
commit
d4da4ba4b2
@ -281,21 +281,27 @@ fn missing_constructor(cx: &MatchCheckCtxt, m: &Matrix, left_ty: ty::t) -> Optio
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn all_constructors(cx: &MatchCheckCtxt, m: &Matrix, left_ty: ty::t) -> Vec<ctor> {
|
fn all_constructors(cx: &MatchCheckCtxt, m: &Matrix, left_ty: ty::t) -> Vec<ctor> {
|
||||||
|
// This produces a list of all vector constructors that we would expect to appear
|
||||||
|
// in an exhaustive set of patterns. Because such a list would normally be infinite,
|
||||||
|
// we narrow it down to only those constructors that actually appear in the inspected
|
||||||
|
// column, plus, any that are missing and not covered by a pattern with a destructured slice.
|
||||||
fn vec_constructors(m: &Matrix) -> Vec<ctor> {
|
fn vec_constructors(m: &Matrix) -> Vec<ctor> {
|
||||||
let max_vec_len = m.iter().map(|r| match r.get(0).node {
|
let max_vec_len = m.iter().map(|r| match r.get(0).node {
|
||||||
PatVec(ref before, _, ref after) => before.len() + after.len(),
|
PatVec(ref before, _, ref after) => before.len() + after.len(),
|
||||||
_ => 0u
|
_ => 0u
|
||||||
}).max().unwrap_or(0u);
|
}).max().unwrap_or(0u);
|
||||||
let contains_slice = m.iter().any(|r| match r.get(0).node {
|
let min_vec_len_with_slice = m.iter().map(|r| match r.get(0).node {
|
||||||
PatVec(_, ref slice, _) => slice.is_some(),
|
PatVec(ref before, Some(_), ref after) => before.len() + after.len(),
|
||||||
_ => false
|
_ => max_vec_len + 1
|
||||||
});
|
}).min().unwrap_or(max_vec_len + 1);
|
||||||
let lengths = iter::range_inclusive(0u, if contains_slice {
|
let other_lengths = m.iter().map(|r| match r.get(0).node {
|
||||||
max_vec_len
|
PatVec(ref before, _, ref after) => before.len() + after.len(),
|
||||||
} else {
|
_ => 0u
|
||||||
max_vec_len + 1
|
}).filter(|&len| len > min_vec_len_with_slice);
|
||||||
});
|
iter::range_inclusive(0u, min_vec_len_with_slice)
|
||||||
lengths.map(|len| vec(len)).collect()
|
.chain(other_lengths)
|
||||||
|
.map(|len| vec(len))
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
match ty::get(left_ty).sty {
|
match ty::get(left_ty).sty {
|
||||||
|
30
src/test/run-pass/issue-15080.rs
Normal file
30
src/test/run-pass/issue-15080.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut x = &[1, 2, 3, 4];
|
||||||
|
|
||||||
|
let mut result = vec!();
|
||||||
|
loop {
|
||||||
|
x = match x {
|
||||||
|
[1, n, 3, ..rest] => {
|
||||||
|
result.push(n);
|
||||||
|
rest
|
||||||
|
}
|
||||||
|
[n, ..rest] => {
|
||||||
|
result.push(n);
|
||||||
|
rest
|
||||||
|
}
|
||||||
|
[] =>
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(result.as_slice() == [2, 4]);
|
||||||
|
}
|
@ -68,9 +68,17 @@ fn d() {
|
|||||||
assert_eq!(branch, 1);
|
assert_eq!(branch, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn e() {
|
||||||
|
match &[1, 2, 3] {
|
||||||
|
[1, 2] => (),
|
||||||
|
[..] => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
a();
|
a();
|
||||||
b();
|
b();
|
||||||
c();
|
c();
|
||||||
d();
|
d();
|
||||||
|
e();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user