2023-03-15 13:00:15 +00:00
|
|
|
use super::NormalizeExt;
|
2023-03-14 13:19:06 +00:00
|
|
|
use super::{ObligationCause, PredicateObligation, SelectionContext};
|
2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2023-03-15 13:00:15 +00:00
|
|
|
use rustc_errors::Diagnostic;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2023-03-15 13:00:15 +00:00
|
|
|
use rustc_infer::infer::InferOk;
|
2023-07-11 21:35:29 +00:00
|
|
|
use rustc_middle::ty::GenericArgsRef;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, ImplSubject, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
|
2023-03-15 13:00:15 +00:00
|
|
|
use rustc_span::Span;
|
|
|
|
use smallvec::SmallVec;
|
2014-09-12 14:53:35 +00:00
|
|
|
|
2021-10-05 23:04:09 +00:00
|
|
|
pub use rustc_infer::traits::{self, util::*};
|
|
|
|
|
2019-03-26 17:34:32 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2019-04-29 02:58:24 +00:00
|
|
|
// `TraitAliasExpander` iterator
|
2019-03-26 17:34:32 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-04-29 02:58:24 +00:00
|
|
|
/// "Trait alias expansion" is the process of expanding a sequence of trait
|
2019-03-26 17:34:32 +00:00
|
|
|
/// references into another sequence by transitively following all trait
|
|
|
|
/// aliases. e.g. If you have bounds like `Foo + Send`, a trait alias
|
|
|
|
/// `trait Foo = Bar + Sync;`, and another trait alias
|
|
|
|
/// `trait Bar = Read + Write`, then the bounds would expand to
|
|
|
|
/// `Read + Write + Sync + Send`.
|
2019-05-02 01:21:20 +00:00
|
|
|
/// Expansion is done via a DFS (depth-first search), and the `visited` field
|
|
|
|
/// is used to avoid cycles.
|
2019-06-13 21:48:52 +00:00
|
|
|
pub struct TraitAliasExpander<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-04-29 02:58:24 +00:00
|
|
|
stack: Vec<TraitAliasExpansionInfo<'tcx>>,
|
2019-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 01:21:20 +00:00
|
|
|
/// Stores information about the expansion of a trait via a path of zero or more trait aliases.
|
2019-03-26 17:34:32 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-04-29 02:58:24 +00:00
|
|
|
pub struct TraitAliasExpansionInfo<'tcx> {
|
2019-05-02 17:03:29 +00:00
|
|
|
pub path: SmallVec<[(ty::PolyTraitRef<'tcx>, Span); 4]>,
|
2019-04-29 02:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TraitAliasExpansionInfo<'tcx> {
|
2019-05-02 01:21:20 +00:00
|
|
|
fn new(trait_ref: ty::PolyTraitRef<'tcx>, span: Span) -> Self {
|
2019-05-02 17:03:29 +00:00
|
|
|
Self { path: smallvec![(trait_ref, span)] }
|
2019-04-29 02:58:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 18:51:33 +00:00
|
|
|
/// Adds diagnostic labels to `diag` for the expansion path of a trait through all intermediate
|
|
|
|
/// trait aliases.
|
Use `Cow` in `{D,Subd}iagnosticMessage`.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment:
```
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
```
This commit answers that question in the affirmative. It's not the most
compelling change ever, but it might be worth merging.
This requires changing the `impl<'a> From<&'a str>` impls to `impl
From<&'static str>`, which involves a bunch of knock-on changes that
require/result in call sites being a little more precise about exactly
what kind of string they use to create errors, and not just `&str`. This
will result in fewer unnecessary allocations, though this will not have
any notable perf effects given that these are error paths.
Note that I was lazy within Clippy, using `to_string` in a few places to
preserve the existing string imprecision. I could have used `impl
Into<{D,Subd}iagnosticMessage>` in various places as is done in the
compiler, but that would have required changes to *many* call sites
(mostly changing `&format("...")` to `format!("...")`) which didn't seem
worthwhile.
2023-05-04 00:55:21 +00:00
|
|
|
pub fn label_with_exp_info(
|
|
|
|
&self,
|
|
|
|
diag: &mut Diagnostic,
|
|
|
|
top_label: &'static str,
|
|
|
|
use_desc: &str,
|
|
|
|
) {
|
2019-05-13 18:51:33 +00:00
|
|
|
diag.span_label(self.top().1, top_label);
|
|
|
|
if self.path.len() > 1 {
|
|
|
|
for (_, sp) in self.path.iter().rev().skip(1).take(self.path.len() - 2) {
|
|
|
|
diag.span_label(*sp, format!("referenced here ({})", use_desc));
|
|
|
|
}
|
2019-04-29 02:58:24 +00:00
|
|
|
}
|
2020-08-14 19:08:49 +00:00
|
|
|
if self.top().1 != self.bottom().1 {
|
|
|
|
// When the trait object is in a return type these two spans match, we don't want
|
|
|
|
// redundant labels.
|
|
|
|
diag.span_label(
|
|
|
|
self.bottom().1,
|
|
|
|
format!("trait alias used in trait object type ({})", use_desc),
|
|
|
|
);
|
|
|
|
}
|
2019-04-29 02:58:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:20:17 +00:00
|
|
|
pub fn trait_ref(&self) -> ty::PolyTraitRef<'tcx> {
|
|
|
|
self.top().0
|
2019-04-29 02:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn top(&self) -> &(ty::PolyTraitRef<'tcx>, Span) {
|
2019-05-02 17:03:29 +00:00
|
|
|
self.path.last().unwrap()
|
2019-04-29 02:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bottom(&self) -> &(ty::PolyTraitRef<'tcx>, Span) {
|
2019-05-02 17:03:29 +00:00
|
|
|
self.path.first().unwrap()
|
2019-04-29 02:58:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 18:51:33 +00:00
|
|
|
fn clone_and_push(&self, trait_ref: ty::PolyTraitRef<'tcx>, span: Span) -> Self {
|
|
|
|
let mut path = self.path.clone();
|
|
|
|
path.push((trait_ref, span));
|
2019-04-29 02:58:24 +00:00
|
|
|
|
2019-05-13 18:51:33 +00:00
|
|
|
Self { path }
|
2019-04-29 02:58:24 +00:00
|
|
|
}
|
2019-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn expand_trait_aliases<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-18 22:09:28 +00:00
|
|
|
trait_refs: impl Iterator<Item = (ty::PolyTraitRef<'tcx>, Span)>,
|
2019-06-13 21:48:52 +00:00
|
|
|
) -> TraitAliasExpander<'tcx> {
|
2020-04-18 22:09:28 +00:00
|
|
|
let items: Vec<_> =
|
|
|
|
trait_refs.map(|(trait_ref, span)| TraitAliasExpansionInfo::new(trait_ref, span)).collect();
|
2019-05-02 17:03:29 +00:00
|
|
|
TraitAliasExpander { tcx, stack: items }
|
2019-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'tcx> TraitAliasExpander<'tcx> {
|
2019-04-29 02:58:24 +00:00
|
|
|
/// If `item` is a trait alias and its predicate has not yet been visited, then expands `item`
|
2019-05-02 01:21:20 +00:00
|
|
|
/// to the definition, pushes the resulting expansion onto `self.stack`, and returns `false`.
|
|
|
|
/// Otherwise, immediately returns `true` if `item` is a regular trait, or `false` if it is a
|
2019-04-29 02:58:24 +00:00
|
|
|
/// trait alias.
|
2019-05-02 01:21:20 +00:00
|
|
|
/// The return value indicates whether `item` should be yielded to the user.
|
2019-05-02 17:03:29 +00:00
|
|
|
fn expand(&mut self, item: &TraitAliasExpansionInfo<'tcx>) -> bool {
|
|
|
|
let tcx = self.tcx;
|
2019-04-29 02:58:24 +00:00
|
|
|
let trait_ref = item.trait_ref();
|
2020-05-07 10:12:19 +00:00
|
|
|
let pred = trait_ref.without_const().to_predicate(tcx);
|
2019-04-29 02:58:24 +00:00
|
|
|
|
|
|
|
debug!("expand_trait_aliases: trait_ref={:?}", trait_ref);
|
|
|
|
|
2019-05-02 17:03:29 +00:00
|
|
|
// Don't recurse if this bound is not a trait alias.
|
2019-04-29 02:58:24 +00:00
|
|
|
let is_alias = tcx.is_trait_alias(trait_ref.def_id());
|
2019-05-02 17:03:29 +00:00
|
|
|
if !is_alias {
|
|
|
|
return true;
|
2019-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 17:03:29 +00:00
|
|
|
// Don't recurse if this trait alias is already on the stack for the DFS search.
|
2020-05-23 09:09:32 +00:00
|
|
|
let anon_pred = anonymize_predicate(tcx, pred);
|
2020-06-17 21:20:17 +00:00
|
|
|
if item.path.iter().rev().skip(1).any(|&(tr, _)| {
|
2020-05-23 09:09:32 +00:00
|
|
|
anonymize_predicate(tcx, tr.without_const().to_predicate(tcx)) == anon_pred
|
2020-01-14 04:30:32 +00:00
|
|
|
}) {
|
2019-05-02 17:03:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-02 01:21:20 +00:00
|
|
|
|
2019-04-29 02:58:24 +00:00
|
|
|
// Get components of trait alias.
|
2023-02-02 20:37:02 +00:00
|
|
|
let predicates = tcx.implied_predicates_of(trait_ref.def_id());
|
2022-05-04 20:46:24 +00:00
|
|
|
debug!(?predicates);
|
2019-03-26 17:34:32 +00:00
|
|
|
|
2019-05-13 18:51:33 +00:00
|
|
|
let items = predicates.predicates.iter().rev().filter_map(|(pred, span)| {
|
2019-04-29 02:58:24 +00:00
|
|
|
pred.subst_supertrait(tcx, &trait_ref)
|
2023-06-22 18:17:13 +00:00
|
|
|
.as_trait_clause()
|
2021-12-12 04:34:46 +00:00
|
|
|
.map(|trait_ref| item.clone_and_push(trait_ref.map_bound(|t| t.trait_ref), *span))
|
2019-05-13 18:51:33 +00:00
|
|
|
});
|
2022-05-04 20:46:24 +00:00
|
|
|
debug!("expand_trait_aliases: items={:?}", items.clone().collect::<Vec<_>>());
|
2019-03-26 17:34:32 +00:00
|
|
|
|
2019-04-29 02:58:24 +00:00
|
|
|
self.stack.extend(items);
|
2019-03-26 17:34:32 +00:00
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'tcx> Iterator for TraitAliasExpander<'tcx> {
|
2019-04-29 02:58:24 +00:00
|
|
|
type Item = TraitAliasExpansionInfo<'tcx>;
|
2019-03-26 17:34:32 +00:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.stack.len(), None)
|
|
|
|
}
|
|
|
|
|
2019-04-29 02:58:24 +00:00
|
|
|
fn next(&mut self) -> Option<TraitAliasExpansionInfo<'tcx>> {
|
2019-03-27 15:14:41 +00:00
|
|
|
while let Some(item) = self.stack.pop() {
|
2019-05-02 17:03:29 +00:00
|
|
|
if self.expand(&item) {
|
2019-03-27 15:14:41 +00:00
|
|
|
return Some(item);
|
2019-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 15:14:41 +00:00
|
|
|
None
|
2019-03-26 17:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 19:51:11 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2019-03-27 15:14:41 +00:00
|
|
|
// Iterator over def-IDs of supertraits
|
2019-03-26 17:34:32 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-03-26 19:51:11 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
pub struct SupertraitDefIds<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2015-08-16 10:32:28 +00:00
|
|
|
stack: Vec<DefId>,
|
2016-11-08 03:02:55 +00:00
|
|
|
visited: FxHashSet<DefId>,
|
2015-03-26 19:51:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 16:12:39 +00:00
|
|
|
pub fn supertrait_def_ids(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SupertraitDefIds<'_> {
|
2015-03-26 19:51:11 +00:00
|
|
|
SupertraitDefIds {
|
2017-07-03 18:19:51 +00:00
|
|
|
tcx,
|
2015-03-26 19:51:11 +00:00
|
|
|
stack: vec![trait_def_id],
|
|
|
|
visited: Some(trait_def_id).into_iter().collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 09:44:49 +00:00
|
|
|
impl Iterator for SupertraitDefIds<'_> {
|
2015-08-16 10:32:28 +00:00
|
|
|
type Item = DefId;
|
2015-03-26 19:51:11 +00:00
|
|
|
|
2015-08-16 10:32:28 +00:00
|
|
|
fn next(&mut self) -> Option<DefId> {
|
2019-03-25 22:16:40 +00:00
|
|
|
let def_id = self.stack.pop()?;
|
2017-04-24 12:20:46 +00:00
|
|
|
let predicates = self.tcx.super_predicates_of(def_id);
|
2015-03-26 19:51:11 +00:00
|
|
|
let visited = &mut self.visited;
|
|
|
|
self.stack.extend(
|
|
|
|
predicates
|
|
|
|
.predicates
|
|
|
|
.iter()
|
2023-06-22 18:17:13 +00:00
|
|
|
.filter_map(|(pred, _)| pred.as_trait_clause())
|
2021-12-12 04:34:46 +00:00
|
|
|
.map(|trait_ref| trait_ref.def_id())
|
2015-03-26 19:51:11 +00:00
|
|
|
.filter(|&super_def_id| visited.insert(super_def_id)),
|
|
|
|
);
|
|
|
|
Some(def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 20:30:14 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Other
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
/// Instantiate all bound parameters of the impl subject with the given args,
|
2022-03-24 21:41:30 +00:00
|
|
|
/// returning the resulting subject and all obligations that arise.
|
2015-12-30 21:47:23 +00:00
|
|
|
/// The obligations are closed under normalization.
|
2022-03-24 21:41:30 +00:00
|
|
|
pub fn impl_subject_and_oblig<'a, 'tcx>(
|
2019-06-13 22:32:15 +00:00
|
|
|
selcx: &mut SelectionContext<'a, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
impl_def_id: DefId,
|
2023-07-11 21:35:29 +00:00
|
|
|
impl_args: GenericArgsRef<'tcx>,
|
2023-05-05 14:52:53 +00:00
|
|
|
cause: impl Fn(usize, Span) -> ObligationCause<'tcx>,
|
2022-03-24 21:41:30 +00:00
|
|
|
) -> (ImplSubject<'tcx>, impl Iterator<Item = PredicateObligation<'tcx>>) {
|
2023-04-13 21:23:54 +00:00
|
|
|
let subject = selcx.tcx().impl_subject(impl_def_id);
|
2023-07-11 21:35:29 +00:00
|
|
|
let subject = subject.instantiate(selcx.tcx(), impl_args);
|
2023-03-15 13:00:15 +00:00
|
|
|
|
2022-11-25 17:11:15 +00:00
|
|
|
let InferOk { value: subject, obligations: normalization_obligations1 } =
|
|
|
|
selcx.infcx.at(&ObligationCause::dummy(), param_env).normalize(subject);
|
2015-12-30 21:47:23 +00:00
|
|
|
|
2017-04-24 12:20:46 +00:00
|
|
|
let predicates = selcx.tcx().predicates_of(impl_def_id);
|
2023-07-11 21:35:29 +00:00
|
|
|
let predicates = predicates.instantiate(selcx.tcx(), impl_args);
|
2022-11-25 17:11:15 +00:00
|
|
|
let InferOk { value: predicates, obligations: normalization_obligations2 } =
|
|
|
|
selcx.infcx.at(&ObligationCause::dummy(), param_env).normalize(predicates);
|
2023-05-05 14:52:53 +00:00
|
|
|
let impl_obligations = super::predicates_for_generics(cause, param_env, predicates);
|
2015-12-30 21:47:23 +00:00
|
|
|
|
2020-04-18 22:09:28 +00:00
|
|
|
let impl_obligations = impl_obligations
|
|
|
|
.chain(normalization_obligations1.into_iter())
|
|
|
|
.chain(normalization_obligations2.into_iter());
|
2015-12-30 21:47:23 +00:00
|
|
|
|
2022-03-24 21:41:30 +00:00
|
|
|
(subject, impl_obligations)
|
2022-03-24 20:47:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 19:27:00 +00:00
|
|
|
/// Casts a trait reference into a reference to one of its super
|
|
|
|
/// traits; returns `None` if `target_trait_def_id` is not a
|
|
|
|
/// supertrait.
|
2021-12-14 09:44:49 +00:00
|
|
|
pub fn upcast_choices<'tcx>(
|
2020-01-05 19:27:00 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
source_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
target_trait_def_id: DefId,
|
|
|
|
) -> Vec<ty::PolyTraitRef<'tcx>> {
|
|
|
|
if source_trait_ref.def_id() == target_trait_def_id {
|
|
|
|
return vec![source_trait_ref]; // Shortcut the most common case.
|
2015-01-11 20:18:06 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 19:27:00 +00:00
|
|
|
supertraits(tcx, source_trait_ref).filter(|r| r.def_id() == target_trait_def_id).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an upcast trait object described by `object`, returns the
|
|
|
|
/// index of the method `method_def_id` (which should be part of
|
|
|
|
/// `object.upcast_trait_ref`) within the vtable for `object`.
|
2021-12-14 09:44:49 +00:00
|
|
|
pub fn get_vtable_index_of_object_method<'tcx, N>(
|
2020-01-05 19:27:00 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-06-16 00:48:29 +00:00
|
|
|
object: &super::ImplSourceObjectData<N>,
|
2020-01-05 19:27:00 +00:00
|
|
|
method_def_id: DefId,
|
2022-05-31 18:34:04 +00:00
|
|
|
) -> Option<usize> {
|
2020-01-05 19:27:00 +00:00
|
|
|
// Count number of methods preceding the one we are selecting and
|
|
|
|
// add them to the total offset.
|
2023-06-20 21:48:05 +00:00
|
|
|
tcx.own_existential_vtable_entries(tcx.parent(method_def_id))
|
2021-09-01 22:04:28 +00:00
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.position(|def_id| def_id == method_def_id)
|
2023-04-15 18:18:30 +00:00
|
|
|
.map(|index| object.vtable_base + index)
|
2020-01-05 19:27:00 +00:00
|
|
|
}
|
2017-04-24 09:56:54 +00:00
|
|
|
|
2021-12-14 09:44:49 +00:00
|
|
|
pub fn closure_trait_ref_and_return_type<'tcx>(
|
2020-01-05 19:27:00 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
fn_trait_def_id: DefId,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
sig: ty::PolyFnSig<'tcx>,
|
|
|
|
tuple_arguments: TupleArgumentsFlag,
|
2020-10-05 20:51:33 +00:00
|
|
|
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>)> {
|
2022-11-24 18:45:02 +00:00
|
|
|
assert!(!self_ty.has_escaping_bound_vars());
|
2020-01-05 19:27:00 +00:00
|
|
|
let arguments_tuple = match tuple_arguments {
|
|
|
|
TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
|
2023-07-05 19:13:26 +00:00
|
|
|
TupleArgumentsFlag::Yes => Ty::new_tup(tcx, sig.skip_binder().inputs()),
|
2020-01-05 19:27:00 +00:00
|
|
|
};
|
2023-04-25 16:07:48 +00:00
|
|
|
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty, arguments_tuple]);
|
2020-12-17 03:36:14 +00:00
|
|
|
sig.map_bound(|sig| (trait_ref, sig.output()))
|
2020-01-05 19:27:00 +00:00
|
|
|
}
|
2017-07-05 21:57:26 +00:00
|
|
|
|
2021-12-14 09:44:49 +00:00
|
|
|
pub fn generator_trait_ref_and_outputs<'tcx>(
|
2020-01-05 19:27:00 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
fn_trait_def_id: DefId,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
sig: ty::PolyGenSig<'tcx>,
|
2020-10-05 20:51:33 +00:00
|
|
|
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>)> {
|
2022-11-24 18:45:02 +00:00
|
|
|
assert!(!self_ty.has_escaping_bound_vars());
|
2023-04-25 16:07:48 +00:00
|
|
|
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty, sig.skip_binder().resume_ty]);
|
2020-12-17 03:36:14 +00:00
|
|
|
sig.map_bound(|sig| (trait_ref, sig.yield_ty, sig.return_ty))
|
2020-01-05 19:27:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 21:56:22 +00:00
|
|
|
pub fn future_trait_ref_and_outputs<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
fn_trait_def_id: DefId,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
sig: ty::PolyGenSig<'tcx>,
|
|
|
|
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>)> {
|
2022-11-24 18:45:02 +00:00
|
|
|
assert!(!self_ty.has_escaping_bound_vars());
|
2023-04-25 16:07:48 +00:00
|
|
|
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty]);
|
2022-11-18 21:56:22 +00:00
|
|
|
sig.map_bound(|sig| (trait_ref, sig.return_ty))
|
|
|
|
}
|
|
|
|
|
2020-01-05 19:27:00 +00:00
|
|
|
pub fn impl_item_is_final(tcx: TyCtxt<'_>, assoc_item: &ty::AssocItem) -> bool {
|
2022-03-12 18:36:11 +00:00
|
|
|
assoc_item.defaultness(tcx).is_final()
|
2023-06-01 06:14:06 +00:00
|
|
|
&& tcx.defaultness(assoc_item.container_id(tcx)).is_final()
|
2016-03-16 22:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum TupleArgumentsFlag {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
2023-06-21 05:32:35 +00:00
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
// Verify that the trait item and its implementation have compatible args lists
|
|
|
|
pub fn check_args_compatible<'tcx>(
|
2023-06-21 05:32:35 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
assoc_item: ty::AssocItem,
|
2023-07-11 21:35:29 +00:00
|
|
|
args: ty::GenericArgsRef<'tcx>,
|
2023-06-21 05:32:35 +00:00
|
|
|
) -> bool {
|
2023-07-11 21:35:29 +00:00
|
|
|
fn check_args_compatible_inner<'tcx>(
|
2023-06-21 05:32:35 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
generics: &'tcx ty::Generics,
|
|
|
|
args: &'tcx [ty::GenericArg<'tcx>],
|
|
|
|
) -> bool {
|
|
|
|
if generics.count() != args.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (parent_args, own_args) = args.split_at(generics.parent_count);
|
|
|
|
|
|
|
|
if let Some(parent) = generics.parent
|
|
|
|
&& let parent_generics = tcx.generics_of(parent)
|
2023-07-11 21:35:29 +00:00
|
|
|
&& !check_args_compatible_inner(tcx, parent_generics, parent_args) {
|
2023-06-21 05:32:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (param, arg) in std::iter::zip(&generics.params, own_args) {
|
|
|
|
match (¶m.kind, arg.unpack()) {
|
|
|
|
(ty::GenericParamDefKind::Type { .. }, ty::GenericArgKind::Type(_))
|
|
|
|
| (ty::GenericParamDefKind::Lifetime, ty::GenericArgKind::Lifetime(_))
|
|
|
|
| (ty::GenericParamDefKind::Const { .. }, ty::GenericArgKind::Const(_)) => {}
|
|
|
|
_ => return false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
let generics = tcx.generics_of(assoc_item.def_id);
|
2023-07-11 21:35:29 +00:00
|
|
|
// Chop off any additional args (RPITIT) args
|
|
|
|
let args = &args[0..generics.count().min(args.len())];
|
|
|
|
check_args_compatible_inner(tcx, generics, args)
|
2023-06-21 05:32:35 +00:00
|
|
|
}
|