Skip to content

Commit 9dca26c

Browse files
author
Jakub Wieczorek
committedJun 20, 2014
Add unreachability detection for missized patterns of fixed size vectors
Fixed #13482
1 parent 1e68d57 commit 9dca26c

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed
 

‎src/librustc/middle/typeck/check/_match.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,9 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
632632
fcx.infcx().next_region_var(
633633
infer::PatternRegion(pat.span));
634634

635-
let check_err = || {
636-
for elt in before.iter() {
637-
check_pat(pcx, &**elt, ty::mk_err());
635+
let check_err = |found: String| {
636+
for &elt in before.iter() {
637+
check_pat(pcx, &*elt, ty::mk_err());
638638
}
639639
for elt in slice.iter() {
640640
check_pat(pcx, &**elt, ty::mk_err());
@@ -653,15 +653,16 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
653653
})
654654
},
655655
Some(expected),
656-
"a vector pattern".to_string(),
656+
found,
657657
None);
658658
fcx.write_error(pat.id);
659659
};
660660

661-
let (elt_type, region_var, mutbl) = match *structure_of(fcx,
661+
let (elt_type, region_var, mutbl, fixed) = match *structure_of(fcx,
662662
pat.span,
663663
expected) {
664-
ty::ty_vec(mt, Some(_)) => (mt.ty, default_region_var, ast::MutImmutable),
664+
ty::ty_vec(mt, Some(fixed)) =>
665+
(mt.ty, default_region_var, ast::MutImmutable, Some(fixed)),
665666
ty::ty_uniq(t) => match ty::get(t).sty {
666667
ty::ty_vec(mt, None) => {
667668
fcx.type_error_message(pat.span,
@@ -671,25 +672,40 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
671672
},
672673
expected,
673674
None);
674-
(mt.ty, default_region_var, ast::MutImmutable)
675+
(mt.ty, default_region_var, ast::MutImmutable, None)
675676
}
676677
_ => {
677-
check_err();
678+
check_err("a vector pattern".to_string());
678679
return;
679680
}
680681
},
681682
ty::ty_rptr(r, mt) => match ty::get(mt.ty).sty {
682-
ty::ty_vec(mt, None) => (mt.ty, r, mt.mutbl),
683+
ty::ty_vec(mt, None) => (mt.ty, r, mt.mutbl, None),
683684
_ => {
684-
check_err();
685+
check_err("a vector pattern".to_string());
685686
return;
686687
}
687688
},
688689
_ => {
689-
check_err();
690+
check_err("a vector pattern".to_string());
690691
return;
691692
}
692693
};
694+
695+
let min_len = before.len() + after.len();
696+
fixed.and_then(|count| match slice {
697+
Some(_) if count < min_len =>
698+
Some(format!("a fixed vector pattern of size at least {}", min_len)),
699+
700+
None if count != min_len =>
701+
Some(format!("a fixed vector pattern of size {}", min_len)),
702+
703+
_ => None
704+
}).and_then(|message| {
705+
check_err(message);
706+
Some(())
707+
});
708+
693709
for elt in before.iter() {
694710
check_pat(pcx, &**elt, elt_type);
695711
}

‎src/test/compile-fail/issue-13482.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x = [1,2];
13+
let y = match x {
14+
[] => None,
15+
//~^ ERROR expected `[<generic integer #1>, .. 2]` but found a fixed vector pattern of size 0
16+
[a,_] => Some(a)
17+
};
18+
}

0 commit comments

Comments
 (0)
Please sign in to comment.