]> git.lizzy.rs Git - rust.git/blob - src/librustc_trait_selection/traits/util.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / librustc_trait_selection / traits / util.rs
1 use rustc_errors::DiagnosticBuilder;
2 use rustc_span::Span;
3 use smallvec::smallvec;
4 use smallvec::SmallVec;
5
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;
10 use rustc_hir as hir;
11 use rustc_hir::def_id::DefId;
12
13 use super::{Normalized, Obligation, ObligationCause, PredicateObligation, SelectionContext};
14
15 fn anonymize_predicate<'tcx>(tcx: TyCtxt<'tcx>, pred: &ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
16     match *pred {
17         ty::Predicate::Trait(ref data, constness) => {
18             ty::Predicate::Trait(tcx.anonymize_late_bound_regions(data), constness)
19         }
20
21         ty::Predicate::RegionOutlives(ref data) => {
22             ty::Predicate::RegionOutlives(tcx.anonymize_late_bound_regions(data))
23         }
24
25         ty::Predicate::TypeOutlives(ref data) => {
26             ty::Predicate::TypeOutlives(tcx.anonymize_late_bound_regions(data))
27         }
28
29         ty::Predicate::Projection(ref data) => {
30             ty::Predicate::Projection(tcx.anonymize_late_bound_regions(data))
31         }
32
33         ty::Predicate::WellFormed(data) => ty::Predicate::WellFormed(data),
34
35         ty::Predicate::ObjectSafe(data) => ty::Predicate::ObjectSafe(data),
36
37         ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
38             ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind)
39         }
40
41         ty::Predicate::Subtype(ref data) => {
42             ty::Predicate::Subtype(tcx.anonymize_late_bound_regions(data))
43         }
44
45         ty::Predicate::ConstEvaluatable(def_id, substs) => {
46             ty::Predicate::ConstEvaluatable(def_id, substs)
47         }
48     }
49 }
50
51 struct PredicateSet<'tcx> {
52     tcx: TyCtxt<'tcx>,
53     set: FxHashSet<ty::Predicate<'tcx>>,
54 }
55
56 impl PredicateSet<'tcx> {
57     fn new(tcx: TyCtxt<'tcx>) -> Self {
58         Self { tcx, set: Default::default() }
59     }
60
61     fn insert(&mut self, pred: &ty::Predicate<'tcx>) -> bool {
62         // We have to be careful here because we want
63         //
64         //    for<'a> Foo<&'a int>
65         //
66         // and
67         //
68         //    for<'b> Foo<&'b int>
69         //
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))
73     }
74 }
75
76 impl<T: AsRef<ty::Predicate<'tcx>>> Extend<T> for PredicateSet<'tcx> {
77     fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
78         for pred in iter {
79             self.insert(pred.as_ref());
80         }
81     }
82 }
83
84 ///////////////////////////////////////////////////////////////////////////
85 // `Elaboration` iterator
86 ///////////////////////////////////////////////////////////////////////////
87
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>,
97 }
98
99 pub fn elaborate_trait_ref<'tcx>(
100     tcx: TyCtxt<'tcx>,
101     trait_ref: ty::PolyTraitRef<'tcx>,
102 ) -> Elaborator<'tcx> {
103     elaborate_predicates(tcx, vec![trait_ref.without_const().to_predicate()])
104 }
105
106 pub fn elaborate_trait_refs<'tcx>(
107     tcx: TyCtxt<'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)
112 }
113
114 pub fn elaborate_predicates<'tcx>(
115     tcx: TyCtxt<'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 }
121 }
122
123 impl Elaborator<'tcx> {
124     pub fn filter_to_traits(self) -> FilterToTraits<Self> {
125         FilterToTraits::new(self)
126     }
127
128     fn elaborate(&mut self, predicate: &ty::Predicate<'tcx>) {
129         let tcx = self.visited.tcx;
130         match *predicate {
131             ty::Predicate::Trait(ref data, _) => {
132                 // Get predicates declared on the trait.
133                 let predicates = tcx.super_predicates_of(data.def_id());
134
135                 let predicates = predicates
136                     .predicates
137                     .iter()
138                     .map(|(pred, _)| pred.subst_supertrait(tcx, &data.to_poly_trait_ref()));
139                 debug!("super_predicates: data={:?} predicates={:?}", data, predicates.clone());
140
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));
147
148                 self.stack.extend(predicates);
149             }
150             ty::Predicate::WellFormed(..) => {
151                 // Currently, we do not elaborate WF predicates,
152                 // although we easily could.
153             }
154             ty::Predicate::ObjectSafe(..) => {
155                 // Currently, we do not elaborate object-safe
156                 // predicates.
157             }
158             ty::Predicate::Subtype(..) => {
159                 // Currently, we do not "elaborate" predicates like `X <: Y`,
160                 // though conceivably we might.
161             }
162             ty::Predicate::Projection(..) => {
163                 // Nothing to elaborate in a projection predicate.
164             }
165             ty::Predicate::ClosureKind(..) => {
166                 // Nothing to elaborate when waiting for a closure's kind to be inferred.
167             }
168             ty::Predicate::ConstEvaluatable(..) => {
169                 // Currently, we do not elaborate const-evaluatable
170                 // predicates.
171             }
172             ty::Predicate::RegionOutlives(..) => {
173                 // Nothing to elaborate from `'a: 'b`.
174             }
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
180                 // `U: 'b`.
181                 //
182                 // We can basically ignore bound regions here. So for
183                 // example `for<'c> Foo<'a,'c>: 'b` can be elaborated to
184                 // `'a: 'b`.
185
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() {
193                     return;
194                 }
195
196                 let visited = &mut self.visited;
197                 let mut components = smallvec![];
198                 tcx.push_outlives_components(ty_max, &mut components);
199                 self.stack.extend(
200                     components
201                         .into_iter()
202                         .filter_map(|component| match component {
203                             Component::Region(r) => {
204                                 if r.is_late_bound() {
205                                     None
206                                 } else {
207                                     Some(ty::Predicate::RegionOutlives(ty::Binder::dummy(
208                                         ty::OutlivesPredicate(r, r_min),
209                                     )))
210                                 }
211                             }
212
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),
217                                 )))
218                             }
219
220                             Component::UnresolvedInferenceVariable(_) => None,
221
222                             Component::Projection(_) | Component::EscapingProjection(_) => {
223                                 // We can probably do more here. This
224                                 // corresponds to a case like `<T as
225                                 // Foo<'a>>::U: 'b`.
226                                 None
227                             }
228                         })
229                         .filter(|p| visited.insert(p)),
230                 );
231             }
232         }
233     }
234 }
235
236 impl Iterator for Elaborator<'tcx> {
237     type Item = ty::Predicate<'tcx>;
238
239     fn size_hint(&self) -> (usize, Option<usize>) {
240         (self.stack.len(), None)
241     }
242
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);
247             Some(pred)
248         } else {
249             None
250         }
251     }
252 }
253
254 ///////////////////////////////////////////////////////////////////////////
255 // Supertrait iterator
256 ///////////////////////////////////////////////////////////////////////////
257
258 pub type Supertraits<'tcx> = FilterToTraits<Elaborator<'tcx>>;
259
260 pub fn supertraits<'tcx>(
261     tcx: TyCtxt<'tcx>,
262     trait_ref: ty::PolyTraitRef<'tcx>,
263 ) -> Supertraits<'tcx> {
264     elaborate_trait_ref(tcx, trait_ref).filter_to_traits()
265 }
266
267 pub fn transitive_bounds<'tcx>(
268     tcx: TyCtxt<'tcx>,
269     bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
270 ) -> Supertraits<'tcx> {
271     elaborate_trait_refs(tcx, bounds).filter_to_traits()
272 }
273
274 ///////////////////////////////////////////////////////////////////////////
275 // `TraitAliasExpander` iterator
276 ///////////////////////////////////////////////////////////////////////////
277
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> {
287     tcx: TyCtxt<'tcx>,
288     stack: Vec<TraitAliasExpansionInfo<'tcx>>,
289 }
290
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]>,
295 }
296
297 impl<'tcx> TraitAliasExpansionInfo<'tcx> {
298     fn new(trait_ref: ty::PolyTraitRef<'tcx>, span: Span) -> Self {
299         Self { path: smallvec![(trait_ref, span)] }
300     }
301
302     /// Adds diagnostic labels to `diag` for the expansion path of a trait through all intermediate
303     /// trait aliases.
304     pub fn label_with_exp_info(
305         &self,
306         diag: &mut DiagnosticBuilder<'_>,
307         top_label: &str,
308         use_desc: &str,
309     ) {
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));
314             }
315         }
316         diag.span_label(
317             self.bottom().1,
318             format!("trait alias used in trait object type ({})", use_desc),
319         );
320     }
321
322     pub fn trait_ref(&self) -> &ty::PolyTraitRef<'tcx> {
323         &self.top().0
324     }
325
326     pub fn top(&self) -> &(ty::PolyTraitRef<'tcx>, Span) {
327         self.path.last().unwrap()
328     }
329
330     pub fn bottom(&self) -> &(ty::PolyTraitRef<'tcx>, Span) {
331         self.path.first().unwrap()
332     }
333
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));
337
338         Self { path }
339     }
340 }
341
342 pub fn expand_trait_aliases<'tcx>(
343     tcx: TyCtxt<'tcx>,
344     trait_refs: impl IntoIterator<Item = (ty::PolyTraitRef<'tcx>, Span)>,
345 ) -> TraitAliasExpander<'tcx> {
346     let items: Vec<_> = trait_refs
347         .into_iter()
348         .map(|(trait_ref, span)| TraitAliasExpansionInfo::new(trait_ref, span))
349         .collect();
350     TraitAliasExpander { tcx, stack: items }
351 }
352
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
357     /// trait alias.
358     /// The return value indicates whether `item` should be yielded to the user.
359     fn expand(&mut self, item: &TraitAliasExpansionInfo<'tcx>) -> bool {
360         let tcx = self.tcx;
361         let trait_ref = item.trait_ref();
362         let pred = trait_ref.without_const().to_predicate();
363
364         debug!("expand_trait_aliases: trait_ref={:?}", trait_ref);
365
366         // Don't recurse if this bound is not a trait alias.
367         let is_alias = tcx.is_trait_alias(trait_ref.def_id());
368         if !is_alias {
369             return true;
370         }
371
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
376         }) {
377             return false;
378         }
379
380         // Get components of trait alias.
381         let predicates = tcx.super_predicates_of(trait_ref.def_id());
382
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))
387         });
388         debug!("expand_trait_aliases: items={:?}", items.clone());
389
390         self.stack.extend(items);
391
392         false
393     }
394 }
395
396 impl<'tcx> Iterator for TraitAliasExpander<'tcx> {
397     type Item = TraitAliasExpansionInfo<'tcx>;
398
399     fn size_hint(&self) -> (usize, Option<usize>) {
400         (self.stack.len(), None)
401     }
402
403     fn next(&mut self) -> Option<TraitAliasExpansionInfo<'tcx>> {
404         while let Some(item) = self.stack.pop() {
405             if self.expand(&item) {
406                 return Some(item);
407             }
408         }
409         None
410     }
411 }
412
413 ///////////////////////////////////////////////////////////////////////////
414 // Iterator over def-IDs of supertraits
415 ///////////////////////////////////////////////////////////////////////////
416
417 pub struct SupertraitDefIds<'tcx> {
418     tcx: TyCtxt<'tcx>,
419     stack: Vec<DefId>,
420     visited: FxHashSet<DefId>,
421 }
422
423 pub fn supertrait_def_ids(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SupertraitDefIds<'_> {
424     SupertraitDefIds {
425         tcx,
426         stack: vec![trait_def_id],
427         visited: Some(trait_def_id).into_iter().collect(),
428     }
429 }
430
431 impl Iterator for SupertraitDefIds<'tcx> {
432     type Item = DefId;
433
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;
438         self.stack.extend(
439             predicates
440                 .predicates
441                 .iter()
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)),
445         );
446         Some(def_id)
447     }
448 }
449
450 ///////////////////////////////////////////////////////////////////////////
451 // Other
452 ///////////////////////////////////////////////////////////////////////////
453
454 /// A filter around an iterator of predicates that makes it yield up
455 /// just trait references.
456 pub struct FilterToTraits<I> {
457     base_iterator: I,
458 }
459
460 impl<I> FilterToTraits<I> {
461     fn new(base: I) -> FilterToTraits<I> {
462         FilterToTraits { base_iterator: base }
463     }
464 }
465
466 impl<'tcx, I: Iterator<Item = ty::Predicate<'tcx>>> Iterator for FilterToTraits<I> {
467     type Item = ty::PolyTraitRef<'tcx>;
468
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());
473             }
474         }
475         None
476     }
477
478     fn size_hint(&self) -> (usize, Option<usize>) {
479         let (_, upper) = self.base_iterator.size_hint();
480         (0, upper)
481     }
482 }
483
484 ///////////////////////////////////////////////////////////////////////////
485 // Other
486 ///////////////////////////////////////////////////////////////////////////
487
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>,
494     impl_def_id: DefId,
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);
501
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);
508
509     let impl_obligations: Vec<_> = impl_obligations
510         .into_iter()
511         .chain(normalization_obligations1)
512         .chain(normalization_obligations2)
513         .collect();
514
515     (impl_trait_ref, impl_obligations)
516 }
517
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);
526
527     generic_bounds
528         .predicates
529         .iter()
530         .map(|&predicate| Obligation {
531             cause: cause.clone(),
532             recursion_depth,
533             param_env,
534             predicate,
535         })
536         .collect()
537 }
538
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> {
545     Obligation {
546         cause,
547         param_env,
548         recursion_depth,
549         predicate: trait_ref.without_const().to_predicate(),
550     }
551 }
552
553 pub fn predicate_for_trait_def(
554     tcx: TyCtxt<'tcx>,
555     param_env: ty::ParamEnv<'tcx>,
556     cause: ObligationCause<'tcx>,
557     trait_def_id: DefId,
558     recursion_depth: usize,
559     self_ty: Ty<'tcx>,
560     params: &[GenericArg<'tcx>],
561 ) -> PredicateObligation<'tcx> {
562     let trait_ref =
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)
565 }
566
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
569 /// supertrait.
570 pub fn upcast_choices(
571     tcx: TyCtxt<'tcx>,
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.
577     }
578
579     supertraits(tcx, source_trait_ref).filter(|r| r.def_id() == target_trait_def_id).collect()
580 }
581
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 {
586     let mut entries = 0;
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 {
591             entries += 1;
592         }
593     }
594     entries
595 }
596
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>(
601     tcx: TyCtxt<'tcx>,
602     object: &super::VtableObjectData<'tcx, N>,
603     method_def_id: DefId,
604 ) -> usize {
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);
613             return entries;
614         }
615         if trait_item.kind == ty::AssocKind::Method {
616             entries += 1;
617         }
618     }
619
620     bug!("get_vtable_index_of_object_method: {:?} was not found", method_def_id);
621 }
622
623 pub fn closure_trait_ref_and_return_type(
624     tcx: TyCtxt<'tcx>,
625     fn_trait_def_id: DefId,
626     self_ty: Ty<'tcx>,
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()),
633     };
634     let trait_ref = ty::TraitRef {
635         def_id: fn_trait_def_id,
636         substs: tcx.mk_substs_trait(self_ty, &[arguments_tuple.into()]),
637     };
638     ty::Binder::bind((trait_ref, sig.skip_binder().output()))
639 }
640
641 pub fn generator_trait_ref_and_outputs(
642     tcx: TyCtxt<'tcx>,
643     fn_trait_def_id: DefId,
644     self_ty: Ty<'tcx>,
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()]),
650     };
651     ty::Binder::bind((trait_ref, sig.skip_binder().yield_ty, sig.skip_binder().return_ty))
652 }
653
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) {
656         Some(hir_id) => {
657             let item = tcx.hir().expect_item(hir_id);
658             if let hir::ItemKind::Impl { defaultness, .. } = item.kind {
659                 defaultness.is_default()
660             } else {
661                 false
662             }
663         }
664         None => tcx.impl_defaultness(node_item_def_id).is_default(),
665     }
666 }
667
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())
670 }
671
672 pub enum TupleArgumentsFlag {
673     Yes,
674     No,
675 }