Evaluate projection predicates during trait selection. Fixes #20296.

This commit is contained in:
Niko Matsakis 2015-01-02 11:39:47 -05:00 committed by Jorge Aparicio
parent cddb41dd1f
commit ea1ad792f9
2 changed files with 107 additions and 12 deletions

View file

@ -289,6 +289,23 @@ fn evaluate_builtin_bound_recursively<'o>(&mut self,
}
}
fn evaluate_predicates_recursively<'a,'o,I>(&mut self,
stack: Option<&TraitObligationStack<'o, 'tcx>>,
mut predicates: I)
-> EvaluationResult<'tcx>
where I : Iterator<&'a PredicateObligation<'tcx>>, 'tcx:'a
{
let mut result = EvaluatedToOk;
for obligation in predicates {
match self.evaluate_predicate_recursively(stack, obligation) {
EvaluatedToErr(e) => { return EvaluatedToErr(e); }
EvaluatedToAmbig => { result = EvaluatedToAmbig; }
EvaluatedToOk => { }
}
}
result
}
fn evaluate_predicate_recursively<'o>(&mut self,
previous_stack: Option<&TraitObligationStack<'o, 'tcx>>,
obligation: &PredicateObligation<'tcx>)
@ -320,9 +337,22 @@ fn evaluate_predicate_recursively<'o>(&mut self,
EvaluatedToOk
}
ty::Predicate::Projection(..) => {
// FIXME(#20296) -- we should be able to give a more precise answer here
EvaluatedToAmbig
ty::Predicate::Projection(ref data) => {
let result = self.infcx.probe(|_| {
let project_obligation = obligation.with(data.clone());
project::poly_project_and_unify_type(self, &project_obligation)
});
match result {
Ok(Some(subobligations)) => {
self.evaluate_predicates_recursively(previous_stack, subobligations.iter())
}
Ok(None) => {
EvaluatedToAmbig
}
Err(_) => {
EvaluatedToErr(Unimplemented)
}
}
}
}
}
@ -1026,15 +1056,7 @@ fn winnow_selection<'o>(&mut self,
selection: Selection<'tcx>)
-> EvaluationResult<'tcx>
{
let mut result = EvaluatedToOk;
for obligation in selection.iter_nested() {
match self.evaluate_predicate_recursively(stack, obligation) {
EvaluatedToErr(e) => { return EvaluatedToErr(e); }
EvaluatedToAmbig => { result = EvaluatedToAmbig; }
EvaluatedToOk => { }
}
}
result
self.evaluate_predicates_recursively(stack, selection.iter_nested())
}
/// Returns true if `candidate_i` should be dropped in favor of `candidate_j`.

View file

@ -0,0 +1,73 @@
// Copyright 2014 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 evaluate projection predicates to winnow out
// candidates during trait selection and method resolution (#20296).
// If we don't properly winnow out candidates based on the output type
// `Output=[A]`, then the impl marked with `(*)` is seen to conflict
// with all the others.
#![feature(associated_types, default_type_params)]
use std::ops::Deref;
pub trait MyEq<Sized? U=Self> for Sized? {
fn eq(&self, u: &U) -> bool;
}
impl<A, B> MyEq<[B]> for [A]
where A : MyEq<B>
{
fn eq(&self, other: &[B]) -> bool {
self.len() == other.len() &&
self.iter().zip(other.iter())
.all(|(a, b)| MyEq::eq(a, b))
}
}
// (*) This impl conflicts with everything unless the `Output=[A]`
// constraint is considered.
impl<'a, A, B, Lhs> MyEq<[B; 0]> for Lhs
where A: MyEq<B>, Lhs: Deref<Output=[A]>
{
fn eq(&self, other: &[B; 0]) -> bool {
MyEq::eq(&**self, other.as_slice())
}
}
struct DerefWithHelper<H, T> {
pub helper: H
}
trait Helper<T> {
fn helper_borrow(&self) -> &T;
}
impl<T> Helper<T> for Option<T> {
fn helper_borrow(&self) -> &T {
self.as_ref().unwrap()
}
}
impl<T, H: Helper<T>> Deref for DerefWithHelper<H, T> {
type Output = T;
fn deref(&self) -> &T {
self.helper.helper_borrow()
}
}
pub fn check<T: MyEq>(x: T, y: T) -> bool {
let d: DerefWithHelper<Option<T>, T> = DerefWithHelper { helper: Some(x) };
d.eq(&y)
}
pub fn main() {
}