Skip to content

Commit

Permalink
Auto merge of #33632 - Manishearth:rollup, r=Manishearth
Browse files Browse the repository at this point in the history
Rollup of 9 pull requests

- Successful merges: #33544, #33552, #33554, #33555, #33560, #33566, #33572, #33574, #33576
- Failed merges:
  • Loading branch information
bors committed May 14, 2016
2 parents d3ec9d4 + 61d87f0 commit 6ba8a1a
Show file tree
Hide file tree
Showing 38 changed files with 1,182 additions and 943 deletions.
48 changes: 38 additions & 10 deletions src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub enum Vtable<'tcx, N> {
VtableParam(Vec<N>),

/// Virtual calls through an object
VtableObject(VtableObjectData<'tcx>),
VtableObject(VtableObjectData<'tcx, N>),

/// Successful resolution for a builtin trait.
VtableBuiltin(VtableBuiltinData<N>),
Expand All @@ -250,7 +250,7 @@ pub enum Vtable<'tcx, N> {
VtableClosure(VtableClosureData<'tcx, N>),

/// Same as above, but for a fn pointer type with the given signature.
VtableFnPointer(ty::Ty<'tcx>),
VtableFnPointer(VtableFnPointerData<'tcx, N>),
}

/// Identifies a particular impl in the source, along with a set of
Expand Down Expand Up @@ -293,14 +293,22 @@ pub struct VtableBuiltinData<N> {
/// A vtable for some object-safe trait `Foo` automatically derived
/// for the object type `Foo`.
#[derive(PartialEq,Eq,Clone)]
pub struct VtableObjectData<'tcx> {
pub struct VtableObjectData<'tcx, N> {
/// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`.
pub upcast_trait_ref: ty::PolyTraitRef<'tcx>,

/// The vtable is formed by concatenating together the method lists of
/// the base object trait and all supertraits; this is the start of
/// `upcast_trait_ref`'s methods in that vtable.
pub vtable_base: usize
pub vtable_base: usize,

pub nested: Vec<N>,
}

#[derive(Clone, PartialEq, Eq)]
pub struct VtableFnPointerData<'tcx, N> {
pub fn_ty: ty::Ty<'tcx>,
pub nested: Vec<N>
}

/// Creates predicate obligations from the generic bounds.
Expand Down Expand Up @@ -569,7 +577,20 @@ impl<'tcx, N> Vtable<'tcx, N> {
VtableBuiltin(i) => i.nested,
VtableDefaultImpl(d) => d.nested,
VtableClosure(c) => c.nested,
VtableObject(_) | VtableFnPointer(..) => vec![]
VtableObject(d) => d.nested,
VtableFnPointer(d) => d.nested,
}
}

fn nested_obligations_mut(&mut self) -> &mut Vec<N> {
match self {
&mut VtableImpl(ref mut i) => &mut i.nested,
&mut VtableParam(ref mut n) => n,
&mut VtableBuiltin(ref mut i) => &mut i.nested,
&mut VtableDefaultImpl(ref mut d) => &mut d.nested,
&mut VtableClosure(ref mut c) => &mut c.nested,
&mut VtableObject(ref mut d) => &mut d.nested,
&mut VtableFnPointer(ref mut d) => &mut d.nested,
}
}

Expand All @@ -578,18 +599,25 @@ impl<'tcx, N> Vtable<'tcx, N> {
VtableImpl(i) => VtableImpl(VtableImplData {
impl_def_id: i.impl_def_id,
substs: i.substs,
nested: i.nested.into_iter().map(f).collect()
nested: i.nested.into_iter().map(f).collect(),
}),
VtableParam(n) => VtableParam(n.into_iter().map(f).collect()),
VtableBuiltin(i) => VtableBuiltin(VtableBuiltinData {
nested: i.nested.into_iter().map(f).collect()
nested: i.nested.into_iter().map(f).collect(),
}),
VtableObject(o) => VtableObject(VtableObjectData {
upcast_trait_ref: o.upcast_trait_ref,
vtable_base: o.vtable_base,
nested: o.nested.into_iter().map(f).collect(),
}),
VtableObject(o) => VtableObject(o),
VtableDefaultImpl(d) => VtableDefaultImpl(VtableDefaultImplData {
trait_def_id: d.trait_def_id,
nested: d.nested.into_iter().map(f).collect()
nested: d.nested.into_iter().map(f).collect(),
}),
VtableFnPointer(p) => VtableFnPointer(VtableFnPointerData {
fn_ty: p.fn_ty,
nested: p.nested.into_iter().map(f).collect(),
}),
VtableFnPointer(f) => VtableFnPointer(f),
VtableClosure(c) => VtableClosure(VtableClosureData {
closure_def_id: c.closure_def_id,
substs: c.substs,
Expand Down
76 changes: 12 additions & 64 deletions src/librustc/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::PredicateObligation;
use super::SelectionContext;
use super::SelectionError;
use super::VtableClosureData;
use super::VtableFnPointerData;
use super::VtableImplData;
use super::util;

Expand Down Expand Up @@ -158,7 +159,7 @@ enum ProjectionTyCandidate<'tcx> {
Closure(VtableClosureData<'tcx, PredicateObligation<'tcx>>),

// fn pointer return type
FnPointer(Ty<'tcx>),
FnPointer(VtableFnPointerData<'tcx, PredicateObligation<'tcx>>),
}

struct ProjectionTyCandidateSet<'tcx> {
Expand Down Expand Up @@ -218,10 +219,7 @@ fn project_and_unify_type<'cx, 'gcx, 'tcx>(
obligation.cause.clone(),
obligation.recursion_depth) {
Some(n) => n,
None => {
consider_unification_despite_ambiguity(selcx, obligation);
return Ok(None);
}
None => return Ok(None),
};

debug!("project_and_unify_type: normalized_ty={:?} obligations={:?}",
Expand All @@ -240,59 +238,6 @@ fn project_and_unify_type<'cx, 'gcx, 'tcx>(
}
}

fn consider_unification_despite_ambiguity<'cx, 'gcx, 'tcx>(
selcx: &mut SelectionContext<'cx, 'gcx, 'tcx>,
obligation: &ProjectionObligation<'tcx>)
{
debug!("consider_unification_despite_ambiguity(obligation={:?})",
obligation);

let def_id = obligation.predicate.projection_ty.trait_ref.def_id;
match selcx.tcx().lang_items.fn_trait_kind(def_id) {
Some(_) => { }
None => { return; }
}

let infcx = selcx.infcx();
let self_ty = obligation.predicate.projection_ty.trait_ref.self_ty();
let self_ty = infcx.shallow_resolve(self_ty);
debug!("consider_unification_despite_ambiguity: self_ty.sty={:?}",
self_ty.sty);
match self_ty.sty {
ty::TyClosure(closure_def_id, substs) => {
let closure_typer = selcx.closure_typer();
let closure_type = closure_typer.closure_type(closure_def_id, substs);
let ty::Binder((_, ret_type)) =
infcx.tcx.closure_trait_ref_and_return_type(def_id,
self_ty,
&closure_type.sig,
util::TupleArgumentsFlag::No);
// We don't have to normalize the return type here - this is only
// reached for TyClosure: Fn inputs where the closure kind is
// still unknown, which should only occur in typeck where the
// closure type is already normalized.
let (ret_type, _) =
infcx.replace_late_bound_regions_with_fresh_var(
obligation.cause.span,
infer::AssocTypeProjection(obligation.predicate.projection_ty.item_name),
&ty::Binder(ret_type));

debug!("consider_unification_despite_ambiguity: ret_type={:?}",
ret_type);
let origin = TypeOrigin::RelateOutputImplTypes(obligation.cause.span);
let obligation_ty = obligation.predicate.ty;
match infcx.eq_types(true, origin, obligation_ty, ret_type) {
Ok(InferOk { obligations, .. }) => {
// FIXME(#32730) propagate obligations
assert!(obligations.is_empty());
}
Err(_) => { /* ignore errors */ }
}
}
_ => { }
}
}

/// Normalizes any associated type projections in `value`, replacing
/// them with a fully resolved type where possible. The return value
/// combines the normalized result and any additional obligations that
Expand Down Expand Up @@ -929,9 +874,9 @@ fn assemble_candidates_from_impls<'cx, 'gcx, 'tcx>(
candidate_set.vec.push(
ProjectionTyCandidate::Closure(data));
}
super::VtableFnPointer(fn_type) => {
super::VtableFnPointer(data) => {
candidate_set.vec.push(
ProjectionTyCandidate::FnPointer(fn_type));
ProjectionTyCandidate::FnPointer(data));
}
super::VtableParam(..) => {
// This case tell us nothing about the value of an
Expand Down Expand Up @@ -997,19 +942,22 @@ fn confirm_candidate<'cx, 'gcx, 'tcx>(
confirm_closure_candidate(selcx, obligation, closure_vtable)
}

ProjectionTyCandidate::FnPointer(fn_type) => {
confirm_fn_pointer_candidate(selcx, obligation, fn_type)
ProjectionTyCandidate::FnPointer(fn_pointer_vtable) => {
confirm_fn_pointer_candidate(selcx, obligation, fn_pointer_vtable)
}
}
}

fn confirm_fn_pointer_candidate<'cx, 'gcx, 'tcx>(
selcx: &mut SelectionContext<'cx, 'gcx, 'tcx>,
obligation: &ProjectionTyObligation<'tcx>,
fn_type: Ty<'tcx>)
fn_pointer_vtable: VtableFnPointerData<'tcx, PredicateObligation<'tcx>>)
-> (Ty<'tcx>, Vec<PredicateObligation<'tcx>>)
{
let fn_type = selcx.infcx().shallow_resolve(fn_type);
// FIXME(#32730) propagate obligations (fn pointer vtable nested obligations ONLY come from
// unification in inference)
assert!(fn_pointer_vtable.nested.is_empty());
let fn_type = selcx.infcx().shallow_resolve(fn_pointer_vtable.fn_ty);
let sig = fn_type.fn_sig();
confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
}
Expand Down
Loading

0 comments on commit 6ba8a1a

Please sign in to comment.