2020-03-05 18:07:42 -03:00
|
|
|
//! Trait Resolution. See the [rustc dev guide] for more information on how this works.
|
2018-02-25 15:24:14 -06:00
|
|
|
//!
|
2020-03-09 18:33:04 -03:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
pub mod auto_trait;
|
2020-03-03 11:25:03 -05:00
|
|
|
mod chalk_fulfill;
|
2019-12-24 17:38:22 -05:00
|
|
|
pub mod codegen;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod coherence;
|
2020-11-22 09:37:37 +00:00
|
|
|
pub mod const_evaluatable;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod engine;
|
2019-12-24 17:38:22 -05:00
|
|
|
pub mod error_reporting;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod fulfill;
|
2020-01-07 19:36:50 +01:00
|
|
|
pub mod misc;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod object_safety;
|
|
|
|
mod on_unimplemented;
|
2022-08-22 16:53:34 -03:00
|
|
|
pub mod outlives_bounds;
|
2019-12-24 17:38:22 -05:00
|
|
|
mod project;
|
|
|
|
pub mod query;
|
2021-04-26 19:00:55 -04:00
|
|
|
pub(crate) mod relationships;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod select;
|
|
|
|
mod specialize;
|
2020-01-05 23:02:07 +01:00
|
|
|
mod structural_match;
|
2018-11-27 02:59:49 +00:00
|
|
|
mod util;
|
2020-01-05 20:52:34 +01:00
|
|
|
pub mod wf;
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2022-08-26 13:08:58 -05:00
|
|
|
use crate::errors::DumpVTableEntries;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::infer::outlives::env::OutlivesEnvironment;
|
2022-06-13 01:11:16 -04:00
|
|
|
use crate::infer::{InferCtxt, TyCtxtInferExt};
|
2020-02-22 11:44:18 +01:00
|
|
|
use crate::traits::error_reporting::InferCtxtExt as _;
|
|
|
|
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
2021-08-18 12:45:18 +08:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2022-08-02 06:02:04 +00:00
|
|
|
use rustc_infer::traits::TraitEngineExt as _;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::fold::TypeFoldable;
|
|
|
|
use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
|
2022-06-17 13:15:00 +01:00
|
|
|
use rustc_middle::ty::visit::TypeVisitable;
|
2022-08-07 05:47:32 +00:00
|
|
|
use rustc_middle::ty::{
|
|
|
|
self, DefIdTree, GenericParamDefKind, Subst, ToPredicate, Ty, TyCtxt, TypeSuperVisitable,
|
|
|
|
VtblEntry,
|
|
|
|
};
|
2021-07-17 15:44:19 +08:00
|
|
|
use rustc_span::{sym, Span};
|
2021-06-17 12:20:18 +08:00
|
|
|
use smallvec::SmallVec;
|
2015-09-24 19:58:00 +03:00
|
|
|
|
2018-12-07 01:40:42 +00:00
|
|
|
use std::fmt::Debug;
|
2021-06-17 12:20:18 +08:00
|
|
|
use std::ops::ControlFlow;
|
2018-12-07 01:40:42 +00:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
pub use self::FulfillmentErrorCode::*;
|
2020-05-11 15:25:33 +00:00
|
|
|
pub use self::ImplSource::*;
|
2020-01-06 20:13:24 +01:00
|
|
|
pub use self::ObligationCauseCode::*;
|
|
|
|
pub use self::SelectionError::*;
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2018-11-20 11:20:05 -05:00
|
|
|
pub use self::coherence::{add_placeholder_note, orphan_check, overlapping_impls};
|
|
|
|
pub use self::coherence::{OrphanCheckErr, OverlapResult};
|
2022-07-07 15:12:32 +02:00
|
|
|
pub use self::engine::{ObligationCtxt, TraitEngineExt};
|
2018-03-11 10:29:22 +08:00
|
|
|
pub use self::fulfill::{FulfillmentContext, PendingPredicateObligation};
|
2020-01-05 18:07:29 +01:00
|
|
|
pub use self::object_safety::astconv_object_safety_violations;
|
|
|
|
pub use self::object_safety::is_vtable_safe_method;
|
2014-12-15 21:11:09 -05:00
|
|
|
pub use self::object_safety::MethodViolationCode;
|
2019-12-24 17:38:22 -05:00
|
|
|
pub use self::object_safety::ObjectSafetyViolation;
|
2017-08-30 23:40:43 +03:00
|
|
|
pub use self::on_unimplemented::{OnUnimplementedDirective, OnUnimplementedNote};
|
2020-08-13 20:45:08 +01:00
|
|
|
pub use self::project::{normalize, normalize_projection_type, normalize_to};
|
2020-01-06 20:13:24 +01:00
|
|
|
pub use self::select::{EvaluationCache, SelectionCache, SelectionContext};
|
|
|
|
pub use self::select::{EvaluationResult, IntercrateAmbiguityCause, OverflowError};
|
2018-12-03 23:27:25 +02:00
|
|
|
pub use self::specialize::specialization_graph::FutureCompatOverlapError;
|
|
|
|
pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind;
|
2019-12-24 17:38:22 -05:00
|
|
|
pub use self::specialize::{specialization_graph, translate_substs, OverlapError};
|
2022-07-24 20:44:19 +00:00
|
|
|
pub use self::structural_match::{
|
|
|
|
search_for_adt_const_param_violation, search_for_structural_match_violation,
|
|
|
|
};
|
2021-09-30 21:42:09 +01:00
|
|
|
pub use self::util::{
|
2021-11-13 23:56:22 +01:00
|
|
|
elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span,
|
|
|
|
elaborate_trait_ref, elaborate_trait_refs,
|
2021-09-30 21:42:09 +01:00
|
|
|
};
|
2019-12-24 17:38:22 -05:00
|
|
|
pub use self::util::{expand_trait_aliases, TraitAliasExpander};
|
2020-01-05 20:27:00 +01:00
|
|
|
pub use self::util::{
|
2020-03-29 00:57:49 +01:00
|
|
|
get_vtable_index_of_object_method, impl_item_is_final, predicate_for_trait_def, upcast_choices,
|
2020-01-05 20:27:00 +01:00
|
|
|
};
|
2019-04-29 03:58:24 +01:00
|
|
|
pub use self::util::{
|
2020-12-03 20:10:55 -03:00
|
|
|
supertrait_def_ids, supertraits, transitive_bounds, transitive_bounds_that_define_assoc_type,
|
|
|
|
SupertraitDefIds, Supertraits,
|
2019-04-29 03:58:24 +01:00
|
|
|
};
|
2014-09-12 10:53:35 -04:00
|
|
|
|
2020-03-03 11:25:03 -05:00
|
|
|
pub use self::chalk_fulfill::FulfillmentContext as ChalkFulfillmentContext;
|
|
|
|
|
2020-02-22 11:44:18 +01:00
|
|
|
pub use rustc_infer::traits::*;
|
2018-02-25 10:58:54 -05:00
|
|
|
|
2020-01-24 15:57:01 -05:00
|
|
|
/// Whether to skip the leak check, as part of a future compatibility warning step.
|
2021-11-22 20:17:53 -05:00
|
|
|
///
|
|
|
|
/// The "default" for skip-leak-check corresponds to the current
|
|
|
|
/// behavior (do not skip the leak check) -- not the behavior we are
|
|
|
|
/// transitioning into.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
|
2020-01-24 15:57:01 -05:00
|
|
|
pub enum SkipLeakCheck {
|
|
|
|
Yes,
|
2021-11-22 20:17:53 -05:00
|
|
|
#[default]
|
2020-01-24 15:57:01 -05:00
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SkipLeakCheck {
|
|
|
|
fn is_yes(self) -> bool {
|
|
|
|
self == SkipLeakCheck::Yes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// The mode that trait queries run in.
|
2020-01-16 18:47:52 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2018-04-19 02:28:03 -05:00
|
|
|
pub enum TraitQueryMode {
|
2020-11-27 14:17:25 -05:00
|
|
|
/// Standard/un-canonicalized queries get accurate
|
|
|
|
/// spans etc. passed in and hence can do reasonable
|
|
|
|
/// error reporting on their own.
|
2018-04-19 02:28:03 -05:00
|
|
|
Standard,
|
2020-11-27 14:17:25 -05:00
|
|
|
/// Canonicalized queries get dummy spans and hence
|
|
|
|
/// must generally propagate errors to
|
|
|
|
/// pre-canonicalization callsites.
|
2018-04-19 02:28:03 -05:00
|
|
|
Canonical,
|
|
|
|
}
|
|
|
|
|
2014-12-07 11:10:48 -05:00
|
|
|
/// Creates predicate obligations from the generic bounds.
|
2019-08-25 21:58:59 -07:00
|
|
|
pub fn predicates_for_generics<'tcx>(
|
2022-08-16 06:27:22 +00:00
|
|
|
cause: impl Fn(usize, Span) -> ObligationCause<'tcx>,
|
2019-08-25 21:58:59 -07:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2020-04-18 15:09:28 -07:00
|
|
|
generic_bounds: ty::InstantiatedPredicates<'tcx>,
|
|
|
|
) -> impl Iterator<Item = PredicateObligation<'tcx>> {
|
2022-08-16 06:27:22 +00:00
|
|
|
let generic_bounds = generic_bounds;
|
|
|
|
debug!("predicates_for_generics(generic_bounds={:?})", generic_bounds);
|
|
|
|
|
|
|
|
std::iter::zip(generic_bounds.predicates, generic_bounds.spans).enumerate().map(
|
|
|
|
move |(idx, (predicate, span))| Obligation {
|
|
|
|
cause: cause(idx, span),
|
|
|
|
recursion_depth: 0,
|
|
|
|
param_env: param_env,
|
|
|
|
predicate,
|
|
|
|
},
|
|
|
|
)
|
2014-09-12 10:53:35 -04:00
|
|
|
}
|
|
|
|
|
2014-12-18 09:26:10 -05: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-14 00:48:52 +03:00
|
|
|
pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
2018-09-20 13:56:11 -04:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
span: Span,
|
|
|
|
) -> bool {
|
2019-12-24 17:38:22 -05:00
|
|
|
debug!(
|
|
|
|
"type_known_to_meet_bound_modulo_regions(ty={:?}, bound={:?})",
|
|
|
|
ty,
|
|
|
|
infcx.tcx.def_path_str(def_id)
|
|
|
|
);
|
2016-11-13 19:42:15 -07:00
|
|
|
|
2021-09-15 20:54:50 -04:00
|
|
|
let trait_ref =
|
|
|
|
ty::Binder::dummy(ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) });
|
2016-11-13 19:42:15 -07:00
|
|
|
let obligation = Obligation {
|
2017-05-23 04:19:47 -04:00
|
|
|
param_env,
|
2020-04-12 18:42:45 +01:00
|
|
|
cause: ObligationCause::misc(span, hir::CRATE_HIR_ID),
|
2016-11-13 19:42:15 -07:00
|
|
|
recursion_depth: 0,
|
2020-05-07 10:12:19 +00:00
|
|
|
predicate: trait_ref.without_const().to_predicate(infcx.tcx),
|
2016-11-13 19:42:15 -07:00
|
|
|
};
|
|
|
|
|
2018-09-20 13:56:11 -04:00
|
|
|
let result = infcx.predicate_must_hold_modulo_regions(&obligation);
|
2019-12-24 17:38:22 -05:00
|
|
|
debug!(
|
|
|
|
"type_known_to_meet_ty={:?} bound={} => {:?}",
|
|
|
|
ty,
|
|
|
|
infcx.tcx.def_path_str(def_id),
|
|
|
|
result
|
|
|
|
);
|
2015-10-21 19:01:58 +03:00
|
|
|
|
2020-02-22 14:10:17 +00:00
|
|
|
if result && ty.has_infer_types_or_consts() {
|
2015-10-21 19:01:58 +03: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.
|
|
|
|
|
|
|
|
// 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).
|
2020-04-12 18:42:45 +01:00
|
|
|
let cause = ObligationCause::misc(span, hir::CRATE_HIR_ID);
|
2014-12-18 09:26:10 -05:00
|
|
|
|
2022-08-02 06:02:04 +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.
|
|
|
|
let errors = fully_solve_bound(infcx, cause, param_env, ty, def_id);
|
2015-10-21 19:01:58 +03: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.
|
2022-08-02 06:02:04 +00:00
|
|
|
match &errors[..] {
|
2021-11-08 23:35:23 +08:00
|
|
|
[] => {
|
2019-12-24 17:38:22 -05:00
|
|
|
debug!(
|
|
|
|
"type_known_to_meet_bound_modulo_regions: ty={:?} bound={} success",
|
|
|
|
ty,
|
|
|
|
infcx.tcx.def_path_str(def_id)
|
|
|
|
);
|
2015-10-21 19:01:58 +03:00
|
|
|
true
|
|
|
|
}
|
2021-11-08 23:35:23 +08:00
|
|
|
errors => {
|
2019-12-24 17:38:22 -05:00
|
|
|
debug!(
|
2021-11-08 23:35:23 +08:00
|
|
|
?ty,
|
|
|
|
bound = %infcx.tcx.def_path_str(def_id),
|
|
|
|
?errors,
|
|
|
|
"type_known_to_meet_bound_modulo_regions"
|
2019-12-24 17:38:22 -05:00
|
|
|
);
|
2015-10-21 19:01:58 +03:00
|
|
|
false
|
|
|
|
}
|
2015-01-02 04:01:30 -05:00
|
|
|
}
|
2015-10-21 19:01:58 +03:00
|
|
|
} else {
|
|
|
|
result
|
2015-01-02 04:01:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 17:18:49 +02:00
|
|
|
#[instrument(level = "debug", skip(tcx, elaborated_env))]
|
2019-06-12 00:11:55 +03:00
|
|
|
fn do_normalize_predicates<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
elaborated_env: ty::ParamEnv<'tcx>,
|
|
|
|
predicates: Vec<ty::Predicate<'tcx>>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<Vec<ty::Predicate<'tcx>>, ErrorGuaranteed> {
|
2015-02-20 06:21:46 -05:00
|
|
|
let span = cause.span;
|
2022-07-20 11:40:15 +02: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*.
|
|
|
|
tcx.infer_ctxt().ignoring_regions().enter(|infcx| {
|
2022-07-26 03:24:16 +00:00
|
|
|
let predicates = match fully_normalize(&infcx, cause, elaborated_env, predicates) {
|
|
|
|
Ok(predicates) => predicates,
|
|
|
|
Err(errors) => {
|
|
|
|
let reported = infcx.report_fulfillment_errors(&errors, None, false);
|
|
|
|
return Err(reported);
|
|
|
|
}
|
|
|
|
};
|
2016-03-25 05:22:44 +02:00
|
|
|
|
2018-09-30 20:09:05 +03:00
|
|
|
debug!("do_normalize_predictes: normalized predicates = {:?}", predicates);
|
2016-03-25 05:22:44 +02:00
|
|
|
|
2017-12-01 05:07:52 -05:00
|
|
|
// We can use the `elaborated_env` here; the region code only
|
|
|
|
// cares about declarations like `'a: 'b`.
|
2020-06-21 12:26:17 +02:00
|
|
|
let outlives_env = OutlivesEnvironment::new(elaborated_env);
|
2017-12-01 05:07:52 -05:00
|
|
|
|
2022-07-08 10:59:35 +02:00
|
|
|
// FIXME: It's very weird that we ignore region obligations but apparently
|
|
|
|
// still need to use `resolve_regions` as we need the resolved regions in
|
|
|
|
// the normalized predicates.
|
|
|
|
let errors = infcx.resolve_regions(&outlives_env);
|
|
|
|
if !errors.is_empty() {
|
|
|
|
tcx.sess.delay_span_bug(
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"failed region resolution while normalizing {elaborated_env:?}: {errors:?}"
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2017-12-01 05:07:52 -05:00
|
|
|
|
2022-07-08 10:59:35 +02:00
|
|
|
match infcx.fully_resolve(predicates) {
|
|
|
|
Ok(predicates) => Ok(predicates),
|
2016-03-25 05:22:44 +02:00
|
|
|
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
|
2022-07-08 10:59:35 +02:00
|
|
|
// unconstrained variable.
|
|
|
|
//
|
|
|
|
// @lcnr: Let's still ICE here for now. I want a test case
|
|
|
|
// for that.
|
|
|
|
span_bug!(
|
|
|
|
span,
|
|
|
|
"inference variables in normalized parameter environment: {}",
|
|
|
|
fixup_err
|
|
|
|
);
|
2016-03-25 05:22:44 +02:00
|
|
|
}
|
2018-09-30 20:09:05 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this is gonna need to be removed ...
|
|
|
|
/// Normalizes the parameter environment, reporting errors if they occur.
|
2022-06-27 17:18:49 +02:00
|
|
|
#[instrument(level = "debug", skip(tcx))]
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn normalize_param_env_or_error<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
unnormalized_env: ty::ParamEnv<'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
) -> ty::ParamEnv<'tcx> {
|
2018-09-30 20:09:05 +03: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,
|
2021-10-25 15:43:07 -05:00
|
|
|
// and errors will get reported then; so outside of type inference we
|
2018-09-30 20:09:05 +03:00
|
|
|
// can be sure that no errors should occur.
|
|
|
|
let mut predicates: Vec<_> =
|
2020-07-02 20:52:40 -04:00
|
|
|
util::elaborate_predicates(tcx, unnormalized_env.caller_bounds().into_iter())
|
2020-03-03 15:07:04 -08:00
|
|
|
.map(|obligation| obligation.predicate)
|
|
|
|
.collect();
|
2018-09-30 20:09:05 +03:00
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
debug!("normalize_param_env_or_error: elaborated-predicates={:?}", predicates);
|
2018-09-30 20:09:05 +03:00
|
|
|
|
2021-12-12 12:34:46 +08:00
|
|
|
let elaborated_env = ty::ParamEnv::new(
|
|
|
|
tcx.intern_predicates(&predicates),
|
|
|
|
unnormalized_env.reveal(),
|
|
|
|
unnormalized_env.constness(),
|
|
|
|
);
|
2018-09-30 20:09:05 +03: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 17:38:22 -05:00
|
|
|
let outlives_predicates: Vec<_> = predicates
|
2020-12-24 02:55:21 +01:00
|
|
|
.drain_filter(|predicate| {
|
2021-01-07 11:20:28 -05:00
|
|
|
matches!(predicate.kind().skip_binder(), ty::PredicateKind::TypeOutlives(..))
|
2019-12-24 17:38:22 -05:00
|
|
|
})
|
|
|
|
.collect();
|
2018-09-30 20:09:05 +03:00
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
debug!(
|
|
|
|
"normalize_param_env_or_error: predicates=(non-outlives={:?}, outlives={:?})",
|
|
|
|
predicates, outlives_predicates
|
|
|
|
);
|
2022-02-19 00:48:31 +01:00
|
|
|
let Ok(non_outlives_predicates) = do_normalize_predicates(
|
2019-12-24 17:38:22 -05:00
|
|
|
tcx,
|
|
|
|
cause.clone(),
|
|
|
|
elaborated_env,
|
|
|
|
predicates,
|
2022-02-19 00:48:31 +01:00
|
|
|
) else {
|
2019-12-24 17:38:22 -05:00
|
|
|
// An unnormalized env is better than nothing.
|
2022-02-19 00:48:31 +01:00
|
|
|
debug!("normalize_param_env_or_error: errored resolving non-outlives predicates");
|
|
|
|
return elaborated_env;
|
2019-12-24 17:38:22 -05:00
|
|
|
};
|
2016-05-11 04:14:41 +03:00
|
|
|
|
2018-09-30 20:09:05 +03: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();
|
2021-12-12 12:34:46 +08:00
|
|
|
let outlives_env = ty::ParamEnv::new(
|
|
|
|
tcx.intern_predicates(&outlives_env),
|
|
|
|
unnormalized_env.reveal(),
|
|
|
|
unnormalized_env.constness(),
|
|
|
|
);
|
2022-02-19 00:48:31 +01:00
|
|
|
let Ok(outlives_predicates) = do_normalize_predicates(
|
2019-12-24 17:38:22 -05:00
|
|
|
tcx,
|
|
|
|
cause,
|
|
|
|
outlives_env,
|
|
|
|
outlives_predicates,
|
2022-02-19 00:48:31 +01:00
|
|
|
) else {
|
2019-12-24 17:38:22 -05:00
|
|
|
// An unnormalized env is better than nothing.
|
2022-02-19 00:48:31 +01:00
|
|
|
debug!("normalize_param_env_or_error: errored resolving outlives predicates");
|
|
|
|
return elaborated_env;
|
2019-12-24 17:38:22 -05:00
|
|
|
};
|
2018-09-30 20:09:05 +03:00
|
|
|
debug!("normalize_param_env_or_error: outlives predicates={:?}", outlives_predicates);
|
2015-12-22 19:51:29 -05:00
|
|
|
|
2018-09-30 20:09:05 +03:00
|
|
|
let mut predicates = non_outlives_predicates;
|
|
|
|
predicates.extend(outlives_predicates);
|
|
|
|
debug!("normalize_param_env_or_error: final predicates={:?}", predicates);
|
2021-12-12 12:34:46 +08:00
|
|
|
ty::ParamEnv::new(
|
|
|
|
tcx.intern_predicates(&predicates),
|
|
|
|
unnormalized_env.reveal(),
|
|
|
|
unnormalized_env.constness(),
|
|
|
|
)
|
2015-01-26 14:20:38 -05:00
|
|
|
}
|
|
|
|
|
2022-08-04 13:59:25 +00:00
|
|
|
/// Normalize a type and process all resulting obligations, returning any errors
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn fully_normalize<'a, 'tcx, T>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
2017-11-23 23:03:47 +02:00
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2020-10-24 02:21:18 +02:00
|
|
|
value: T,
|
2019-06-14 01:32:15 +03:00
|
|
|
) -> Result<T, Vec<FulfillmentError<'tcx>>>
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
2017-11-23 23:03:47 +02:00
|
|
|
{
|
|
|
|
debug!("fully_normalize_with_fulfillcx(value={:?})", value);
|
|
|
|
let selcx = &mut SelectionContext::new(infcx);
|
2015-02-13 19:51:43 -05:00
|
|
|
let Normalized { value: normalized_value, obligations } =
|
2017-05-23 04:19:47 -04:00
|
|
|
project::normalize(selcx, param_env, cause, value);
|
2019-12-24 17:38:22 -05:00
|
|
|
debug!(
|
|
|
|
"fully_normalize: normalized_value={:?} obligations={:?}",
|
|
|
|
normalized_value, obligations
|
|
|
|
);
|
2022-07-26 03:24:16 +00:00
|
|
|
|
|
|
|
let mut fulfill_cx = FulfillmentContext::new();
|
2015-02-13 19:51:43 -05:00
|
|
|
for obligation in obligations {
|
2022-07-26 03:24:16 +00:00
|
|
|
fulfill_cx.register_predicate_obligation(infcx, obligation);
|
2015-02-13 19:51:43 -05:00
|
|
|
}
|
2015-07-01 13:08:25 -07:00
|
|
|
|
2015-12-22 19:51:29 -05:00
|
|
|
debug!("fully_normalize: select_all_or_error start");
|
2021-11-08 23:35:23 +08:00
|
|
|
let errors = fulfill_cx.select_all_or_error(infcx);
|
|
|
|
if !errors.is_empty() {
|
|
|
|
return Err(errors);
|
|
|
|
}
|
2015-12-22 19:51:29 -05:00
|
|
|
debug!("fully_normalize: select_all_or_error complete");
|
2020-10-24 02:21:18 +02:00
|
|
|
let resolved_value = infcx.resolve_vars_if_possible(normalized_value);
|
2015-12-22 19:51:29 -05:00
|
|
|
debug!("fully_normalize: resolved_value={:?}", resolved_value);
|
2015-02-13 19:51:43 -05:00
|
|
|
Ok(resolved_value)
|
|
|
|
}
|
|
|
|
|
2022-08-04 13:59:25 +00:00
|
|
|
/// Process an obligation (and any nested obligations that come from it) to
|
|
|
|
/// completion, returning any errors
|
2022-08-02 06:02:04 +00:00
|
|
|
pub fn fully_solve_obligation<'a, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
obligation: PredicateObligation<'tcx>,
|
|
|
|
) -> Vec<FulfillmentError<'tcx>> {
|
|
|
|
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
|
|
|
|
engine.register_predicate_obligation(infcx, obligation);
|
|
|
|
engine.select_all_or_error(infcx)
|
|
|
|
}
|
|
|
|
|
2022-08-04 13:59:25 +00:00
|
|
|
/// Process a set of obligations (and any nested obligations that come from them)
|
|
|
|
/// to completion
|
2022-08-02 06:02:04 +00:00
|
|
|
pub fn fully_solve_obligations<'a, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
|
|
|
|
) -> Vec<FulfillmentError<'tcx>> {
|
|
|
|
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
|
|
|
|
engine.register_predicate_obligations(infcx, obligations);
|
|
|
|
engine.select_all_or_error(infcx)
|
|
|
|
}
|
|
|
|
|
2022-08-04 13:59:25 +00:00
|
|
|
/// Process a bound (and any nested obligations that come from it) to completion.
|
|
|
|
/// This is a convenience function for traits that have no generic arguments, such
|
|
|
|
/// as auto traits, and builtin traits like Copy or Sized.
|
2022-08-02 06:02:04 +00:00
|
|
|
pub fn fully_solve_bound<'a, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
bound: DefId,
|
|
|
|
) -> Vec<FulfillmentError<'tcx>> {
|
|
|
|
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
|
|
|
|
engine.register_bound(infcx, param_env, ty, bound, cause);
|
|
|
|
engine.select_all_or_error(infcx)
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:22:45 +01:00
|
|
|
/// Normalizes the predicates and checks whether they hold in an empty environment. If this
|
|
|
|
/// returns true, then either normalize encountered an error or one of the predicates did not
|
|
|
|
/// hold. Used when creating vtables to check for unsatisfiable methods.
|
|
|
|
pub fn impossible_predicates<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
predicates: Vec<ty::Predicate<'tcx>>,
|
|
|
|
) -> bool {
|
2020-06-22 13:22:45 +01:00
|
|
|
debug!("impossible_predicates(predicates={:?})", predicates);
|
2016-09-19 12:47:47 +03:00
|
|
|
|
2017-06-21 14:50:43 +03:00
|
|
|
let result = tcx.infer_ctxt().enter(|infcx| {
|
2018-02-10 13:18:02 -05:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2022-07-26 04:01:34 +00:00
|
|
|
let ocx = ObligationCtxt::new(&infcx);
|
|
|
|
let predicates = ocx.normalize(ObligationCause::dummy(), param_env, predicates);
|
2016-09-19 12:47:47 +03:00
|
|
|
for predicate in predicates {
|
2022-07-26 04:01:34 +00:00
|
|
|
let obligation = Obligation::new(ObligationCause::dummy(), param_env, predicate);
|
|
|
|
ocx.register_obligation(obligation);
|
2016-09-19 12:47:47 +03:00
|
|
|
}
|
2022-07-26 04:01:34 +00:00
|
|
|
let errors = ocx.select_all_or_error();
|
2021-11-08 23:35:23 +08:00
|
|
|
|
2022-04-30 20:03:27 +02:00
|
|
|
// Clean up after ourselves
|
|
|
|
let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
|
|
|
|
|
2021-11-08 23:35:23 +08:00
|
|
|
!errors.is_empty()
|
2017-06-21 14:50:43 +03:00
|
|
|
});
|
2020-10-24 02:21:18 +02:00
|
|
|
debug!("impossible_predicates = {:?}", result);
|
2017-06-21 14:50:43 +03:00
|
|
|
result
|
2016-09-19 12:47:47 +03:00
|
|
|
}
|
|
|
|
|
2020-06-22 13:22:45 +01:00
|
|
|
fn subst_and_check_impossible_predicates<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-01-16 18:47:52 -05:00
|
|
|
key: (DefId, SubstsRef<'tcx>),
|
2019-06-12 00:11:55 +03:00
|
|
|
) -> bool {
|
2020-06-22 13:22:45 +01:00
|
|
|
debug!("subst_and_check_impossible_predicates(key={:?})", key);
|
2017-12-27 12:32:44 -05:00
|
|
|
|
2020-06-22 13:22:45 +01:00
|
|
|
let mut predicates = tcx.predicates_of(key.0).instantiate(tcx, key.1).predicates;
|
2022-05-07 10:24:45 +02:00
|
|
|
|
|
|
|
// Specifically check trait fulfillment to avoid an error when trying to resolve
|
|
|
|
// associated items.
|
|
|
|
if let Some(trait_def_id) = tcx.trait_of_item(key.0) {
|
|
|
|
let trait_ref = ty::TraitRef::from_method(tcx, trait_def_id, key.1);
|
|
|
|
predicates.push(ty::Binder::dummy(trait_ref).to_poly_trait_predicate().to_predicate(tcx));
|
|
|
|
}
|
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
predicates.retain(|predicate| !predicate.needs_subst());
|
2020-06-22 13:22:45 +01:00
|
|
|
let result = impossible_predicates(tcx, predicates);
|
2017-12-27 12:32:44 -05:00
|
|
|
|
2020-06-22 13:22:45 +01:00
|
|
|
debug!("subst_and_check_impossible_predicates(key={:?}) = {:?}", key, result);
|
2017-12-27 12:32:44 -05:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:47:32 +00:00
|
|
|
/// Checks whether a trait's method is impossible to call on a given impl.
|
|
|
|
///
|
|
|
|
/// This only considers predicates that reference the impl's generics, and not
|
|
|
|
/// those that reference the method's generics.
|
|
|
|
fn is_impossible_method<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
(impl_def_id, trait_item_def_id): (DefId, DefId),
|
|
|
|
) -> bool {
|
|
|
|
struct ReferencesOnlyParentGenerics<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
generics: &'tcx ty::Generics,
|
|
|
|
trait_item_def_id: DefId,
|
|
|
|
}
|
|
|
|
impl<'tcx> ty::TypeVisitor<'tcx> for ReferencesOnlyParentGenerics<'tcx> {
|
|
|
|
type BreakTy = ();
|
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
// If this is a parameter from the trait item's own generics, then bail
|
|
|
|
if let ty::Param(param) = t.kind()
|
|
|
|
&& let param_def_id = self.generics.type_param(param, self.tcx).def_id
|
|
|
|
&& self.tcx.parent(param_def_id) == self.trait_item_def_id
|
|
|
|
{
|
|
|
|
return ControlFlow::BREAK;
|
|
|
|
}
|
|
|
|
t.super_visit_with(self)
|
|
|
|
}
|
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
if let ty::ReEarlyBound(param) = r.kind()
|
|
|
|
&& let param_def_id = self.generics.region_param(¶m, self.tcx).def_id
|
|
|
|
&& self.tcx.parent(param_def_id) == self.trait_item_def_id
|
|
|
|
{
|
|
|
|
return ControlFlow::BREAK;
|
|
|
|
}
|
|
|
|
r.super_visit_with(self)
|
|
|
|
}
|
|
|
|
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
if let ty::ConstKind::Param(param) = ct.kind()
|
|
|
|
&& let param_def_id = self.generics.const_param(¶m, self.tcx).def_id
|
|
|
|
&& self.tcx.parent(param_def_id) == self.trait_item_def_id
|
|
|
|
{
|
|
|
|
return ControlFlow::BREAK;
|
|
|
|
}
|
|
|
|
ct.super_visit_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let generics = tcx.generics_of(trait_item_def_id);
|
|
|
|
let predicates = tcx.predicates_of(trait_item_def_id);
|
|
|
|
let impl_trait_ref =
|
|
|
|
tcx.impl_trait_ref(impl_def_id).expect("expected impl to correspond to trait");
|
|
|
|
let param_env = tcx.param_env(impl_def_id);
|
|
|
|
|
|
|
|
let mut visitor = ReferencesOnlyParentGenerics { tcx, generics, trait_item_def_id };
|
|
|
|
let predicates_for_trait = predicates.predicates.iter().filter_map(|(pred, span)| {
|
|
|
|
if pred.visit_with(&mut visitor).is_continue() {
|
|
|
|
Some(Obligation::new(
|
|
|
|
ObligationCause::dummy_with_span(*span),
|
|
|
|
param_env,
|
|
|
|
ty::EarlyBinder(*pred).subst(tcx, impl_trait_ref.substs),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
tcx.infer_ctxt().ignoring_regions().enter(|ref infcx| {
|
2022-08-18 01:12:46 +00:00
|
|
|
for obligation in predicates_for_trait {
|
|
|
|
// Ignore overflow error, to be conservative.
|
|
|
|
if let Ok(result) = infcx.evaluate_obligation(&obligation)
|
|
|
|
&& !result.may_apply()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2022-08-07 05:47:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
enum VtblSegment<'tcx> {
|
|
|
|
MetadataDSA,
|
|
|
|
TraitOwnEntries { trait_ref: ty::PolyTraitRef<'tcx>, emit_vptr: bool },
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare the segments for a vtable
|
|
|
|
fn prepare_vtable_segments<'tcx, T>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
mut segment_visitor: impl FnMut(VtblSegment<'tcx>) -> ControlFlow<T>,
|
|
|
|
) -> Option<T> {
|
|
|
|
// The following constraints holds for the final arrangement.
|
|
|
|
// 1. The whole virtual table of the first direct super trait is included as the
|
|
|
|
// the prefix. If this trait doesn't have any super traits, then this step
|
|
|
|
// consists of the dsa metadata.
|
|
|
|
// 2. Then comes the proper pointer metadata(vptr) and all own methods for all
|
|
|
|
// other super traits except those already included as part of the first
|
|
|
|
// direct super trait virtual table.
|
|
|
|
// 3. finally, the own methods of this trait.
|
|
|
|
|
|
|
|
// This has the advantage that trait upcasting to the first direct super trait on each level
|
|
|
|
// is zero cost, and to another trait includes only replacing the pointer with one level indirection,
|
|
|
|
// while not using too much extra memory.
|
|
|
|
|
|
|
|
// For a single inheritance relationship like this,
|
|
|
|
// D --> C --> B --> A
|
|
|
|
// The resulting vtable will consists of these segments:
|
|
|
|
// DSA, A, B, C, D
|
|
|
|
|
|
|
|
// For a multiple inheritance relationship like this,
|
|
|
|
// D --> C --> A
|
|
|
|
// \-> B
|
|
|
|
// The resulting vtable will consists of these segments:
|
|
|
|
// DSA, A, B, B-vptr, C, D
|
|
|
|
|
|
|
|
// For a diamond inheritance relationship like this,
|
|
|
|
// D --> B --> A
|
|
|
|
// \-> C -/
|
|
|
|
// The resulting vtable will consists of these segments:
|
|
|
|
// DSA, A, B, C, C-vptr, D
|
|
|
|
|
|
|
|
// For a more complex inheritance relationship like this:
|
|
|
|
// O --> G --> C --> A
|
|
|
|
// \ \ \-> B
|
|
|
|
// | |-> F --> D
|
|
|
|
// | \-> E
|
|
|
|
// |-> N --> J --> H
|
|
|
|
// \ \-> I
|
|
|
|
// |-> M --> K
|
|
|
|
// \-> L
|
|
|
|
// The resulting vtable will consists of these segments:
|
|
|
|
// DSA, A, B, B-vptr, C, D, D-vptr, E, E-vptr, F, F-vptr, G,
|
|
|
|
// H, H-vptr, I, I-vptr, J, J-vptr, K, K-vptr, L, L-vptr, M, M-vptr,
|
|
|
|
// N, N-vptr, O
|
|
|
|
|
|
|
|
// emit dsa segment first.
|
|
|
|
if let ControlFlow::Break(v) = (segment_visitor)(VtblSegment::MetadataDSA) {
|
|
|
|
return Some(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut emit_vptr_on_new_entry = false;
|
|
|
|
let mut visited = util::PredicateSet::new(tcx);
|
|
|
|
let predicate = trait_ref.without_const().to_predicate(tcx);
|
|
|
|
let mut stack: SmallVec<[(ty::PolyTraitRef<'tcx>, _, _); 5]> =
|
|
|
|
smallvec![(trait_ref, emit_vptr_on_new_entry, None)];
|
|
|
|
visited.insert(predicate);
|
|
|
|
|
|
|
|
// the main traversal loop:
|
|
|
|
// basically we want to cut the inheritance directed graph into a few non-overlapping slices of nodes
|
2022-03-16 20:12:30 +08:00
|
|
|
// that each node is emitted after all its descendents have been emitted.
|
2022-03-30 15:14:15 -04:00
|
|
|
// so we convert the directed graph into a tree by skipping all previously visited nodes using a visited set.
|
2021-06-17 12:20:18 +08:00
|
|
|
// this is done on the fly.
|
|
|
|
// Each loop run emits a slice - it starts by find a "childless" unvisited node, backtracking upwards, and it
|
|
|
|
// stops after it finds a node that has a next-sibling node.
|
|
|
|
// This next-sibling node will used as the starting point of next slice.
|
|
|
|
|
|
|
|
// Example:
|
|
|
|
// For a diamond inheritance relationship like this,
|
|
|
|
// D#1 --> B#0 --> A#0
|
|
|
|
// \-> C#1 -/
|
|
|
|
|
|
|
|
// Starting point 0 stack [D]
|
|
|
|
// Loop run #0: Stack after diving in is [D B A], A is "childless"
|
|
|
|
// after this point, all newly visited nodes won't have a vtable that equals to a prefix of this one.
|
2022-03-16 20:12:30 +08:00
|
|
|
// Loop run #0: Emitting the slice [B A] (in reverse order), B has a next-sibling node, so this slice stops here.
|
2021-06-17 12:20:18 +08:00
|
|
|
// Loop run #0: Stack after exiting out is [D C], C is the next starting point.
|
|
|
|
// Loop run #1: Stack after diving in is [D C], C is "childless", since its child A is skipped(already emitted).
|
2022-03-16 20:12:30 +08:00
|
|
|
// Loop run #1: Emitting the slice [D C] (in reverse order). No one has a next-sibling node.
|
2021-06-17 12:20:18 +08:00
|
|
|
// Loop run #1: Stack after exiting out is []. Now the function exits.
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// dive deeper into the stack, recording the path
|
|
|
|
'diving_in: loop {
|
|
|
|
if let Some((inner_most_trait_ref, _, _)) = stack.last() {
|
|
|
|
let inner_most_trait_ref = *inner_most_trait_ref;
|
|
|
|
let mut direct_super_traits_iter = tcx
|
|
|
|
.super_predicates_of(inner_most_trait_ref.def_id())
|
|
|
|
.predicates
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(move |(pred, _)| {
|
2021-12-12 12:34:46 +08:00
|
|
|
pred.subst_supertrait(tcx, &inner_most_trait_ref).to_opt_poly_trait_pred()
|
2021-06-17 12:20:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
'diving_in_skip_visited_traits: loop {
|
|
|
|
if let Some(next_super_trait) = direct_super_traits_iter.next() {
|
|
|
|
if visited.insert(next_super_trait.to_predicate(tcx)) {
|
2021-12-12 12:34:46 +08:00
|
|
|
// We're throwing away potential constness of super traits here.
|
|
|
|
// FIXME: handle ~const super traits
|
|
|
|
let next_super_trait = next_super_trait.map_bound(|t| t.trait_ref);
|
2021-06-17 12:20:18 +08:00
|
|
|
stack.push((
|
2021-12-12 12:34:46 +08:00
|
|
|
next_super_trait,
|
2021-06-17 12:20:18 +08:00
|
|
|
emit_vptr_on_new_entry,
|
|
|
|
Some(direct_super_traits_iter),
|
|
|
|
));
|
|
|
|
break 'diving_in_skip_visited_traits;
|
|
|
|
} else {
|
|
|
|
continue 'diving_in_skip_visited_traits;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break 'diving_in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other than the left-most path, vptr should be emitted for each trait.
|
|
|
|
emit_vptr_on_new_entry = true;
|
|
|
|
|
|
|
|
// emit innermost item, move to next sibling and stop there if possible, otherwise jump to outer level.
|
|
|
|
'exiting_out: loop {
|
|
|
|
if let Some((inner_most_trait_ref, emit_vptr, siblings_opt)) = stack.last_mut() {
|
|
|
|
if let ControlFlow::Break(v) = (segment_visitor)(VtblSegment::TraitOwnEntries {
|
|
|
|
trait_ref: *inner_most_trait_ref,
|
|
|
|
emit_vptr: *emit_vptr,
|
|
|
|
}) {
|
|
|
|
return Some(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
'exiting_out_skip_visited_traits: loop {
|
|
|
|
if let Some(siblings) = siblings_opt {
|
|
|
|
if let Some(next_inner_most_trait_ref) = siblings.next() {
|
|
|
|
if visited.insert(next_inner_most_trait_ref.to_predicate(tcx)) {
|
2021-12-12 12:34:46 +08:00
|
|
|
// We're throwing away potential constness of super traits here.
|
|
|
|
// FIXME: handle ~const super traits
|
|
|
|
let next_inner_most_trait_ref =
|
|
|
|
next_inner_most_trait_ref.map_bound(|t| t.trait_ref);
|
|
|
|
*inner_most_trait_ref = next_inner_most_trait_ref;
|
2021-06-17 12:20:18 +08:00
|
|
|
*emit_vptr = emit_vptr_on_new_entry;
|
|
|
|
break 'exiting_out;
|
|
|
|
} else {
|
|
|
|
continue 'exiting_out_skip_visited_traits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stack.pop();
|
|
|
|
continue 'exiting_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// all done
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 17:23:37 +08:00
|
|
|
fn dump_vtable_entries<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
sp: Span,
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
entries: &[VtblEntry<'tcx>],
|
|
|
|
) {
|
2022-08-26 13:08:58 -05:00
|
|
|
tcx.sess.emit_err(DumpVTableEntries {
|
|
|
|
span: sp,
|
|
|
|
trait_ref,
|
|
|
|
entries: format!("{:#?}", entries),
|
|
|
|
});
|
2021-07-17 15:44:19 +08:00
|
|
|
}
|
|
|
|
|
2021-09-01 23:04:28 +01:00
|
|
|
fn own_existential_vtable_entries<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
|
|
|
|
) -> &'tcx [DefId] {
|
|
|
|
let trait_methods = tcx
|
|
|
|
.associated_items(trait_ref.def_id())
|
|
|
|
.in_definition_order()
|
|
|
|
.filter(|item| item.kind == ty::AssocKind::Fn);
|
|
|
|
// Now list each method's DefId (for within its trait).
|
|
|
|
let own_entries = trait_methods.filter_map(move |trait_method| {
|
|
|
|
debug!("own_existential_vtable_entry: trait_method={:?}", trait_method);
|
|
|
|
let def_id = trait_method.def_id;
|
|
|
|
|
|
|
|
// Some methods cannot be called on an object; skip those.
|
|
|
|
if !is_vtable_safe_method(tcx, trait_ref.def_id(), &trait_method) {
|
|
|
|
debug!("own_existential_vtable_entry: not vtable safe");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(def_id)
|
|
|
|
});
|
|
|
|
|
|
|
|
tcx.arena.alloc_from_iter(own_entries.into_iter())
|
|
|
|
}
|
|
|
|
|
2016-09-19 12:47:47 +03:00
|
|
|
/// Given a trait `trait_ref`, iterates the vtable entries
|
|
|
|
/// that come from `trait_ref`, including its supertraits.
|
2021-06-14 18:02:53 +08:00
|
|
|
fn vtable_entries<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
2021-06-14 18:02:53 +08:00
|
|
|
) -> &'tcx [VtblEntry<'tcx>] {
|
|
|
|
debug!("vtable_entries({:?})", trait_ref);
|
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
let mut entries = vec![];
|
2017-10-09 10:39:53 -05:00
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
let vtable_segment_callback = |segment| -> ControlFlow<()> {
|
|
|
|
match segment {
|
|
|
|
VtblSegment::MetadataDSA => {
|
2022-05-26 20:22:28 -07:00
|
|
|
entries.extend(TyCtxt::COMMON_VTABLE_ENTRIES);
|
2021-06-17 12:20:18 +08:00
|
|
|
}
|
|
|
|
VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
|
2021-09-01 23:04:28 +01:00
|
|
|
let existential_trait_ref = trait_ref
|
|
|
|
.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
|
|
|
|
|
|
|
|
// Lookup the shape of vtable for the trait.
|
|
|
|
let own_existential_entries =
|
|
|
|
tcx.own_existential_vtable_entries(existential_trait_ref);
|
|
|
|
|
|
|
|
let own_entries = own_existential_entries.iter().copied().map(|def_id| {
|
|
|
|
debug!("vtable_entries: trait_method={:?}", def_id);
|
2021-06-17 12:20:18 +08: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);
|
|
|
|
if impossible_predicates(tcx, predicates.predicates) {
|
|
|
|
debug!("vtable_entries: predicates do not hold");
|
2021-09-01 23:04:28 +01:00
|
|
|
return VtblEntry::Vacant;
|
2021-06-17 12:20:18 +08:00
|
|
|
}
|
|
|
|
|
2021-07-18 17:23:37 +08:00
|
|
|
let instance = ty::Instance::resolve_for_vtable(
|
|
|
|
tcx,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
def_id,
|
|
|
|
substs,
|
|
|
|
)
|
|
|
|
.expect("resolution failed during building vtable representation");
|
2021-09-01 23:04:28 +01:00
|
|
|
VtblEntry::Method(instance)
|
2021-06-14 18:02:53 +08:00
|
|
|
});
|
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
entries.extend(own_entries);
|
|
|
|
|
|
|
|
if emit_vptr {
|
|
|
|
entries.push(VtblEntry::TraitVPtr(trait_ref));
|
2021-06-14 18:02:53 +08:00
|
|
|
}
|
2021-06-17 12:20:18 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-09 10:39:53 -05:00
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
ControlFlow::Continue(())
|
|
|
|
};
|
2021-06-14 18:02:53 +08:00
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
let _ = prepare_vtable_segments(tcx, trait_ref, vtable_segment_callback);
|
2021-07-17 15:44:19 +08:00
|
|
|
|
|
|
|
if tcx.has_attr(trait_ref.def_id(), sym::rustc_dump_vtable) {
|
2021-07-18 17:23:37 +08:00
|
|
|
let sp = tcx.def_span(trait_ref.def_id());
|
|
|
|
dump_vtable_entries(tcx, sp, trait_ref, &entries);
|
2021-07-17 15:44:19 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
tcx.arena.alloc_from_iter(entries.into_iter())
|
2021-06-14 18:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Find slot base for trait methods within vtable entries of another trait
|
|
|
|
fn vtable_trait_first_method_offset<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
key: (
|
|
|
|
ty::PolyTraitRef<'tcx>, // trait_to_be_found
|
|
|
|
ty::PolyTraitRef<'tcx>, // trait_owning_vtable
|
|
|
|
),
|
|
|
|
) -> usize {
|
|
|
|
let (trait_to_be_found, trait_owning_vtable) = key;
|
|
|
|
|
2021-11-03 23:37:50 +08:00
|
|
|
// #90177
|
|
|
|
let trait_to_be_found_erased = tcx.erase_regions(trait_to_be_found);
|
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
let vtable_segment_callback = {
|
|
|
|
let mut vtable_base = 0;
|
|
|
|
|
|
|
|
move |segment| {
|
|
|
|
match segment {
|
|
|
|
VtblSegment::MetadataDSA => {
|
2022-05-26 20:22:28 -07:00
|
|
|
vtable_base += TyCtxt::COMMON_VTABLE_ENTRIES.len();
|
2021-06-17 12:20:18 +08:00
|
|
|
}
|
|
|
|
VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
|
2021-11-03 23:37:50 +08:00
|
|
|
if tcx.erase_regions(trait_ref) == trait_to_be_found_erased {
|
2021-06-17 12:20:18 +08:00
|
|
|
return ControlFlow::Break(vtable_base);
|
|
|
|
}
|
|
|
|
vtable_base += util::count_own_vtable_entries(tcx, trait_ref);
|
|
|
|
if emit_vptr {
|
|
|
|
vtable_base += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
|
|
|
};
|
2021-06-14 18:02:53 +08:00
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
if let Some(vtable_base) =
|
|
|
|
prepare_vtable_segments(tcx, trait_owning_vtable, vtable_segment_callback)
|
|
|
|
{
|
|
|
|
vtable_base
|
|
|
|
} else {
|
|
|
|
bug!("Failed to find info for expected trait in vtable");
|
|
|
|
}
|
|
|
|
}
|
2021-06-14 18:02:53 +08:00
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
/// Find slot offset for trait vptr within vtable entries of another trait
|
2021-12-14 09:44:49 +00:00
|
|
|
pub fn vtable_trait_upcasting_coercion_new_vptr_slot<'tcx>(
|
2021-06-17 12:20:18 +08:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
key: (
|
2021-08-18 12:45:18 +08:00
|
|
|
Ty<'tcx>, // trait object type whose trait owning vtable
|
|
|
|
Ty<'tcx>, // trait object for supertrait
|
2021-06-17 12:20:18 +08:00
|
|
|
),
|
|
|
|
) -> Option<usize> {
|
2021-08-18 12:45:18 +08:00
|
|
|
let (source, target) = key;
|
2021-08-20 02:37:00 +08:00
|
|
|
assert!(matches!(&source.kind(), &ty::Dynamic(..)) && !source.needs_infer());
|
|
|
|
assert!(matches!(&target.kind(), &ty::Dynamic(..)) && !target.needs_infer());
|
2021-06-17 12:20:18 +08:00
|
|
|
|
2021-08-18 12:45:18 +08:00
|
|
|
// this has been typecked-before, so diagnostics is not really needed.
|
|
|
|
let unsize_trait_did = tcx.require_lang_item(LangItem::Unsize, None);
|
2021-06-17 12:20:18 +08:00
|
|
|
|
2021-08-18 12:45:18 +08:00
|
|
|
let trait_ref = ty::TraitRef {
|
|
|
|
def_id: unsize_trait_did,
|
|
|
|
substs: tcx.mk_substs_trait(source, &[target.into()]),
|
2021-06-17 12:20:18 +08:00
|
|
|
};
|
2021-08-18 12:45:18 +08:00
|
|
|
let obligation = Obligation::new(
|
|
|
|
ObligationCause::dummy(),
|
|
|
|
ty::ParamEnv::reveal_all(),
|
2021-08-27 08:09:00 +00:00
|
|
|
ty::Binder::dummy(ty::TraitPredicate {
|
|
|
|
trait_ref,
|
|
|
|
constness: ty::BoundConstness::NotConst,
|
2021-10-11 18:10:35 -03:00
|
|
|
polarity: ty::ImplPolarity::Positive,
|
2021-08-27 08:09:00 +00:00
|
|
|
}),
|
2021-08-18 12:45:18 +08:00
|
|
|
);
|
2021-06-17 12:20:18 +08:00
|
|
|
|
2021-08-18 12:45:18 +08:00
|
|
|
let implsrc = tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
let mut selcx = SelectionContext::new(&infcx);
|
|
|
|
selcx.select(&obligation).unwrap()
|
|
|
|
});
|
|
|
|
|
2022-02-19 00:48:31 +01:00
|
|
|
let Some(ImplSource::TraitUpcasting(implsrc_traitcasting)) = implsrc else {
|
|
|
|
bug!();
|
2021-08-18 12:45:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
implsrc_traitcasting.vtable_vptr_slot
|
2016-09-19 12:47:47 +03:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
2020-02-10 19:55:49 +01:00
|
|
|
object_safety::provide(providers);
|
2020-05-13 13:40:22 -07:00
|
|
|
structural_match::provide(providers);
|
2018-06-13 16:44:43 +03:00
|
|
|
*providers = ty::query::Providers {
|
2017-05-11 16:01:19 +02:00
|
|
|
specialization_graph_of: specialize::specialization_graph_provider,
|
2017-08-29 09:25:25 -07:00
|
|
|
specializes: specialize::specializes,
|
2018-05-08 16:10:16 +03:00
|
|
|
codegen_fulfill_obligation: codegen::codegen_fulfill_obligation,
|
2021-09-01 23:04:28 +01:00
|
|
|
own_existential_vtable_entries,
|
2021-06-14 18:02:53 +08:00
|
|
|
vtable_entries,
|
2021-07-31 22:46:23 +08:00
|
|
|
vtable_trait_upcasting_coercion_new_vptr_slot,
|
2020-06-22 13:22:45 +01:00
|
|
|
subst_and_check_impossible_predicates,
|
2022-08-07 05:47:32 +00:00
|
|
|
is_impossible_method,
|
2022-03-22 10:38:46 +01:00
|
|
|
try_unify_abstract_consts: |tcx, param_env_and| {
|
|
|
|
let (param_env, (a, b)) = param_env_and.into_parts();
|
2022-07-12 07:11:05 +00:00
|
|
|
const_evaluatable::try_unify_abstract_consts(tcx, (a, b), param_env)
|
2022-03-22 10:38:46 +01:00
|
|
|
},
|
2017-05-11 16:01:19 +02:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|