Skip to content

Commit e94a9f0

Browse files
committed
auto merge of #20997 : nikomatsakis/rust/assoc-types-enum-field-access, r=nick29581
Various fixes to enum field access. Builds on PR #20955. r? @nick29581
2 parents 6ba9acd + 487a4a1 commit e94a9f0

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

src/librustc_trans/trans/base.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,8 @@ pub fn iter_structural_ty<'blk, 'tcx, F>(cx: Block<'blk, 'tcx>,
702702
let mut cx = cx;
703703

704704
for (i, &arg) in variant.args.iter().enumerate() {
705-
cx = (*f)(cx,
706-
adt::trans_field_ptr(cx, repr, av, variant.disr_val, i),
707-
arg.subst(tcx, substs));
705+
let arg = monomorphize::apply_param_substs(tcx, substs, &arg);
706+
cx = f(cx, adt::trans_field_ptr(cx, repr, av, variant.disr_val, i), arg);
708707
}
709708
return cx;
710709
}

src/librustc_typeck/check/_match.rs

+2
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ pub fn check_struct_pat_fields<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
619619
}
620620
};
621621

622+
let field_type = pcx.fcx.normalize_associated_types_in(span, &field_type);
623+
622624
check_pat(pcx, &*field.pat, field_type);
623625
}
624626

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2015 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+
// Test associated types appearing in struct-like enum variants.
12+
13+
use self::VarValue::*;
14+
15+
pub trait UnifyKey {
16+
type Value;
17+
fn to_index(&self) -> usize;
18+
}
19+
20+
pub enum VarValue<K:UnifyKey> {
21+
Redirect { to: K },
22+
Root { value: K::Value, rank: usize },
23+
}
24+
25+
fn get<'a,K:UnifyKey<Value=Option<V>>,V>(table: &'a Vec<VarValue<K>>, key: &K) -> &'a Option<V> {
26+
match table[key.to_index()] {
27+
VarValue::Redirect { to: ref k } => get(table, k),
28+
VarValue::Root { value: ref v, rank: _ } => v,
29+
}
30+
}
31+
32+
impl UnifyKey for usize {
33+
type Value = Option<char>;
34+
fn to_index(&self) -> usize { *self }
35+
}
36+
37+
fn main() {
38+
let table = vec![/* 0 */ Redirect { to: 1 },
39+
/* 1 */ Redirect { to: 3 },
40+
/* 2 */ Root { value: Some('x'), rank: 0 },
41+
/* 3 */ Redirect { to: 2 }];
42+
assert_eq!(get(&table, &0), &Some('x'));
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2015 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+
// Test associated types appearing in tuple-like enum variants.
12+
13+
use self::VarValue::*;
14+
15+
pub trait UnifyKey {
16+
type Value;
17+
fn to_index(&self) -> usize;
18+
}
19+
20+
pub enum VarValue<K:UnifyKey> {
21+
Redirect(K),
22+
Root(K::Value, usize),
23+
}
24+
25+
fn get<'a,K:UnifyKey<Value=Option<V>>,V>(table: &'a Vec<VarValue<K>>, key: &K) -> &'a Option<V> {
26+
match table[key.to_index()] {
27+
VarValue::Redirect(ref k) => get(table, k),
28+
VarValue::Root(ref v, _) => v,
29+
}
30+
}
31+
32+
impl UnifyKey for usize {
33+
type Value = Option<char>;
34+
fn to_index(&self) -> usize { *self }
35+
}
36+
37+
fn main() {
38+
let table = vec![/* 0 */ Redirect(1),
39+
/* 1 */ Redirect(3),
40+
/* 2 */ Root(Some('x'), 0),
41+
/* 3 */ Redirect(2)];
42+
assert_eq!(get(&table, &0), &Some('x'));
43+
}

0 commit comments

Comments
 (0)