Skip to content

Assoc types enum field access #20997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/librustc/middle/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,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
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_trans/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
60 changes: 32 additions & 28 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,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> {
Expand Down Expand Up @@ -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 <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)]
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
43 changes: 43 additions & 0 deletions src/test/run-pass/associated-types-enum-field-named.rs
Original file line number Diff line number Diff line change
@@ -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 <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 associated types appearing in struct-like enum variants.

use self::VarValue::*;

pub trait UnifyKey {
type Value;
fn to_index(&self) -> usize;
}

pub enum VarValue<K:UnifyKey> {
Redirect { to: K },
Root { value: K::Value, rank: usize },
}

fn get<'a,K:UnifyKey<Value=Option<V>>,V>(table: &'a Vec<VarValue<K>>, key: &K) -> &'a Option<V> {
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<char>;
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'));
}
43 changes: 43 additions & 0 deletions src/test/run-pass/associated-types-enum-field-numbered.rs
Original file line number Diff line number Diff line change
@@ -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 <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 associated types appearing in tuple-like enum variants.

use self::VarValue::*;

pub trait UnifyKey {
type Value;
fn to_index(&self) -> usize;
}

pub enum VarValue<K:UnifyKey> {
Redirect(K),
Root(K::Value, usize),
}

fn get<'a,K:UnifyKey<Value=Option<V>>,V>(table: &'a Vec<VarValue<K>>, key: &K) -> &'a Option<V> {
match table[key.to_index()] {
VarValue::Redirect(ref k) => get(table, k),
VarValue::Root(ref v, _) => v,
}
}

impl UnifyKey for usize {
type Value = Option<char>;
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'));
}
41 changes: 41 additions & 0 deletions src/test/run-pass/associated-types-struct-field-named.rs
Original file line number Diff line number Diff line change
@@ -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));
}
38 changes: 38 additions & 0 deletions src/test/run-pass/associated-types-struct-field-numbered.rs
Original file line number Diff line number Diff line change
@@ -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));
}
Original file line number Diff line number Diff line change
@@ -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));
}