Skip to content

Commit 9a583bb

Browse files
committed
auto merge of #15086 : jakub-/rust/xc-struct-variants-match, r=alexcrichton
Turns out field names of struct variants are not encoded in crate metadata.
2 parents 1efc02a + 26e692d commit 9a583bb

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

Diff for: src/librustc/middle/check_match.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, m: &Matrix) {
194194
#[deriving(Clone, PartialEq)]
195195
enum ctor {
196196
single,
197-
variant(DefId /* variant */, bool /* is_structure */),
197+
variant(DefId),
198198
val(const_val),
199199
range(const_val, const_val),
200200
vec(uint)
@@ -218,7 +218,8 @@ fn construct_witness(cx: &MatchCheckCtxt, ctor: &ctor, pats: Vec<Gc<Pat>>, lty:
218218

219219
ty::ty_enum(cid, _) | ty::ty_struct(cid, _) => {
220220
let (vid, is_structure) = match ctor {
221-
&variant(vid, is_structure) => (vid, is_structure),
221+
&variant(vid) => (vid,
222+
ty::enum_variant_with_id(cx.tcx, cid, vid).arg_names.is_some()),
222223
_ => (cid, true)
223224
};
224225
if is_structure {
@@ -316,7 +317,7 @@ fn all_constructors(cx: &MatchCheckCtxt, m: &Matrix, left_ty: ty::t) -> Vec<ctor
316317
ty::ty_enum(eid, _) =>
317318
ty::enum_variants(cx.tcx, eid)
318319
.iter()
319-
.map(|va| variant(va.id, va.arg_names.is_some()))
320+
.map(|va| variant(va.id))
320321
.collect(),
321322

322323
ty::ty_vec(_, None) =>
@@ -440,7 +441,7 @@ fn pat_ctor_id(cx: &MatchCheckCtxt, left_ty: ty::t, p: Gc<Pat>) -> Option<ctor>
440441
let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
441442
Some(val(eval_const_expr(cx.tcx, &*const_expr)))
442443
},
443-
Some(&DefVariant(_, id, is_structure)) => Some(variant(id, is_structure)),
444+
Some(&DefVariant(_, id, _)) => Some(variant(id)),
444445
_ => None
445446
},
446447
PatEnum(..) =>
@@ -449,12 +450,12 @@ fn pat_ctor_id(cx: &MatchCheckCtxt, left_ty: ty::t, p: Gc<Pat>) -> Option<ctor>
449450
let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
450451
Some(val(eval_const_expr(cx.tcx, &*const_expr)))
451452
},
452-
Some(&DefVariant(_, id, is_structure)) => Some(variant(id, is_structure)),
453+
Some(&DefVariant(_, id, _)) => Some(variant(id)),
453454
_ => Some(single)
454455
},
455456
PatStruct(..) =>
456457
match cx.tcx.def_map.borrow().find(&pat.id) {
457-
Some(&DefVariant(_, id, is_structure)) => Some(variant(id, is_structure)),
458+
Some(&DefVariant(_, id, _)) => Some(variant(id)),
458459
_ => Some(single)
459460
},
460461
PatLit(expr) =>
@@ -504,7 +505,7 @@ fn constructor_arity(cx: &MatchCheckCtxt, ctor: &ctor, ty: ty::t) -> uint {
504505
},
505506
ty::ty_enum(eid, _) => {
506507
match *ctor {
507-
variant(id, _) => enum_variant_with_id(cx.tcx, eid, id).args.len(),
508+
variant(id) => enum_variant_with_id(cx.tcx, eid, id).args.len(),
508509
_ => unreachable!()
509510
}
510511
}
@@ -551,9 +552,10 @@ fn specialize(cx: &MatchCheckCtxt, r: &[Gc<Pat>],
551552
&PatIdent(_, _, _) => {
552553
let opt_def = cx.tcx.def_map.borrow().find_copy(pat_id);
553554
match opt_def {
554-
Some(DefVariant(_, id, _)) => match *ctor_id {
555-
variant(vid, _) if vid == id => Some(vec!()),
556-
_ => None
555+
Some(DefVariant(_, id, _)) => if *ctor_id == variant(id) {
556+
Some(vec!())
557+
} else {
558+
None
557559
},
558560
Some(DefStatic(did, _)) => {
559561
let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
@@ -587,7 +589,7 @@ fn specialize(cx: &MatchCheckCtxt, r: &[Gc<Pat>],
587589
}
588590
}
589591
}
590-
DefVariant(_, id, _) if variant(id, false) != *ctor_id => None,
592+
DefVariant(_, id, _) if *ctor_id != variant(id) => None,
591593
DefVariant(..) | DefFn(..) | DefStruct(..) => {
592594
Some(match args {
593595
&Some(ref args) => args.clone(),
@@ -602,7 +604,7 @@ fn specialize(cx: &MatchCheckCtxt, r: &[Gc<Pat>],
602604
// Is this a struct or an enum variant?
603605
let def = cx.tcx.def_map.borrow().get_copy(pat_id);
604606
let class_id = match def {
605-
DefVariant(_, variant_id, _) => if *ctor_id == variant(variant_id, true) {
607+
DefVariant(_, variant_id, _) => if *ctor_id == variant(variant_id) {
606608
Some(variant_id)
607609
} else {
608610
None

Diff for: src/test/auxiliary/struct_variant_xc_aux.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
#![feature(struct_variant)]
1515

1616
pub enum Enum {
17-
Variant { pub arg: u8 }
17+
Variant(u8),
18+
StructVariant { pub arg: u8 }
1819
}

Diff for: src/test/run-pass/struct_variant_xc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
// aux-build:struct_variant_xc_aux.rs
1212
extern crate struct_variant_xc_aux;
1313

14-
use struct_variant_xc_aux::Variant;
14+
use struct_variant_xc_aux::StructVariant;
1515

1616
pub fn main() {
17-
let _ = Variant { arg: 1 };
17+
let _ = StructVariant { arg: 1 };
1818
}

Diff for: src/test/run-pass/struct_variant_xc_match.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2013-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+
// aux-build:struct_variant_xc_aux.rs
12+
extern crate struct_variant_xc_aux;
13+
14+
use struct_variant_xc_aux::{StructVariant, Variant};
15+
16+
pub fn main() {
17+
let arg = match StructVariant { arg: 42 } {
18+
Variant(_) => unreachable!(),
19+
StructVariant { arg } => arg
20+
};
21+
assert_eq!(arg, 42);
22+
}

0 commit comments

Comments
 (0)