Skip to content

Commit 06e798a

Browse files
committed
Normalize the types of fields we project out of a struct or tuple struct.
Fixes #20954.
1 parent 2127e0d commit 06e798a

File tree

3 files changed

+111
-28
lines changed

3 files changed

+111
-28
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22392239

22402240
obligations.map_move(|o| self.register_predicate(o));
22412241
}
2242+
2243+
// Only for fields! Returns <none> for methods>
2244+
// Indifferent to privacy flags
2245+
pub fn lookup_field_ty(&self,
2246+
span: Span,
2247+
class_id: ast::DefId,
2248+
items: &[ty::field_ty],
2249+
fieldname: ast::Name,
2250+
substs: &subst::Substs<'tcx>)
2251+
-> Option<Ty<'tcx>>
2252+
{
2253+
let o_field = items.iter().find(|f| f.name == fieldname);
2254+
o_field.map(|f| ty::lookup_field_type(self.tcx(), class_id, f.id, substs))
2255+
.map(|t| self.normalize_associated_types_in(span, &t))
2256+
}
2257+
2258+
pub fn lookup_tup_field_ty(&self,
2259+
span: Span,
2260+
class_id: ast::DefId,
2261+
items: &[ty::field_ty],
2262+
idx: uint,
2263+
substs: &subst::Substs<'tcx>)
2264+
-> Option<Ty<'tcx>>
2265+
{
2266+
let o_field = if idx < items.len() { Some(&items[idx]) } else { None };
2267+
o_field.map(|f| ty::lookup_field_type(self.tcx(), class_id, f.id, substs))
2268+
.map(|t| self.normalize_associated_types_in(span, &t))
2269+
}
22422270
}
22432271

22442272
impl<'a, 'tcx> RegionScope for FnCtxt<'a, 'tcx> {
@@ -2953,30 +2981,6 @@ pub fn impl_self_ty<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
29532981
TypeAndSubsts { substs: substs, ty: substd_ty }
29542982
}
29552983

2956-
// Only for fields! Returns <none> for methods>
2957-
// Indifferent to privacy flags
2958-
pub fn lookup_field_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
2959-
class_id: ast::DefId,
2960-
items: &[ty::field_ty],
2961-
fieldname: ast::Name,
2962-
substs: &subst::Substs<'tcx>)
2963-
-> Option<Ty<'tcx>> {
2964-
2965-
let o_field = items.iter().find(|f| f.name == fieldname);
2966-
o_field.map(|f| ty::lookup_field_type(tcx, class_id, f.id, substs))
2967-
}
2968-
2969-
pub fn lookup_tup_field_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
2970-
class_id: ast::DefId,
2971-
items: &[ty::field_ty],
2972-
idx: uint,
2973-
substs: &subst::Substs<'tcx>)
2974-
-> Option<Ty<'tcx>> {
2975-
2976-
let o_field = if idx < items.len() { Some(&items[idx]) } else { None };
2977-
o_field.map(|f| ty::lookup_field_type(tcx, class_id, f.id, substs))
2978-
}
2979-
29802984
// Controls whether the arguments are automatically referenced. This is useful
29812985
// for overloaded binary and unary operators.
29822986
#[derive(Copy, PartialEq)]
@@ -3398,8 +3402,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
33983402
ty::ty_struct(base_id, substs) => {
33993403
debug!("struct named {}", ppaux::ty_to_string(tcx, base_t));
34003404
let fields = ty::lookup_struct_fields(tcx, base_id);
3401-
lookup_field_ty(tcx, base_id, &fields[],
3402-
field.node.name, &(*substs))
3405+
fcx.lookup_field_ty(expr.span, base_id, &fields[],
3406+
field.node.name, &(*substs))
34033407
}
34043408
_ => None
34053409
}
@@ -3461,8 +3465,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34613465
if tuple_like {
34623466
debug!("tuple struct named {}", ppaux::ty_to_string(tcx, base_t));
34633467
let fields = ty::lookup_struct_fields(tcx, base_id);
3464-
lookup_tup_field_ty(tcx, base_id, &fields[],
3465-
idx.node, &(*substs))
3468+
fcx.lookup_tup_field_ty(expr.span, base_id, &fields[],
3469+
idx.node, &(*substs))
34663470
} else {
34673471
None
34683472
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 that we correctly normalize the type of a struct field
12+
// which has an associated type.
13+
14+
pub trait UnifyKey {
15+
type Value;
16+
}
17+
18+
pub struct Node<K:UnifyKey> {
19+
pub key: K,
20+
pub value: K::Value,
21+
}
22+
23+
fn foo<K : UnifyKey<Value=Option<V>>,V : Clone>(node: &Node<K>) -> Option<V> {
24+
node.value.clone()
25+
}
26+
27+
impl UnifyKey for i32 {
28+
type Value = Option<u32>;
29+
}
30+
31+
impl UnifyKey for u32 {
32+
type Value = Option<i32>;
33+
}
34+
35+
pub fn main() {
36+
let node: Node<i32> = Node { key: 1, value: Some(22) };
37+
assert_eq!(foo(&node), Some(22_u32));
38+
39+
let node: Node<u32> = Node { key: 1, value: Some(22) };
40+
assert_eq!(foo(&node), Some(22_i32));
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 that we correctly normalize the type of a struct field
12+
// which has an associated type.
13+
14+
pub trait UnifyKey {
15+
type Value;
16+
}
17+
18+
pub struct Node<K:UnifyKey>(K, K::Value);
19+
20+
fn foo<K : UnifyKey<Value=Option<V>>,V : Clone>(node: &Node<K>) -> Option<V> {
21+
node.1.clone()
22+
}
23+
24+
impl UnifyKey for i32 {
25+
type Value = Option<u32>;
26+
}
27+
28+
impl UnifyKey for u32 {
29+
type Value = Option<i32>;
30+
}
31+
32+
pub fn main() {
33+
let node: Node<i32> = Node(1, Some(22));
34+
assert_eq!(foo(&node), Some(22_u32));
35+
36+
let node: Node<u32> = Node(1, Some(22));
37+
assert_eq!(foo(&node), Some(22_i32));
38+
}

0 commit comments

Comments
 (0)