From 06e798a881d80eb959a71da5ebbd164f056a4531 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sun, 11 Jan 2015 13:52:40 -0500 Subject: [PATCH 1/3] Normalize the types of fields we project out of a struct or tuple struct. Fixes #20954. --- src/librustc_typeck/check/mod.rs | 60 ++++++++++--------- .../associated-types-struct-field-named.rs | 41 +++++++++++++ .../associated-types-struct-field-numbered.rs | 38 ++++++++++++ 3 files changed, 111 insertions(+), 28 deletions(-) create mode 100644 src/test/run-pass/associated-types-struct-field-named.rs create mode 100644 src/test/run-pass/associated-types-struct-field-numbered.rs diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 1d184131dede3..a6c2c0942b852 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2239,6 +2239,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { obligations.map_move(|o| self.register_predicate(o)); } + + // Only for fields! Returns 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> + { + 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> + { + 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> { @@ -2953,30 +2981,6 @@ pub fn impl_self_ty<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, TypeAndSubsts { substs: substs, ty: substd_ty } } -// Only for fields! Returns 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> { - - 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> { - - 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)] @@ -3398,8 +3402,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 } @@ -3461,8 +3465,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 0000000000000..1ded34ff3ffe3 --- /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 or the MIT license +// , 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 { + pub key: K, + pub value: K::Value, +} + +fn foo>,V : Clone>(node: &Node) -> Option { + node.value.clone() +} + +impl UnifyKey for i32 { + type Value = Option; +} + +impl UnifyKey for u32 { + type Value = Option; +} + +pub fn main() { + let node: Node = Node { key: 1, value: Some(22) }; + assert_eq!(foo(&node), Some(22_u32)); + + let node: Node = 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 0000000000000..3669dec4fbdd4 --- /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 or the MIT license +// , 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, K::Value); + +fn foo>,V : Clone>(node: &Node) -> Option { + node.1.clone() +} + +impl UnifyKey for i32 { + type Value = Option; +} + +impl UnifyKey for u32 { + type Value = Option; +} + +pub fn main() { + let node: Node = Node(1, Some(22)); + assert_eq!(foo(&node), Some(22_u32)); + + let node: Node = Node(1, Some(22)); + assert_eq!(foo(&node), Some(22_i32)); +} From 2b8678cf5df2b3521ed88b8aea8d7699799e67a0 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sun, 11 Jan 2015 14:52:37 -0500 Subject: [PATCH 2/3] Give where clauses priority over builtin rules. Fixes #20959. --- src/librustc/middle/traits/select.rs | 8 +++++++ ...se-ambiguity-where-clause-builtin-bound.rs | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/test/run-pass/trait-false-ambiguity-where-clause-builtin-bound.rs diff --git a/src/librustc/middle/traits/select.rs b/src/librustc/middle/traits/select.rs index d6302976b9f11..c3cf23042f9be 100644 --- a/src/librustc/middle/traits/select.rs +++ b/src/librustc/middle/traits/select.rs @@ -1166,6 +1166,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .is_ok() }) } + (&BuiltinCandidate(_), &ParamCandidate(_)) => { + // If we have a where-clause like `Option : Send`, + // then we wind up in a situation where there is a + // default rule (`Option: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/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 0000000000000..ca66a106c437e --- /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 or the MIT license +// , 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(x: Option) + where Option : Sized +{ + let _y = x; +} + +fn main() { + foo(Some(22)); +} From 487a4a174afb21de5ce5573e16dfe876f6face91 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 12 Jan 2015 04:59:59 -0500 Subject: [PATCH 3/3] Fix various oversights in enum field treatment in trans and typeck. Fixes #20996. --- src/librustc_trans/trans/base.rs | 5 +-- src/librustc_typeck/check/_match.rs | 2 + .../associated-types-enum-field-named.rs | 43 +++++++++++++++++++ .../associated-types-enum-field-numbered.rs | 43 +++++++++++++++++++ 4 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 src/test/run-pass/associated-types-enum-field-named.rs create mode 100644 src/test/run-pass/associated-types-enum-field-numbered.rs diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index ea98d6bb74e95..c1efec8600114 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -702,9 +702,8 @@ pub fn iter_structural_ty<'blk, 'tcx, F>(cx: Block<'blk, 'tcx>, let mut cx = cx; for (i, &arg) in variant.args.iter().enumerate() { - cx = (*f)(cx, - adt::trans_field_ptr(cx, repr, av, variant.disr_val, i), - arg.subst(tcx, substs)); + let arg = monomorphize::apply_param_substs(tcx, substs, &arg); + cx = f(cx, adt::trans_field_ptr(cx, repr, av, variant.disr_val, i), arg); } return cx; } diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs index cacf9bb19d20f..5c8c8b485d87d 100644 --- a/src/librustc_typeck/check/_match.rs +++ b/src/librustc_typeck/check/_match.rs @@ -619,6 +619,8 @@ pub fn check_struct_pat_fields<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>, } }; + let field_type = pcx.fcx.normalize_associated_types_in(span, &field_type); + check_pat(pcx, &*field.pat, field_type); } diff --git a/src/test/run-pass/associated-types-enum-field-named.rs b/src/test/run-pass/associated-types-enum-field-named.rs new file mode 100644 index 0000000000000..a499aa6733aae --- /dev/null +++ b/src/test/run-pass/associated-types-enum-field-named.rs @@ -0,0 +1,43 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test associated types appearing in struct-like enum variants. + +use self::VarValue::*; + +pub trait UnifyKey { + type Value; + fn to_index(&self) -> usize; +} + +pub enum VarValue { + Redirect { to: K }, + Root { value: K::Value, rank: usize }, +} + +fn get<'a,K:UnifyKey>,V>(table: &'a Vec>, key: &K) -> &'a Option { + match table[key.to_index()] { + VarValue::Redirect { to: ref k } => get(table, k), + VarValue::Root { value: ref v, rank: _ } => v, + } +} + +impl UnifyKey for usize { + type Value = Option; + fn to_index(&self) -> usize { *self } +} + +fn main() { + let table = vec![/* 0 */ Redirect { to: 1 }, + /* 1 */ Redirect { to: 3 }, + /* 2 */ Root { value: Some('x'), rank: 0 }, + /* 3 */ Redirect { to: 2 }]; + assert_eq!(get(&table, &0), &Some('x')); +} diff --git a/src/test/run-pass/associated-types-enum-field-numbered.rs b/src/test/run-pass/associated-types-enum-field-numbered.rs new file mode 100644 index 0000000000000..e710c53327ec7 --- /dev/null +++ b/src/test/run-pass/associated-types-enum-field-numbered.rs @@ -0,0 +1,43 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test associated types appearing in tuple-like enum variants. + +use self::VarValue::*; + +pub trait UnifyKey { + type Value; + fn to_index(&self) -> usize; +} + +pub enum VarValue { + Redirect(K), + Root(K::Value, usize), +} + +fn get<'a,K:UnifyKey>,V>(table: &'a Vec>, key: &K) -> &'a Option { + match table[key.to_index()] { + VarValue::Redirect(ref k) => get(table, k), + VarValue::Root(ref v, _) => v, + } +} + +impl UnifyKey for usize { + type Value = Option; + fn to_index(&self) -> usize { *self } +} + +fn main() { + let table = vec![/* 0 */ Redirect(1), + /* 1 */ Redirect(3), + /* 2 */ Root(Some('x'), 0), + /* 3 */ Redirect(2)]; + assert_eq!(get(&table, &0), &Some('x')); +}