diff options
| author | bors <bors@rust-lang.org> | 2015-01-13 05:01:34 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-01-13 05:01:34 +0000 |
| commit | 4fc9b41238c5b8d36afd6765c770b6604bf7ef04 (patch) | |
| tree | bfa3452784ac12e34c4dc90f06a28316aefb320e /src | |
| parent | 3d5fbae33897a8340542f21b6ded913148ca9199 (diff) | |
| parent | 2b8678cf5df2b3521ed88b8aea8d7699799e67a0 (diff) | |
| download | rust-4fc9b41238c5b8d36afd6765c770b6604bf7ef04.tar.gz rust-4fc9b41238c5b8d36afd6765c770b6604bf7ef04.zip | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc/middle/traits/select.rs | 8 | ||||
| -rw-r--r-- | src/librustc_typeck/check/mod.rs | 60 | ||||
| -rw-r--r-- | src/test/run-pass/associated-types-struct-field-named.rs | 41 | ||||
| -rw-r--r-- | src/test/run-pass/associated-types-struct-field-numbered.rs | 38 | ||||
| -rw-r--r-- | src/test/run-pass/trait-false-ambiguity-where-clause-builtin-bound.rs | 23 |
5 files changed, 142 insertions, 28 deletions
diff --git a/src/librustc/middle/traits/select.rs b/src/librustc/middle/traits/select.rs index 2520434f68f..a38814580f5 100644 --- a/src/librustc/middle/traits/select.rs +++ b/src/librustc/middle/traits/select.rs @@ -1186,6 +1186,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .is_ok() }) } + (&BuiltinCandidate(_), &ParamCandidate(_)) => { + // If we have a where-clause like `Option<K> : Send`, + // then we wind up in a situation where there is a + // default rule (`Option<K>:Send if K:Send) and the + // where-clause that both seem applicable. Just take + // the where-clause in that case. + true + } (&ProjectionCandidate, &ParamCandidate(_)) => { // FIXME(#20297) -- this gives where clauses precedent // over projections. Really these are just two means diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 7473f0223a6..c03cb860dfb 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2289,6 +2289,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { obligations.map_move(|o| self.register_predicate(o)); } + + // Only for fields! Returns <none> for methods> + // Indifferent to privacy flags + pub fn lookup_field_ty(&self, + span: Span, + class_id: ast::DefId, + items: &[ty::field_ty], + fieldname: ast::Name, + substs: &subst::Substs<'tcx>) + -> Option<Ty<'tcx>> + { + let o_field = items.iter().find(|f| f.name == fieldname); + o_field.map(|f| ty::lookup_field_type(self.tcx(), class_id, f.id, substs)) + .map(|t| self.normalize_associated_types_in(span, &t)) + } + + pub fn lookup_tup_field_ty(&self, + span: Span, + class_id: ast::DefId, + items: &[ty::field_ty], + idx: uint, + substs: &subst::Substs<'tcx>) + -> Option<Ty<'tcx>> + { + let o_field = if idx < items.len() { Some(&items[idx]) } else { None }; + o_field.map(|f| ty::lookup_field_type(self.tcx(), class_id, f.id, substs)) + .map(|t| self.normalize_associated_types_in(span, &t)) + } } impl<'a, 'tcx> RegionScope for FnCtxt<'a, 'tcx> { @@ -3043,30 +3071,6 @@ pub fn impl_self_ty<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, TypeAndSubsts { substs: substs, ty: substd_ty } } -// Only for fields! Returns <none> for methods> -// Indifferent to privacy flags -pub fn lookup_field_ty<'tcx>(tcx: &ty::ctxt<'tcx>, - class_id: ast::DefId, - items: &[ty::field_ty], - fieldname: ast::Name, - substs: &subst::Substs<'tcx>) - -> Option<Ty<'tcx>> { - - let o_field = items.iter().find(|f| f.name == fieldname); - o_field.map(|f| ty::lookup_field_type(tcx, class_id, f.id, substs)) -} - -pub fn lookup_tup_field_ty<'tcx>(tcx: &ty::ctxt<'tcx>, - class_id: ast::DefId, - items: &[ty::field_ty], - idx: uint, - substs: &subst::Substs<'tcx>) - -> Option<Ty<'tcx>> { - - let o_field = if idx < items.len() { Some(&items[idx]) } else { None }; - o_field.map(|f| ty::lookup_field_type(tcx, class_id, f.id, substs)) -} - // Controls whether the arguments are automatically referenced. This is useful // for overloaded binary and unary operators. #[derive(Copy, PartialEq)] @@ -3530,8 +3534,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, ty::ty_struct(base_id, substs) => { debug!("struct named {}", ppaux::ty_to_string(tcx, base_t)); let fields = ty::lookup_struct_fields(tcx, base_id); - lookup_field_ty(tcx, base_id, &fields[], - field.node.name, &(*substs)) + fcx.lookup_field_ty(expr.span, base_id, &fields[], + field.node.name, &(*substs)) } _ => None } @@ -3593,8 +3597,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, if tuple_like { debug!("tuple struct named {}", ppaux::ty_to_string(tcx, base_t)); let fields = ty::lookup_struct_fields(tcx, base_id); - lookup_tup_field_ty(tcx, base_id, &fields[], - idx.node, &(*substs)) + fcx.lookup_tup_field_ty(expr.span, base_id, &fields[], + idx.node, &(*substs)) } else { None } diff --git a/src/test/run-pass/associated-types-struct-field-named.rs b/src/test/run-pass/associated-types-struct-field-named.rs new file mode 100644 index 00000000000..1ded34ff3ff --- /dev/null +++ b/src/test/run-pass/associated-types-struct-field-named.rs @@ -0,0 +1,41 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that we correctly normalize the type of a struct field +// which has an associated type. + +pub trait UnifyKey { + type Value; +} + +pub struct Node<K:UnifyKey> { + pub key: K, + pub value: K::Value, +} + +fn foo<K : UnifyKey<Value=Option<V>>,V : Clone>(node: &Node<K>) -> Option<V> { + node.value.clone() +} + +impl UnifyKey for i32 { + type Value = Option<u32>; +} + +impl UnifyKey for u32 { + type Value = Option<i32>; +} + +pub fn main() { + let node: Node<i32> = Node { key: 1, value: Some(22) }; + assert_eq!(foo(&node), Some(22_u32)); + + let node: Node<u32> = Node { key: 1, value: Some(22) }; + assert_eq!(foo(&node), Some(22_i32)); +} diff --git a/src/test/run-pass/associated-types-struct-field-numbered.rs b/src/test/run-pass/associated-types-struct-field-numbered.rs new file mode 100644 index 00000000000..3669dec4fbd --- /dev/null +++ b/src/test/run-pass/associated-types-struct-field-numbered.rs @@ -0,0 +1,38 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that we correctly normalize the type of a struct field +// which has an associated type. + +pub trait UnifyKey { + type Value; +} + +pub struct Node<K:UnifyKey>(K, K::Value); + +fn foo<K : UnifyKey<Value=Option<V>>,V : Clone>(node: &Node<K>) -> Option<V> { + node.1.clone() +} + +impl UnifyKey for i32 { + type Value = Option<u32>; +} + +impl UnifyKey for u32 { + type Value = Option<i32>; +} + +pub fn main() { + let node: Node<i32> = Node(1, Some(22)); + assert_eq!(foo(&node), Some(22_u32)); + + let node: Node<u32> = Node(1, Some(22)); + assert_eq!(foo(&node), Some(22_i32)); +} diff --git a/src/test/run-pass/trait-false-ambiguity-where-clause-builtin-bound.rs b/src/test/run-pass/trait-false-ambiguity-where-clause-builtin-bound.rs new file mode 100644 index 00000000000..ca66a106c43 --- /dev/null +++ b/src/test/run-pass/trait-false-ambiguity-where-clause-builtin-bound.rs @@ -0,0 +1,23 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that we do not error out because of a (False) ambiguity +// between the builtin rules for Sized and the where clause. Issue +// #20959. + +fn foo<K>(x: Option<K>) + where Option<K> : Sized +{ + let _y = x; +} + +fn main() { + foo(Some(22)); +} |
