Skip to content

Fix #8391 #14171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 30 additions & 35 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
@@ -239,7 +239,15 @@ fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@Pat]) -> useful {
return not_useful
}
let real_pat = match m.iter().find(|r| r.get(0).id != 0) {
Some(r) => *r.get(0), None => v[0]
Some(r) => {
match r.get(0).node {
// An arm of the form `ref x @ sub_pat` has type
// `sub_pat`, not `&sub_pat` as `x` itself does.
PatIdent(BindByRef(_), _, Some(sub)) => sub,
_ => *r.get(0)
}
}
None => v[0]
};
let left_ty = if real_pat.id == 0 { ty::mk_nil() }
else { ty::node_id_to_type(cx.tcx, real_pat.id) };
@@ -258,15 +266,15 @@ fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@Pat]) -> useful {
val(const_bool(false)),
0u, left_ty)
}
ref u => (*u).clone(),
u => u,
}
}
ty::ty_enum(eid, _) => {
for va in (*ty::enum_variants(cx.tcx, eid)).iter() {
match is_useful_specialized(cx, m, v, variant(va.id),
va.args.len(), left_ty) {
not_useful => (),
ref u => return (*u).clone(),
u => return u,
}
}
not_useful
@@ -288,7 +296,7 @@ fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@Pat]) -> useful {
for n in iter::range(0u, max_len + 1) {
match is_useful_specialized(cx, m, v, vec(n), n, left_ty) {
not_useful => (),
ref u => return (*u).clone(),
u => return u,
}
}
not_useful
@@ -304,21 +312,21 @@ fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@Pat]) -> useful {
}
}
}
Some(ref ctor) => {
Some(ctor) => {
match is_useful(cx,
&m.iter().filter_map(|r| {
default(cx, r.as_slice())
}).collect::<matrix>(),
v.tail()) {
useful_ => useful(left_ty, (*ctor).clone()),
ref u => (*u).clone(),
useful_ => useful(left_ty, ctor),
u => u,
}
}
}
}
Some(ref v0_ctor) => {
let arity = ctor_arity(cx, v0_ctor, left_ty);
is_useful_specialized(cx, m, v, (*v0_ctor).clone(), arity, left_ty)
Some(v0_ctor) => {
let arity = ctor_arity(cx, &v0_ctor, left_ty);
is_useful_specialized(cx, m, v, v0_ctor, arity, left_ty)
}
}
}
@@ -337,7 +345,7 @@ fn is_useful_specialized(cx: &MatchCheckCtxt,
cx, &ms, specialize(cx, v, &ctor, arity, lty).unwrap().as_slice());
match could_be_useful {
useful_ => useful(lty, ctor),
ref u => (*u).clone(),
u => u,
}
}

@@ -408,9 +416,9 @@ fn missing_ctor(cx: &MatchCheckCtxt,
let mut found = Vec::new();
for r in m.iter() {
let r = pat_ctor_id(cx, *r.get(0));
for id in r.iter() {
if !found.contains(id) {
found.push((*id).clone());
for id in r.move_iter() {
if !found.contains(&id) {
found.push(id);
}
}
}
@@ -812,30 +820,17 @@ fn specialize(cx: &MatchCheckCtxt,
let num_elements = before.len() + after.len();
if num_elements < arity && slice.is_some() {
let mut result = Vec::new();
for pat in before.iter() {
result.push((*pat).clone());
}
for _ in iter::range(0, arity - num_elements) {
result.push(wild())
}
for pat in after.iter() {
result.push((*pat).clone());
}
for pat in r.tail().iter() {
result.push((*pat).clone());
}
let wilds = Vec::from_elem(arity - num_elements, wild());
result.push_all_move(before);
result.push_all_move(wilds);
result.push_all_move(after);
result.push_all(r.tail());
Some(result)
} else if num_elements == arity {
let mut result = Vec::new();
for pat in before.iter() {
result.push((*pat).clone());
}
for pat in after.iter() {
result.push((*pat).clone());
}
for pat in r.tail().iter() {
result.push((*pat).clone());
}
result.push_all_move(before);
result.push_all_move(after);
result.push_all(r.tail());
Some(result)
} else {
None
5 changes: 3 additions & 2 deletions src/test/run-pass/issue-8391.rs
Original file line number Diff line number Diff line change
@@ -9,8 +9,9 @@
// except according to those terms.

fn main() {
let _x = match Some(1) {
_y @ Some(_) => 1,
let x = match Some(1) {
ref _y @ Some(_) => 1,
None => 2,
};
assert_eq!(x, 1);
}