1 use rustc_errors::DiagnosticBuilder;
3 use smallvec::smallvec;
4 use smallvec::SmallVec;
6 use rustc::ty::outlives::Component;
7 use rustc::ty::subst::{GenericArg, Subst, SubstsRef};
8 use rustc::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};
9 use rustc_data_structures::fx::FxHashSet;
11 use rustc_hir::def_id::DefId;
13 use super::{Normalized, Obligation, ObligationCause, PredicateObligation, SelectionContext};
15 fn anonymize_predicate<'tcx>(tcx: TyCtxt<'tcx>, pred: &ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
17 ty::Predicate::Trait(ref data, constness) => {
18 ty::Predicate::Trait(tcx.anonymize_late_bound_regions(data), constness)
21 ty::Predicate::RegionOutlives(ref data) => {
22 ty::Predicate::RegionOutlives(tcx.anonymize_late_bound_regions(data))
25 ty::Predicate::TypeOutlives(ref data) => {
26 ty::Predicate::TypeOutlives(tcx.anonymize_late_bound_regions(data))
29 ty::Predicate::Projection(ref data) => {
30 ty::Predicate::Projection(tcx.anonymize_late_bound_regions(data))
33 ty::Predicate::WellFormed(data) => ty::Predicate::WellFormed(data),
35 ty::Predicate::ObjectSafe(data) => ty::Predicate::ObjectSafe(data),
37 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
38 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind)
41 ty::Predicate::Subtype(ref data) => {
42 ty::Predicate::Subtype(tcx.anonymize_late_bound_regions(data))
45 ty::Predicate::ConstEvaluatable(def_id, substs) => {
46 ty::Predicate::ConstEvaluatable(def_id, substs)
51 struct PredicateSet<'tcx> {
53 set: FxHashSet<ty::Predicate<'tcx>>,
56 impl PredicateSet<'tcx> {
57 fn new(tcx: TyCtxt<'tcx>) -> Self {
58 Self { tcx, set: Default::default() }
61 fn insert(&mut self, pred: &ty::Predicate<'tcx>) -> bool {
62 // We have to be careful here because we want
64 // for<'a> Foo<&'a int>
68 // for<'b> Foo<&'b int>
70 // to be considered equivalent. So normalize all late-bound
71 // regions before we throw things into the underlying set.
72 self.set.insert(anonymize_predicate(self.tcx, pred))
76 impl<T: AsRef<ty::Predicate<'tcx>>> Extend<T> for PredicateSet<'tcx> {
77 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
79 self.insert(pred.as_ref());
84 ///////////////////////////////////////////////////////////////////////////
85 // `Elaboration` iterator
86 ///////////////////////////////////////////////////////////////////////////
88 /// "Elaboration" is the process of identifying all the predicates that
89 /// are implied by a source predicate. Currently, this basically means
90 /// walking the "supertraits" and other similar assumptions. For example,
91 /// if we know that `T: Ord`, the elaborator would deduce that `T: PartialOrd`
92 /// holds as well. Similarly, if we have `trait Foo: 'static`, and we know that
93 /// `T: Foo`, then we know that `T: 'static`.
94 pub struct Elaborator<'tcx> {
95 stack: Vec<ty::Predicate<'tcx>>,
96 visited: PredicateSet<'tcx>,
99 pub fn elaborate_trait_ref<'tcx>(
101 trait_ref: ty::PolyTraitRef<'tcx>,
102 ) -> Elaborator<'tcx> {
103 elaborate_predicates(tcx, vec![trait_ref.without_const().to_predicate()])
106 pub fn elaborate_trait_refs<'tcx>(
108 trait_refs: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
109 ) -> Elaborator<'tcx> {
110 let predicates = trait_refs.map(|trait_ref| trait_ref.without_const().to_predicate()).collect();
111 elaborate_predicates(tcx, predicates)
114 pub fn elaborate_predicates<'tcx>(
116 mut predicates: Vec<ty::Predicate<'tcx>>,
117 ) -> Elaborator<'tcx> {
118 let mut visited = PredicateSet::new(tcx);
119 predicates.retain(|pred| visited.insert(pred));
120 Elaborator { stack: predicates, visited }
123 impl Elaborator<'tcx> {
124 pub fn filter_to_traits(self) -> FilterToTraits<Self> {
125 FilterToTraits::new(self)
128 fn elaborate(&mut self, predicate: &ty::Predicate<'tcx>) {
129 let tcx = self.visited.tcx;
131 ty::Predicate::Trait(ref data, _) => {
132 // Get predicates declared on the trait.
133 let predicates = tcx.super_predicates_of(data.def_id());
135 let predicates = predicates
138 .map(|(pred, _)| pred.subst_supertrait(tcx, &data.to_poly_trait_ref()));
139 debug!("super_predicates: data={:?} predicates={:?}", data, predicates.clone());
141 // Only keep those bounds that we haven't already seen.
142 // This is necessary to prevent infinite recursion in some
143 // cases. One common case is when people define
144 // `trait Sized: Sized { }` rather than `trait Sized { }`.
145 let visited = &mut self.visited;
146 let predicates = predicates.filter(|pred| visited.insert(pred));
148 self.stack.extend(predicates);
150 ty::Predicate::WellFormed(..) => {
151 // Currently, we do not elaborate WF predicates,
152 // although we easily could.
154 ty::Predicate::ObjectSafe(..) => {
155 // Currently, we do not elaborate object-safe
158 ty::Predicate::Subtype(..) => {
159 // Currently, we do not "elaborate" predicates like `X <: Y`,
160 // though conceivably we might.
162 ty::Predicate::Projection(..) => {
163 // Nothing to elaborate in a projection predicate.
165 ty::Predicate::ClosureKind(..) => {
166 // Nothing to elaborate when waiting for a closure's kind to be inferred.
168 ty::Predicate::ConstEvaluatable(..) => {
169 // Currently, we do not elaborate const-evaluatable
172 ty::Predicate::RegionOutlives(..) => {
173 // Nothing to elaborate from `'a: 'b`.
175 ty::Predicate::TypeOutlives(ref data) => {
176 // We know that `T: 'a` for some type `T`. We can
177 // often elaborate this. For example, if we know that
178 // `[U]: 'a`, that implies that `U: 'a`. Similarly, if
179 // we know `&'a U: 'b`, then we know that `'a: 'b` and
182 // We can basically ignore bound regions here. So for
183 // example `for<'c> Foo<'a,'c>: 'b` can be elaborated to
186 // Ignore `for<'a> T: 'a` -- we might in the future
187 // consider this as evidence that `T: 'static`, but
188 // I'm a bit wary of such constructions and so for now
189 // I want to be conservative. --nmatsakis
190 let ty_max = data.skip_binder().0;
191 let r_min = data.skip_binder().1;
192 if r_min.is_late_bound() {
196 let visited = &mut self.visited;
197 let mut components = smallvec![];
198 tcx.push_outlives_components(ty_max, &mut components);
202 .filter_map(|component| match component {
203 Component::Region(r) => {
204 if r.is_late_bound() {
207 Some(ty::Predicate::RegionOutlives(ty::Binder::dummy(
208 ty::OutlivesPredicate(r, r_min),
213 Component::Param(p) => {
214 let ty = tcx.mk_ty_param(p.index, p.name);
215 Some(ty::Predicate::TypeOutlives(ty::Binder::dummy(
216 ty::OutlivesPredicate(ty, r_min),
220 Component::UnresolvedInferenceVariable(_) => None,
222 Component::Projection(_) | Component::EscapingProjection(_) => {
223 // We can probably do more here. This
224 // corresponds to a case like `<T as
229 .filter(|p| visited.insert(p)),
236 impl Iterator for Elaborator<'tcx> {
237 type Item = ty::Predicate<'tcx>;
239 fn size_hint(&self) -> (usize, Option<usize>) {
240 (self.stack.len(), None)
243 fn next(&mut self) -> Option<ty::Predicate<'tcx>> {
244 // Extract next item from top-most stack frame, if any.
245 if let Some(pred) = self.stack.pop() {
246 self.elaborate(&pred);
254 ///////////////////////////////////////////////////////////////////////////
255 // Supertrait iterator
256 ///////////////////////////////////////////////////////////////////////////
258 pub type Supertraits<'tcx> = FilterToTraits<Elaborator<'tcx>>;
260 pub fn supertraits<'tcx>(
262 trait_ref: ty::PolyTraitRef<'tcx>,
263 ) -> Supertraits<'tcx> {
264 elaborate_trait_ref(tcx, trait_ref).filter_to_traits()
267 pub fn transitive_bounds<'tcx>(
269 bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
270 ) -> Supertraits<'tcx> {
271 elaborate_trait_refs(tcx, bounds).filter_to_traits()
274 ///////////////////////////////////////////////////////////////////////////
275 // `TraitAliasExpander` iterator
276 ///////////////////////////////////////////////////////////////////////////
278 /// "Trait alias expansion" is the process of expanding a sequence of trait
279 /// references into another sequence by transitively following all trait
280 /// aliases. e.g. If you have bounds like `Foo + Send`, a trait alias
281 /// `trait Foo = Bar + Sync;`, and another trait alias
282 /// `trait Bar = Read + Write`, then the bounds would expand to
283 /// `Read + Write + Sync + Send`.
284 /// Expansion is done via a DFS (depth-first search), and the `visited` field
285 /// is used to avoid cycles.
286 pub struct TraitAliasExpander<'tcx> {
288 stack: Vec<TraitAliasExpansionInfo<'tcx>>,
291 /// Stores information about the expansion of a trait via a path of zero or more trait aliases.
292 #[derive(Debug, Clone)]
293 pub struct TraitAliasExpansionInfo<'tcx> {
294 pub path: SmallVec<[(ty::PolyTraitRef<'tcx>, Span); 4]>,
297 impl<'tcx> TraitAliasExpansionInfo<'tcx> {
298 fn new(trait_ref: ty::PolyTraitRef<'tcx>, span: Span) -> Self {
299 Self { path: smallvec![(trait_ref, span)] }
302 /// Adds diagnostic labels to `diag` for the expansion path of a trait through all intermediate
304 pub fn label_with_exp_info(
306 diag: &mut DiagnosticBuilder<'_>,
310 diag.span_label(self.top().1, top_label);
311 if self.path.len() > 1 {
312 for (_, sp) in self.path.iter().rev().skip(1).take(self.path.len() - 2) {
313 diag.span_label(*sp, format!("referenced here ({})", use_desc));
318 format!("trait alias used in trait object type ({})", use_desc),
322 pub fn trait_ref(&self) -> &ty::PolyTraitRef<'tcx> {
326 pub fn top(&self) -> &(ty::PolyTraitRef<'tcx>, Span) {
327 self.path.last().unwrap()
330 pub fn bottom(&self) -> &(ty::PolyTraitRef<'tcx>, Span) {
331 self.path.first().unwrap()
334 fn clone_and_push(&self, trait_ref: ty::PolyTraitRef<'tcx>, span: Span) -> Self {
335 let mut path = self.path.clone();
336 path.push((trait_ref, span));
342 pub fn expand_trait_aliases<'tcx>(
344 trait_refs: impl IntoIterator<Item = (ty::PolyTraitRef<'tcx>, Span)>,
345 ) -> TraitAliasExpander<'tcx> {
346 let items: Vec<_> = trait_refs
348 .map(|(trait_ref, span)| TraitAliasExpansionInfo::new(trait_ref, span))
350 TraitAliasExpander { tcx, stack: items }
353 impl<'tcx> TraitAliasExpander<'tcx> {
354 /// If `item` is a trait alias and its predicate has not yet been visited, then expands `item`
355 /// to the definition, pushes the resulting expansion onto `self.stack`, and returns `false`.
356 /// Otherwise, immediately returns `true` if `item` is a regular trait, or `false` if it is a
358 /// The return value indicates whether `item` should be yielded to the user.
359 fn expand(&mut self, item: &TraitAliasExpansionInfo<'tcx>) -> bool {
361 let trait_ref = item.trait_ref();
362 let pred = trait_ref.without_const().to_predicate();
364 debug!("expand_trait_aliases: trait_ref={:?}", trait_ref);
366 // Don't recurse if this bound is not a trait alias.
367 let is_alias = tcx.is_trait_alias(trait_ref.def_id());
372 // Don't recurse if this trait alias is already on the stack for the DFS search.
373 let anon_pred = anonymize_predicate(tcx, &pred);
374 if item.path.iter().rev().skip(1).any(|(tr, _)| {
375 anonymize_predicate(tcx, &tr.without_const().to_predicate()) == anon_pred
380 // Get components of trait alias.
381 let predicates = tcx.super_predicates_of(trait_ref.def_id());
383 let items = predicates.predicates.iter().rev().filter_map(|(pred, span)| {
384 pred.subst_supertrait(tcx, &trait_ref)
385 .to_opt_poly_trait_ref()
386 .map(|trait_ref| item.clone_and_push(trait_ref, *span))
388 debug!("expand_trait_aliases: items={:?}", items.clone());
390 self.stack.extend(items);
396 impl<'tcx> Iterator for TraitAliasExpander<'tcx> {
397 type Item = TraitAliasExpansionInfo<'tcx>;
399 fn size_hint(&self) -> (usize, Option<usize>) {
400 (self.stack.len(), None)
403 fn next(&mut self) -> Option<TraitAliasExpansionInfo<'tcx>> {
404 while let Some(item) = self.stack.pop() {
405 if self.expand(&item) {
413 ///////////////////////////////////////////////////////////////////////////
414 // Iterator over def-IDs of supertraits
415 ///////////////////////////////////////////////////////////////////////////
417 pub struct SupertraitDefIds<'tcx> {
420 visited: FxHashSet<DefId>,
423 pub fn supertrait_def_ids(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SupertraitDefIds<'_> {
426 stack: vec![trait_def_id],
427 visited: Some(trait_def_id).into_iter().collect(),
431 impl Iterator for SupertraitDefIds<'tcx> {
434 fn next(&mut self) -> Option<DefId> {
435 let def_id = self.stack.pop()?;
436 let predicates = self.tcx.super_predicates_of(def_id);
437 let visited = &mut self.visited;
442 .filter_map(|(pred, _)| pred.to_opt_poly_trait_ref())
443 .map(|trait_ref| trait_ref.def_id())
444 .filter(|&super_def_id| visited.insert(super_def_id)),
450 ///////////////////////////////////////////////////////////////////////////
452 ///////////////////////////////////////////////////////////////////////////
454 /// A filter around an iterator of predicates that makes it yield up
455 /// just trait references.
456 pub struct FilterToTraits<I> {
460 impl<I> FilterToTraits<I> {
461 fn new(base: I) -> FilterToTraits<I> {
462 FilterToTraits { base_iterator: base }
466 impl<'tcx, I: Iterator<Item = ty::Predicate<'tcx>>> Iterator for FilterToTraits<I> {
467 type Item = ty::PolyTraitRef<'tcx>;
469 fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
470 while let Some(pred) = self.base_iterator.next() {
471 if let ty::Predicate::Trait(data, _) = pred {
472 return Some(data.to_poly_trait_ref());
478 fn size_hint(&self) -> (usize, Option<usize>) {
479 let (_, upper) = self.base_iterator.size_hint();
484 ///////////////////////////////////////////////////////////////////////////
486 ///////////////////////////////////////////////////////////////////////////
488 /// Instantiate all bound parameters of the impl with the given substs,
489 /// returning the resulting trait ref and all obligations that arise.
490 /// The obligations are closed under normalization.
491 pub fn impl_trait_ref_and_oblig<'a, 'tcx>(
492 selcx: &mut SelectionContext<'a, 'tcx>,
493 param_env: ty::ParamEnv<'tcx>,
495 impl_substs: SubstsRef<'tcx>,
496 ) -> (ty::TraitRef<'tcx>, Vec<PredicateObligation<'tcx>>) {
497 let impl_trait_ref = selcx.tcx().impl_trait_ref(impl_def_id).unwrap();
498 let impl_trait_ref = impl_trait_ref.subst(selcx.tcx(), impl_substs);
499 let Normalized { value: impl_trait_ref, obligations: normalization_obligations1 } =
500 super::normalize(selcx, param_env, ObligationCause::dummy(), &impl_trait_ref);
502 let predicates = selcx.tcx().predicates_of(impl_def_id);
503 let predicates = predicates.instantiate(selcx.tcx(), impl_substs);
504 let Normalized { value: predicates, obligations: normalization_obligations2 } =
505 super::normalize(selcx, param_env, ObligationCause::dummy(), &predicates);
506 let impl_obligations =
507 predicates_for_generics(ObligationCause::dummy(), 0, param_env, &predicates);
509 let impl_obligations: Vec<_> = impl_obligations
511 .chain(normalization_obligations1)
512 .chain(normalization_obligations2)
515 (impl_trait_ref, impl_obligations)
518 /// See [`super::obligations_for_generics`].
519 pub fn predicates_for_generics<'tcx>(
520 cause: ObligationCause<'tcx>,
521 recursion_depth: usize,
522 param_env: ty::ParamEnv<'tcx>,
523 generic_bounds: &ty::InstantiatedPredicates<'tcx>,
524 ) -> Vec<PredicateObligation<'tcx>> {
525 debug!("predicates_for_generics(generic_bounds={:?})", generic_bounds);
530 .map(|&predicate| Obligation {
531 cause: cause.clone(),
539 pub fn predicate_for_trait_ref<'tcx>(
540 cause: ObligationCause<'tcx>,
541 param_env: ty::ParamEnv<'tcx>,
542 trait_ref: ty::TraitRef<'tcx>,
543 recursion_depth: usize,
544 ) -> PredicateObligation<'tcx> {
549 predicate: trait_ref.without_const().to_predicate(),
553 pub fn predicate_for_trait_def(
555 param_env: ty::ParamEnv<'tcx>,
556 cause: ObligationCause<'tcx>,
558 recursion_depth: usize,
560 params: &[GenericArg<'tcx>],
561 ) -> PredicateObligation<'tcx> {
563 ty::TraitRef { def_id: trait_def_id, substs: tcx.mk_substs_trait(self_ty, params) };
564 predicate_for_trait_ref(cause, param_env, trait_ref, recursion_depth)
567 /// Casts a trait reference into a reference to one of its super
568 /// traits; returns `None` if `target_trait_def_id` is not a
570 pub fn upcast_choices(
572 source_trait_ref: ty::PolyTraitRef<'tcx>,
573 target_trait_def_id: DefId,
574 ) -> Vec<ty::PolyTraitRef<'tcx>> {
575 if source_trait_ref.def_id() == target_trait_def_id {
576 return vec![source_trait_ref]; // Shortcut the most common case.
579 supertraits(tcx, source_trait_ref).filter(|r| r.def_id() == target_trait_def_id).collect()
582 /// Given a trait `trait_ref`, returns the number of vtable entries
583 /// that come from `trait_ref`, excluding its supertraits. Used in
584 /// computing the vtable base for an upcast trait of a trait object.
585 pub fn count_own_vtable_entries(tcx: TyCtxt<'tcx>, trait_ref: ty::PolyTraitRef<'tcx>) -> usize {
587 // Count number of methods and add them to the total offset.
588 // Skip over associated types and constants.
589 for trait_item in tcx.associated_items(trait_ref.def_id()).in_definition_order() {
590 if trait_item.kind == ty::AssocKind::Method {
597 /// Given an upcast trait object described by `object`, returns the
598 /// index of the method `method_def_id` (which should be part of
599 /// `object.upcast_trait_ref`) within the vtable for `object`.
600 pub fn get_vtable_index_of_object_method<N>(
602 object: &super::VtableObjectData<'tcx, N>,
603 method_def_id: DefId,
605 // Count number of methods preceding the one we are selecting and
606 // add them to the total offset.
607 // Skip over associated types and constants.
608 let mut entries = object.vtable_base;
609 for trait_item in tcx.associated_items(object.upcast_trait_ref.def_id()).in_definition_order() {
610 if trait_item.def_id == method_def_id {
611 // The item with the ID we were given really ought to be a method.
612 assert_eq!(trait_item.kind, ty::AssocKind::Method);
615 if trait_item.kind == ty::AssocKind::Method {
620 bug!("get_vtable_index_of_object_method: {:?} was not found", method_def_id);
623 pub fn closure_trait_ref_and_return_type(
625 fn_trait_def_id: DefId,
627 sig: ty::PolyFnSig<'tcx>,
628 tuple_arguments: TupleArgumentsFlag,
629 ) -> ty::Binder<(ty::TraitRef<'tcx>, Ty<'tcx>)> {
630 let arguments_tuple = match tuple_arguments {
631 TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
632 TupleArgumentsFlag::Yes => tcx.intern_tup(sig.skip_binder().inputs()),
634 let trait_ref = ty::TraitRef {
635 def_id: fn_trait_def_id,
636 substs: tcx.mk_substs_trait(self_ty, &[arguments_tuple.into()]),
638 ty::Binder::bind((trait_ref, sig.skip_binder().output()))
641 pub fn generator_trait_ref_and_outputs(
643 fn_trait_def_id: DefId,
645 sig: ty::PolyGenSig<'tcx>,
646 ) -> ty::Binder<(ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>)> {
647 let trait_ref = ty::TraitRef {
648 def_id: fn_trait_def_id,
649 substs: tcx.mk_substs_trait(self_ty, &[sig.skip_binder().resume_ty.into()]),
651 ty::Binder::bind((trait_ref, sig.skip_binder().yield_ty, sig.skip_binder().return_ty))
654 pub fn impl_is_default(tcx: TyCtxt<'_>, node_item_def_id: DefId) -> bool {
655 match tcx.hir().as_local_hir_id(node_item_def_id) {
657 let item = tcx.hir().expect_item(hir_id);
658 if let hir::ItemKind::Impl { defaultness, .. } = item.kind {
659 defaultness.is_default()
664 None => tcx.impl_defaultness(node_item_def_id).is_default(),
668 pub fn impl_item_is_final(tcx: TyCtxt<'_>, assoc_item: &ty::AssocItem) -> bool {
669 assoc_item.defaultness.is_final() && !impl_is_default(tcx, assoc_item.container.id())
672 pub enum TupleArgumentsFlag {