2018-11-27 02:59:49 +00:00
|
|
|
//! Trait Resolution. See the [rustc guide] for more information on how this works.
|
2018-02-25 21:24:14 +00:00
|
|
|
//!
|
2020-03-06 14:13:26 +00:00
|
|
|
//! [rustc guide]: https://rust-lang.github.io/rustc-dev-guide/traits/resolution.html
|
2014-09-12 14:53:35 +00:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub mod auto_trait;
|
2019-12-24 22:38:22 +00:00
|
|
|
pub mod codegen;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod coherence;
|
|
|
|
mod engine;
|
2019-12-24 22:38:22 +00:00
|
|
|
pub mod error_reporting;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod fulfill;
|
2020-01-07 18:36:50 +00:00
|
|
|
pub mod misc;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod object_safety;
|
|
|
|
mod on_unimplemented;
|
2019-12-24 22:38:22 +00:00
|
|
|
mod project;
|
|
|
|
pub mod query;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod select;
|
|
|
|
mod specialize;
|
|
|
|
mod structural_impls;
|
2020-01-05 22:02:07 +00:00
|
|
|
mod structural_match;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod util;
|
2020-01-05 19:52:34 +00:00
|
|
|
pub mod wf;
|
2014-11-06 08:05:53 +00:00
|
|
|
|
2019-02-05 17:20:45 +00:00
|
|
|
use crate::infer::outlives::env::OutlivesEnvironment;
|
2020-01-06 19:13:24 +00:00
|
|
|
use crate::infer::{InferCtxt, SuppressRegionErrors, TyCtxtInferExt};
|
2020-01-07 21:07:22 +00:00
|
|
|
use rustc::middle::region;
|
|
|
|
use rustc::ty::error::{ExpectedFound, TypeError};
|
|
|
|
use rustc::ty::fold::TypeFoldable;
|
|
|
|
use rustc::ty::subst::{InternalSubsts, SubstsRef};
|
|
|
|
use rustc::ty::{self, GenericParamDefKind, ToPredicate, Ty, TyCtxt, WithConstness};
|
|
|
|
use rustc::util::common::ErrorReported;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2015-09-24 16:58:00 +00:00
|
|
|
|
2018-12-07 01:40:42 +00:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
pub use self::FulfillmentErrorCode::*;
|
2020-01-06 19:13:24 +00:00
|
|
|
pub use self::ObligationCauseCode::*;
|
|
|
|
pub use self::SelectionError::*;
|
|
|
|
pub use self::Vtable::*;
|
2014-09-12 14:53:35 +00:00
|
|
|
|
2018-11-20 16:20:05 +00:00
|
|
|
pub use self::coherence::{add_placeholder_note, orphan_check, overlapping_impls};
|
|
|
|
pub use self::coherence::{OrphanCheckErr, OverlapResult};
|
2019-12-24 22:38:22 +00:00
|
|
|
pub use self::engine::{TraitEngine, TraitEngineExt};
|
2018-03-11 02:29:22 +00:00
|
|
|
pub use self::fulfill::{FulfillmentContext, PendingPredicateObligation};
|
2020-01-05 17:07:29 +00:00
|
|
|
pub use self::object_safety::astconv_object_safety_violations;
|
|
|
|
pub use self::object_safety::is_vtable_safe_method;
|
2014-12-16 02:11:09 +00:00
|
|
|
pub use self::object_safety::MethodViolationCode;
|
2019-12-24 22:38:22 +00:00
|
|
|
pub use self::object_safety::ObjectSafetyViolation;
|
2017-08-30 20:40:43 +00:00
|
|
|
pub use self::on_unimplemented::{OnUnimplementedDirective, OnUnimplementedNote};
|
2019-12-24 22:38:22 +00:00
|
|
|
pub use self::project::MismatchedProjectionTypes;
|
2020-01-08 21:06:25 +00:00
|
|
|
pub use self::project::{
|
|
|
|
normalize, normalize_projection_type, normalize_to, poly_project_and_unify_type,
|
|
|
|
};
|
2020-01-06 19:13:24 +00:00
|
|
|
pub use self::project::{Normalized, ProjectionCache, ProjectionCacheSnapshot, Reveal};
|
|
|
|
pub use self::select::{EvaluationCache, SelectionCache, SelectionContext};
|
|
|
|
pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError};
|
2018-10-16 14:57:53 +00:00
|
|
|
pub use self::specialize::find_associated_item;
|
2018-12-03 21:27:25 +00:00
|
|
|
pub use self::specialize::specialization_graph::FutureCompatOverlapError;
|
|
|
|
pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind;
|
2019-12-24 22:38:22 +00:00
|
|
|
pub use self::specialize::{specialization_graph, translate_substs, OverlapError};
|
2020-01-05 22:02:07 +00:00
|
|
|
pub use self::structural_match::search_for_structural_match_violation;
|
|
|
|
pub use self::structural_match::type_marked_structural;
|
|
|
|
pub use self::structural_match::NonStructuralMatchTy;
|
2018-11-05 02:02:43 +00:00
|
|
|
pub use self::util::{elaborate_predicates, elaborate_trait_ref, elaborate_trait_refs};
|
2019-12-24 22:38:22 +00:00
|
|
|
pub use self::util::{expand_trait_aliases, TraitAliasExpander};
|
2020-01-05 19:27:00 +00:00
|
|
|
pub use self::util::{
|
|
|
|
get_vtable_index_of_object_method, impl_is_default, impl_item_is_final,
|
|
|
|
predicate_for_trait_def, upcast_choices,
|
|
|
|
};
|
2019-04-29 02:58:24 +00:00
|
|
|
pub use self::util::{
|
2019-12-24 22:38:22 +00:00
|
|
|
supertrait_def_ids, supertraits, transitive_bounds, SupertraitDefIds, Supertraits,
|
2019-04-29 02:58:24 +00:00
|
|
|
};
|
2014-09-12 14:53:35 +00:00
|
|
|
|
2020-01-06 19:13:24 +00:00
|
|
|
pub use rustc::traits::*;
|
2018-02-25 15:58:54 +00:00
|
|
|
|
2020-01-24 20:57:01 +00:00
|
|
|
/// Whether to skip the leak check, as part of a future compatibility warning step.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum SkipLeakCheck {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SkipLeakCheck {
|
|
|
|
fn is_yes(self) -> bool {
|
|
|
|
self == SkipLeakCheck::Yes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The "default" for skip-leak-check corresponds to the current
|
|
|
|
/// behavior (do not skip the leak check) -- not the behavior we are
|
|
|
|
/// transitioning into.
|
|
|
|
impl Default for SkipLeakCheck {
|
|
|
|
fn default() -> Self {
|
|
|
|
SkipLeakCheck::No
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The mode that trait queries run in.
|
2020-01-16 23:47:52 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2018-04-19 07:28:03 +00:00
|
|
|
pub enum TraitQueryMode {
|
|
|
|
// Standard/un-canonicalized queries get accurate
|
|
|
|
// spans etc. passed in and hence can do reasonable
|
|
|
|
// error reporting on their own.
|
|
|
|
Standard,
|
|
|
|
// Canonicalized queries get dummy spans and hence
|
|
|
|
// must generally propagate errors to
|
|
|
|
// pre-canonicalization callsites.
|
|
|
|
Canonical,
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// An `Obligation` represents some trait reference (e.g., `int: Eq`) for
|
|
|
|
/// which the vtable must be found. The process of finding a vtable is
|
2014-11-25 01:06:06 +00:00
|
|
|
/// called "resolving" the `Obligation`. This process consists of
|
|
|
|
/// either identifying an `impl` (e.g., `impl Eq for int`) that
|
|
|
|
/// provides the required vtable, or else finding a bound that is in
|
|
|
|
/// scope. The eventual result is usually a `Selection` (defined below).
|
2018-02-18 12:23:20 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2014-12-05 05:03:03 +00:00
|
|
|
pub struct Obligation<'tcx, T> {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The reason we have to prove this thing.
|
2014-09-29 19:11:30 +00:00
|
|
|
pub cause: ObligationCause<'tcx>,
|
2017-07-15 10:47:30 +00:00
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The environment in which we should prove this thing.
|
2017-05-23 08:19:47 +00:00
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
2017-07-15 10:47:30 +00:00
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// The thing we are trying to prove.
|
2014-12-17 21:00:34 +00:00
|
|
|
pub predicate: T,
|
2017-07-15 10:47:30 +00:00
|
|
|
|
|
|
|
/// 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
|
2019-02-08 13:53:55 +00:00
|
|
|
/// holds for certain. Stupid halting problem; such a drag.
|
2017-07-15 10:47:30 +00:00
|
|
|
pub recursion_depth: usize,
|
2014-09-12 14:53:35 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 16:10:48 +00:00
|
|
|
pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
|
2014-12-17 19:16:28 +00:00
|
|
|
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
|
2014-12-05 05:03:03 +00:00
|
|
|
|
2019-09-09 04:06:14 +00:00
|
|
|
// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2019-09-09 02:40:54 +00:00
|
|
|
static_assert_size!(PredicateObligation<'_>, 112);
|
2019-09-09 04:06:14 +00:00
|
|
|
|
2015-06-02 23:14:45 +00:00
|
|
|
pub type Obligations<'tcx, O> = Vec<Obligation<'tcx, O>>;
|
|
|
|
pub type PredicateObligations<'tcx> = Vec<PredicateObligation<'tcx>>;
|
|
|
|
pub type TraitObligations<'tcx> = Vec<TraitObligation<'tcx>>;
|
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-06 01:01:33 +00:00
|
|
|
|
2014-12-07 16:10:48 +00:00
|
|
|
pub type Selection<'tcx> = Vtable<'tcx, PredicateObligation<'tcx>>;
|
2014-09-12 14:53:35 +00:00
|
|
|
|
2014-09-29 19:11:30 +00:00
|
|
|
pub struct FulfillmentError<'tcx> {
|
2014-12-07 16:10:48 +00:00
|
|
|
pub obligation: PredicateObligation<'tcx>,
|
2019-09-16 22:54:31 +00:00
|
|
|
pub code: FulfillmentErrorCode<'tcx>,
|
2019-09-18 19:05:37 +00:00
|
|
|
/// 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.
|
2019-09-16 22:54:31 +00:00
|
|
|
pub points_at_arg_span: bool,
|
2014-09-12 14:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-01-04 03:54:18 +00:00
|
|
|
#[derive(Clone)]
|
2014-09-29 19:11:30 +00:00
|
|
|
pub enum FulfillmentErrorCode<'tcx> {
|
|
|
|
CodeSelectionError(SelectionError<'tcx>),
|
2014-12-17 19:16:28 +00:00
|
|
|
CodeProjectionError(MismatchedProjectionTypes<'tcx>),
|
2019-12-24 22:38:22 +00:00
|
|
|
CodeSubtypeError(ExpectedFound<Ty<'tcx>>, TypeError<'tcx>), // always comes from a SubtypePredicate
|
2014-09-11 05:07:49 +00:00
|
|
|
CodeAmbiguity,
|
2014-09-12 14:53:35 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 16:10:48 +00:00
|
|
|
/// Creates predicate obligations from the generic bounds.
|
2019-08-26 04:58:59 +00:00
|
|
|
pub fn predicates_for_generics<'tcx>(
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
generic_bounds: &ty::InstantiatedPredicates<'tcx>,
|
|
|
|
) -> PredicateObligations<'tcx> {
|
2017-05-23 08:19:47 +00:00
|
|
|
util::predicates_for_generics(cause, 0, param_env, generic_bounds)
|
2014-09-12 14:53:35 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 14:26:10 +00:00
|
|
|
/// Determines whether the type `ty` is known to meet `bound` and
|
|
|
|
/// returns true if so. Returns false if `ty` either does not meet
|
|
|
|
/// `bound` or is not known to meet bound (note that this is
|
|
|
|
/// conservative towards *no impl*, which is the opposite of the
|
|
|
|
/// `evaluate` methods).
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
2018-09-20 17:56:11 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
span: Span,
|
|
|
|
) -> bool {
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!(
|
|
|
|
"type_known_to_meet_bound_modulo_regions(ty={:?}, bound={:?})",
|
|
|
|
ty,
|
|
|
|
infcx.tcx.def_path_str(def_id)
|
|
|
|
);
|
2016-11-14 02:42:15 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
|
2016-11-14 02:42:15 +00:00
|
|
|
let obligation = Obligation {
|
2017-05-23 08:19:47 +00:00
|
|
|
param_env,
|
2019-02-04 19:01:14 +00:00
|
|
|
cause: ObligationCause::misc(span, hir::DUMMY_HIR_ID),
|
2016-11-14 02:42:15 +00:00
|
|
|
recursion_depth: 0,
|
2020-01-14 04:30:32 +00:00
|
|
|
predicate: trait_ref.without_const().to_predicate(),
|
2016-11-14 02:42:15 +00:00
|
|
|
};
|
|
|
|
|
2018-09-20 17:56:11 +00:00
|
|
|
let result = infcx.predicate_must_hold_modulo_regions(&obligation);
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!(
|
|
|
|
"type_known_to_meet_ty={:?} bound={} => {:?}",
|
|
|
|
ty,
|
|
|
|
infcx.tcx.def_path_str(def_id),
|
|
|
|
result
|
|
|
|
);
|
2015-10-21 16:01:58 +00:00
|
|
|
|
2020-02-22 14:10:17 +00:00
|
|
|
if result && ty.has_infer_types_or_consts() {
|
2015-10-21 16:01:58 +00:00
|
|
|
// Because of inference "guessing", selection can sometimes claim
|
|
|
|
// to succeed while the success requires a guess. To ensure
|
|
|
|
// this function's result remains infallible, we must confirm
|
|
|
|
// that guess. While imperfect, I believe this is sound.
|
|
|
|
|
2017-11-23 21:21:56 +00:00
|
|
|
// The handling of regions in this area of the code is terrible,
|
|
|
|
// see issue #29149. We should be able to improve on this with
|
|
|
|
// NLL.
|
2017-11-23 21:03:47 +00:00
|
|
|
let mut fulfill_cx = FulfillmentContext::new_ignoring_regions();
|
2015-10-21 16:01:58 +00:00
|
|
|
|
|
|
|
// We can use a dummy node-id here because we won't pay any mind
|
|
|
|
// to region obligations that arise (there shouldn't really be any
|
|
|
|
// anyhow).
|
2019-02-04 19:01:14 +00:00
|
|
|
let cause = ObligationCause::misc(span, hir::DUMMY_HIR_ID);
|
2014-12-18 14:26:10 +00:00
|
|
|
|
2017-05-23 08:19:47 +00:00
|
|
|
fulfill_cx.register_bound(infcx, param_env, ty, def_id, cause);
|
2015-10-21 16:01:58 +00:00
|
|
|
|
|
|
|
// Note: we only assume something is `Copy` if we can
|
|
|
|
// *definitively* show that it implements `Copy`. Otherwise,
|
|
|
|
// assume it is move; linear is always ok.
|
|
|
|
match fulfill_cx.select_all_or_error(infcx) {
|
|
|
|
Ok(()) => {
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!(
|
|
|
|
"type_known_to_meet_bound_modulo_regions: ty={:?} bound={} success",
|
|
|
|
ty,
|
|
|
|
infcx.tcx.def_path_str(def_id)
|
|
|
|
);
|
2015-10-21 16:01:58 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!(
|
|
|
|
"type_known_to_meet_bound_modulo_regions: ty={:?} bound={} errors={:?}",
|
|
|
|
ty,
|
|
|
|
infcx.tcx.def_path_str(def_id),
|
|
|
|
e
|
|
|
|
);
|
2015-10-21 16:01:58 +00:00
|
|
|
false
|
|
|
|
}
|
2015-01-02 09:01:30 +00:00
|
|
|
}
|
2015-10-21 16:01:58 +00:00
|
|
|
} else {
|
|
|
|
result
|
2015-01-02 09:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 21:11:55 +00:00
|
|
|
fn do_normalize_predicates<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 21:11:55 +00:00
|
|
|
region_context: DefId,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
elaborated_env: ty::ParamEnv<'tcx>,
|
|
|
|
predicates: Vec<ty::Predicate<'tcx>>,
|
|
|
|
) -> Result<Vec<ty::Predicate<'tcx>>, ErrorReported> {
|
2018-11-16 13:25:46 +00:00
|
|
|
debug!(
|
|
|
|
"do_normalize_predicates(predicates={:?}, region_context={:?}, cause={:?})",
|
2019-12-24 22:38:22 +00:00
|
|
|
predicates, region_context, cause,
|
2018-11-16 13:25:46 +00:00
|
|
|
);
|
2015-02-20 11:21:46 +00:00
|
|
|
let span = cause.span;
|
2017-06-09 07:55:16 +00:00
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2017-11-23 21:21:56 +00:00
|
|
|
// FIXME. We should really... do something with these region
|
|
|
|
// obligations. But this call just continues the older
|
|
|
|
// behavior (i.e., doesn't cause any new bugs), and it would
|
|
|
|
// take some further refactoring to actually solve them. In
|
|
|
|
// particular, we would have to handle implied bounds
|
|
|
|
// properly, and that code is currently largely confined to
|
|
|
|
// regionck (though I made some efforts to extract it
|
|
|
|
// out). -nmatsakis
|
|
|
|
//
|
|
|
|
// @arielby: In any case, these obligations are checked
|
|
|
|
// by wfcheck anyway, so I'm not sure we have to check
|
|
|
|
// them here too, and we will remove this function when
|
|
|
|
// we move over to lazy normalization *anyway*.
|
|
|
|
let fulfill_cx = FulfillmentContext::new_ignoring_regions();
|
2019-12-24 22:38:22 +00:00
|
|
|
let predicates =
|
|
|
|
match fully_normalize(&infcx, fulfill_cx, cause, elaborated_env, &predicates) {
|
|
|
|
Ok(predicates) => predicates,
|
|
|
|
Err(errors) => {
|
|
|
|
infcx.report_fulfillment_errors(&errors, None, false);
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
};
|
2016-03-25 03:22:44 +00:00
|
|
|
|
2018-09-30 17:09:05 +00:00
|
|
|
debug!("do_normalize_predictes: normalized predicates = {:?}", predicates);
|
2016-03-25 03:22:44 +00:00
|
|
|
|
2017-08-31 18:37:38 +00:00
|
|
|
let region_scope_tree = region::ScopeTree::default();
|
2017-11-01 18:52:24 +00:00
|
|
|
|
2017-12-01 10:07:52 +00:00
|
|
|
// We can use the `elaborated_env` here; the region code only
|
|
|
|
// cares about declarations like `'a: 'b`.
|
|
|
|
let outlives_env = OutlivesEnvironment::new(elaborated_env);
|
|
|
|
|
2018-09-12 19:43:26 +00:00
|
|
|
infcx.resolve_regions_and_report_errors(
|
|
|
|
region_context,
|
|
|
|
®ion_scope_tree,
|
|
|
|
&outlives_env,
|
2018-09-24 17:05:10 +00:00
|
|
|
SuppressRegionErrors::default(),
|
2018-09-12 19:43:26 +00:00
|
|
|
);
|
2017-12-01 10:07:52 +00:00
|
|
|
|
2016-03-25 03:22:44 +00:00
|
|
|
let predicates = match infcx.fully_resolve(&predicates) {
|
|
|
|
Ok(predicates) => predicates,
|
|
|
|
Err(fixup_err) => {
|
|
|
|
// If we encounter a fixup error, it means that some type
|
|
|
|
// variable wound up unconstrained. I actually don't know
|
|
|
|
// if this can happen, and I certainly don't expect it to
|
|
|
|
// happen often, but if it did happen it probably
|
|
|
|
// represents a legitimate failure due to some kind of
|
|
|
|
// unconstrained variable, and it seems better not to ICE,
|
|
|
|
// all things considered.
|
|
|
|
tcx.sess.span_err(span, &fixup_err.to_string());
|
2019-12-24 22:38:22 +00:00
|
|
|
return Err(ErrorReported);
|
2016-03-25 03:22:44 +00:00
|
|
|
}
|
|
|
|
};
|
2019-05-31 08:23:22 +00:00
|
|
|
if predicates.has_local_value() {
|
|
|
|
// FIXME: shouldn't we, you know, actually report an error here? or an ICE?
|
|
|
|
Err(ErrorReported)
|
|
|
|
} else {
|
|
|
|
Ok(predicates)
|
2018-09-30 17:09:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this is gonna need to be removed ...
|
|
|
|
/// Normalizes the parameter environment, reporting errors if they occur.
|
2019-06-11 21:11:55 +00:00
|
|
|
pub fn normalize_param_env_or_error<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 21:11:55 +00:00
|
|
|
region_context: DefId,
|
|
|
|
unnormalized_env: ty::ParamEnv<'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
) -> ty::ParamEnv<'tcx> {
|
2018-09-30 17:09:05 +00:00
|
|
|
// I'm not wild about reporting errors here; I'd prefer to
|
|
|
|
// have the errors get reported at a defined place (e.g.,
|
|
|
|
// during typeck). Instead I have all parameter
|
|
|
|
// environments, in effect, going through this function
|
|
|
|
// and hence potentially reporting errors. This ensures of
|
|
|
|
// course that we never forget to normalize (the
|
|
|
|
// alternative seemed like it would involve a lot of
|
|
|
|
// manual invocations of this fn -- and then we'd have to
|
|
|
|
// deal with the errors at each of those sites).
|
|
|
|
//
|
|
|
|
// In any case, in practice, typeck constructs all the
|
|
|
|
// parameter environments once for every fn as it goes,
|
|
|
|
// and errors will get reported then; so after typeck we
|
|
|
|
// can be sure that no errors should occur.
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!(
|
|
|
|
"normalize_param_env_or_error(region_context={:?}, unnormalized_env={:?}, cause={:?})",
|
|
|
|
region_context, unnormalized_env, cause
|
|
|
|
);
|
2018-09-30 17:09:05 +00:00
|
|
|
|
|
|
|
let mut predicates: Vec<_> =
|
2019-12-24 22:38:22 +00:00
|
|
|
util::elaborate_predicates(tcx, unnormalized_env.caller_bounds.to_vec()).collect();
|
2018-09-30 17:09:05 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!("normalize_param_env_or_error: elaborated-predicates={:?}", predicates);
|
2018-09-30 17:09:05 +00:00
|
|
|
|
2018-11-17 17:56:14 +00:00
|
|
|
let elaborated_env = ty::ParamEnv::new(
|
|
|
|
tcx.intern_predicates(&predicates),
|
|
|
|
unnormalized_env.reveal,
|
2019-12-24 22:38:22 +00:00
|
|
|
unnormalized_env.def_id,
|
2018-11-17 17:56:14 +00:00
|
|
|
);
|
2018-09-30 17:09:05 +00:00
|
|
|
|
|
|
|
// HACK: we are trying to normalize the param-env inside *itself*. The problem is that
|
|
|
|
// normalization expects its param-env to be already normalized, which means we have
|
|
|
|
// a circularity.
|
|
|
|
//
|
|
|
|
// The way we handle this is by normalizing the param-env inside an unnormalized version
|
|
|
|
// of the param-env, which means that if the param-env contains unnormalized projections,
|
|
|
|
// we'll have some normalization failures. This is unfortunate.
|
|
|
|
//
|
|
|
|
// Lazy normalization would basically handle this by treating just the
|
|
|
|
// normalizing-a-trait-ref-requires-itself cycles as evaluation failures.
|
|
|
|
//
|
|
|
|
// Inferred outlives bounds can create a lot of `TypeOutlives` predicates for associated
|
|
|
|
// types, so to make the situation less bad, we normalize all the predicates *but*
|
|
|
|
// the `TypeOutlives` predicates first inside the unnormalized parameter environment, and
|
|
|
|
// then we normalize the `TypeOutlives` bounds inside the normalized parameter environment.
|
|
|
|
//
|
|
|
|
// This works fairly well because trait matching does not actually care about param-env
|
|
|
|
// TypeOutlives predicates - these are normally used by regionck.
|
2019-12-24 22:38:22 +00:00
|
|
|
let outlives_predicates: Vec<_> = predicates
|
|
|
|
.drain_filter(|predicate| match predicate {
|
2018-09-30 17:09:05 +00:00
|
|
|
ty::Predicate::TypeOutlives(..) => true,
|
2019-12-24 22:38:22 +00:00
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
.collect();
|
2018-09-30 17:09:05 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!(
|
|
|
|
"normalize_param_env_or_error: predicates=(non-outlives={:?}, outlives={:?})",
|
|
|
|
predicates, outlives_predicates
|
|
|
|
);
|
|
|
|
let non_outlives_predicates = match do_normalize_predicates(
|
|
|
|
tcx,
|
|
|
|
region_context,
|
|
|
|
cause.clone(),
|
|
|
|
elaborated_env,
|
|
|
|
predicates,
|
|
|
|
) {
|
|
|
|
Ok(predicates) => predicates,
|
|
|
|
// An unnormalized env is better than nothing.
|
|
|
|
Err(ErrorReported) => {
|
|
|
|
debug!("normalize_param_env_or_error: errored resolving non-outlives predicates");
|
|
|
|
return elaborated_env;
|
|
|
|
}
|
|
|
|
};
|
2016-05-11 01:14:41 +00:00
|
|
|
|
2018-09-30 17:09:05 +00:00
|
|
|
debug!("normalize_param_env_or_error: non-outlives predicates={:?}", non_outlives_predicates);
|
|
|
|
|
|
|
|
// Not sure whether it is better to include the unnormalized TypeOutlives predicates
|
|
|
|
// here. I believe they should not matter, because we are ignoring TypeOutlives param-env
|
|
|
|
// predicates here anyway. Keeping them here anyway because it seems safer.
|
|
|
|
let outlives_env: Vec<_> =
|
|
|
|
non_outlives_predicates.iter().chain(&outlives_predicates).cloned().collect();
|
2019-12-24 22:38:22 +00:00
|
|
|
let outlives_env =
|
|
|
|
ty::ParamEnv::new(tcx.intern_predicates(&outlives_env), unnormalized_env.reveal, None);
|
|
|
|
let outlives_predicates = match do_normalize_predicates(
|
|
|
|
tcx,
|
|
|
|
region_context,
|
|
|
|
cause,
|
|
|
|
outlives_env,
|
|
|
|
outlives_predicates,
|
|
|
|
) {
|
|
|
|
Ok(predicates) => predicates,
|
|
|
|
// An unnormalized env is better than nothing.
|
|
|
|
Err(ErrorReported) => {
|
|
|
|
debug!("normalize_param_env_or_error: errored resolving outlives predicates");
|
|
|
|
return elaborated_env;
|
|
|
|
}
|
|
|
|
};
|
2018-09-30 17:09:05 +00:00
|
|
|
debug!("normalize_param_env_or_error: outlives predicates={:?}", outlives_predicates);
|
2015-12-23 00:51:29 +00:00
|
|
|
|
2018-09-30 17:09:05 +00:00
|
|
|
let mut predicates = non_outlives_predicates;
|
|
|
|
predicates.extend(outlives_predicates);
|
|
|
|
debug!("normalize_param_env_or_error: final predicates={:?}", predicates);
|
2018-11-17 17:56:14 +00:00
|
|
|
ty::ParamEnv::new(
|
|
|
|
tcx.intern_predicates(&predicates),
|
|
|
|
unnormalized_env.reveal,
|
2019-12-24 22:38:22 +00:00
|
|
|
unnormalized_env.def_id,
|
2018-11-17 17:56:14 +00:00
|
|
|
)
|
2015-01-26 19:20:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn fully_normalize<'a, 'tcx, T>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
2017-11-23 21:03:47 +00:00
|
|
|
mut fulfill_cx: FulfillmentContext<'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2019-06-13 22:32:15 +00:00
|
|
|
value: &T,
|
|
|
|
) -> Result<T, Vec<FulfillmentError<'tcx>>>
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
2017-11-23 21:03:47 +00:00
|
|
|
{
|
|
|
|
debug!("fully_normalize_with_fulfillcx(value={:?})", value);
|
|
|
|
let selcx = &mut SelectionContext::new(infcx);
|
2015-02-14 00:51:43 +00:00
|
|
|
let Normalized { value: normalized_value, obligations } =
|
2017-05-23 08:19:47 +00:00
|
|
|
project::normalize(selcx, param_env, cause, value);
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!(
|
|
|
|
"fully_normalize: normalized_value={:?} obligations={:?}",
|
|
|
|
normalized_value, obligations
|
|
|
|
);
|
2015-02-14 00:51:43 +00:00
|
|
|
for obligation in obligations {
|
|
|
|
fulfill_cx.register_predicate_obligation(selcx.infcx(), obligation);
|
|
|
|
}
|
2015-07-01 20:08:25 +00:00
|
|
|
|
2015-12-23 00:51:29 +00:00
|
|
|
debug!("fully_normalize: select_all_or_error start");
|
2018-05-08 14:38:35 +00:00
|
|
|
fulfill_cx.select_all_or_error(infcx)?;
|
2015-12-23 00:51:29 +00:00
|
|
|
debug!("fully_normalize: select_all_or_error complete");
|
2019-05-11 18:08:26 +00:00
|
|
|
let resolved_value = infcx.resolve_vars_if_possible(&normalized_value);
|
2015-12-23 00:51:29 +00:00
|
|
|
debug!("fully_normalize: resolved_value={:?}", resolved_value);
|
2015-02-14 00:51:43 +00:00
|
|
|
Ok(resolved_value)
|
|
|
|
}
|
|
|
|
|
2017-05-23 08:19:47 +00:00
|
|
|
/// Normalizes the predicates and checks whether they hold in an empty
|
|
|
|
/// environment. If this returns false, then either normalize
|
|
|
|
/// encountered an error or one of the predicates did not hold. Used
|
|
|
|
/// when creating vtables to check for unsatisfiable methods.
|
2020-01-16 23:47:52 +00:00
|
|
|
pub fn normalize_and_test_predicates<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 21:11:55 +00:00
|
|
|
predicates: Vec<ty::Predicate<'tcx>>,
|
|
|
|
) -> bool {
|
2020-01-16 23:47:52 +00:00
|
|
|
debug!("normalize_and_test_predicates(predicates={:?})", predicates);
|
2016-09-19 09:47:47 +00:00
|
|
|
|
2017-06-21 11:50:43 +00:00
|
|
|
let result = tcx.infer_ctxt().enter(|infcx| {
|
2018-02-10 18:18:02 +00:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2020-01-16 23:47:52 +00:00
|
|
|
let mut selcx = SelectionContext::new(&infcx);
|
|
|
|
let mut fulfill_cx = FulfillmentContext::new();
|
2016-09-19 09:47:47 +00:00
|
|
|
let cause = ObligationCause::dummy();
|
|
|
|
let Normalized { value: predicates, obligations } =
|
2017-05-23 08:19:47 +00:00
|
|
|
normalize(&mut selcx, param_env, cause.clone(), &predicates);
|
2016-09-19 09:47:47 +00:00
|
|
|
for obligation in obligations {
|
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, obligation);
|
|
|
|
}
|
|
|
|
for predicate in predicates {
|
2017-05-23 08:19:47 +00:00
|
|
|
let obligation = Obligation::new(cause.clone(), param_env, predicate);
|
2016-09-19 09:47:47 +00:00
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, obligation);
|
|
|
|
}
|
|
|
|
|
|
|
|
fulfill_cx.select_all_or_error(&infcx).is_ok()
|
2017-06-21 11:50:43 +00:00
|
|
|
});
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!("normalize_and_test_predicates(predicates={:?}) = {:?}", predicates, result);
|
2017-06-21 11:50:43 +00:00
|
|
|
result
|
2016-09-19 09:47:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 21:11:55 +00:00
|
|
|
fn substitute_normalize_and_test_predicates<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-01-16 23:47:52 +00:00
|
|
|
key: (DefId, SubstsRef<'tcx>),
|
2019-06-11 21:11:55 +00:00
|
|
|
) -> bool {
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!("substitute_normalize_and_test_predicates(key={:?})", key);
|
2017-12-27 17:32:44 +00:00
|
|
|
|
2018-09-16 17:15:49 +00:00
|
|
|
let predicates = tcx.predicates_of(key.0).instantiate(tcx, key.1).predicates;
|
2020-01-16 23:47:52 +00:00
|
|
|
let result = normalize_and_test_predicates(tcx, predicates);
|
2017-12-27 17:32:44 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!("substitute_normalize_and_test_predicates(key={:?}) = {:?}", key, result);
|
2017-12-27 17:32:44 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2016-09-19 09:47:47 +00:00
|
|
|
/// Given a trait `trait_ref`, iterates the vtable entries
|
|
|
|
/// that come from `trait_ref`, including its supertraits.
|
2018-11-27 02:59:49 +00:00
|
|
|
#[inline] // FIXME(#35870): avoid closures being unexported due to `impl Trait`.
|
2019-06-11 20:35:39 +00:00
|
|
|
fn vtable_methods<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 21:11:55 +00:00
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
) -> &'tcx [Option<(DefId, SubstsRef<'tcx>)>] {
|
2017-10-07 21:55:09 +00:00
|
|
|
debug!("vtable_methods({:?})", trait_ref);
|
2016-09-19 09:47:47 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
tcx.arena.alloc_from_iter(supertraits(tcx, trait_ref).flat_map(move |trait_ref| {
|
|
|
|
let trait_methods = tcx
|
|
|
|
.associated_items(trait_ref.def_id())
|
2020-02-17 21:09:01 +00:00
|
|
|
.in_definition_order()
|
2019-12-24 22:38:22 +00:00
|
|
|
.filter(|item| item.kind == ty::AssocKind::Method);
|
|
|
|
|
|
|
|
// Now list each method's DefId and InternalSubsts (for within its trait).
|
|
|
|
// If the method can never be called from this object, produce None.
|
|
|
|
trait_methods.map(move |trait_method| {
|
|
|
|
debug!("vtable_methods: trait_method={:?}", trait_method);
|
|
|
|
let def_id = trait_method.def_id;
|
|
|
|
|
|
|
|
// Some methods cannot be called on an object; skip those.
|
2020-01-05 17:07:29 +00:00
|
|
|
if !is_vtable_safe_method(tcx, trait_ref.def_id(), &trait_method) {
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!("vtable_methods: not vtable safe");
|
|
|
|
return None;
|
|
|
|
}
|
2017-10-09 15:39:53 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
// The method may have some early-bound lifetimes; add regions for those.
|
|
|
|
let substs = trait_ref.map_bound(|trait_ref| {
|
|
|
|
InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
|
|
|
|
GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
|
|
|
|
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => {
|
|
|
|
trait_ref.substs[param.index as usize]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
// The trait type may have higher-ranked lifetimes in it;
|
|
|
|
// erase them if they appear, so that we get the type
|
|
|
|
// at some particular call site.
|
|
|
|
let substs =
|
|
|
|
tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &substs);
|
|
|
|
|
|
|
|
// It's possible that the method relies on where-clauses that
|
|
|
|
// do not hold for this particular set of type parameters.
|
|
|
|
// Note that this method could then never be called, so we
|
|
|
|
// do not want to try and codegen it, in that case (see #23435).
|
|
|
|
let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs);
|
2020-01-16 23:47:52 +00:00
|
|
|
if !normalize_and_test_predicates(tcx, predicates.predicates) {
|
2019-12-24 22:38:22 +00:00
|
|
|
debug!("vtable_methods: predicates do not hold");
|
|
|
|
return None;
|
|
|
|
}
|
2017-10-09 15:39:53 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
Some((def_id, substs))
|
2019-03-29 16:49:11 +00:00
|
|
|
})
|
2019-12-24 22:38:22 +00:00
|
|
|
}))
|
2016-09-19 09:47:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 15:14:41 +00:00
|
|
|
impl<'tcx, O> Obligation<'tcx, O> {
|
2019-12-24 22:38:22 +00:00
|
|
|
pub fn new(
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
predicate: O,
|
|
|
|
) -> Obligation<'tcx, O> {
|
2017-05-23 08:19:47 +00:00
|
|
|
Obligation { cause, param_env, recursion_depth: 0, predicate }
|
2014-09-12 14:53:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
fn with_depth(
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
recursion_depth: usize,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
predicate: O,
|
|
|
|
) -> Obligation<'tcx, O> {
|
2017-05-23 08:19:47 +00:00
|
|
|
Obligation { cause, param_env, recursion_depth, predicate }
|
2014-12-30 22:42:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
pub fn misc(
|
|
|
|
span: Span,
|
|
|
|
body_id: hir::HirId,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
trait_ref: O,
|
|
|
|
) -> Obligation<'tcx, O> {
|
2017-05-23 08:19:47 +00:00
|
|
|
Obligation::new(ObligationCause::misc(span, body_id), param_env, trait_ref)
|
2014-09-12 14:53:35 +00:00
|
|
|
}
|
2014-12-07 16:10:48 +00:00
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
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,
|
|
|
|
}
|
2014-12-07 16:10:48 +00:00
|
|
|
}
|
2014-12-05 05:03:03 +00:00
|
|
|
}
|
2014-09-12 14:53:35 +00:00
|
|
|
|
2014-09-29 19:11:30 +00:00
|
|
|
impl<'tcx> FulfillmentError<'tcx> {
|
2019-12-24 22:38:22 +00:00
|
|
|
fn new(
|
|
|
|
obligation: PredicateObligation<'tcx>,
|
|
|
|
code: FulfillmentErrorCode<'tcx>,
|
|
|
|
) -> FulfillmentError<'tcx> {
|
2019-09-16 22:54:31 +00:00
|
|
|
FulfillmentError { obligation: obligation, code: code, points_at_arg_span: false }
|
2014-09-12 14:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-17 19:16:28 +00:00
|
|
|
|
|
|
|
impl<'tcx> TraitObligation<'tcx> {
|
2015-03-26 19:51:11 +00:00
|
|
|
fn self_ty(&self) -> ty::Binder<Ty<'tcx>> {
|
2018-04-25 02:45:49 +00:00
|
|
|
self.predicate.map_bound(|p| p.self_ty())
|
2014-12-17 19:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-11 14:01:19 +00:00
|
|
|
|
2018-08-30 05:02:42 +00:00
|
|
|
pub fn provide(providers: &mut ty::query::Providers<'_>) {
|
2020-02-10 18:55:49 +00:00
|
|
|
object_safety::provide(providers);
|
2018-06-13 13:44:43 +00:00
|
|
|
*providers = ty::query::Providers {
|
2017-05-11 14:01:19 +00:00
|
|
|
specialization_graph_of: specialize::specialization_graph_provider,
|
2017-08-29 16:25:25 +00:00
|
|
|
specializes: specialize::specializes,
|
2018-05-08 13:10:16 +00:00
|
|
|
codegen_fulfill_obligation: codegen::codegen_fulfill_obligation,
|
2017-10-07 21:55:09 +00:00
|
|
|
vtable_methods,
|
2017-12-27 17:32:44 +00:00
|
|
|
substitute_normalize_and_test_predicates,
|
2017-05-11 14:01:19 +00:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|