2014-09-12 10:53:35 -04:00
|
|
|
// 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.
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
//! See `doc.rs` for high-level documentation
|
2014-10-09 17:19:50 -04:00
|
|
|
#![allow(dead_code)] // FIXME -- just temporarily
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::MethodMatchResult::*;
|
|
|
|
pub use self::MethodMatchedData::*;
|
2014-12-17 14:16:28 -05:00
|
|
|
use self::SelectionCandidate::*;
|
2014-11-06 00:05:53 -08:00
|
|
|
use self::BuiltinBoundConditions::*;
|
|
|
|
use self::EvaluationResult::*;
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
use super::{DerivedObligationCause};
|
2015-01-01 13:15:14 -05:00
|
|
|
use super::{project};
|
2015-01-08 06:58:41 -05:00
|
|
|
use super::project::Normalized;
|
2014-12-07 11:10:48 -05:00
|
|
|
use super::{PredicateObligation, Obligation, TraitObligation, ObligationCause};
|
2014-12-06 11:39:25 -05:00
|
|
|
use super::{ObligationCauseCode, BuiltinDerivedObligation};
|
2014-12-07 11:10:48 -05:00
|
|
|
use super::{SelectionError, Unimplemented, Overflow, OutputTypeParameterMismatch};
|
2014-09-12 10:53:35 -04:00
|
|
|
use super::{Selection};
|
|
|
|
use super::{SelectionResult};
|
2015-01-24 22:00:03 +02:00
|
|
|
use super::{VtableBuiltin, VtableImpl, VtableParam, VtableClosure,
|
2014-12-23 05:26:34 -05:00
|
|
|
VtableFnPointer, VtableObject};
|
|
|
|
use super::{VtableImplData, VtableObjectData, VtableBuiltinData};
|
|
|
|
use super::object_safety;
|
2014-09-12 10:53:35 -04:00
|
|
|
use super::{util};
|
|
|
|
|
2014-11-16 07:10:37 -05:00
|
|
|
use middle::fast_reject;
|
2014-09-18 11:08:04 -04:00
|
|
|
use middle::mem_categorization::Typer;
|
2015-01-01 13:15:14 -05:00
|
|
|
use middle::subst::{Subst, Substs, TypeSpace, VecPerParamSpace};
|
2015-01-03 22:42:21 -05:00
|
|
|
use middle::ty::{self, AsPredicate, RegionEscape, ToPolyTraitRef, Ty};
|
2014-11-25 16:59:02 -05:00
|
|
|
use middle::infer;
|
2014-12-12 06:13:42 -05:00
|
|
|
use middle::infer::{InferCtxt, TypeFreshener};
|
2014-10-09 17:19:50 -04:00
|
|
|
use middle::ty_fold::TypeFoldable;
|
2014-09-17 16:12:02 -04:00
|
|
|
use std::cell::RefCell;
|
2014-10-30 21:25:08 -04:00
|
|
|
use std::collections::hash_map::HashMap;
|
2014-09-12 10:53:35 -04:00
|
|
|
use std::rc::Rc;
|
2014-12-01 09:23:40 -05:00
|
|
|
use syntax::{abi, ast};
|
2014-11-08 06:59:10 -05:00
|
|
|
use util::common::ErrorReported;
|
2014-09-12 10:53:35 -04:00
|
|
|
use util::ppaux::Repr;
|
|
|
|
|
|
|
|
pub struct SelectionContext<'cx, 'tcx:'cx> {
|
|
|
|
infcx: &'cx InferCtxt<'cx, 'tcx>,
|
2015-01-24 22:00:03 +02:00
|
|
|
closure_typer: &'cx (ty::ClosureTyper<'tcx>+'cx),
|
2014-10-09 17:19:50 -04:00
|
|
|
|
2014-12-12 06:13:42 -05:00
|
|
|
/// Freshener used specifically for skolemizing entries on the
|
2014-10-09 17:19:50 -04:00
|
|
|
/// obligation stack. This ensures that all entries on the stack
|
|
|
|
/// at one time will have the same set of skolemized entries,
|
|
|
|
/// which is important for checking for trait bounds that
|
|
|
|
/// recursively require themselves.
|
2014-12-12 06:13:42 -05:00
|
|
|
freshener: TypeFreshener<'cx, 'tcx>,
|
2014-11-03 14:48:03 -05:00
|
|
|
|
|
|
|
/// If true, indicates that the evaluation should be conservative
|
|
|
|
/// and consider the possibility of types outside this crate.
|
|
|
|
/// This comes up primarily when resolving ambiguity. Imagine
|
|
|
|
/// there is some trait reference `$0 : Bar` where `$0` is an
|
|
|
|
/// inference variable. If `intercrate` is true, then we can never
|
|
|
|
/// say for sure that this reference is not implemented, even if
|
|
|
|
/// there are *no impls at all for `Bar`*, because `$0` could be
|
|
|
|
/// bound to some type that in a downstream crate that implements
|
|
|
|
/// `Bar`. This is the suitable mode for coherence. Elsewhere,
|
|
|
|
/// though, we set this to false, because we are only interested
|
|
|
|
/// in types that the user could actually have written --- in
|
|
|
|
/// other words, we consider `$0 : Bar` to be unimplemented if
|
|
|
|
/// there is no type that the user could *actually name* that
|
|
|
|
/// would satisfy it. This avoids crippling inference, basically.
|
|
|
|
intercrate: bool,
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// A stack that walks back up the stack frame.
|
2014-12-05 00:03:03 -05:00
|
|
|
struct TraitObligationStack<'prev, 'tcx: 'prev> {
|
|
|
|
obligation: &'prev TraitObligation<'tcx>,
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
/// Trait ref from `obligation` but skolemized with the
|
2014-12-12 06:13:42 -05:00
|
|
|
/// selection-context's freshener. Used to check for recursion.
|
2014-12-17 14:16:28 -05:00
|
|
|
fresh_trait_ref: ty::PolyTraitRef<'tcx>,
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-12-05 00:03:03 -05:00
|
|
|
previous: Option<&'prev TraitObligationStack<'prev, 'tcx>>
|
2014-09-17 16:12:02 -04:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2014-09-29 22:11:30 +03:00
|
|
|
pub struct SelectionCache<'tcx> {
|
2014-12-17 14:16:28 -05:00
|
|
|
hashmap: RefCell<HashMap<Rc<ty::TraitRef<'tcx>>,
|
|
|
|
SelectionResult<'tcx, SelectionCandidate<'tcx>>>>,
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-10-17 08:51:43 -04:00
|
|
|
pub enum MethodMatchResult {
|
|
|
|
MethodMatched(MethodMatchedData),
|
|
|
|
MethodAmbiguous(/* list of impls that could apply */ Vec<ast::DefId>),
|
|
|
|
MethodDidNotMatch,
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Copy, Show)]
|
2014-10-17 08:51:43 -04:00
|
|
|
pub enum MethodMatchedData {
|
|
|
|
// In the case of a precise match, we don't really need to store
|
|
|
|
// how the match was found. So don't.
|
|
|
|
PreciseMethodMatch,
|
|
|
|
|
|
|
|
// In the case of a coercion, we need to know the precise impl so
|
|
|
|
// that we can determine the type to which things were coerced.
|
|
|
|
CoerciveMethodMatch(/* impl we matched */ ast::DefId)
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:06:06 -05:00
|
|
|
/// The selection process begins by considering all impls, where
|
|
|
|
/// clauses, and so forth that might resolve an obligation. Sometimes
|
|
|
|
/// we'll be able to say definitively that (e.g.) an impl does not
|
|
|
|
/// apply to the obligation: perhaps it is defined for `uint` but the
|
|
|
|
/// obligation is for `int`. In that case, we drop the impl out of the
|
|
|
|
/// list. But the other cases are considered *candidates*.
|
|
|
|
///
|
|
|
|
/// Candidates can either be definitive or ambiguous. An ambiguous
|
|
|
|
/// candidate is one that might match or might not, depending on how
|
|
|
|
/// type variables wind up being resolved. This only occurs during inference.
|
|
|
|
///
|
2014-11-07 16:14:32 -05:00
|
|
|
/// For selection to succeed, there must be exactly one non-ambiguous
|
2014-11-24 20:06:06 -05:00
|
|
|
/// candidate. Usually, it is not possible to have more than one
|
|
|
|
/// definitive candidate, due to the coherence rules. However, there is
|
|
|
|
/// one case where it could occur: if there is a blanket impl for a
|
|
|
|
/// trait (that is, an impl applied to all T), and a type parameter
|
|
|
|
/// with a where clause. In that case, we can have a candidate from the
|
|
|
|
/// where clause and a second candidate from the impl. This is not a
|
|
|
|
/// problem because coherence guarantees us that the impl which would
|
|
|
|
/// be used to satisfy the where clause is the same one that we see
|
|
|
|
/// now. To resolve this issue, therefore, we ignore impls if we find a
|
|
|
|
/// matching where clause. Part of the reason for this is that where
|
|
|
|
/// clauses can give additional information (like, the types of output
|
|
|
|
/// parameters) that would have to be inferred from the impl.
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(PartialEq,Eq,Show,Clone)]
|
2014-12-17 14:16:28 -05:00
|
|
|
enum SelectionCandidate<'tcx> {
|
2014-10-09 17:19:50 -04:00
|
|
|
BuiltinCandidate(ty::BuiltinBound),
|
2014-12-27 04:22:29 -05:00
|
|
|
ParamCandidate(ty::PolyTraitRef<'tcx>),
|
2014-10-09 17:19:50 -04:00
|
|
|
ImplCandidate(ast::DefId),
|
2014-12-01 09:23:40 -05:00
|
|
|
|
2014-12-27 04:22:29 -05:00
|
|
|
/// This is a trait matching with a projected type as `Self`, and
|
|
|
|
/// we found an applicable bound in the trait definition.
|
|
|
|
ProjectionCandidate,
|
|
|
|
|
2014-12-01 09:23:40 -05:00
|
|
|
/// Implementation of a `Fn`-family trait by one of the
|
|
|
|
/// anonymous types generated for a `||` expression.
|
2015-01-24 22:00:03 +02:00
|
|
|
ClosureCandidate(/* closure */ ast::DefId, Substs<'tcx>),
|
2014-12-01 09:23:40 -05:00
|
|
|
|
|
|
|
/// Implementation of a `Fn`-family trait by one of the anonymous
|
|
|
|
/// types generated for a fn pointer type (e.g., `fn(int)->int`)
|
|
|
|
FnPointerCandidate,
|
|
|
|
|
2014-12-23 05:26:34 -05:00
|
|
|
ObjectCandidate,
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
ErrorCandidate,
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
struct SelectionCandidateSet<'tcx> {
|
2014-12-30 08:59:33 -05:00
|
|
|
// a list of candidates that definitely apply to the current
|
|
|
|
// obligation (meaning: types unify).
|
2014-12-17 14:16:28 -05:00
|
|
|
vec: Vec<SelectionCandidate<'tcx>>,
|
2014-12-30 08:59:33 -05:00
|
|
|
|
|
|
|
// if this is true, then there were candidates that might or might
|
|
|
|
// not have applied, but we couldn't tell. This occurs when some
|
|
|
|
// of the input types are type variables, in which case there are
|
|
|
|
// various "builtin" rules that might or might not trigger.
|
|
|
|
ambiguous: bool,
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
enum BuiltinBoundConditions<'tcx> {
|
|
|
|
If(Vec<Ty<'tcx>>),
|
2014-10-09 17:19:50 -04:00
|
|
|
ParameterBuiltin,
|
|
|
|
AmbiguousBuiltin
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Show)]
|
2014-12-02 14:04:10 -05:00
|
|
|
enum EvaluationResult<'tcx> {
|
2014-10-09 17:19:50 -04:00
|
|
|
EvaluatedToOk,
|
|
|
|
EvaluatedToAmbig,
|
2014-12-02 14:04:10 -05:00
|
|
|
EvaluatedToErr(SelectionError<'tcx>),
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
|
|
|
pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>,
|
2015-01-24 22:00:03 +02:00
|
|
|
closure_typer: &'cx ty::ClosureTyper<'tcx>)
|
2014-09-12 10:53:35 -04:00
|
|
|
-> SelectionContext<'cx, 'tcx> {
|
2014-09-18 11:08:04 -04:00
|
|
|
SelectionContext {
|
|
|
|
infcx: infcx,
|
2015-01-01 16:04:51 -05:00
|
|
|
closure_typer: closure_typer,
|
2014-12-12 06:13:42 -05:00
|
|
|
freshener: infcx.freshener(),
|
2014-11-03 14:48:03 -05:00
|
|
|
intercrate: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn intercrate(infcx: &'cx InferCtxt<'cx, 'tcx>,
|
2015-01-24 22:00:03 +02:00
|
|
|
closure_typer: &'cx ty::ClosureTyper<'tcx>)
|
2014-11-03 14:48:03 -05:00
|
|
|
-> SelectionContext<'cx, 'tcx> {
|
|
|
|
SelectionContext {
|
|
|
|
infcx: infcx,
|
2015-01-01 16:04:51 -05:00
|
|
|
closure_typer: closure_typer,
|
2014-12-12 06:13:42 -05:00
|
|
|
freshener: infcx.freshener(),
|
2014-11-03 14:48:03 -05:00
|
|
|
intercrate: true,
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-07 11:10:48 -05:00
|
|
|
pub fn infcx(&self) -> &'cx InferCtxt<'cx, 'tcx> {
|
|
|
|
self.infcx
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:53:35 -04:00
|
|
|
pub fn tcx(&self) -> &'cx ty::ctxt<'tcx> {
|
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
2015-01-02 04:09:35 -05:00
|
|
|
pub fn param_env(&self) -> &'cx ty::ParameterEnvironment<'cx, 'tcx> {
|
|
|
|
self.closure_typer.param_env()
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:53:35 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Selection
|
|
|
|
//
|
|
|
|
// The selection phase tries to identify *how* an obligation will
|
|
|
|
// be resolved. For example, it will identify which impl or
|
|
|
|
// parameter bound is to be used. The process can be inconclusive
|
|
|
|
// if the self type in the obligation is not fully inferred. Selection
|
|
|
|
// can result in an error in one of two ways:
|
|
|
|
//
|
|
|
|
// 1. If no applicable impl or parameter bound can be found.
|
|
|
|
// 2. If the output type parameters in the obligation do not match
|
|
|
|
// those specified by the impl/bound. For example, if the obligation
|
|
|
|
// is `Vec<Foo>:Iterable<Bar>`, but the impl specifies
|
|
|
|
// `impl<T> Iterable<T> for Vec<T>`, than an error would result.
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Evaluates whether the obligation can be satisfied. Returns an indication of whether the
|
|
|
|
/// obligation can be satisfied and, if so, by what means. Never affects surrounding typing
|
|
|
|
/// environment.
|
2014-12-05 00:03:03 -05:00
|
|
|
pub fn select(&mut self, obligation: &TraitObligation<'tcx>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> SelectionResult<'tcx, Selection<'tcx>> {
|
2014-09-12 10:53:35 -04:00
|
|
|
debug!("select({})", obligation.repr(self.tcx()));
|
2014-12-17 16:00:34 -05:00
|
|
|
assert!(!obligation.predicate.has_escaping_regions());
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
let stack = self.push_stack(None, obligation);
|
2014-09-18 11:08:04 -04:00
|
|
|
match try!(self.candidate_from_obligation(&stack)) {
|
2014-09-12 10:53:35 -04:00
|
|
|
None => Ok(None),
|
2014-10-09 17:19:50 -04:00
|
|
|
Some(candidate) => Ok(Some(try!(self.confirm_candidate(obligation, candidate)))),
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// EVALUATION
|
|
|
|
//
|
2014-10-17 08:51:43 -04:00
|
|
|
// Tests whether an obligation can be selected or whether an impl
|
|
|
|
// can be applied to particular types. It skips the "confirmation"
|
|
|
|
// step and hence completely ignores output type parameters.
|
2014-10-09 17:19:50 -04:00
|
|
|
//
|
2014-10-25 23:10:16 -04:00
|
|
|
// The result is "true" if the obligation *may* hold and "false" if
|
2014-10-09 17:19:50 -04:00
|
|
|
// we can be sure it does not.
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Evaluates whether the obligation `obligation` can be satisfied (by any means).
|
2014-11-03 14:48:03 -05:00
|
|
|
pub fn evaluate_obligation(&mut self,
|
2014-12-07 11:10:48 -05:00
|
|
|
obligation: &PredicateObligation<'tcx>)
|
2014-11-03 14:48:03 -05:00
|
|
|
-> bool
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-11-03 14:48:03 -05:00
|
|
|
debug!("evaluate_obligation({})",
|
2014-09-12 10:53:35 -04:00
|
|
|
obligation.repr(self.tcx()));
|
|
|
|
|
2014-12-07 11:10:48 -05:00
|
|
|
self.evaluate_predicate_recursively(None, obligation).may_apply()
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn evaluate_builtin_bound_recursively<'o>(&mut self,
|
|
|
|
bound: ty::BuiltinBound,
|
2014-12-05 00:03:03 -05:00
|
|
|
previous_stack: &TraitObligationStack<'o, 'tcx>,
|
2014-09-29 22:11:30 +03:00
|
|
|
ty: Ty<'tcx>)
|
2014-12-02 14:04:10 -05:00
|
|
|
-> EvaluationResult<'tcx>
|
2014-09-18 11:08:04 -04:00
|
|
|
{
|
|
|
|
let obligation =
|
2014-12-07 11:10:48 -05:00
|
|
|
util::predicate_for_builtin_bound(
|
2014-09-18 11:08:04 -04:00
|
|
|
self.tcx(),
|
2014-12-06 11:39:25 -05:00
|
|
|
previous_stack.obligation.cause.clone(),
|
2014-09-18 11:08:04 -04:00
|
|
|
bound,
|
|
|
|
previous_stack.obligation.recursion_depth + 1,
|
|
|
|
ty);
|
2014-09-23 17:18:54 +12:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
match obligation {
|
|
|
|
Ok(obligation) => {
|
2014-12-07 11:10:48 -05:00
|
|
|
self.evaluate_predicate_recursively(Some(previous_stack), &obligation)
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
|
|
|
Err(ErrorReported) => {
|
|
|
|
EvaluatedToOk
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2015-01-02 11:39:47 -05:00
|
|
|
fn evaluate_predicates_recursively<'a,'o,I>(&mut self,
|
|
|
|
stack: Option<&TraitObligationStack<'o, 'tcx>>,
|
|
|
|
mut predicates: I)
|
|
|
|
-> EvaluationResult<'tcx>
|
2015-01-01 23:26:38 -05:00
|
|
|
where I : Iterator<Item=&'a PredicateObligation<'tcx>>, 'tcx:'a
|
2015-01-02 11:39:47 -05:00
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-12-07 11:10:48 -05:00
|
|
|
fn evaluate_predicate_recursively<'o>(&mut self,
|
|
|
|
previous_stack: Option<&TraitObligationStack<'o, 'tcx>>,
|
|
|
|
obligation: &PredicateObligation<'tcx>)
|
|
|
|
-> EvaluationResult<'tcx>
|
|
|
|
{
|
|
|
|
debug!("evaluate_predicate_recursively({})",
|
|
|
|
obligation.repr(self.tcx()));
|
|
|
|
|
2014-12-17 16:00:34 -05:00
|
|
|
match obligation.predicate {
|
2014-12-07 11:10:48 -05:00
|
|
|
ty::Predicate::Trait(ref t) => {
|
|
|
|
assert!(!t.has_escaping_regions());
|
|
|
|
let obligation = obligation.with(t.clone());
|
|
|
|
self.evaluate_obligation_recursively(previous_stack, &obligation)
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:34:34 -05:00
|
|
|
ty::Predicate::Equate(ref p) => {
|
2014-12-14 07:17:23 -05:00
|
|
|
let result = self.infcx.probe(|_| {
|
2014-12-13 05:34:34 -05:00
|
|
|
self.infcx.equality_predicate(obligation.cause.span, p)
|
|
|
|
});
|
|
|
|
match result {
|
2014-12-07 11:10:48 -05:00
|
|
|
Ok(()) => EvaluatedToOk,
|
|
|
|
Err(_) => EvaluatedToErr(Unimplemented),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::Predicate::TypeOutlives(..) | ty::Predicate::RegionOutlives(..) => {
|
|
|
|
// we do not consider region relationships when
|
|
|
|
// evaluating trait matches
|
|
|
|
EvaluatedToOk
|
|
|
|
}
|
2014-12-17 14:16:28 -05:00
|
|
|
|
2015-01-02 11:39:47 -05:00
|
|
|
ty::Predicate::Projection(ref data) => {
|
2015-01-02 13:59:32 -05:00
|
|
|
self.infcx.probe(|_| {
|
2015-01-02 11:39:47 -05:00
|
|
|
let project_obligation = obligation.with(data.clone());
|
2015-01-02 13:59:32 -05:00
|
|
|
match project::poly_project_and_unify_type(self, &project_obligation) {
|
|
|
|
Ok(Some(subobligations)) => {
|
|
|
|
self.evaluate_predicates_recursively(previous_stack,
|
|
|
|
subobligations.iter())
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
EvaluatedToAmbig
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
EvaluatedToErr(Unimplemented)
|
|
|
|
}
|
2015-01-02 11:39:47 -05:00
|
|
|
}
|
2015-01-02 13:59:32 -05:00
|
|
|
})
|
2014-12-17 14:16:28 -05:00
|
|
|
}
|
2014-12-07 11:10:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn evaluate_obligation_recursively<'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
previous_stack: Option<&TraitObligationStack<'o, 'tcx>>,
|
|
|
|
obligation: &TraitObligation<'tcx>)
|
2014-12-02 14:04:10 -05:00
|
|
|
-> EvaluationResult<'tcx>
|
2014-09-18 11:08:04 -04:00
|
|
|
{
|
|
|
|
debug!("evaluate_obligation_recursively({})",
|
|
|
|
obligation.repr(self.tcx()));
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
let stack = self.push_stack(previous_stack.map(|x| x), obligation);
|
2014-10-17 08:51:43 -04:00
|
|
|
|
2014-11-03 14:48:03 -05:00
|
|
|
let result = self.evaluate_stack(&stack);
|
2014-10-17 08:51:43 -04:00
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("result: {:?}", result);
|
2014-10-09 17:17:49 -04:00
|
|
|
result
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn evaluate_stack<'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
stack: &TraitObligationStack<'o, 'tcx>)
|
2014-12-02 14:04:10 -05:00
|
|
|
-> EvaluationResult<'tcx>
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
2014-11-03 14:48:03 -05:00
|
|
|
// In intercrate mode, whenever any of the types are unbound,
|
|
|
|
// there can always be an impl. Even if there are no impls in
|
|
|
|
// this crate, perhaps the type would be unified with
|
|
|
|
// something from another crate that does provide an impl.
|
|
|
|
//
|
|
|
|
// In intracrate mode, we must still be conservative. The reason is
|
|
|
|
// that we want to avoid cycles. Imagine an impl like:
|
|
|
|
//
|
|
|
|
// impl<T:Eq> Eq for Vec<T>
|
|
|
|
//
|
|
|
|
// and a trait reference like `$0 : Eq` where `$0` is an
|
|
|
|
// unbound variable. When we evaluate this trait-reference, we
|
|
|
|
// will unify `$0` with `Vec<$1>` (for some fresh variable
|
|
|
|
// `$1`), on the condition that `$1 : Eq`. We will then wind
|
|
|
|
// up with many candidates (since that are other `Eq` impls
|
|
|
|
// that apply) and try to winnow things down. This results in
|
2015-01-06 20:53:18 -05:00
|
|
|
// a recursive evaluation that `$1 : Eq` -- as you can
|
2014-11-03 14:48:03 -05:00
|
|
|
// imagine, this is just where we started. To avoid that, we
|
|
|
|
// check for unbound variables and return an ambiguous (hence possible)
|
|
|
|
// match if we've seen this trait before.
|
|
|
|
//
|
|
|
|
// This suffices to allow chains like `FnMut` implemented in
|
|
|
|
// terms of `Fn` etc, but we could probably make this more
|
|
|
|
// precise still.
|
2014-12-12 11:28:35 -05:00
|
|
|
let input_types = stack.fresh_trait_ref.0.input_types();
|
2014-12-12 06:13:42 -05:00
|
|
|
let unbound_input_types = input_types.iter().any(|&t| ty::type_is_fresh(t));
|
2014-11-03 14:48:03 -05:00
|
|
|
if
|
|
|
|
unbound_input_types &&
|
|
|
|
(self.intercrate ||
|
|
|
|
stack.iter().skip(1).any(
|
2014-12-12 11:28:35 -05:00
|
|
|
|prev| stack.fresh_trait_ref.def_id() == prev.fresh_trait_ref.def_id()))
|
2014-11-03 14:48:03 -05:00
|
|
|
{
|
2014-12-07 11:10:48 -05:00
|
|
|
debug!("evaluate_stack({}) --> unbound argument, recursion --> ambiguous",
|
2014-12-12 06:13:42 -05:00
|
|
|
stack.fresh_trait_ref.repr(self.tcx()));
|
2014-10-09 17:19:50 -04:00
|
|
|
return EvaluatedToAmbig;
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
// If there is any previous entry on the stack that precisely
|
|
|
|
// matches this obligation, then we can assume that the
|
|
|
|
// obligation is satisfied for now (still all other conditions
|
|
|
|
// must be met of course). One obvious case this comes up is
|
2014-11-17 14:41:47 -05:00
|
|
|
// marker traits like `Send`. Think of a linked list:
|
2014-09-18 11:08:04 -04:00
|
|
|
//
|
|
|
|
// struct List<T> { data: T, next: Option<Box<List<T>>> {
|
|
|
|
//
|
|
|
|
// `Box<List<T>>` will be `Send` if `T` is `Send` and
|
|
|
|
// `Option<Box<List<T>>>` is `Send`, and in turn
|
|
|
|
// `Option<Box<List<T>>>` is `Send` if `Box<List<T>>` is
|
|
|
|
// `Send`.
|
2014-10-09 17:19:50 -04:00
|
|
|
//
|
2014-12-12 06:13:42 -05:00
|
|
|
// Note that we do this comparison using the `fresh_trait_ref`
|
2014-10-09 17:19:50 -04:00
|
|
|
// fields. Because these have all been skolemized using
|
2014-12-12 06:13:42 -05:00
|
|
|
// `self.freshener`, we can be sure that (a) this will not
|
2014-10-09 17:19:50 -04:00
|
|
|
// affect the inferencer state and (b) that if we see two
|
|
|
|
// skolemized types with the same index, they refer to the
|
|
|
|
// same unbound type variable.
|
2014-09-18 11:08:04 -04:00
|
|
|
if
|
2014-10-09 17:19:50 -04:00
|
|
|
stack.iter()
|
|
|
|
.skip(1) // skip top-most frame
|
2014-12-12 06:13:42 -05:00
|
|
|
.any(|prev| stack.fresh_trait_ref == prev.fresh_trait_ref)
|
2014-09-18 11:08:04 -04:00
|
|
|
{
|
2014-12-07 11:10:48 -05:00
|
|
|
debug!("evaluate_stack({}) --> recursive",
|
2014-12-12 06:13:42 -05:00
|
|
|
stack.fresh_trait_ref.repr(self.tcx()));
|
2014-10-09 17:19:50 -04:00
|
|
|
return EvaluatedToOk;
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
match self.candidate_from_obligation(stack) {
|
|
|
|
Ok(Some(c)) => self.winnow_candidate(stack, &c),
|
|
|
|
Ok(None) => EvaluatedToAmbig,
|
2014-12-02 14:04:10 -05:00
|
|
|
Err(e) => EvaluatedToErr(e),
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Evaluates whether the impl with id `impl_def_id` could be applied to the self type
|
|
|
|
/// `obligation_self_ty`. This can be used either for trait or inherent impls.
|
2014-09-18 11:08:04 -04:00
|
|
|
pub fn evaluate_impl(&mut self,
|
2014-09-12 10:53:35 -04:00
|
|
|
impl_def_id: ast::DefId,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>)
|
2014-10-09 17:19:50 -04:00
|
|
|
-> bool
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-10-09 17:19:50 -04:00
|
|
|
debug!("evaluate_impl(impl_def_id={}, obligation={})",
|
2014-09-12 10:53:35 -04:00
|
|
|
impl_def_id.repr(self.tcx()),
|
2014-10-09 17:19:50 -04:00
|
|
|
obligation.repr(self.tcx()));
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-12-14 07:17:23 -05:00
|
|
|
self.infcx.probe(|snapshot| {
|
|
|
|
let (skol_obligation_trait_ref, skol_map) =
|
2014-12-17 14:16:28 -05:00
|
|
|
self.infcx().skolemize_late_bound_regions(&obligation.predicate, snapshot);
|
2014-12-14 07:17:23 -05:00
|
|
|
match self.match_impl(impl_def_id, obligation, snapshot,
|
2014-12-17 14:16:28 -05:00
|
|
|
&skol_map, skol_obligation_trait_ref.trait_ref.clone()) {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(substs) => {
|
2014-10-17 08:51:43 -04:00
|
|
|
let vtable_impl = self.vtable_impl(impl_def_id,
|
|
|
|
substs,
|
2014-12-06 11:39:25 -05:00
|
|
|
obligation.cause.clone(),
|
2014-12-14 07:17:23 -05:00
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
skol_map,
|
|
|
|
snapshot);
|
2014-10-17 08:51:43 -04:00
|
|
|
self.winnow_selection(None, VtableImpl(vtable_impl)).may_apply()
|
|
|
|
}
|
|
|
|
Err(()) => {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:53:35 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// CANDIDATE ASSEMBLY
|
|
|
|
//
|
|
|
|
// The selection process begins by examining all in-scope impls,
|
|
|
|
// caller obligations, and so forth and assembling a list of
|
|
|
|
// candidates. See `doc.rs` and the `Candidate` type for more details.
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn candidate_from_obligation<'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
stack: &TraitObligationStack<'o, 'tcx>)
|
2014-12-17 14:16:28 -05:00
|
|
|
-> SelectionResult<'tcx, SelectionCandidate<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-10-09 17:19:50 -04:00
|
|
|
// Watch out for overflow. This intentionally bypasses (and does
|
|
|
|
// not update) the cache.
|
|
|
|
let recursion_limit = self.infcx.tcx.sess.recursion_limit.get();
|
|
|
|
if stack.obligation.recursion_depth >= recursion_limit {
|
|
|
|
debug!("{} --> overflow (limit={})",
|
|
|
|
stack.obligation.repr(self.tcx()),
|
|
|
|
recursion_limit);
|
|
|
|
return Err(Overflow)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the cache. Note that we skolemize the trait-ref
|
2014-12-12 06:13:42 -05:00
|
|
|
// separately rather than using `stack.fresh_trait_ref` -- this
|
2014-10-09 17:19:50 -04:00
|
|
|
// is because we want the unbound variables to be replaced
|
|
|
|
// with fresh skolemized types starting from index 0.
|
2014-12-17 14:16:28 -05:00
|
|
|
let cache_fresh_trait_pred =
|
2014-12-17 16:00:34 -05:00
|
|
|
self.infcx.freshen(stack.obligation.predicate.clone());
|
2014-12-17 14:16:28 -05:00
|
|
|
debug!("candidate_from_obligation(cache_fresh_trait_pred={}, obligation={})",
|
|
|
|
cache_fresh_trait_pred.repr(self.tcx()),
|
2014-09-18 11:08:04 -04:00
|
|
|
stack.repr(self.tcx()));
|
2014-12-17 16:00:34 -05:00
|
|
|
assert!(!stack.obligation.predicate.has_escaping_regions());
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
match self.check_candidate_cache(&cache_fresh_trait_pred) {
|
2014-09-12 10:53:35 -04:00
|
|
|
Some(c) => {
|
2014-12-17 14:16:28 -05:00
|
|
|
debug!("CACHE HIT: cache_fresh_trait_pred={}, candidate={}",
|
|
|
|
cache_fresh_trait_pred.repr(self.tcx()),
|
2014-09-17 16:12:02 -04:00
|
|
|
c.repr(self.tcx()));
|
|
|
|
return c;
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
// If no match, compute result and insert into cache.
|
2014-10-09 17:19:50 -04:00
|
|
|
let candidate = self.candidate_from_obligation_no_cache(stack);
|
2015-01-21 17:45:52 -05:00
|
|
|
|
|
|
|
if self.should_update_candidate_cache(&cache_fresh_trait_pred, &candidate) {
|
|
|
|
debug!("CACHE MISS: cache_fresh_trait_pred={}, candidate={}",
|
|
|
|
cache_fresh_trait_pred.repr(self.tcx()), candidate.repr(self.tcx()));
|
|
|
|
self.insert_candidate_cache(cache_fresh_trait_pred, candidate.clone());
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
candidate
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn candidate_from_obligation_no_cache<'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
stack: &TraitObligationStack<'o, 'tcx>)
|
2014-12-17 14:16:28 -05:00
|
|
|
-> SelectionResult<'tcx, SelectionCandidate<'tcx>>
|
2014-09-18 11:08:04 -04:00
|
|
|
{
|
2014-12-17 14:16:28 -05:00
|
|
|
if ty::type_is_error(stack.obligation.predicate.0.self_ty()) {
|
2014-09-18 11:08:04 -04:00
|
|
|
return Ok(Some(ErrorCandidate));
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
let candidate_set = try!(self.assemble_candidates(stack));
|
|
|
|
|
|
|
|
if candidate_set.ambiguous {
|
|
|
|
debug!("candidate set contains ambig");
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut candidates = candidate_set.vec;
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-12-27 04:22:29 -05:00
|
|
|
debug!("assembled {} candidates for {}: {}",
|
|
|
|
candidates.len(),
|
|
|
|
stack.repr(self.tcx()),
|
|
|
|
candidates.repr(self.tcx()));
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
// At this point, we know that each of the entries in the
|
|
|
|
// candidate set is *individually* applicable. Now we have to
|
|
|
|
// figure out if they contain mutual incompatibilities. This
|
|
|
|
// frequently arises if we have an unconstrained input type --
|
|
|
|
// for example, we are looking for $0:Eq where $0 is some
|
|
|
|
// unconstrained type variable. In that case, we'll get a
|
|
|
|
// candidate which assumes $0 == int, one that assumes $0 ==
|
|
|
|
// uint, etc. This spells an ambiguity.
|
|
|
|
|
|
|
|
// If there is more than one candidate, first winnow them down
|
|
|
|
// by considering extra conditions (nested obligations and so
|
|
|
|
// forth). We don't winnow if there is exactly one
|
|
|
|
// candidate. This is a relatively minor distinction but it
|
|
|
|
// can lead to better inference and error-reporting. An
|
|
|
|
// example would be if there was an impl:
|
|
|
|
//
|
|
|
|
// impl<T:Clone> Vec<T> { fn push_clone(...) { ... } }
|
|
|
|
//
|
|
|
|
// and we were to see some code `foo.push_clone()` where `boo`
|
|
|
|
// is a `Vec<Bar>` and `Bar` does not implement `Clone`. If
|
|
|
|
// we were to winnow, we'd wind up with zero candidates.
|
|
|
|
// Instead, we select the right impl now but report `Bar does
|
|
|
|
// not implement Clone`.
|
|
|
|
if candidates.len() > 1 {
|
|
|
|
candidates.retain(|c| self.winnow_candidate(stack, c).may_apply())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are STILL multiple candidate, we can further reduce
|
|
|
|
// the list by dropping duplicates.
|
|
|
|
if candidates.len() > 1 {
|
|
|
|
let mut i = 0;
|
|
|
|
while i < candidates.len() {
|
|
|
|
let is_dup =
|
|
|
|
range(0, candidates.len())
|
|
|
|
.filter(|&j| i != j)
|
|
|
|
.any(|j| self.candidate_should_be_dropped_in_favor_of(stack,
|
|
|
|
&candidates[i],
|
|
|
|
&candidates[j]));
|
|
|
|
if is_dup {
|
2014-10-28 07:24:25 -04:00
|
|
|
debug!("Dropping candidate #{}/{}: {}",
|
2014-10-09 17:19:50 -04:00
|
|
|
i, candidates.len(), candidates[i].repr(self.tcx()));
|
|
|
|
candidates.swap_remove(i);
|
|
|
|
} else {
|
2014-10-28 07:24:25 -04:00
|
|
|
debug!("Retaining candidate #{}/{}: {}",
|
|
|
|
i, candidates.len(), candidates[i].repr(self.tcx()));
|
2014-10-09 17:19:50 -04:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are *STILL* multiple candidates, give up and
|
2015-01-06 20:53:18 -05:00
|
|
|
// report ambiguity.
|
2014-10-09 17:19:50 -04:00
|
|
|
if candidates.len() > 1 {
|
|
|
|
debug!("multiple matches, ambig");
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2015-01-08 00:41:50 +01:00
|
|
|
|
2014-10-17 08:51:43 -04:00
|
|
|
// If there are *NO* candidates, that there are no impls --
|
|
|
|
// that we know of, anyway. Note that in the case where there
|
|
|
|
// are unbound type variables within the obligation, it might
|
|
|
|
// be the case that you could still satisfy the obligation
|
|
|
|
// from another crate by instantiating the type variables with
|
|
|
|
// a type from another crate that does have an impl. This case
|
2014-11-03 14:48:03 -05:00
|
|
|
// is checked for in `evaluate_stack` (and hence users
|
2014-10-17 08:51:43 -04:00
|
|
|
// who might care about this case, like coherence, should use
|
|
|
|
// that function).
|
2014-09-12 10:53:35 -04:00
|
|
|
if candidates.len() == 0 {
|
2014-10-17 08:51:43 -04:00
|
|
|
return Err(Unimplemented);
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
|
|
|
|
// Just one candidate left.
|
|
|
|
let candidate = candidates.pop().unwrap();
|
2015-01-08 00:41:50 +01:00
|
|
|
|
|
|
|
match candidate {
|
|
|
|
ImplCandidate(def_id) => {
|
|
|
|
match ty::trait_impl_polarity(self.tcx(), def_id) {
|
|
|
|
Some(ast::ImplPolarity::Negative) => return Err(Unimplemented),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(Some(candidate))
|
2014-09-17 16:12:02 -04:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-09-17 16:12:02 -04:00
|
|
|
fn pick_candidate_cache(&self,
|
2014-12-17 14:16:28 -05:00
|
|
|
cache_fresh_trait_pred: &ty::PolyTraitPredicate<'tcx>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> &SelectionCache<'tcx>
|
2014-09-17 16:12:02 -04:00
|
|
|
{
|
2014-10-22 11:35:53 -04:00
|
|
|
// High-level idea: we have to decide whether to consult the
|
|
|
|
// cache that is specific to this scope, or to consult the
|
|
|
|
// global cache. We want the cache that is specific to this
|
|
|
|
// scope whenever where clauses might affect the result.
|
|
|
|
|
2014-11-03 14:48:03 -05:00
|
|
|
// Avoid using the master cache during coherence and just rely
|
|
|
|
// on the local cache. This effectively disables caching
|
|
|
|
// during coherence. It is really just a simplification to
|
|
|
|
// avoid us having to fear that coherence results "pollute"
|
|
|
|
// the master cache. Since coherence executes pretty quickly,
|
|
|
|
// it's not worth going to more trouble to increase the
|
|
|
|
// hit-rate I don't think.
|
|
|
|
if self.intercrate {
|
2015-01-02 04:09:35 -05:00
|
|
|
return &self.param_env().selection_cache;
|
2014-11-03 14:48:03 -05:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
// If the trait refers to any parameters in scope, then use
|
2014-10-22 11:35:53 -04:00
|
|
|
// the cache of the param-environment.
|
2014-09-17 16:12:02 -04:00
|
|
|
if
|
2014-12-17 14:16:28 -05:00
|
|
|
cache_fresh_trait_pred.0.input_types().iter().any(
|
2014-10-09 17:19:50 -04:00
|
|
|
|&t| ty::type_has_self(t) || ty::type_has_params(t))
|
2014-09-17 16:12:02 -04:00
|
|
|
{
|
2015-01-02 04:09:35 -05:00
|
|
|
return &self.param_env().selection_cache;
|
2014-09-17 16:12:02 -04:00
|
|
|
}
|
2014-10-22 11:35:53 -04:00
|
|
|
|
|
|
|
// If the trait refers to unbound type variables, and there
|
|
|
|
// are where clauses in scope, then use the local environment.
|
|
|
|
// If there are no where clauses in scope, which is a very
|
|
|
|
// common case, then we can use the global environment.
|
|
|
|
// See the discussion in doc.rs for more details.
|
|
|
|
if
|
2015-01-02 04:09:35 -05:00
|
|
|
!self.param_env().caller_bounds.is_empty() &&
|
2014-12-17 14:16:28 -05:00
|
|
|
cache_fresh_trait_pred.0.input_types().iter().any(
|
2014-10-22 11:35:53 -04:00
|
|
|
|&t| ty::type_has_ty_infer(t))
|
|
|
|
{
|
2015-01-02 04:09:35 -05:00
|
|
|
return &self.param_env().selection_cache;
|
2014-10-22 11:35:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we can use the global cache.
|
|
|
|
&self.tcx().selection_cache
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
fn check_candidate_cache(&mut self,
|
2014-12-17 14:16:28 -05:00
|
|
|
cache_fresh_trait_pred: &ty::PolyTraitPredicate<'tcx>)
|
|
|
|
-> Option<SelectionResult<'tcx, SelectionCandidate<'tcx>>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-12-17 14:16:28 -05:00
|
|
|
let cache = self.pick_candidate_cache(cache_fresh_trait_pred);
|
2014-09-17 16:12:02 -04:00
|
|
|
let hashmap = cache.hashmap.borrow();
|
2014-12-17 14:16:28 -05:00
|
|
|
hashmap.get(&cache_fresh_trait_pred.0.trait_ref).map(|c| (*c).clone())
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
fn insert_candidate_cache(&mut self,
|
2014-12-17 14:16:28 -05:00
|
|
|
cache_fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
|
|
|
candidate: SelectionResult<'tcx, SelectionCandidate<'tcx>>)
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-12-17 14:16:28 -05:00
|
|
|
let cache = self.pick_candidate_cache(&cache_fresh_trait_pred);
|
2014-09-17 16:12:02 -04:00
|
|
|
let mut hashmap = cache.hashmap.borrow_mut();
|
2014-12-17 14:16:28 -05:00
|
|
|
hashmap.insert(cache_fresh_trait_pred.0.trait_ref.clone(), candidate);
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-01-21 17:45:52 -05:00
|
|
|
fn should_update_candidate_cache(&mut self,
|
|
|
|
cache_fresh_trait_pred: &ty::PolyTraitPredicate<'tcx>,
|
|
|
|
candidate: &SelectionResult<'tcx, SelectionCandidate<'tcx>>)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
// In general, it's a good idea to cache results, even
|
|
|
|
// ambigious ones, to save us some trouble later. But we have
|
|
|
|
// to be careful not to cache results that could be
|
|
|
|
// invalidated later by advances in inference. Normally, this
|
|
|
|
// is not an issue, because any inference variables whose
|
|
|
|
// types are not yet bound are "freshened" in the cache key,
|
|
|
|
// which means that if we later get the same request once that
|
|
|
|
// type variable IS bound, we'll have a different cache key.
|
|
|
|
// For example, if we have `Vec<_#0t> : Foo`, and `_#0t` is
|
|
|
|
// not yet known, we may cache the result as `None`. But if
|
|
|
|
// later `_#0t` is bound to `Bar`, then when we freshen we'll
|
|
|
|
// have `Vec<Bar> : Foo` as the cache key.
|
|
|
|
//
|
|
|
|
// HOWEVER, it CAN happen that we get an ambiguity result in
|
|
|
|
// one particular case around closures where the cache key
|
|
|
|
// would not change. That is when the precise types of the
|
|
|
|
// upvars that a closure references have not yet been figured
|
|
|
|
// out (i.e., because it is not yet known if they are captured
|
|
|
|
// by ref, and if by ref, what kind of ref). In these cases,
|
|
|
|
// when matching a builtin bound, we will yield back an
|
|
|
|
// ambiguous result. But the *cache key* is just the closure type,
|
|
|
|
// it doesn't capture the state of the upvar computation.
|
|
|
|
//
|
|
|
|
// To avoid this trap, just don't cache ambiguous results if
|
|
|
|
// the self-type contains no inference byproducts (that really
|
|
|
|
// shouldn't happen in other circumstances anyway, given
|
|
|
|
// coherence).
|
|
|
|
|
|
|
|
match *candidate {
|
|
|
|
Ok(Some(_)) | Err(_) => true,
|
|
|
|
Ok(None) => {
|
|
|
|
cache_fresh_trait_pred.0.input_types().iter().any(|&t| ty::type_has_ty_infer(t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn assemble_candidates<'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
stack: &TraitObligationStack<'o, 'tcx>)
|
2014-12-17 14:16:28 -05:00
|
|
|
-> Result<SelectionCandidateSet<'tcx>, SelectionError<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
|
|
|
// Check for overflow.
|
|
|
|
|
2014-12-05 00:03:03 -05:00
|
|
|
let TraitObligationStack { obligation, .. } = *stack;
|
2014-09-18 11:08:04 -04:00
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
let mut candidates = SelectionCandidateSet {
|
2014-10-09 17:19:50 -04:00
|
|
|
vec: Vec::new(),
|
|
|
|
ambiguous: false
|
|
|
|
};
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
// Other bounds. Consider both in-scope bounds from fn decl
|
|
|
|
// and applicable impls. There is a certain set of precedence rules here.
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-12-17 16:00:34 -05:00
|
|
|
match self.tcx().lang_items.to_builtin_kind(obligation.predicate.def_id()) {
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
Some(ty::BoundCopy) => {
|
|
|
|
debug!("obligation self ty is {}",
|
2014-12-17 14:16:28 -05:00
|
|
|
obligation.predicate.0.self_ty().repr(self.tcx()));
|
2014-12-08 13:21:35 -05:00
|
|
|
|
2015-01-08 00:41:50 +01:00
|
|
|
try!(self.assemble_candidates_from_impls(obligation, &mut candidates));
|
2014-12-08 13:21:35 -05:00
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
try!(self.assemble_builtin_bound_candidates(ty::BoundCopy,
|
|
|
|
stack,
|
|
|
|
&mut candidates));
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
Some(bound @ ty::BoundSend) |
|
|
|
|
Some(bound @ ty::BoundSync) => {
|
2015-01-08 00:41:50 +01:00
|
|
|
try!(self.assemble_candidates_from_impls(obligation, &mut candidates));
|
2014-12-06 11:39:25 -05:00
|
|
|
|
|
|
|
// No explicit impls were declared for this type, consider the fallback rules.
|
2015-01-11 01:01:48 +01:00
|
|
|
if candidates.vec.is_empty() && !candidates.ambiguous {
|
2014-12-06 11:39:25 -05:00
|
|
|
try!(self.assemble_builtin_bound_candidates(bound, stack, &mut candidates));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(bound @ ty::BoundSized) => {
|
|
|
|
// Sized and Copy are always automatically computed.
|
|
|
|
try!(self.assemble_builtin_bound_candidates(bound, stack, &mut candidates));
|
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
None => {
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
// For the time being, we ignore user-defined impls for builtin-bounds, other than
|
|
|
|
// `Copy`.
|
2014-10-09 17:19:50 -04:00
|
|
|
// (And unboxed candidates only apply to the Fn/FnMut/etc traits.)
|
2015-01-24 22:00:03 +02:00
|
|
|
try!(self.assemble_closure_candidates(obligation, &mut candidates));
|
2014-12-01 09:23:40 -05:00
|
|
|
try!(self.assemble_fn_pointer_candidates(obligation, &mut candidates));
|
2015-01-08 00:41:50 +01:00
|
|
|
try!(self.assemble_candidates_from_impls(obligation, &mut candidates));
|
2014-12-23 05:26:34 -05:00
|
|
|
self.assemble_candidates_from_object_ty(obligation, &mut candidates);
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2014-12-27 04:22:29 -05:00
|
|
|
self.assemble_candidates_from_projected_tys(obligation, &mut candidates);
|
2015-01-08 21:41:42 -05:00
|
|
|
try!(self.assemble_candidates_from_caller_bounds(stack, &mut candidates));
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
debug!("candidate list size: {}", candidates.vec.len());
|
2014-09-12 10:53:35 -04:00
|
|
|
Ok(candidates)
|
|
|
|
}
|
|
|
|
|
2014-12-27 04:22:29 -05:00
|
|
|
fn assemble_candidates_from_projected_tys(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
candidates: &mut SelectionCandidateSet<'tcx>)
|
|
|
|
{
|
|
|
|
let poly_trait_predicate =
|
|
|
|
self.infcx().resolve_type_vars_if_possible(&obligation.predicate);
|
|
|
|
|
|
|
|
debug!("assemble_candidates_for_projected_tys({},{})",
|
|
|
|
obligation.repr(self.tcx()),
|
|
|
|
poly_trait_predicate.repr(self.tcx()));
|
|
|
|
|
|
|
|
// FIXME(#20297) -- just examining the self-type is very simplistic
|
|
|
|
|
|
|
|
// before we go into the whole skolemization thing, just
|
|
|
|
// quickly check if the self-type is a projection at all.
|
|
|
|
let trait_def_id = match poly_trait_predicate.0.trait_ref.self_ty().sty {
|
|
|
|
ty::ty_projection(ref data) => data.trait_ref.def_id,
|
|
|
|
ty::ty_infer(ty::TyVar(_)) => {
|
2014-12-29 11:22:16 -05:00
|
|
|
// If the self-type is an inference variable, then it MAY wind up
|
|
|
|
// being a projected type, so induce an ambiguity.
|
|
|
|
//
|
|
|
|
// FIXME(#20297) -- being strict about this can cause
|
|
|
|
// inference failures with BorrowFrom, which is
|
|
|
|
// unfortunate. Can we do better here?
|
2015-01-21 17:45:52 -05:00
|
|
|
debug!("assemble_candidates_for_projected_tys: ambiguous self-type");
|
2014-12-29 11:22:16 -05:00
|
|
|
candidates.ambiguous = true;
|
2014-12-27 04:22:29 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => { return; }
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("assemble_candidates_for_projected_tys: trait_def_id={}",
|
|
|
|
trait_def_id.repr(self.tcx()));
|
|
|
|
|
|
|
|
let result = self.infcx.probe(|snapshot| {
|
|
|
|
self.match_projection_obligation_against_bounds_from_trait(obligation,
|
|
|
|
snapshot)
|
|
|
|
});
|
|
|
|
|
|
|
|
if result {
|
|
|
|
candidates.vec.push(ProjectionCandidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_projection_obligation_against_bounds_from_trait(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
snapshot: &infer::CombinedSnapshot)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
let poly_trait_predicate =
|
|
|
|
self.infcx().resolve_type_vars_if_possible(&obligation.predicate);
|
|
|
|
let (skol_trait_predicate, skol_map) =
|
|
|
|
self.infcx().skolemize_late_bound_regions(&poly_trait_predicate, snapshot);
|
|
|
|
debug!("match_projection_obligation_against_bounds_from_trait: \
|
|
|
|
skol_trait_predicate={} skol_map={}",
|
|
|
|
skol_trait_predicate.repr(self.tcx()),
|
|
|
|
skol_map.repr(self.tcx()));
|
|
|
|
|
|
|
|
let projection_trait_ref = match skol_trait_predicate.trait_ref.self_ty().sty {
|
|
|
|
ty::ty_projection(ref data) => &data.trait_ref,
|
|
|
|
_ => {
|
|
|
|
self.tcx().sess.span_bug(
|
|
|
|
obligation.cause.span,
|
|
|
|
format!("match_projection_obligation_against_bounds_from_trait() called \
|
|
|
|
but self-ty not a projection: {}",
|
|
|
|
skol_trait_predicate.trait_ref.self_ty().repr(self.tcx())).as_slice());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
debug!("match_projection_obligation_against_bounds_from_trait: \
|
|
|
|
projection_trait_ref={}",
|
|
|
|
projection_trait_ref.repr(self.tcx()));
|
|
|
|
|
|
|
|
let trait_def = ty::lookup_trait_def(self.tcx(), projection_trait_ref.def_id);
|
2014-12-29 13:41:25 -05:00
|
|
|
let bounds = trait_def.generics.to_bounds(self.tcx(), projection_trait_ref.substs);
|
2014-12-27 04:22:29 -05:00
|
|
|
debug!("match_projection_obligation_against_bounds_from_trait: \
|
|
|
|
bounds={}",
|
|
|
|
bounds.repr(self.tcx()));
|
|
|
|
|
|
|
|
let matching_bound =
|
2015-01-05 21:50:01 -05:00
|
|
|
util::elaborate_predicates(self.tcx(), bounds.predicates.into_vec())
|
2014-12-27 04:22:29 -05:00
|
|
|
.filter_to_traits()
|
|
|
|
.find(
|
|
|
|
|bound| self.infcx.probe(
|
|
|
|
|_| self.match_projection(obligation,
|
|
|
|
bound.clone(),
|
|
|
|
skol_trait_predicate.trait_ref.clone(),
|
|
|
|
&skol_map,
|
|
|
|
snapshot)));
|
|
|
|
|
|
|
|
debug!("match_projection_obligation_against_bounds_from_trait: \
|
|
|
|
matching_bound={}",
|
|
|
|
matching_bound.repr(self.tcx()));
|
|
|
|
match matching_bound {
|
|
|
|
None => false,
|
|
|
|
Some(bound) => {
|
|
|
|
// Repeat the successful match, if any, this time outside of a probe.
|
|
|
|
let result = self.match_projection(obligation,
|
|
|
|
bound,
|
|
|
|
skol_trait_predicate.trait_ref.clone(),
|
|
|
|
&skol_map,
|
|
|
|
snapshot);
|
|
|
|
assert!(result);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_projection(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
trait_bound: ty::PolyTraitRef<'tcx>,
|
|
|
|
skol_trait_ref: Rc<ty::TraitRef<'tcx>>,
|
|
|
|
skol_map: &infer::SkolemizationMap,
|
|
|
|
snapshot: &infer::CombinedSnapshot)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
assert!(!skol_trait_ref.has_escaping_regions());
|
|
|
|
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
|
|
|
|
match self.infcx.sub_poly_trait_refs(false,
|
|
|
|
origin,
|
|
|
|
trait_bound.clone(),
|
|
|
|
ty::Binder(skol_trait_ref.clone())) {
|
|
|
|
Ok(()) => { }
|
|
|
|
Err(_) => { return false; }
|
|
|
|
}
|
|
|
|
|
|
|
|
self.infcx.leak_check(skol_map, snapshot).is_ok()
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Given an obligation like `<SomeTrait for T>`, search the obligations that the caller
|
|
|
|
/// supplied to find out whether it is listed among them.
|
|
|
|
///
|
|
|
|
/// Never affects inference environment.
|
2015-01-08 21:41:42 -05:00
|
|
|
fn assemble_candidates_from_caller_bounds<'o>(&mut self,
|
|
|
|
stack: &TraitObligationStack<'o, 'tcx>,
|
|
|
|
candidates: &mut SelectionCandidateSet<'tcx>)
|
|
|
|
-> Result<(),SelectionError<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-10-09 17:19:50 -04:00
|
|
|
debug!("assemble_candidates_from_caller_bounds({})",
|
2015-01-08 21:41:42 -05:00
|
|
|
stack.obligation.repr(self.tcx()));
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-12-11 13:37:37 -05:00
|
|
|
let caller_trait_refs: Vec<_> =
|
2015-01-26 16:37:45 -05:00
|
|
|
self.param_env().caller_bounds.iter()
|
2014-12-17 14:16:28 -05:00
|
|
|
.filter_map(|o| o.to_opt_poly_trait_ref())
|
2014-10-09 17:19:50 -04:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let all_bounds =
|
|
|
|
util::transitive_bounds(
|
2015-01-07 11:58:31 -05:00
|
|
|
self.tcx(), &caller_trait_refs[]);
|
2014-10-09 17:19:50 -04:00
|
|
|
|
|
|
|
let matching_bounds =
|
|
|
|
all_bounds.filter(
|
2015-01-08 21:41:42 -05:00
|
|
|
|bound| self.evaluate_where_clause(stack, bound.clone()).may_apply());
|
2014-10-09 17:19:50 -04:00
|
|
|
|
|
|
|
let param_candidates =
|
2014-12-27 04:22:29 -05:00
|
|
|
matching_bounds.map(|bound| ParamCandidate(bound));
|
2014-10-09 17:19:50 -04:00
|
|
|
|
|
|
|
candidates.vec.extend(param_candidates);
|
2014-09-12 10:53:35 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-01-08 21:41:42 -05:00
|
|
|
fn evaluate_where_clause<'o>(&mut self,
|
|
|
|
stack: &TraitObligationStack<'o, 'tcx>,
|
|
|
|
where_clause_trait_ref: ty::PolyTraitRef<'tcx>)
|
|
|
|
-> EvaluationResult<'tcx>
|
|
|
|
{
|
|
|
|
self.infcx().probe(move |_| {
|
|
|
|
match self.match_where_clause_trait_ref(stack.obligation, where_clause_trait_ref) {
|
|
|
|
Ok(obligations) => {
|
|
|
|
self.evaluate_predicates_recursively(Some(stack), obligations.iter())
|
|
|
|
}
|
|
|
|
Err(()) => {
|
|
|
|
EvaluatedToErr(Unimplemented)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Check for the artificial impl that the compiler will create for an obligation like `X :
|
2015-01-24 22:15:08 +02:00
|
|
|
/// FnMut<..>` where `X` is a closure type.
|
2014-11-25 21:17:11 -05:00
|
|
|
///
|
2015-01-24 22:15:08 +02:00
|
|
|
/// Note: the type parameters on a closure candidate are modeled as *output* type
|
2014-11-25 21:17:11 -05:00
|
|
|
/// parameters and hence do not affect whether this trait is a match or not. They will be
|
|
|
|
/// unified during the confirmation step.
|
2015-01-24 22:00:03 +02:00
|
|
|
fn assemble_closure_candidates(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
candidates: &mut SelectionCandidateSet<'tcx>)
|
|
|
|
-> Result<(),SelectionError<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-12-17 14:16:28 -05:00
|
|
|
let kind = match self.fn_family_trait_kind(obligation.predicate.0.def_id()) {
|
2014-12-01 09:23:40 -05:00
|
|
|
Some(k) => k,
|
|
|
|
None => { return Ok(()); }
|
2014-10-13 19:54:01 -04:00
|
|
|
};
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
|
2014-10-31 10:51:16 +02:00
|
|
|
let (closure_def_id, substs) = match self_ty.sty {
|
2015-01-24 22:00:03 +02:00
|
|
|
ty::ty_closure(id, _, ref substs) => (id, substs.clone()),
|
2014-10-09 17:19:50 -04:00
|
|
|
ty::ty_infer(ty::TyVar(_)) => {
|
2015-01-21 17:45:52 -05:00
|
|
|
debug!("assemble_unboxed_closure_candidates: ambiguous self-type");
|
2014-10-09 17:19:50 -04:00
|
|
|
candidates.ambiguous = true;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
_ => { return Ok(()); }
|
|
|
|
};
|
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("assemble_unboxed_candidates: self_ty={} kind={:?} obligation={}",
|
2014-10-09 17:19:50 -04:00
|
|
|
self_ty.repr(self.tcx()),
|
2014-11-26 10:07:22 -05:00
|
|
|
kind,
|
2014-10-09 17:19:50 -04:00
|
|
|
obligation.repr(self.tcx()));
|
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
let closure_kind = self.closure_typer.closure_kind(closure_def_id);
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("closure_kind = {:?}", closure_kind);
|
2014-11-26 10:07:22 -05:00
|
|
|
|
2014-10-13 19:54:01 -04:00
|
|
|
if closure_kind == kind {
|
2015-01-24 22:00:03 +02:00
|
|
|
candidates.vec.push(ClosureCandidate(closure_def_id, substs.clone()));
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-12-01 09:23:40 -05:00
|
|
|
/// Implement one of the `Fn()` family for a fn pointer.
|
|
|
|
fn assemble_fn_pointer_candidates(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
2014-12-17 14:16:28 -05:00
|
|
|
candidates: &mut SelectionCandidateSet<'tcx>)
|
2014-12-01 09:23:40 -05:00
|
|
|
-> Result<(),SelectionError<'tcx>>
|
|
|
|
{
|
2014-12-04 00:57:38 -05:00
|
|
|
// We provide a `Fn` impl for fn pointers. There is no need to provide
|
|
|
|
// the other traits (e.g. `FnMut`) since those are provided by blanket
|
|
|
|
// impls.
|
2014-12-17 16:00:34 -05:00
|
|
|
if Some(obligation.predicate.def_id()) != self.tcx().lang_items.fn_trait() {
|
2014-12-01 09:23:40 -05:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
|
|
|
|
match self_ty.sty {
|
2014-12-23 05:26:34 -05:00
|
|
|
ty::ty_infer(ty::TyVar(_)) => {
|
2015-01-21 17:45:52 -05:00
|
|
|
debug!("assemble_fn_pointer_candidates: ambiguous self-type");
|
2014-12-01 09:23:40 -05:00
|
|
|
candidates.ambiguous = true; // could wind up being a fn() type
|
|
|
|
}
|
|
|
|
|
|
|
|
// provide an impl, but only for suitable `fn` pointers
|
2014-12-04 14:52:20 -08:00
|
|
|
ty::ty_bare_fn(_, &ty::BareFnTy {
|
2014-12-09 10:36:46 -05:00
|
|
|
unsafety: ast::Unsafety::Normal,
|
2014-12-01 09:23:40 -05:00
|
|
|
abi: abi::Rust,
|
2014-12-12 11:28:35 -05:00
|
|
|
sig: ty::Binder(ty::FnSig {
|
2014-12-01 09:23:40 -05:00
|
|
|
inputs: _,
|
|
|
|
output: ty::FnConverging(_),
|
|
|
|
variadic: false
|
2014-12-12 11:28:35 -05:00
|
|
|
})
|
2014-12-01 09:23:40 -05:00
|
|
|
}) => {
|
|
|
|
candidates.vec.push(FnPointerCandidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Search for impls that might apply to `obligation`.
|
2014-09-18 11:08:04 -04:00
|
|
|
fn assemble_candidates_from_impls(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
2015-01-08 00:41:50 +01:00
|
|
|
candidates: &mut SelectionCandidateSet<'tcx>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Result<(), SelectionError<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2015-01-08 00:41:50 +01:00
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
|
|
|
|
debug!("assemble_candidates_from_impls(self_ty={})", self_ty.repr(self.tcx()));
|
|
|
|
|
2014-12-17 16:00:34 -05:00
|
|
|
let all_impls = self.all_impls(obligation.predicate.def_id());
|
2014-09-12 10:53:35 -04:00
|
|
|
for &impl_def_id in all_impls.iter() {
|
2014-12-14 07:17:23 -05:00
|
|
|
self.infcx.probe(|snapshot| {
|
2014-12-17 14:16:28 -05:00
|
|
|
let (skol_obligation_trait_pred, skol_map) =
|
|
|
|
self.infcx().skolemize_late_bound_regions(&obligation.predicate, snapshot);
|
2014-12-14 07:17:23 -05:00
|
|
|
match self.match_impl(impl_def_id, obligation, snapshot,
|
2014-12-17 14:16:28 -05:00
|
|
|
&skol_map, skol_obligation_trait_pred.trait_ref.clone()) {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(_) => {
|
2015-01-08 00:41:50 +01:00
|
|
|
candidates.vec.push(ImplCandidate(impl_def_id));
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
Err(()) => { }
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2014-12-23 05:26:34 -05:00
|
|
|
/// Search for impls that might apply to `obligation`.
|
|
|
|
fn assemble_candidates_from_object_ty(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
candidates: &mut SelectionCandidateSet<'tcx>)
|
|
|
|
{
|
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
|
|
|
|
|
|
|
|
debug!("assemble_candidates_from_object_ty(self_ty={})",
|
|
|
|
self_ty.repr(self.tcx()));
|
|
|
|
|
|
|
|
// Object-safety candidates are only applicable to object-safe
|
|
|
|
// traits. Including this check is useful because it helps
|
|
|
|
// inference in cases of traits like `BorrowFrom`, which are
|
|
|
|
// not object-safe, and which rely on being able to infer the
|
|
|
|
// self-type from one of the other inputs. Without this check,
|
|
|
|
// these cases wind up being considered ambiguous due to a
|
|
|
|
// (spurious) ambiguity introduced here.
|
|
|
|
if !object_safety::is_object_safe(self.tcx(), obligation.predicate.to_poly_trait_ref()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let poly_trait_ref = match self_ty.sty {
|
|
|
|
ty::ty_trait(ref data) => {
|
|
|
|
data.principal_trait_ref_with_self_ty(self.tcx(), self_ty)
|
|
|
|
}
|
|
|
|
ty::ty_infer(ty::TyVar(_)) => {
|
|
|
|
debug!("assemble_candidates_from_object_ty: ambiguous");
|
|
|
|
candidates.ambiguous = true; // could wind up being an object type
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("assemble_candidates_from_object_ty: poly_trait_ref={}",
|
|
|
|
poly_trait_ref.repr(self.tcx()));
|
|
|
|
|
|
|
|
// see whether the object trait can be upcast to the trait we are looking for
|
|
|
|
let obligation_def_id = obligation.predicate.def_id();
|
|
|
|
let upcast_trait_ref = match util::upcast(self.tcx(), poly_trait_ref, obligation_def_id) {
|
|
|
|
Some(r) => r,
|
|
|
|
None => { return; }
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("assemble_candidates_from_object_ty: upcast_trait_ref={}",
|
|
|
|
upcast_trait_ref.repr(self.tcx()));
|
|
|
|
|
|
|
|
// check whether the upcast version of the trait-ref matches what we are looking for
|
2015-01-02 05:34:49 -05:00
|
|
|
if let Ok(()) = self.infcx.probe(|_| self.match_poly_trait_ref(obligation,
|
|
|
|
upcast_trait_ref.clone())) {
|
|
|
|
debug!("assemble_candidates_from_object_ty: matched, pushing candidate");
|
|
|
|
candidates.vec.push(ObjectCandidate);
|
2014-12-23 05:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// WINNOW
|
|
|
|
//
|
|
|
|
// Winnowing is the process of attempting to resolve ambiguity by
|
|
|
|
// probing further. During the winnowing process, we unify all
|
|
|
|
// type variables (ignoring skolemization) and then we also
|
|
|
|
// attempt to evaluate recursive bounds to see if they are
|
|
|
|
// satisfied.
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Further evaluate `candidate` to decide whether all type parameters match and whether nested
|
|
|
|
/// obligations are met. Returns true if `candidate` remains viable after this further
|
|
|
|
/// scrutiny.
|
2014-09-29 22:11:30 +03:00
|
|
|
fn winnow_candidate<'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
stack: &TraitObligationStack<'o, 'tcx>,
|
2014-12-17 14:16:28 -05:00
|
|
|
candidate: &SelectionCandidate<'tcx>)
|
2014-12-02 14:04:10 -05:00
|
|
|
-> EvaluationResult<'tcx>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-12-14 07:17:23 -05:00
|
|
|
debug!("winnow_candidate: candidate={}", candidate.repr(self.tcx()));
|
|
|
|
let result = self.infcx.probe(|_| {
|
2014-10-09 17:19:50 -04:00
|
|
|
let candidate = (*candidate).clone();
|
|
|
|
match self.confirm_candidate(stack.obligation, candidate) {
|
|
|
|
Ok(selection) => self.winnow_selection(Some(stack), selection),
|
2014-12-02 14:04:10 -05:00
|
|
|
Err(error) => EvaluatedToErr(error),
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2014-11-26 10:07:22 -05:00
|
|
|
});
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("winnow_candidate depth={} result={:?}",
|
2014-11-26 10:07:22 -05:00
|
|
|
stack.obligation.recursion_depth, result);
|
|
|
|
result
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn winnow_selection<'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
stack: Option<&TraitObligationStack<'o, 'tcx>>,
|
2014-09-29 22:11:30 +03:00
|
|
|
selection: Selection<'tcx>)
|
2014-12-02 14:04:10 -05:00
|
|
|
-> EvaluationResult<'tcx>
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
2015-01-02 11:39:47 -05:00
|
|
|
self.evaluate_predicates_recursively(stack, selection.iter_nested())
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Returns true if `candidate_i` should be dropped in favor of `candidate_j`.
|
|
|
|
///
|
|
|
|
/// This is generally true if either:
|
|
|
|
/// - candidate i and candidate j are equivalent; or,
|
2015-01-06 20:53:18 -05:00
|
|
|
/// - candidate i is a concrete impl and candidate j is a where clause bound,
|
2014-11-25 21:17:11 -05:00
|
|
|
/// and the concrete impl is applicable to the types in the where clause bound.
|
|
|
|
///
|
|
|
|
/// The last case refers to cases where there are blanket impls (often conditional
|
|
|
|
/// blanket impls) as well as a where clause. This can come down to one of two cases:
|
|
|
|
///
|
|
|
|
/// - The impl is truly unconditional (it has no where clauses
|
|
|
|
/// of its own), in which case the where clause is
|
|
|
|
/// unnecessary, because coherence requires that we would
|
|
|
|
/// pick that particular impl anyhow (at least so long as we
|
|
|
|
/// don't have specialization).
|
|
|
|
///
|
|
|
|
/// - The impl is conditional, in which case we may not have winnowed it out
|
|
|
|
/// because we don't know if the conditions apply, but the where clause is basically
|
|
|
|
/// telling us taht there is some impl, though not necessarily the one we see.
|
|
|
|
///
|
|
|
|
/// In both cases we prefer to take the where clause, which is
|
|
|
|
/// essentially harmless. See issue #18453 for more details of
|
|
|
|
/// a case where doing the opposite caused us harm.
|
2014-09-29 22:11:30 +03:00
|
|
|
fn candidate_should_be_dropped_in_favor_of<'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
stack: &TraitObligationStack<'o, 'tcx>,
|
2014-12-17 14:16:28 -05:00
|
|
|
candidate_i: &SelectionCandidate<'tcx>,
|
|
|
|
candidate_j: &SelectionCandidate<'tcx>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> bool
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
2015-01-08 21:41:42 -05:00
|
|
|
if candidate_i == candidate_j {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
match (candidate_i, candidate_j) {
|
2014-12-27 04:22:29 -05:00
|
|
|
(&ImplCandidate(impl_def_id), &ParamCandidate(ref bound)) => {
|
2014-10-09 17:19:50 -04:00
|
|
|
debug!("Considering whether to drop param {} in favor of impl {}",
|
|
|
|
candidate_i.repr(self.tcx()),
|
|
|
|
candidate_j.repr(self.tcx()));
|
|
|
|
|
2014-12-14 07:17:23 -05:00
|
|
|
self.infcx.probe(|snapshot| {
|
|
|
|
let (skol_obligation_trait_ref, skol_map) =
|
|
|
|
self.infcx().skolemize_late_bound_regions(
|
2014-12-17 14:16:28 -05:00
|
|
|
&stack.obligation.predicate, snapshot);
|
2014-10-09 17:19:50 -04:00
|
|
|
let impl_substs =
|
2014-12-14 07:17:23 -05:00
|
|
|
self.rematch_impl(impl_def_id, stack.obligation, snapshot,
|
2014-12-17 14:16:28 -05:00
|
|
|
&skol_map, skol_obligation_trait_ref.trait_ref.clone());
|
2014-10-09 17:19:50 -04:00
|
|
|
let impl_trait_ref =
|
|
|
|
ty::impl_trait_ref(self.tcx(), impl_def_id).unwrap();
|
|
|
|
let impl_trait_ref =
|
2015-01-08 06:58:41 -05:00
|
|
|
impl_trait_ref.subst(self.tcx(), &impl_substs.value);
|
2014-12-14 07:17:23 -05:00
|
|
|
let poly_impl_trait_ref =
|
2014-12-17 14:16:28 -05:00
|
|
|
ty::Binder(impl_trait_ref);
|
2014-10-09 17:19:50 -04:00
|
|
|
let origin =
|
|
|
|
infer::RelateOutputImplTypes(stack.obligation.cause.span);
|
|
|
|
self.infcx
|
2014-12-27 04:22:29 -05:00
|
|
|
.sub_poly_trait_refs(false, origin, poly_impl_trait_ref, bound.clone())
|
2014-10-09 17:19:50 -04:00
|
|
|
.is_ok()
|
|
|
|
})
|
|
|
|
}
|
2015-01-11 14:52:37 -05:00
|
|
|
(&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
|
|
|
|
}
|
2014-12-27 04:22:29 -05:00
|
|
|
(&ProjectionCandidate, &ParamCandidate(_)) => {
|
|
|
|
// FIXME(#20297) -- this gives where clauses precedent
|
|
|
|
// over projections. Really these are just two means
|
|
|
|
// of deducing information (one based on the where
|
|
|
|
// clauses on the trait definition; one based on those
|
|
|
|
// on the enclosing scope), and it'd be better to
|
|
|
|
// integrate them more intelligently. But for now this
|
|
|
|
// seems ok. If we DON'T give where clauses
|
|
|
|
// precedence, we run into trouble in default methods,
|
|
|
|
// where both the projection bounds for `Self::A` and
|
|
|
|
// the where clauses are in scope.
|
|
|
|
true
|
|
|
|
}
|
2015-01-08 21:41:42 -05:00
|
|
|
(&ParamCandidate(ref bound1), &ParamCandidate(ref bound2)) => {
|
|
|
|
self.infcx.probe(|_| {
|
|
|
|
let bound1 =
|
|
|
|
project::normalize_with_depth(self,
|
|
|
|
stack.obligation.cause.clone(),
|
|
|
|
stack.obligation.recursion_depth+1,
|
|
|
|
bound1);
|
|
|
|
let bound2 =
|
|
|
|
project::normalize_with_depth(self,
|
|
|
|
stack.obligation.cause.clone(),
|
|
|
|
stack.obligation.recursion_depth+1,
|
|
|
|
bound2);
|
|
|
|
let origin =
|
|
|
|
infer::RelateOutputImplTypes(stack.obligation.cause.span);
|
|
|
|
self.infcx
|
|
|
|
.sub_poly_trait_refs(false, origin, bound1.value, bound2.value)
|
|
|
|
.is_ok()
|
|
|
|
})
|
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
_ => {
|
2015-01-08 21:41:42 -05:00
|
|
|
false
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// BUILTIN BOUNDS
|
|
|
|
//
|
|
|
|
// These cover the traits that are built-in to the language
|
|
|
|
// itself. This includes `Copy` and `Sized` for sure. For the
|
|
|
|
// moment, it also includes `Send` / `Sync` and a few others, but
|
|
|
|
// those will hopefully change to library-defined traits in the
|
|
|
|
// future.
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn assemble_builtin_bound_candidates<'o>(&mut self,
|
|
|
|
bound: ty::BuiltinBound,
|
2014-12-05 00:03:03 -05:00
|
|
|
stack: &TraitObligationStack<'o, 'tcx>,
|
2014-12-17 14:16:28 -05:00
|
|
|
candidates: &mut SelectionCandidateSet<'tcx>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Result<(),SelectionError<'tcx>>
|
2014-09-18 11:08:04 -04:00
|
|
|
{
|
2014-12-07 11:10:48 -05:00
|
|
|
match self.builtin_bound(bound, stack.obligation) {
|
|
|
|
Ok(If(..)) => {
|
2014-11-07 16:14:32 -05:00
|
|
|
debug!("builtin_bound: bound={}",
|
|
|
|
bound.repr(self.tcx()));
|
|
|
|
candidates.vec.push(BuiltinCandidate(bound));
|
|
|
|
Ok(())
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(ParameterBuiltin) => { Ok(()) }
|
2015-01-21 17:45:52 -05:00
|
|
|
Ok(AmbiguousBuiltin) => {
|
|
|
|
debug!("assemble_builtin_bound_candidates: ambiguous builtin");
|
|
|
|
Ok(candidates.ambiguous = true)
|
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
Err(e) => { Err(e) }
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
fn builtin_bound(&mut self,
|
|
|
|
bound: ty::BuiltinBound,
|
2014-12-07 11:10:48 -05:00
|
|
|
obligation: &TraitObligation<'tcx>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Result<BuiltinBoundConditions<'tcx>,SelectionError<'tcx>>
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
2014-12-28 10:07:21 -05:00
|
|
|
// Note: these tests operate on types that may contain bound
|
|
|
|
// regions. To be proper, we ought to skolemize here, but we
|
|
|
|
// forego the skolemization and defer it until the
|
|
|
|
// confirmation step.
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.predicate.0.self_ty());
|
2014-10-31 10:51:16 +02:00
|
|
|
return match self_ty.sty {
|
2014-10-09 17:19:50 -04:00
|
|
|
ty::ty_infer(ty::IntVar(_)) |
|
|
|
|
ty::ty_infer(ty::FloatVar(_)) |
|
|
|
|
ty::ty_uint(_) |
|
|
|
|
ty::ty_int(_) |
|
|
|
|
ty::ty_bool |
|
|
|
|
ty::ty_float(_) |
|
2014-11-26 06:01:28 -05:00
|
|
|
ty::ty_bare_fn(..) |
|
2014-10-09 17:19:50 -04:00
|
|
|
ty::ty_char => {
|
2014-09-18 11:08:04 -04:00
|
|
|
// safe for everything
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(Vec::new()))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_uniq(referent_ty) => { // Box<T>
|
|
|
|
match bound {
|
|
|
|
ty::BoundCopy => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Err(Unimplemented)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSized => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(Vec::new()))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSync |
|
|
|
|
ty::BoundSend => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(vec![referent_ty]))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 11:39:25 -05:00
|
|
|
ty::ty_ptr(..) => { // *const T, *mut T
|
2014-09-18 11:08:04 -04:00
|
|
|
match bound {
|
|
|
|
ty::BoundCopy |
|
|
|
|
ty::BoundSized => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(Vec::new()))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSync |
|
|
|
|
ty::BoundSend => {
|
2014-12-06 11:39:25 -05:00
|
|
|
// sync and send are not implemented for *const, *mut
|
|
|
|
Err(Unimplemented)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 13:37:37 -05:00
|
|
|
ty::ty_trait(ref data) => {
|
2014-09-18 11:08:04 -04:00
|
|
|
match bound {
|
|
|
|
ty::BoundSized => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Err(Unimplemented)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
ty::BoundCopy | ty::BoundSync | ty::BoundSend => {
|
2014-12-11 13:37:37 -05:00
|
|
|
if data.bounds.builtin_bounds.contains(&bound) {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(Vec::new()))
|
|
|
|
} else {
|
2014-11-16 18:20:19 +00:00
|
|
|
// Recursively check all supertraits to find out if any further
|
|
|
|
// bounds are required and thus we must fulfill.
|
2014-12-29 11:05:35 -05:00
|
|
|
let principal =
|
2014-12-25 07:20:48 -05:00
|
|
|
data.principal_trait_ref_with_self_ty(self.tcx(),
|
|
|
|
self.tcx().types.err);
|
2014-12-29 11:05:35 -05:00
|
|
|
for tr in util::supertraits(self.tcx(), principal) {
|
2014-12-12 11:28:35 -05:00
|
|
|
let td = ty::lookup_trait_def(self.tcx(), tr.def_id());
|
2014-11-16 18:20:19 +00:00
|
|
|
if td.bounds.builtin_bounds.contains(&bound) {
|
|
|
|
return Ok(If(Vec::new()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
Err(Unimplemented)
|
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 13:36:53 +13:00
|
|
|
ty::ty_rptr(_, ty::mt { ty: referent_ty, mutbl }) => {
|
2014-09-18 11:08:04 -04:00
|
|
|
// &mut T or &T
|
|
|
|
match bound {
|
|
|
|
ty::BoundCopy => {
|
2014-10-09 17:19:50 -04:00
|
|
|
match mutbl {
|
|
|
|
// &mut T is affine and hence never `Copy`
|
2014-12-07 11:10:48 -05:00
|
|
|
ast::MutMutable => {
|
|
|
|
Err(Unimplemented)
|
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
|
|
|
|
// &T is always copyable
|
2014-12-07 11:10:48 -05:00
|
|
|
ast::MutImmutable => {
|
|
|
|
Ok(If(Vec::new()))
|
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSized => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(Vec::new()))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSync |
|
|
|
|
ty::BoundSend => {
|
|
|
|
// Note: technically, a region pointer is only
|
|
|
|
// sendable if it has lifetime
|
|
|
|
// `'static`. However, we don't take regions
|
|
|
|
// into account when doing trait matching:
|
|
|
|
// instead, when we decide that `T : Send`, we
|
|
|
|
// will register a separate constraint with
|
|
|
|
// the region inferencer that `T : 'static`
|
|
|
|
// holds as well (because the trait `Send`
|
|
|
|
// requires it). This will ensure that there
|
|
|
|
// is no borrowed data in `T` (or else report
|
|
|
|
// an inference error). The reason we do it
|
|
|
|
// this way is that we do not yet *know* what
|
|
|
|
// lifetime the borrowed reference has, since
|
|
|
|
// we haven't finished running inference -- in
|
|
|
|
// other words, there's a kind of
|
|
|
|
// chicken-and-egg problem.
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(vec![referent_ty]))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_vec(element_ty, ref len) => {
|
|
|
|
// [T, ..n] and [T]
|
|
|
|
match bound {
|
|
|
|
ty::BoundCopy => {
|
|
|
|
match *len {
|
2014-12-07 11:10:48 -05:00
|
|
|
Some(_) => {
|
|
|
|
// [T, ..n] is copy iff T is copy
|
|
|
|
Ok(If(vec![element_ty]))
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// [T] is unsized and hence affine
|
|
|
|
Err(Unimplemented)
|
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSized => {
|
2014-10-09 17:19:50 -04:00
|
|
|
if len.is_some() {
|
|
|
|
Ok(If(Vec::new()))
|
|
|
|
} else {
|
|
|
|
Err(Unimplemented)
|
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSync |
|
|
|
|
ty::BoundSend => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(vec![element_ty]))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_str => {
|
|
|
|
// Equivalent to [u8]
|
|
|
|
match bound {
|
|
|
|
ty::BoundSync |
|
|
|
|
ty::BoundSend => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(Vec::new()))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundCopy |
|
|
|
|
ty::BoundSized => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Err(Unimplemented)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_tup(ref tys) => {
|
|
|
|
// (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
|
2014-10-11 13:03:55 +07:00
|
|
|
Ok(If(tys.clone()))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
ty::ty_closure(def_id, _, substs) => {
|
2014-09-18 11:08:04 -04:00
|
|
|
// FIXME -- This case is tricky. In the case of by-ref
|
|
|
|
// closures particularly, we need the results of
|
|
|
|
// inference to decide how to reflect the type of each
|
|
|
|
// upvar (the upvar may have type `T`, but the runtime
|
|
|
|
// type could be `&mut`, `&`, or just `T`). For now,
|
|
|
|
// though, we'll do this unsoundly and assume that all
|
|
|
|
// captures are by value. Really what we ought to do
|
|
|
|
// is reserve judgement and then intertwine this
|
|
|
|
// analysis with closure inference.
|
|
|
|
assert_eq!(def_id.krate, ast::LOCAL_CRATE);
|
2014-12-15 21:49:49 +01:00
|
|
|
|
|
|
|
// Unboxed closures shouldn't be
|
|
|
|
// implicitly copyable
|
|
|
|
if bound == ty::BoundCopy {
|
|
|
|
return Ok(ParameterBuiltin);
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
match self.closure_typer.closure_upvars(def_id, substs) {
|
2015-01-01 16:04:51 -05:00
|
|
|
Some(upvars) => {
|
|
|
|
Ok(If(upvars.iter().map(|c| c.ty).collect()))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
2015-01-01 16:04:51 -05:00
|
|
|
None => {
|
2015-01-21 17:45:52 -05:00
|
|
|
debug!("assemble_builtin_bound_candidates: no upvar types available yet");
|
2015-01-01 16:04:51 -05:00
|
|
|
Ok(AmbiguousBuiltin)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 16:01:29 -08:00
|
|
|
ty::ty_struct(def_id, substs) => {
|
2014-09-13 21:09:25 +03:00
|
|
|
let types: Vec<Ty> =
|
2015-01-01 16:04:51 -05:00
|
|
|
ty::struct_fields(self.tcx(), def_id, substs).iter()
|
|
|
|
.map(|f| f.mt.ty)
|
|
|
|
.collect();
|
2014-10-09 17:19:50 -04:00
|
|
|
nominal(self, bound, def_id, types)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2014-12-03 16:01:29 -08:00
|
|
|
ty::ty_enum(def_id, substs) => {
|
2014-09-13 21:09:25 +03:00
|
|
|
let types: Vec<Ty> =
|
2014-09-18 11:08:04 -04:00
|
|
|
ty::substd_enum_variants(self.tcx(), def_id, substs)
|
|
|
|
.iter()
|
|
|
|
.flat_map(|variant| variant.args.iter())
|
|
|
|
.map(|&ty| ty)
|
|
|
|
.collect();
|
2014-10-09 17:19:50 -04:00
|
|
|
nominal(self, bound, def_id, types)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
ty::ty_projection(_) |
|
2014-09-18 11:08:04 -04:00
|
|
|
ty::ty_param(_) => {
|
|
|
|
// Note: A type parameter is only considered to meet a
|
|
|
|
// particular bound if there is a where clause telling
|
|
|
|
// us that it does, and that case is handled by
|
|
|
|
// `assemble_candidates_from_caller_bounds()`.
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(ParameterBuiltin)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
ty::ty_infer(ty::TyVar(_)) => {
|
|
|
|
// Unbound type variable. Might or might not have
|
|
|
|
// applicable impls and so forth, depending on what
|
|
|
|
// those type variables wind up being bound to.
|
2015-01-21 17:45:52 -05:00
|
|
|
debug!("assemble_builtin_bound_candidates: ambiguous builtin");
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(AmbiguousBuiltin)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:49:52 -05:00
|
|
|
ty::ty_open(ty) => {
|
|
|
|
// these only crop up in trans, and represent an
|
|
|
|
// "opened" unsized/existential type (one that has
|
|
|
|
// been dereferenced)
|
|
|
|
match bound {
|
|
|
|
ty::BoundCopy |
|
|
|
|
ty::BoundSync |
|
|
|
|
ty::BoundSend => {
|
|
|
|
Ok(If(vec!(ty)))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSized => {
|
|
|
|
Err(Unimplemented)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-18 11:08:04 -04:00
|
|
|
ty::ty_err => {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(Vec::new()))
|
|
|
|
}
|
|
|
|
|
2014-12-12 06:13:42 -05:00
|
|
|
ty::ty_infer(ty::FreshTy(_)) |
|
|
|
|
ty::ty_infer(ty::FreshIntTy(_)) => {
|
2014-10-09 17:19:50 -04:00
|
|
|
self.tcx().sess.bug(
|
2015-01-07 11:58:31 -05:00
|
|
|
&format!(
|
2014-10-09 17:19:50 -04:00
|
|
|
"asked to assemble builtin bounds of unexpected type: {}",
|
2015-01-07 11:58:31 -05:00
|
|
|
self_ty.repr(self.tcx()))[]);
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn nominal<'cx, 'tcx>(this: &mut SelectionContext<'cx, 'tcx>,
|
|
|
|
bound: ty::BuiltinBound,
|
|
|
|
def_id: ast::DefId,
|
|
|
|
types: Vec<Ty<'tcx>>)
|
|
|
|
-> Result<BuiltinBoundConditions<'tcx>,SelectionError<'tcx>>
|
2014-09-18 11:08:04 -04:00
|
|
|
{
|
|
|
|
// First check for markers and other nonsense.
|
|
|
|
let tcx = this.tcx();
|
|
|
|
match bound {
|
|
|
|
ty::BoundSend => {
|
2015-01-19 10:52:11 -05:00
|
|
|
if Some(def_id) == tcx.lang_items.managed_bound() {
|
2014-12-07 11:10:48 -05:00
|
|
|
return Err(Unimplemented)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundCopy => {
|
2015-01-08 12:01:48 +01:00
|
|
|
return Ok(ParameterBuiltin)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSync => {
|
|
|
|
if
|
2014-12-06 11:39:25 -05:00
|
|
|
Some(def_id) == tcx.lang_items.managed_bound() ||
|
2014-09-18 11:08:04 -04:00
|
|
|
Some(def_id) == tcx.lang_items.unsafe_type()
|
|
|
|
{
|
2014-12-06 11:39:25 -05:00
|
|
|
return Err(Unimplemented)
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::BoundSized => { }
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(If(types))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 10:53:35 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// CONFIRMATION
|
|
|
|
//
|
|
|
|
// Confirmation unifies the output type parameters of the trait
|
|
|
|
// with the values found in the obligation, possibly yielding a
|
|
|
|
// type error. See `doc.rs` for more details.
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
fn confirm_candidate(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
2014-12-17 14:16:28 -05:00
|
|
|
candidate: SelectionCandidate<'tcx>)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Result<Selection<'tcx>,SelectionError<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
|
|
|
debug!("confirm_candidate({}, {})",
|
|
|
|
obligation.repr(self.tcx()),
|
|
|
|
candidate.repr(self.tcx()));
|
|
|
|
|
|
|
|
match candidate {
|
2014-11-07 16:14:32 -05:00
|
|
|
BuiltinCandidate(builtin_bound) => {
|
|
|
|
Ok(VtableBuiltin(
|
|
|
|
try!(self.confirm_builtin_candidate(obligation, builtin_bound))))
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
ErrorCandidate => {
|
|
|
|
Ok(VtableBuiltin(VtableBuiltinData { nested: VecPerParamSpace::empty() }))
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
ParamCandidate(param) => {
|
2015-01-08 21:41:42 -05:00
|
|
|
let obligations = self.confirm_param_candidate(obligation, param);
|
|
|
|
Ok(VtableParam(obligations))
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
ImplCandidate(impl_def_id) => {
|
|
|
|
let vtable_impl =
|
|
|
|
try!(self.confirm_impl_candidate(obligation, impl_def_id));
|
|
|
|
Ok(VtableImpl(vtable_impl))
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
ClosureCandidate(closure_def_id, substs) => {
|
|
|
|
try!(self.confirm_closure_candidate(obligation, closure_def_id, &substs));
|
|
|
|
Ok(VtableClosure(closure_def_id, substs))
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2014-12-01 09:23:40 -05:00
|
|
|
|
2014-12-23 05:26:34 -05:00
|
|
|
ObjectCandidate => {
|
|
|
|
let data = self.confirm_object_candidate(obligation);
|
|
|
|
Ok(VtableObject(data))
|
|
|
|
}
|
|
|
|
|
2014-12-01 09:23:40 -05:00
|
|
|
FnPointerCandidate => {
|
|
|
|
let fn_type =
|
|
|
|
try!(self.confirm_fn_pointer_candidate(obligation));
|
|
|
|
Ok(VtableFnPointer(fn_type))
|
|
|
|
}
|
2014-12-27 04:22:29 -05:00
|
|
|
|
|
|
|
ProjectionCandidate => {
|
|
|
|
self.confirm_projection_candidate(obligation);
|
2015-01-08 21:41:42 -05:00
|
|
|
Ok(VtableParam(Vec::new()))
|
2014-12-27 04:22:29 -05:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 04:22:29 -05:00
|
|
|
fn confirm_projection_candidate(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>)
|
|
|
|
{
|
|
|
|
let _: Result<(),()> =
|
|
|
|
self.infcx.try(|snapshot| {
|
|
|
|
let result =
|
|
|
|
self.match_projection_obligation_against_bounds_from_trait(obligation,
|
|
|
|
snapshot);
|
|
|
|
assert!(result);
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
fn confirm_param_candidate(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
2014-12-27 04:22:29 -05:00
|
|
|
param: ty::PolyTraitRef<'tcx>)
|
2015-01-08 21:41:42 -05:00
|
|
|
-> Vec<PredicateObligation<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
|
|
|
debug!("confirm_param_candidate({},{})",
|
|
|
|
obligation.repr(self.tcx()),
|
|
|
|
param.repr(self.tcx()));
|
|
|
|
|
2014-12-14 07:17:23 -05:00
|
|
|
// During evaluation, we already checked that this
|
|
|
|
// where-clause trait-ref could be unified with the obligation
|
|
|
|
// trait-ref. Repeat that unification now without any
|
|
|
|
// transactional boundary; it should not fail.
|
2015-01-08 21:41:42 -05:00
|
|
|
match self.match_where_clause_trait_ref(obligation, param.clone()) {
|
|
|
|
Ok(obligations) => obligations,
|
|
|
|
Err(()) => {
|
2014-12-14 07:17:23 -05:00
|
|
|
self.tcx().sess.bug(
|
|
|
|
format!("Where clause `{}` was applicable to `{}` but now is not",
|
2014-12-27 04:22:29 -05:00
|
|
|
param.repr(self.tcx()),
|
2014-12-14 07:17:23 -05:00
|
|
|
obligation.repr(self.tcx())).as_slice());
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
fn confirm_builtin_candidate(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
2014-10-09 17:19:50 -04:00
|
|
|
bound: ty::BuiltinBound)
|
2014-12-07 11:10:48 -05:00
|
|
|
-> Result<VtableBuiltinData<PredicateObligation<'tcx>>,
|
2014-09-29 22:11:30 +03:00
|
|
|
SelectionError<'tcx>>
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
|
|
|
debug!("confirm_builtin_candidate({})",
|
|
|
|
obligation.repr(self.tcx()));
|
|
|
|
|
2014-12-07 11:10:48 -05:00
|
|
|
match try!(self.builtin_bound(bound, obligation)) {
|
2014-10-09 17:19:50 -04:00
|
|
|
If(nested) => Ok(self.vtable_builtin_data(obligation, bound, nested)),
|
2014-11-07 16:14:32 -05:00
|
|
|
AmbiguousBuiltin | ParameterBuiltin => {
|
2014-10-09 17:19:50 -04:00
|
|
|
self.tcx().sess.span_bug(
|
|
|
|
obligation.cause.span,
|
2015-01-07 11:58:31 -05:00
|
|
|
&format!("builtin bound for {} was ambig",
|
|
|
|
obligation.repr(self.tcx()))[]);
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vtable_builtin_data(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
2014-10-09 17:19:50 -04:00
|
|
|
bound: ty::BuiltinBound,
|
2014-09-29 22:11:30 +03:00
|
|
|
nested: Vec<Ty<'tcx>>)
|
2014-12-07 11:10:48 -05:00
|
|
|
-> VtableBuiltinData<PredicateObligation<'tcx>>
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
2014-12-06 11:39:25 -05:00
|
|
|
let derived_cause = self.derived_cause(obligation, BuiltinDerivedObligation);
|
2014-12-28 10:07:21 -05:00
|
|
|
let obligations = nested.iter().map(|&bound_ty| {
|
|
|
|
// the obligation might be higher-ranked, e.g. for<'a> &'a
|
|
|
|
// int : Copy. In that case, we will wind up with
|
|
|
|
// late-bound regions in the `nested` vector. So for each
|
|
|
|
// one we instantiate to a skolemized region, do our work
|
|
|
|
// to produce something like `&'0 int : Copy`, and then
|
|
|
|
// re-bind it. This is a bit of busy-work but preserves
|
|
|
|
// the invariant that we only manipulate free regions, not
|
|
|
|
// bound ones.
|
|
|
|
self.infcx.try(|snapshot| {
|
|
|
|
let (skol_ty, skol_map) =
|
|
|
|
self.infcx().skolemize_late_bound_regions(&ty::Binder(bound_ty), snapshot);
|
|
|
|
let skol_predicate =
|
|
|
|
util::predicate_for_builtin_bound(
|
|
|
|
self.tcx(),
|
|
|
|
derived_cause.clone(),
|
|
|
|
bound,
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
skol_ty);
|
|
|
|
match skol_predicate {
|
|
|
|
Ok(skol_predicate) => Ok(self.infcx().plug_leaks(skol_map, snapshot,
|
|
|
|
&skol_predicate)),
|
|
|
|
Err(ErrorReported) => Err(ErrorReported)
|
|
|
|
}
|
|
|
|
})
|
2014-10-14 23:05:01 -07:00
|
|
|
}).collect::<Result<_, _>>();
|
2014-12-07 11:10:48 -05:00
|
|
|
let mut obligations = match obligations {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(o) => o,
|
|
|
|
Err(ErrorReported) => Vec::new()
|
|
|
|
};
|
2014-12-07 11:10:48 -05:00
|
|
|
|
|
|
|
// as a special case, `Send` requires `'static`
|
|
|
|
if bound == ty::BoundSend {
|
|
|
|
obligations.push(Obligation {
|
2014-12-06 11:39:25 -05:00
|
|
|
cause: obligation.cause.clone(),
|
2014-12-07 11:10:48 -05:00
|
|
|
recursion_depth: obligation.recursion_depth+1,
|
2014-12-17 16:00:34 -05:00
|
|
|
predicate: ty::Binder(ty::OutlivesPredicate(obligation.self_ty(),
|
2014-12-13 05:34:34 -05:00
|
|
|
ty::ReStatic)).as_predicate(),
|
2014-12-07 11:10:48 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-29 16:32:12 -05:00
|
|
|
let obligations = VecPerParamSpace::new(obligations, Vec::new(), Vec::new());
|
2014-12-07 11:10:48 -05:00
|
|
|
|
|
|
|
debug!("vtable_builtin_data: obligations={}",
|
|
|
|
obligations.repr(self.tcx()));
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
VtableBuiltinData { nested: obligations }
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
fn confirm_impl_candidate(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
2014-09-12 10:53:35 -04:00
|
|
|
impl_def_id: ast::DefId)
|
2014-12-07 11:10:48 -05:00
|
|
|
-> Result<VtableImplData<'tcx, PredicateObligation<'tcx>>,
|
2014-09-29 22:11:30 +03:00
|
|
|
SelectionError<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
|
|
|
debug!("confirm_impl_candidate({},{})",
|
|
|
|
obligation.repr(self.tcx()),
|
|
|
|
impl_def_id.repr(self.tcx()));
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
// First, create the substitutions by matching the impl again,
|
|
|
|
// this time not in a probe.
|
2014-12-14 07:17:23 -05:00
|
|
|
self.infcx.try(|snapshot| {
|
|
|
|
let (skol_obligation_trait_ref, skol_map) =
|
2014-12-17 14:16:28 -05:00
|
|
|
self.infcx().skolemize_late_bound_regions(&obligation.predicate, snapshot);
|
|
|
|
let substs =
|
|
|
|
self.rematch_impl(impl_def_id, obligation,
|
|
|
|
snapshot, &skol_map, skol_obligation_trait_ref.trait_ref);
|
2015-01-08 06:58:41 -05:00
|
|
|
debug!("confirm_impl_candidate substs={}", substs.repr(self.tcx()));
|
2014-12-06 11:39:25 -05:00
|
|
|
Ok(self.vtable_impl(impl_def_id, substs, obligation.cause.clone(),
|
2014-12-14 07:17:23 -05:00
|
|
|
obligation.recursion_depth + 1, skol_map, snapshot))
|
|
|
|
})
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
fn vtable_impl(&mut self,
|
|
|
|
impl_def_id: ast::DefId,
|
2015-01-08 06:58:41 -05:00
|
|
|
substs: Normalized<'tcx, Substs<'tcx>>,
|
2014-09-29 22:11:30 +03:00
|
|
|
cause: ObligationCause<'tcx>,
|
2014-12-14 07:17:23 -05:00
|
|
|
recursion_depth: uint,
|
|
|
|
skol_map: infer::SkolemizationMap,
|
|
|
|
snapshot: &infer::CombinedSnapshot)
|
2014-12-07 11:10:48 -05:00
|
|
|
-> VtableImplData<'tcx, PredicateObligation<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-12-14 07:17:23 -05:00
|
|
|
debug!("vtable_impl(impl_def_id={}, substs={}, recursion_depth={}, skol_map={})",
|
|
|
|
impl_def_id.repr(self.tcx()),
|
|
|
|
substs.repr(self.tcx()),
|
|
|
|
recursion_depth,
|
|
|
|
skol_map.repr(self.tcx()));
|
|
|
|
|
2015-01-08 06:58:41 -05:00
|
|
|
let mut impl_obligations =
|
|
|
|
self.impl_obligations(cause,
|
2014-12-07 11:10:48 -05:00
|
|
|
recursion_depth,
|
|
|
|
impl_def_id,
|
2015-01-08 06:58:41 -05:00
|
|
|
&substs.value,
|
2014-12-14 07:17:23 -05:00
|
|
|
skol_map,
|
|
|
|
snapshot);
|
|
|
|
|
2015-01-08 06:58:41 -05:00
|
|
|
debug!("vtable_impl: impl_def_id={} impl_obligations={}",
|
2014-12-14 07:17:23 -05:00
|
|
|
impl_def_id.repr(self.tcx()),
|
2015-01-08 06:58:41 -05:00
|
|
|
impl_obligations.repr(self.tcx()));
|
|
|
|
|
|
|
|
impl_obligations.extend(TypeSpace, substs.obligations.into_iter());
|
2014-12-14 07:17:23 -05:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
VtableImplData { impl_def_id: impl_def_id,
|
2015-01-08 06:58:41 -05:00
|
|
|
substs: substs.value,
|
|
|
|
nested: impl_obligations }
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-23 05:26:34 -05:00
|
|
|
fn confirm_object_candidate(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>)
|
|
|
|
-> VtableObjectData<'tcx>
|
|
|
|
{
|
|
|
|
debug!("confirm_object_candidate({})",
|
|
|
|
obligation.repr(self.tcx()));
|
|
|
|
|
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
|
|
|
|
let poly_trait_ref = match self_ty.sty {
|
|
|
|
ty::ty_trait(ref data) => {
|
|
|
|
data.principal_trait_ref_with_self_ty(self.tcx(), self_ty)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx().sess.span_bug(obligation.cause.span,
|
|
|
|
"object candidate with non-object");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let obligation_def_id = obligation.predicate.def_id();
|
|
|
|
let upcast_trait_ref = match util::upcast(self.tcx(),
|
|
|
|
poly_trait_ref.clone(),
|
|
|
|
obligation_def_id) {
|
|
|
|
Some(r) => r,
|
|
|
|
None => {
|
|
|
|
self.tcx().sess.span_bug(obligation.cause.span,
|
|
|
|
format!("unable to upcast from {} to {}",
|
|
|
|
poly_trait_ref.repr(self.tcx()),
|
|
|
|
obligation_def_id.repr(self.tcx())).as_slice());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match self.match_poly_trait_ref(obligation, upcast_trait_ref) {
|
|
|
|
Ok(()) => { }
|
|
|
|
Err(()) => {
|
|
|
|
self.tcx().sess.span_bug(obligation.cause.span,
|
|
|
|
"failed to match trait refs");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VtableObjectData { object_ty: self_ty }
|
|
|
|
}
|
|
|
|
|
2014-12-01 09:23:40 -05:00
|
|
|
fn confirm_fn_pointer_candidate(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation<'tcx>)
|
2014-12-01 09:23:40 -05:00
|
|
|
-> Result<ty::Ty<'tcx>,SelectionError<'tcx>>
|
|
|
|
{
|
|
|
|
debug!("confirm_fn_pointer_candidate({})",
|
|
|
|
obligation.repr(self.tcx()));
|
|
|
|
|
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
|
|
|
|
let sig = match self_ty.sty {
|
2014-12-04 14:52:20 -08:00
|
|
|
ty::ty_bare_fn(_, &ty::BareFnTy {
|
2014-12-09 10:36:46 -05:00
|
|
|
unsafety: ast::Unsafety::Normal,
|
2014-12-01 09:23:40 -05:00
|
|
|
abi: abi::Rust,
|
|
|
|
ref sig
|
|
|
|
}) => {
|
2014-12-04 00:57:38 -05:00
|
|
|
sig
|
2014-12-01 09:23:40 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx().sess.span_bug(
|
|
|
|
obligation.cause.span,
|
2015-01-07 11:58:31 -05:00
|
|
|
&format!("Fn pointer candidate for inappropriate self type: {}",
|
|
|
|
self_ty.repr(self.tcx()))[]);
|
2014-12-01 09:23:40 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-12 11:28:35 -05:00
|
|
|
let arguments_tuple = ty::mk_tup(self.tcx(), sig.0.inputs.to_vec());
|
|
|
|
let output_type = sig.0.output.unwrap();
|
2014-12-01 09:23:40 -05:00
|
|
|
let substs =
|
|
|
|
Substs::new_trait(
|
|
|
|
vec![arguments_tuple, output_type],
|
|
|
|
vec![],
|
|
|
|
self_ty);
|
2014-12-17 14:16:28 -05:00
|
|
|
let trait_ref = ty::Binder(Rc::new(ty::TraitRef {
|
2014-12-17 16:00:34 -05:00
|
|
|
def_id: obligation.predicate.def_id(),
|
2014-12-03 16:01:29 -08:00
|
|
|
substs: self.tcx().mk_substs(substs),
|
2014-12-11 13:37:37 -05:00
|
|
|
}));
|
2014-12-01 09:23:40 -05:00
|
|
|
|
2014-12-06 11:39:25 -05:00
|
|
|
try!(self.confirm_poly_trait_refs(obligation.cause.clone(),
|
2014-12-17 14:16:28 -05:00
|
|
|
obligation.predicate.to_poly_trait_ref(),
|
2014-12-14 07:17:23 -05:00
|
|
|
trait_ref));
|
2014-12-01 09:23:40 -05:00
|
|
|
Ok(self_ty)
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
fn confirm_closure_candidate(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
closure_def_id: ast::DefId,
|
|
|
|
substs: &Substs<'tcx>)
|
|
|
|
-> Result<(),SelectionError<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2015-01-24 22:00:03 +02:00
|
|
|
debug!("confirm_closure_candidate({},{},{})",
|
2014-09-12 10:53:35 -04:00
|
|
|
obligation.repr(self.tcx()),
|
2014-10-18 10:46:57 -07:00
|
|
|
closure_def_id.repr(self.tcx()),
|
|
|
|
substs.repr(self.tcx()));
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
let closure_type = self.closure_typer.closure_type(closure_def_id, substs);
|
2015-01-01 17:58:57 -05:00
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
debug!("confirm_closure_candidate: closure_def_id={} closure_type={}",
|
2015-01-01 17:58:57 -05:00
|
|
|
closure_def_id.repr(self.tcx()),
|
|
|
|
closure_type.repr(self.tcx()));
|
|
|
|
|
2014-11-15 17:09:51 -05:00
|
|
|
let closure_sig = &closure_type.sig;
|
2014-12-12 11:28:35 -05:00
|
|
|
let arguments_tuple = closure_sig.0.inputs[0];
|
2015-01-01 16:04:51 -05:00
|
|
|
let trait_substs =
|
2014-11-15 17:09:51 -05:00
|
|
|
Substs::new_trait(
|
2015-01-01 16:04:51 -05:00
|
|
|
vec![arguments_tuple, closure_sig.0.output.unwrap()],
|
2014-09-12 10:53:35 -04:00
|
|
|
vec![],
|
2014-11-15 17:09:51 -05:00
|
|
|
obligation.self_ty());
|
2014-12-17 14:16:28 -05:00
|
|
|
let trait_ref = ty::Binder(Rc::new(ty::TraitRef {
|
2014-12-17 16:00:34 -05:00
|
|
|
def_id: obligation.predicate.def_id(),
|
2015-01-01 16:04:51 -05:00
|
|
|
substs: self.tcx().mk_substs(trait_substs),
|
2014-12-11 13:37:37 -05:00
|
|
|
}));
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2015-01-24 22:00:03 +02:00
|
|
|
debug!("confirm_closure_candidate(closure_def_id={}, trait_ref={})",
|
2014-11-26 10:07:22 -05:00
|
|
|
closure_def_id.repr(self.tcx()),
|
|
|
|
trait_ref.repr(self.tcx()));
|
|
|
|
|
2014-12-06 11:39:25 -05:00
|
|
|
self.confirm_poly_trait_refs(obligation.cause.clone(),
|
2014-12-17 14:16:28 -05:00
|
|
|
obligation.predicate.to_poly_trait_ref(),
|
2014-12-14 07:17:23 -05:00
|
|
|
trait_ref)
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:15:08 +02:00
|
|
|
/// In the case of closure types and fn pointers,
|
2014-12-14 07:17:23 -05:00
|
|
|
/// we currently treat the input type parameters on the trait as
|
|
|
|
/// outputs. This means that when we have a match we have only
|
|
|
|
/// considered the self type, so we have to go back and make sure
|
|
|
|
/// to relate the argument types too. This is kind of wrong, but
|
|
|
|
/// since we control the full set of impls, also not that wrong,
|
|
|
|
/// and it DOES yield better error messages (since we don't report
|
|
|
|
/// errors as if there is no applicable impl, but rather report
|
|
|
|
/// errors are about mismatched argument types.
|
|
|
|
///
|
2015-01-24 22:15:08 +02:00
|
|
|
/// Here is an example. Imagine we have an closure expression
|
2014-12-14 07:17:23 -05:00
|
|
|
/// and we desugared it so that the type of the expression is
|
|
|
|
/// `Closure`, and `Closure` expects an int as argument. Then it
|
|
|
|
/// is "as if" the compiler generated this impl:
|
|
|
|
///
|
|
|
|
/// impl Fn(int) for Closure { ... }
|
|
|
|
///
|
|
|
|
/// Now imagine our obligation is `Fn(uint) for Closure`. So far
|
|
|
|
/// we have matched the self-type `Closure`. At this point we'll
|
|
|
|
/// compare the `int` to `uint` and generate an error.
|
|
|
|
///
|
|
|
|
/// Note that this checking occurs *after* the impl has selected,
|
|
|
|
/// because these output type parameters should not affect the
|
|
|
|
/// selection of the impl. Therefore, if there is a mismatch, we
|
|
|
|
/// report an error to the user.
|
|
|
|
fn confirm_poly_trait_refs(&mut self,
|
|
|
|
obligation_cause: ObligationCause,
|
2014-12-17 14:16:28 -05:00
|
|
|
obligation_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
expected_trait_ref: ty::PolyTraitRef<'tcx>)
|
2014-12-14 07:17:23 -05:00
|
|
|
-> Result<(), SelectionError<'tcx>>
|
|
|
|
{
|
|
|
|
let origin = infer::RelateOutputImplTypes(obligation_cause.span);
|
|
|
|
|
|
|
|
let obligation_trait_ref = obligation_trait_ref.clone();
|
|
|
|
match self.infcx.sub_poly_trait_refs(false,
|
|
|
|
origin,
|
|
|
|
expected_trait_ref.clone(),
|
|
|
|
obligation_trait_ref.clone()) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(e) => Err(OutputTypeParameterMismatch(expected_trait_ref, obligation_trait_ref, e))
|
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Matching
|
|
|
|
//
|
|
|
|
// Matching is a common path used for both evaluation and
|
|
|
|
// confirmation. It basically unifies types that appear in impls
|
|
|
|
// and traits. This does affect the surrounding environment;
|
|
|
|
// therefore, when used during evaluation, match routines must be
|
|
|
|
// run inside of a `probe()` so that their side-effects are
|
|
|
|
// contained.
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
fn rematch_impl(&mut self,
|
|
|
|
impl_def_id: ast::DefId,
|
2014-12-14 07:17:23 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
snapshot: &infer::CombinedSnapshot,
|
|
|
|
skol_map: &infer::SkolemizationMap,
|
|
|
|
skol_obligation_trait_ref: Rc<ty::TraitRef<'tcx>>)
|
2015-01-08 06:58:41 -05:00
|
|
|
-> Normalized<'tcx, Substs<'tcx>>
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
2014-12-14 07:17:23 -05:00
|
|
|
match self.match_impl(impl_def_id, obligation, snapshot,
|
|
|
|
skol_map, skol_obligation_trait_ref) {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(substs) => {
|
|
|
|
substs
|
|
|
|
}
|
|
|
|
Err(()) => {
|
|
|
|
self.tcx().sess.bug(
|
2015-01-07 11:58:31 -05:00
|
|
|
&format!("Impl {} was matchable against {} but now is not",
|
2014-10-09 17:19:50 -04:00
|
|
|
impl_def_id.repr(self.tcx()),
|
2015-01-07 11:58:31 -05:00
|
|
|
obligation.repr(self.tcx()))[]);
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_impl(&mut self,
|
|
|
|
impl_def_id: ast::DefId,
|
2014-12-14 07:17:23 -05:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
snapshot: &infer::CombinedSnapshot,
|
|
|
|
skol_map: &infer::SkolemizationMap,
|
|
|
|
skol_obligation_trait_ref: Rc<ty::TraitRef<'tcx>>)
|
2015-01-08 06:58:41 -05:00
|
|
|
-> Result<Normalized<'tcx, Substs<'tcx>>, ()>
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
2014-12-14 07:17:23 -05:00
|
|
|
let impl_trait_ref = ty::impl_trait_ref(self.tcx(), impl_def_id).unwrap();
|
2014-11-16 07:10:37 -05:00
|
|
|
|
|
|
|
// Before we create the substitutions and everything, first
|
|
|
|
// consider a "quick reject". This avoids creating more types
|
|
|
|
// and so forth that we need to.
|
|
|
|
if self.fast_reject_trait_refs(obligation, &*impl_trait_ref) {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
let impl_substs = util::fresh_substs_for_impl(self.infcx,
|
|
|
|
obligation.cause.span,
|
|
|
|
impl_def_id);
|
|
|
|
|
|
|
|
let impl_trait_ref = impl_trait_ref.subst(self.tcx(),
|
|
|
|
&impl_substs);
|
|
|
|
|
2015-01-08 06:58:41 -05:00
|
|
|
let impl_trait_ref =
|
|
|
|
project::normalize_with_depth(self,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
&impl_trait_ref);
|
|
|
|
|
2014-12-14 07:17:23 -05:00
|
|
|
debug!("match_impl(impl_def_id={}, obligation={}, \
|
|
|
|
impl_trait_ref={}, skol_obligation_trait_ref={})",
|
|
|
|
impl_def_id.repr(self.tcx()),
|
|
|
|
obligation.repr(self.tcx()),
|
|
|
|
impl_trait_ref.repr(self.tcx()),
|
|
|
|
skol_obligation_trait_ref.repr(self.tcx()));
|
|
|
|
|
|
|
|
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
|
|
|
|
match self.infcx.sub_trait_refs(false,
|
|
|
|
origin,
|
2015-01-08 06:58:41 -05:00
|
|
|
impl_trait_ref.value.clone(),
|
2014-12-14 07:17:23 -05:00
|
|
|
skol_obligation_trait_ref) {
|
|
|
|
Ok(()) => { }
|
|
|
|
Err(e) => {
|
|
|
|
debug!("match_impl: failed sub_trait_refs due to `{}`",
|
|
|
|
ty::type_err_to_str(self.tcx(), &e));
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.infcx.leak_check(skol_map, snapshot) {
|
|
|
|
Ok(()) => { }
|
|
|
|
Err(e) => {
|
|
|
|
debug!("match_impl: failed leak check due to `{}`",
|
|
|
|
ty::type_err_to_str(self.tcx(), &e));
|
|
|
|
return Err(());
|
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
2014-12-14 07:17:23 -05:00
|
|
|
|
|
|
|
debug!("match_impl: success impl_substs={}", impl_substs.repr(self.tcx()));
|
2015-01-08 06:58:41 -05:00
|
|
|
Ok(Normalized { value: impl_substs,
|
|
|
|
obligations: impl_trait_ref.obligations })
|
2014-10-09 17:19:50 -04:00
|
|
|
}
|
|
|
|
|
2014-11-16 07:10:37 -05:00
|
|
|
fn fast_reject_trait_refs(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
obligation: &TraitObligation,
|
2014-12-14 07:17:23 -05:00
|
|
|
impl_trait_ref: &ty::TraitRef)
|
2014-11-16 07:10:37 -05:00
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
// We can avoid creating type variables and doing the full
|
|
|
|
// substitution if we find that any of the input types, when
|
|
|
|
// simplified, do not match.
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
obligation.predicate.0.input_types().iter()
|
2014-11-16 07:10:37 -05:00
|
|
|
.zip(impl_trait_ref.input_types().iter())
|
|
|
|
.any(|(&obligation_ty, &impl_ty)| {
|
|
|
|
let simplified_obligation_ty =
|
|
|
|
fast_reject::simplify_type(self.tcx(), obligation_ty, true);
|
|
|
|
let simplified_impl_ty =
|
|
|
|
fast_reject::simplify_type(self.tcx(), impl_ty, false);
|
|
|
|
|
|
|
|
simplified_obligation_ty.is_some() &&
|
|
|
|
simplified_impl_ty.is_some() &&
|
|
|
|
simplified_obligation_ty != simplified_impl_ty
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-08 21:41:42 -05:00
|
|
|
/// Normalize `where_clause_trait_ref` and try to match it against
|
|
|
|
/// `obligation`. If successful, return any predicates that
|
|
|
|
/// result from the normalization. Normalization is necessary
|
|
|
|
/// because where-clauses are stored in the parameter environment
|
|
|
|
/// unnormalized.
|
|
|
|
fn match_where_clause_trait_ref(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
where_clause_trait_ref: ty::PolyTraitRef<'tcx>)
|
|
|
|
-> Result<Vec<PredicateObligation<'tcx>>,()>
|
|
|
|
{
|
|
|
|
let where_clause_trait_ref =
|
|
|
|
project::normalize_with_depth(self,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth+1,
|
|
|
|
&where_clause_trait_ref);
|
|
|
|
|
|
|
|
let () =
|
|
|
|
try!(self.match_poly_trait_ref(obligation, where_clause_trait_ref.value.clone()));
|
|
|
|
|
|
|
|
Ok(where_clause_trait_ref.obligations)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `Ok` if `poly_trait_ref` being true implies that the
|
|
|
|
/// obligation is satisfied.
|
2014-12-23 05:26:34 -05:00
|
|
|
fn match_poly_trait_ref(&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
2015-01-08 21:41:42 -05:00
|
|
|
poly_trait_ref: ty::PolyTraitRef<'tcx>)
|
2014-12-23 05:26:34 -05:00
|
|
|
-> Result<(),()>
|
2014-10-09 17:19:50 -04:00
|
|
|
{
|
2015-01-08 21:41:42 -05:00
|
|
|
debug!("match_poly_trait_ref: obligation={} poly_trait_ref={}",
|
2014-10-17 08:51:43 -04:00
|
|
|
obligation.repr(self.tcx()),
|
2015-01-08 21:41:42 -05:00
|
|
|
poly_trait_ref.repr(self.tcx()));
|
2014-10-17 08:51:43 -04:00
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
|
2014-12-11 13:37:37 -05:00
|
|
|
match self.infcx.sub_poly_trait_refs(false,
|
|
|
|
origin,
|
2015-01-08 21:41:42 -05:00
|
|
|
poly_trait_ref,
|
2014-12-17 14:16:28 -05:00
|
|
|
obligation.predicate.to_poly_trait_ref()) {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Determines whether the self type declared against
|
|
|
|
/// `impl_def_id` matches `obligation_self_ty`. If successful,
|
|
|
|
/// returns the substitutions used to make them match. See
|
|
|
|
/// `match_impl()`. For example, if `impl_def_id` is declared
|
|
|
|
/// as:
|
|
|
|
///
|
|
|
|
/// impl<T:Copy> Foo for ~T { ... }
|
|
|
|
///
|
|
|
|
/// and `obligation_self_ty` is `int`, we'd back an `Err(_)`
|
|
|
|
/// result. But if `obligation_self_ty` were `~int`, we'd get
|
|
|
|
/// back `Ok(T=int)`.
|
2014-10-09 17:19:50 -04:00
|
|
|
fn match_inherent_impl(&mut self,
|
|
|
|
impl_def_id: ast::DefId,
|
2014-12-06 11:39:25 -05:00
|
|
|
obligation_cause: &ObligationCause,
|
2014-09-29 22:11:30 +03:00
|
|
|
obligation_self_ty: Ty<'tcx>)
|
|
|
|
-> Result<Substs<'tcx>,()>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
|
|
|
// Create fresh type variables for each type parameter declared
|
|
|
|
// on the impl etc.
|
|
|
|
let impl_substs = util::fresh_substs_for_impl(self.infcx,
|
|
|
|
obligation_cause.span,
|
|
|
|
impl_def_id);
|
|
|
|
|
|
|
|
// Find the self type for the impl.
|
|
|
|
let impl_self_ty = ty::lookup_item_type(self.tcx(), impl_def_id).ty;
|
|
|
|
let impl_self_ty = impl_self_ty.subst(self.tcx(), &impl_substs);
|
|
|
|
|
|
|
|
debug!("match_impl_self_types(obligation_self_ty={}, impl_self_ty={})",
|
|
|
|
obligation_self_ty.repr(self.tcx()),
|
|
|
|
impl_self_ty.repr(self.tcx()));
|
|
|
|
|
|
|
|
match self.match_self_types(obligation_cause,
|
|
|
|
impl_self_ty,
|
|
|
|
obligation_self_ty) {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(()) => {
|
2014-09-12 10:53:35 -04:00
|
|
|
debug!("Matched impl_substs={}", impl_substs.repr(self.tcx()));
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(impl_substs)
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2014-10-09 17:19:50 -04:00
|
|
|
Err(()) => {
|
2014-09-12 10:53:35 -04:00
|
|
|
debug!("NoMatch");
|
2014-10-09 17:19:50 -04:00
|
|
|
Err(())
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:08:04 -04:00
|
|
|
fn match_self_types(&mut self,
|
2014-12-06 11:39:25 -05:00
|
|
|
cause: &ObligationCause,
|
2014-09-12 10:53:35 -04:00
|
|
|
|
|
|
|
// The self type provided by the impl/caller-obligation:
|
2014-09-29 22:11:30 +03:00
|
|
|
provided_self_ty: Ty<'tcx>,
|
2014-09-12 10:53:35 -04:00
|
|
|
|
|
|
|
// The self type the obligation is for:
|
2014-09-29 22:11:30 +03:00
|
|
|
required_self_ty: Ty<'tcx>)
|
2014-10-09 17:19:50 -04:00
|
|
|
-> Result<(),()>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
|
|
|
// FIXME(#5781) -- equating the types is stronger than
|
|
|
|
// necessary. Should consider variance of trait w/r/t Self.
|
|
|
|
|
|
|
|
let origin = infer::RelateSelfType(cause.span);
|
|
|
|
match self.infcx.eq_types(false,
|
|
|
|
origin,
|
|
|
|
provided_self_ty,
|
|
|
|
required_self_ty) {
|
2014-10-09 17:19:50 -04:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(_) => Err(()),
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Miscellany
|
|
|
|
|
2014-10-09 17:19:50 -04:00
|
|
|
fn push_stack<'o,'s:'o>(&mut self,
|
2014-12-05 00:03:03 -05:00
|
|
|
previous_stack: Option<&'s TraitObligationStack<'s, 'tcx>>,
|
|
|
|
obligation: &'o TraitObligation<'tcx>)
|
|
|
|
-> TraitObligationStack<'o, 'tcx>
|
2014-09-18 11:08:04 -04:00
|
|
|
{
|
2014-12-17 14:16:28 -05:00
|
|
|
let fresh_trait_ref =
|
|
|
|
obligation.predicate.to_poly_trait_ref().fold_with(&mut self.freshener);
|
2014-10-09 17:19:50 -04:00
|
|
|
|
2014-12-05 00:03:03 -05:00
|
|
|
TraitObligationStack {
|
2014-09-18 11:08:04 -04:00
|
|
|
obligation: obligation,
|
2014-12-12 06:13:42 -05:00
|
|
|
fresh_trait_ref: fresh_trait_ref,
|
2014-10-09 17:19:50 -04:00
|
|
|
previous: previous_stack.map(|p| p), // FIXME variance
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Returns set of all impls for a given trait.
|
2014-09-12 10:53:35 -04:00
|
|
|
fn all_impls(&self, trait_def_id: ast::DefId) -> Vec<ast::DefId> {
|
2015-01-08 00:41:50 +01:00
|
|
|
ty::populate_implementations_for_trait_if_necessary(self.tcx(), trait_def_id);
|
|
|
|
|
2015-01-13 20:21:19 +01:00
|
|
|
match self.tcx().trait_impls.borrow().get(&trait_def_id) {
|
2014-09-12 10:53:35 -04:00
|
|
|
None => Vec::new(),
|
|
|
|
Some(impls) => impls.borrow().clone()
|
2015-01-13 20:21:19 +01:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2015-01-08 06:58:41 -05:00
|
|
|
fn impl_obligations(&mut self,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
recursion_depth: uint,
|
|
|
|
impl_def_id: ast::DefId,
|
|
|
|
impl_substs: &Substs<'tcx>,
|
|
|
|
skol_map: infer::SkolemizationMap,
|
|
|
|
snapshot: &infer::CombinedSnapshot)
|
|
|
|
-> VecPerParamSpace<PredicateObligation<'tcx>>
|
2014-09-12 10:53:35 -04:00
|
|
|
{
|
2014-11-15 17:25:05 -05:00
|
|
|
let impl_generics = ty::lookup_item_type(self.tcx(), impl_def_id).generics;
|
|
|
|
let bounds = impl_generics.to_bounds(self.tcx(), impl_substs);
|
2015-01-01 13:15:14 -05:00
|
|
|
let normalized_bounds =
|
|
|
|
project::normalize_with_depth(self, cause.clone(), recursion_depth, &bounds);
|
|
|
|
let normalized_bounds =
|
|
|
|
self.infcx().plug_leaks(skol_map, snapshot, &normalized_bounds);
|
|
|
|
let mut impl_obligations =
|
|
|
|
util::predicates_for_generics(self.tcx(),
|
|
|
|
cause,
|
|
|
|
recursion_depth,
|
|
|
|
&normalized_bounds.value);
|
2015-01-08 06:58:41 -05:00
|
|
|
impl_obligations.extend(TypeSpace, normalized_bounds.obligations.into_iter());
|
2015-01-01 13:15:14 -05:00
|
|
|
impl_obligations
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
2014-12-01 09:23:40 -05:00
|
|
|
|
|
|
|
fn fn_family_trait_kind(&self,
|
|
|
|
trait_def_id: ast::DefId)
|
2015-01-24 22:00:03 +02:00
|
|
|
-> Option<ty::ClosureKind>
|
2014-12-01 09:23:40 -05:00
|
|
|
{
|
|
|
|
let tcx = self.tcx();
|
|
|
|
if Some(trait_def_id) == tcx.lang_items.fn_trait() {
|
2015-01-24 22:00:03 +02:00
|
|
|
Some(ty::FnClosureKind)
|
2014-12-01 09:23:40 -05:00
|
|
|
} else if Some(trait_def_id) == tcx.lang_items.fn_mut_trait() {
|
2015-01-24 22:00:03 +02:00
|
|
|
Some(ty::FnMutClosureKind)
|
2014-12-01 09:23:40 -05:00
|
|
|
} else if Some(trait_def_id) == tcx.lang_items.fn_once_trait() {
|
2015-01-24 22:00:03 +02:00
|
|
|
Some(ty::FnOnceClosureKind)
|
2014-12-01 09:23:40 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2014-12-06 11:39:25 -05:00
|
|
|
|
2014-12-23 10:57:44 +01:00
|
|
|
#[allow(unused_comparisons)]
|
2014-12-06 11:39:25 -05:00
|
|
|
fn derived_cause(&self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
2014-12-17 14:16:28 -05:00
|
|
|
variant: fn(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>)
|
2014-12-06 11:39:25 -05:00
|
|
|
-> ObligationCause<'tcx>
|
|
|
|
{
|
|
|
|
/*!
|
|
|
|
* Creates a cause for obligations that are derived from
|
|
|
|
* `obligation` by a recursive search (e.g., for a builtin
|
|
|
|
* bound, or eventually a `impl Foo for ..`). If `obligation`
|
|
|
|
* is itself a derived obligation, this is just a clone, but
|
|
|
|
* otherwise we create a "derived obligation" cause so as to
|
|
|
|
* keep track of the original root obligation for error
|
|
|
|
* reporting.
|
|
|
|
*/
|
|
|
|
|
2014-12-23 10:57:44 +01:00
|
|
|
// NOTE(flaper87): As of now, it keeps track of the whole error
|
|
|
|
// chain. Ideally, we should have a way to configure this either
|
|
|
|
// by using -Z verbose or just a CLI argument.
|
|
|
|
if obligation.recursion_depth >= 0 {
|
2014-12-17 14:16:28 -05:00
|
|
|
let derived_cause = DerivedObligationCause {
|
|
|
|
parent_trait_ref: obligation.predicate.to_poly_trait_ref(),
|
|
|
|
parent_code: Rc::new(obligation.cause.code.clone()),
|
|
|
|
};
|
2014-12-06 11:39:25 -05:00
|
|
|
ObligationCause::new(obligation.cause.span,
|
2014-12-17 14:16:28 -05:00
|
|
|
obligation.cause.body_id,
|
|
|
|
variant(derived_cause))
|
2014-12-06 11:39:25 -05:00
|
|
|
} else {
|
|
|
|
obligation.cause.clone()
|
|
|
|
}
|
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-17 14:16:28 -05:00
|
|
|
impl<'tcx> Repr<'tcx> for SelectionCandidate<'tcx> {
|
2014-09-29 22:11:30 +03:00
|
|
|
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
|
2014-09-12 10:53:35 -04:00
|
|
|
match *self {
|
2014-09-18 11:08:04 -04:00
|
|
|
ErrorCandidate => format!("ErrorCandidate"),
|
2014-12-20 00:09:35 -08:00
|
|
|
BuiltinCandidate(b) => format!("BuiltinCandidate({:?})", b),
|
2014-12-27 04:22:29 -05:00
|
|
|
ParamCandidate(ref a) => format!("ParamCandidate({})", a.repr(tcx)),
|
|
|
|
ImplCandidate(a) => format!("ImplCandidate({})", a.repr(tcx)),
|
|
|
|
ProjectionCandidate => format!("ProjectionCandidate"),
|
|
|
|
FnPointerCandidate => format!("FnPointerCandidate"),
|
2014-12-23 05:26:34 -05:00
|
|
|
ObjectCandidate => {
|
|
|
|
format!("ObjectCandidate")
|
|
|
|
}
|
2015-01-24 22:00:03 +02:00
|
|
|
ClosureCandidate(c, ref s) => {
|
|
|
|
format!("ClosureCandidate({:?},{})", c, s.repr(tcx))
|
2014-12-01 09:23:40 -05:00
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
impl<'tcx> SelectionCache<'tcx> {
|
|
|
|
pub fn new() -> SelectionCache<'tcx> {
|
2014-09-17 16:12:02 -04:00
|
|
|
SelectionCache {
|
|
|
|
hashmap: RefCell::new(HashMap::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 00:03:03 -05:00
|
|
|
impl<'o, 'tcx> TraitObligationStack<'o, 'tcx> {
|
|
|
|
fn iter(&self) -> Option<&TraitObligationStack<'o, 'tcx>> {
|
2014-09-18 11:08:04 -04:00
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:26:38 -05:00
|
|
|
impl<'o, 'tcx> Iterator for Option<&'o TraitObligationStack<'o, 'tcx>> {
|
|
|
|
type Item = &'o TraitObligationStack<'o,'tcx>;
|
|
|
|
|
2014-12-05 00:03:03 -05:00
|
|
|
fn next(&mut self) -> Option<&'o TraitObligationStack<'o, 'tcx>> {
|
2014-09-18 11:08:04 -04:00
|
|
|
match *self {
|
|
|
|
Some(o) => {
|
|
|
|
*self = o.previous;
|
|
|
|
Some(o)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 00:03:03 -05:00
|
|
|
impl<'o, 'tcx> Repr<'tcx> for TraitObligationStack<'o, 'tcx> {
|
2014-09-29 22:11:30 +03:00
|
|
|
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
|
2014-12-05 00:03:03 -05:00
|
|
|
format!("TraitObligationStack({})",
|
2014-10-09 17:19:50 -04:00
|
|
|
self.obligation.repr(tcx))
|
2014-09-18 11:08:04 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2014-12-02 14:04:10 -05:00
|
|
|
impl<'tcx> EvaluationResult<'tcx> {
|
2014-10-09 17:19:50 -04:00
|
|
|
fn may_apply(&self) -> bool {
|
|
|
|
match *self {
|
2014-12-02 14:04:10 -05:00
|
|
|
EvaluatedToOk |
|
|
|
|
EvaluatedToAmbig |
|
|
|
|
EvaluatedToErr(Overflow) |
|
2014-12-17 14:16:28 -05:00
|
|
|
EvaluatedToErr(OutputTypeParameterMismatch(..)) => {
|
2014-12-02 14:04:10 -05:00
|
|
|
true
|
|
|
|
}
|
|
|
|
EvaluatedToErr(Unimplemented) => {
|
|
|
|
false
|
|
|
|
}
|
2014-09-17 16:12:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-17 08:51:43 -04:00
|
|
|
|
|
|
|
impl MethodMatchResult {
|
|
|
|
pub fn may_apply(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
MethodMatched(_) => true,
|
|
|
|
MethodAmbiguous(_) => true,
|
|
|
|
MethodDidNotMatch => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|