2020-04-12 05:02:35 +09:00
|
|
|
//! Trait Resolution. See the [rustc-dev-guide] for more information on how this works.
|
2020-02-22 11:44:18 +01:00
|
|
|
//!
|
2020-04-12 05:02:35 +09:00
|
|
|
//! [rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html
|
2020-02-22 11:44:18 +01:00
|
|
|
|
|
|
|
mod engine;
|
|
|
|
pub mod error_reporting;
|
|
|
|
mod project;
|
|
|
|
mod structural_impls;
|
2020-03-31 16:50:15 -07:00
|
|
|
pub mod util;
|
2020-02-22 11:44:18 +01:00
|
|
|
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_hir as hir;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::error::{ExpectedFound, TypeError};
|
2020-02-29 10:03:04 +13:00
|
|
|
use rustc_middle::ty::{self, Const, Ty};
|
2020-02-22 11:44:18 +01:00
|
|
|
use rustc_span::Span;
|
|
|
|
|
|
|
|
pub use self::FulfillmentErrorCode::*;
|
2020-05-11 15:25:33 +00:00
|
|
|
pub use self::ImplSource::*;
|
2020-02-22 11:44:18 +01:00
|
|
|
pub use self::ObligationCauseCode::*;
|
|
|
|
pub use self::SelectionError::*;
|
|
|
|
|
|
|
|
pub use self::engine::{TraitEngine, TraitEngineExt};
|
|
|
|
pub use self::project::MismatchedProjectionTypes;
|
2020-02-25 13:08:38 +01:00
|
|
|
pub(crate) use self::project::UndoLog;
|
2020-02-22 11:44:18 +01:00
|
|
|
pub use self::project::{
|
|
|
|
Normalized, NormalizedTy, ProjectionCache, ProjectionCacheEntry, ProjectionCacheKey,
|
2020-03-16 16:43:03 +01:00
|
|
|
ProjectionCacheStorage, Reveal,
|
2020-02-22 11:44:18 +01:00
|
|
|
};
|
2020-03-29 16:41:09 +02:00
|
|
|
pub use rustc_middle::traits::*;
|
2020-02-22 11:44:18 +01:00
|
|
|
|
2020-06-06 12:05:37 +02:00
|
|
|
/// An `Obligation` represents some trait reference (e.g., `i32: Eq`) for
|
2020-05-11 15:25:33 +00:00
|
|
|
/// which the "impl_source" must be found. The process of finding a "impl_source" is
|
2020-02-22 11:44:18 +01:00
|
|
|
/// called "resolving" the `Obligation`. This process consists of
|
2020-06-06 12:05:37 +02:00
|
|
|
/// either identifying an `impl` (e.g., `impl Eq for i32`) that
|
2020-05-11 15:25:33 +00:00
|
|
|
/// satisfies the obligation, or else finding a bound that is in
|
2020-02-22 11:44:18 +01:00
|
|
|
/// scope. The eventual result is usually a `Selection` (defined below).
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct Obligation<'tcx, T> {
|
|
|
|
/// The reason we have to prove this thing.
|
|
|
|
pub cause: ObligationCause<'tcx>,
|
|
|
|
|
|
|
|
/// The environment in which we should prove this thing.
|
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
|
|
|
/// The thing we are trying to prove.
|
|
|
|
pub predicate: T,
|
|
|
|
|
|
|
|
/// If we started proving this as a result of trying to prove
|
|
|
|
/// something else, track the total depth to ensure termination.
|
|
|
|
/// If this goes over a certain threshold, we abort compilation --
|
|
|
|
/// in such cases, we can not say whether or not the predicate
|
|
|
|
/// holds for certain. Stupid halting problem; such a drag.
|
|
|
|
pub recursion_depth: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
|
|
|
|
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
|
|
|
|
|
|
|
|
// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2020-09-22 18:30:53 -07:00
|
|
|
static_assert_size!(PredicateObligation<'_>, 32);
|
2020-02-22 11:44:18 +01:00
|
|
|
|
|
|
|
pub type PredicateObligations<'tcx> = Vec<PredicateObligation<'tcx>>;
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
pub type Selection<'tcx> = ImplSource<'tcx, PredicateObligation<'tcx>>;
|
2020-02-22 11:44:18 +01:00
|
|
|
|
|
|
|
pub struct FulfillmentError<'tcx> {
|
|
|
|
pub obligation: PredicateObligation<'tcx>,
|
|
|
|
pub code: FulfillmentErrorCode<'tcx>,
|
|
|
|
/// Diagnostics only: we opportunistically change the `code.span` when we encounter an
|
|
|
|
/// obligation error caused by a call argument. When this is the case, we also signal that in
|
|
|
|
/// this field to ensure accuracy of suggestions.
|
|
|
|
pub points_at_arg_span: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum FulfillmentErrorCode<'tcx> {
|
|
|
|
CodeSelectionError(SelectionError<'tcx>),
|
|
|
|
CodeProjectionError(MismatchedProjectionTypes<'tcx>),
|
|
|
|
CodeSubtypeError(ExpectedFound<Ty<'tcx>>, TypeError<'tcx>), // always comes from a SubtypePredicate
|
2020-02-29 10:03:04 +13:00
|
|
|
CodeConstEquateError(ExpectedFound<&'tcx Const<'tcx>>, TypeError<'tcx>),
|
2020-02-22 11:44:18 +01:00
|
|
|
CodeAmbiguity,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, O> Obligation<'tcx, O> {
|
|
|
|
pub fn new(
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
predicate: O,
|
|
|
|
) -> Obligation<'tcx, O> {
|
|
|
|
Obligation { cause, param_env, recursion_depth: 0, predicate }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_depth(
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
recursion_depth: usize,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
predicate: O,
|
|
|
|
) -> Obligation<'tcx, O> {
|
|
|
|
Obligation { cause, param_env, recursion_depth, predicate }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn misc(
|
|
|
|
span: Span,
|
|
|
|
body_id: hir::HirId,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
trait_ref: O,
|
|
|
|
) -> Obligation<'tcx, O> {
|
|
|
|
Obligation::new(ObligationCause::misc(span, body_id), param_env, trait_ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with<P>(&self, value: P) -> Obligation<'tcx, P> {
|
|
|
|
Obligation {
|
|
|
|
cause: self.cause.clone(),
|
|
|
|
param_env: self.param_env,
|
|
|
|
recursion_depth: self.recursion_depth,
|
|
|
|
predicate: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> FulfillmentError<'tcx> {
|
|
|
|
pub fn new(
|
|
|
|
obligation: PredicateObligation<'tcx>,
|
|
|
|
code: FulfillmentErrorCode<'tcx>,
|
|
|
|
) -> FulfillmentError<'tcx> {
|
|
|
|
FulfillmentError { obligation, code, points_at_arg_span: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TraitObligation<'tcx> {
|
|
|
|
pub fn self_ty(&self) -> ty::Binder<Ty<'tcx>> {
|
|
|
|
self.predicate.map_bound(|p| p.self_ty())
|
|
|
|
}
|
|
|
|
}
|