Skip to content

Commit 4fc9b41

Browse files
committed
auto merge of #20955 : nikomatsakis/rust/assoc-types-struct-field-access, r=nick29581
Normalize the types of fields we project out of a struct or tuple struct. Fixes #20954. r? @nick29581
2 parents 3d5fbae + 2b8678c commit 4fc9b41

File tree

5 files changed

+142
-28
lines changed

5 files changed

+142
-28
lines changed

src/librustc/middle/traits/select.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11861186
.is_ok()
11871187
})
11881188
}
1189+
(&BuiltinCandidate(_), &ParamCandidate(_)) => {
1190+
// If we have a where-clause like `Option<K> : Send`,
1191+
// then we wind up in a situation where there is a
1192+
// default rule (`Option<K>:Send if K:Send) and the
1193+
// where-clause that both seem applicable. Just take
1194+
// the where-clause in that case.
1195+
true
1196+
}
11891197
(&ProjectionCandidate, &ParamCandidate(_)) => {
11901198
// FIXME(#20297) -- this gives where clauses precedent
11911199
// over projections. Really these are just two means

src/librustc_typeck/check/mod.rs

+32-28
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22892289

22902290
obligations.map_move(|o| self.register_predicate(o));
22912291
}
2292+
2293+
// Only for fields! Returns <none> for methods>
2294+
// Indifferent to privacy flags
2295+
pub fn lookup_field_ty(&self,
2296+
span: Span,
2297+
class_id: ast::DefId,
2298+
items: &[ty::field_ty],
2299+
fieldname: ast::Name,
2300+
substs: &subst::Substs<'tcx>)
2301+
-> Option<Ty<'tcx>>
2302+
{
2303+
let o_field = items.iter().find(|f| f.name == fieldname);
2304+
o_field.map(|f| ty::lookup_field_type(self.tcx(), class_id, f.id, substs))
2305+
.map(|t| self.normalize_associated_types_in(span, &t))
2306+
}
2307+
2308+
pub fn lookup_tup_field_ty(&self,
2309+
span: Span,
2310+
class_id: ast::DefId,
2311+
items: &[ty::field_ty],
2312+
idx: uint,
2313+
substs: &subst::Substs<'tcx>)
2314+
-> Option<Ty<'tcx>>
2315+
{
2316+
let o_field = if idx < items.len() { Some(&items[idx]) } else { None };
2317+
o_field.map(|f| ty::lookup_field_type(self.tcx(), class_id, f.id, substs))
2318+
.map(|t| self.normalize_associated_types_in(span, &t))
2319+
}
22922320
}
22932321

22942322
impl<'a, 'tcx> RegionScope for FnCtxt<'a, 'tcx> {
@@ -3043,30 +3071,6 @@ pub fn impl_self_ty<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
30433071
TypeAndSubsts { substs: substs, ty: substd_ty }
30443072
}
30453073

3046-
// Only for fields! Returns <none> for methods>
3047-
// Indifferent to privacy flags
3048-
pub fn lookup_field_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
3049-
class_id: ast::DefId,
3050-
items: &[ty::field_ty],
3051-
fieldname: ast::Name,
3052-
substs: &subst::Substs<'tcx>)
3053-
-> Option<Ty<'tcx>> {
3054-
3055-
let o_field = items.iter().find(|f| f.name == fieldname);
3056-
o_field.map(|f| ty::lookup_field_type(tcx, class_id, f.id, substs))
3057-
}
3058-
3059-
pub fn lookup_tup_field_ty<'tcx>(tcx: &ty::ctxt<'tcx>,
3060-
class_id: ast::DefId,
3061-
items: &[ty::field_ty],
3062-
idx: uint,
3063-
substs: &subst::Substs<'tcx>)
3064-
-> Option<Ty<'tcx>> {
3065-
3066-
let o_field = if idx < items.len() { Some(&items[idx]) } else { None };
3067-
o_field.map(|f| ty::lookup_field_type(tcx, class_id, f.id, substs))
3068-
}
3069-
30703074
// Controls whether the arguments are automatically referenced. This is useful
30713075
// for overloaded binary and unary operators.
30723076
#[derive(Copy, PartialEq)]
@@ -3530,8 +3534,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35303534
ty::ty_struct(base_id, substs) => {
35313535
debug!("struct named {}", ppaux::ty_to_string(tcx, base_t));
35323536
let fields = ty::lookup_struct_fields(tcx, base_id);
3533-
lookup_field_ty(tcx, base_id, &fields[],
3534-
field.node.name, &(*substs))
3537+
fcx.lookup_field_ty(expr.span, base_id, &fields[],
3538+
field.node.name, &(*substs))
35353539
}
35363540
_ => None
35373541
}
@@ -3593,8 +3597,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35933597
if tuple_like {
35943598
debug!("tuple struct named {}", ppaux::ty_to_string(tcx, base_t));
35953599
let fields = ty::lookup_struct_fields(tcx, base_id);
3596-
lookup_tup_field_ty(tcx, base_id, &fields[],
3597-
idx.node, &(*substs))
3600+
fcx.lookup_tup_field_ty(expr.span, base_id, &fields[],
3601+
idx.node, &(*substs))
35983602
} else {
35993603
None
36003604
}
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 do not error out because of a (False) ambiguity
12+
// between the builtin rules for Sized and the where clause. Issue
13+
// #20959.
14+
15+
fn foo<K>(x: Option<K>)
16+
where Option<K> : Sized
17+
{
18+
let _y = x;
19+
}
20+
21+
fn main() {
22+
foo(Some(22));
23+
}

0 commit comments

Comments
 (0)