]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/auto_trait.rs
Auto merge of #82603 - RalfJung:miri, r=RalfJung
[rust.git] / src / librustdoc / clean / auto_trait.rs
1 use rustc_data_structures::fx::FxHashSet;
2 use rustc_hir as hir;
3 use rustc_hir::lang_items::LangItem;
4 use rustc_middle::ty::{self, Region, RegionVid, TypeFoldable};
5 use rustc_trait_selection::traits::auto_trait::{self, AutoTraitResult};
6
7 use std::fmt::Debug;
8
9 use super::*;
10
11 #[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
12 enum RegionTarget<'tcx> {
13     Region(Region<'tcx>),
14     RegionVid(RegionVid),
15 }
16
17 #[derive(Default, Debug, Clone)]
18 struct RegionDeps<'tcx> {
19     larger: FxHashSet<RegionTarget<'tcx>>,
20     smaller: FxHashSet<RegionTarget<'tcx>>,
21 }
22
23 crate struct AutoTraitFinder<'a, 'tcx> {
24     crate cx: &'a mut core::DocContext<'tcx>,
25 }
26
27 impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
28     crate fn new(cx: &'a mut core::DocContext<'tcx>) -> Self {
29         AutoTraitFinder { cx }
30     }
31
32     fn generate_for_trait(
33         &mut self,
34         ty: Ty<'tcx>,
35         trait_def_id: DefId,
36         param_env: ty::ParamEnv<'tcx>,
37         param_env_def_id: DefId,
38         f: &auto_trait::AutoTraitFinder<'tcx>,
39         // If this is set, show only negative trait implementations, not positive ones.
40         discard_positive_impl: bool,
41     ) -> Option<Item> {
42         let tcx = self.cx.tcx;
43         let trait_ref = ty::TraitRef { def_id: trait_def_id, substs: tcx.mk_substs_trait(ty, &[]) };
44         if !self.cx.generated_synthetics.insert((ty, trait_def_id)) {
45             debug!("get_auto_trait_impl_for({:?}): already generated, aborting", trait_ref);
46             return None;
47         }
48
49         let result = f.find_auto_trait_generics(ty, param_env, trait_def_id, |infcx, info| {
50             let region_data = info.region_data;
51
52             let names_map = tcx
53                 .generics_of(param_env_def_id)
54                 .params
55                 .iter()
56                 .filter_map(|param| match param.kind {
57                     ty::GenericParamDefKind::Lifetime => Some(param.name),
58                     _ => None,
59                 })
60                 .map(|name| (name, Lifetime(name)))
61                 .collect();
62             let lifetime_predicates = Self::handle_lifetimes(&region_data, &names_map);
63             let new_generics = self.param_env_to_generics(
64                 infcx.tcx,
65                 param_env_def_id,
66                 info.full_user_env,
67                 lifetime_predicates,
68                 info.vid_to_region,
69             );
70
71             debug!(
72                 "find_auto_trait_generics(param_env_def_id={:?}, trait_def_id={:?}): \
73                     finished with {:?}",
74                 param_env_def_id, trait_def_id, new_generics
75             );
76
77             new_generics
78         });
79
80         let negative_polarity;
81         let new_generics = match result {
82             AutoTraitResult::PositiveImpl(new_generics) => {
83                 negative_polarity = false;
84                 if discard_positive_impl {
85                     return None;
86                 }
87                 new_generics
88             }
89             AutoTraitResult::NegativeImpl => {
90                 negative_polarity = true;
91
92                 // For negative impls, we use the generic params, but *not* the predicates,
93                 // from the original type. Otherwise, the displayed impl appears to be a
94                 // conditional negative impl, when it's really unconditional.
95                 //
96                 // For example, consider the struct Foo<T: Copy>(*mut T). Using
97                 // the original predicates in our impl would cause us to generate
98                 // `impl !Send for Foo<T: Copy>`, which makes it appear that Foo
99                 // implements Send where T is not copy.
100                 //
101                 // Instead, we generate `impl !Send for Foo<T>`, which better
102                 // expresses the fact that `Foo<T>` never implements `Send`,
103                 // regardless of the choice of `T`.
104                 let params = (tcx.generics_of(param_env_def_id), ty::GenericPredicates::default())
105                     .clean(self.cx)
106                     .params;
107
108                 Generics { params, where_predicates: Vec::new() }
109             }
110             AutoTraitResult::ExplicitImpl => return None,
111         };
112
113         Some(Item {
114             source: Span::dummy(),
115             name: None,
116             attrs: Default::default(),
117             visibility: Inherited,
118             def_id: self.cx.next_def_id(param_env_def_id.krate),
119             kind: box ImplItem(Impl {
120                 unsafety: hir::Unsafety::Normal,
121                 generics: new_generics,
122                 provided_trait_methods: Default::default(),
123                 trait_: Some(trait_ref.clean(self.cx).get_trait_type().unwrap()),
124                 for_: ty.clean(self.cx),
125                 items: Vec::new(),
126                 negative_polarity,
127                 synthetic: true,
128                 blanket_impl: None,
129             }),
130         })
131     }
132
133     // FIXME(eddyb) figure out a better way to pass information about
134     // parametrization of `ty` than `param_env_def_id`.
135     crate fn get_auto_trait_impls(&mut self, ty: Ty<'tcx>, param_env_def_id: DefId) -> Vec<Item> {
136         let tcx = self.cx.tcx;
137         let param_env = tcx.param_env(param_env_def_id);
138         let f = auto_trait::AutoTraitFinder::new(self.cx.tcx);
139
140         debug!("get_auto_trait_impls({:?})", ty);
141         let auto_traits: Vec<_> = self.cx.auto_traits.iter().cloned().collect();
142         let mut auto_traits: Vec<Item> = auto_traits
143             .into_iter()
144             .filter_map(|trait_def_id| {
145                 self.generate_for_trait(ty, trait_def_id, param_env, param_env_def_id, &f, false)
146             })
147             .collect();
148         // We are only interested in case the type *doesn't* implement the Sized trait.
149         if !ty.is_sized(self.cx.tcx.at(rustc_span::DUMMY_SP), param_env) {
150             // In case `#![no_core]` is used, `sized_trait` returns nothing.
151             if let Some(item) = self.cx.tcx.lang_items().sized_trait().and_then(|sized_trait_did| {
152                 self.generate_for_trait(ty, sized_trait_did, param_env, param_env_def_id, &f, true)
153             }) {
154                 auto_traits.push(item);
155             }
156         }
157         auto_traits
158     }
159
160     fn get_lifetime(region: Region<'_>, names_map: &FxHashMap<Symbol, Lifetime>) -> Lifetime {
161         region_name(region)
162             .map(|name| {
163                 names_map.get(&name).unwrap_or_else(|| {
164                     panic!("Missing lifetime with name {:?} for {:?}", name.as_str(), region)
165                 })
166             })
167             .unwrap_or(&Lifetime::statik())
168             .clone()
169     }
170
171     // This method calculates two things: Lifetime constraints of the form 'a: 'b,
172     // and region constraints of the form ReVar: 'a
173     //
174     // This is essentially a simplified version of lexical_region_resolve. However,
175     // handle_lifetimes determines what *needs be* true in order for an impl to hold.
176     // lexical_region_resolve, along with much of the rest of the compiler, is concerned
177     // with determining if a given set up constraints/predicates *are* met, given some
178     // starting conditions (e.g., user-provided code). For this reason, it's easier
179     // to perform the calculations we need on our own, rather than trying to make
180     // existing inference/solver code do what we want.
181     fn handle_lifetimes<'cx>(
182         regions: &RegionConstraintData<'cx>,
183         names_map: &FxHashMap<Symbol, Lifetime>,
184     ) -> Vec<WherePredicate> {
185         // Our goal is to 'flatten' the list of constraints by eliminating
186         // all intermediate RegionVids. At the end, all constraints should
187         // be between Regions (aka region variables). This gives us the information
188         // we need to create the Generics.
189         let mut finished: FxHashMap<_, Vec<_>> = Default::default();
190
191         let mut vid_map: FxHashMap<RegionTarget<'_>, RegionDeps<'_>> = Default::default();
192
193         // Flattening is done in two parts. First, we insert all of the constraints
194         // into a map. Each RegionTarget (either a RegionVid or a Region) maps
195         // to its smaller and larger regions. Note that 'larger' regions correspond
196         // to sub-regions in Rust code (e.g., in 'a: 'b, 'a is the larger region).
197         for constraint in regions.constraints.keys() {
198             match constraint {
199                 &Constraint::VarSubVar(r1, r2) => {
200                     {
201                         let deps1 = vid_map.entry(RegionTarget::RegionVid(r1)).or_default();
202                         deps1.larger.insert(RegionTarget::RegionVid(r2));
203                     }
204
205                     let deps2 = vid_map.entry(RegionTarget::RegionVid(r2)).or_default();
206                     deps2.smaller.insert(RegionTarget::RegionVid(r1));
207                 }
208                 &Constraint::RegSubVar(region, vid) => {
209                     let deps = vid_map.entry(RegionTarget::RegionVid(vid)).or_default();
210                     deps.smaller.insert(RegionTarget::Region(region));
211                 }
212                 &Constraint::VarSubReg(vid, region) => {
213                     let deps = vid_map.entry(RegionTarget::RegionVid(vid)).or_default();
214                     deps.larger.insert(RegionTarget::Region(region));
215                 }
216                 &Constraint::RegSubReg(r1, r2) => {
217                     // The constraint is already in the form that we want, so we're done with it
218                     // Desired order is 'larger, smaller', so flip then
219                     if region_name(r1) != region_name(r2) {
220                         finished
221                             .entry(region_name(r2).expect("no region_name found"))
222                             .or_default()
223                             .push(r1);
224                     }
225                 }
226             }
227         }
228
229         // Here, we 'flatten' the map one element at a time.
230         // All of the element's sub and super regions are connected
231         // to each other. For example, if we have a graph that looks like this:
232         //
233         // (A, B) - C - (D, E)
234         // Where (A, B) are subregions, and (D,E) are super-regions
235         //
236         // then after deleting 'C', the graph will look like this:
237         //  ... - A - (D, E ...)
238         //  ... - B - (D, E, ...)
239         //  (A, B, ...) - D - ...
240         //  (A, B, ...) - E - ...
241         //
242         //  where '...' signifies the existing sub and super regions of an entry
243         //  When two adjacent ty::Regions are encountered, we've computed a final
244         //  constraint, and add it to our list. Since we make sure to never re-add
245         //  deleted items, this process will always finish.
246         while !vid_map.is_empty() {
247             let target = *vid_map.keys().next().expect("Keys somehow empty");
248             let deps = vid_map.remove(&target).expect("Entry somehow missing");
249
250             for smaller in deps.smaller.iter() {
251                 for larger in deps.larger.iter() {
252                     match (smaller, larger) {
253                         (&RegionTarget::Region(r1), &RegionTarget::Region(r2)) => {
254                             if region_name(r1) != region_name(r2) {
255                                 finished
256                                     .entry(region_name(r2).expect("no region name found"))
257                                     .or_default()
258                                     .push(r1) // Larger, smaller
259                             }
260                         }
261                         (&RegionTarget::RegionVid(_), &RegionTarget::Region(_)) => {
262                             if let Entry::Occupied(v) = vid_map.entry(*smaller) {
263                                 let smaller_deps = v.into_mut();
264                                 smaller_deps.larger.insert(*larger);
265                                 smaller_deps.larger.remove(&target);
266                             }
267                         }
268                         (&RegionTarget::Region(_), &RegionTarget::RegionVid(_)) => {
269                             if let Entry::Occupied(v) = vid_map.entry(*larger) {
270                                 let deps = v.into_mut();
271                                 deps.smaller.insert(*smaller);
272                                 deps.smaller.remove(&target);
273                             }
274                         }
275                         (&RegionTarget::RegionVid(_), &RegionTarget::RegionVid(_)) => {
276                             if let Entry::Occupied(v) = vid_map.entry(*smaller) {
277                                 let smaller_deps = v.into_mut();
278                                 smaller_deps.larger.insert(*larger);
279                                 smaller_deps.larger.remove(&target);
280                             }
281
282                             if let Entry::Occupied(v) = vid_map.entry(*larger) {
283                                 let larger_deps = v.into_mut();
284                                 larger_deps.smaller.insert(*smaller);
285                                 larger_deps.smaller.remove(&target);
286                             }
287                         }
288                     }
289                 }
290             }
291         }
292
293         let lifetime_predicates = names_map
294             .iter()
295             .flat_map(|(name, lifetime)| {
296                 let empty = Vec::new();
297                 let bounds: FxHashSet<GenericBound> = finished
298                     .get(name)
299                     .unwrap_or(&empty)
300                     .iter()
301                     .map(|region| GenericBound::Outlives(Self::get_lifetime(region, names_map)))
302                     .collect();
303
304                 if bounds.is_empty() {
305                     return None;
306                 }
307                 Some(WherePredicate::RegionPredicate {
308                     lifetime: lifetime.clone(),
309                     bounds: bounds.into_iter().collect(),
310                 })
311             })
312             .collect();
313
314         lifetime_predicates
315     }
316
317     fn extract_for_generics(
318         &self,
319         tcx: TyCtxt<'tcx>,
320         pred: ty::Predicate<'tcx>,
321     ) -> FxHashSet<GenericParamDef> {
322         let bound_predicate = pred.kind();
323         let regions = match bound_predicate.skip_binder() {
324             ty::PredicateKind::Trait(poly_trait_pred, _) => {
325                 tcx.collect_referenced_late_bound_regions(&bound_predicate.rebind(poly_trait_pred))
326             }
327             ty::PredicateKind::Projection(poly_proj_pred) => {
328                 tcx.collect_referenced_late_bound_regions(&bound_predicate.rebind(poly_proj_pred))
329             }
330             _ => return FxHashSet::default(),
331         };
332
333         regions
334             .into_iter()
335             .filter_map(|br| {
336                 match br {
337                     // We only care about named late bound regions, as we need to add them
338                     // to the 'for<>' section
339                     ty::BrNamed(_, name) => {
340                         Some(GenericParamDef { name, kind: GenericParamDefKind::Lifetime })
341                     }
342                     _ => None,
343                 }
344             })
345             .collect()
346     }
347
348     fn make_final_bounds(
349         &self,
350         ty_to_bounds: FxHashMap<Type, FxHashSet<GenericBound>>,
351         ty_to_fn: FxHashMap<Type, (Option<PolyTrait>, Option<Type>)>,
352         lifetime_to_bounds: FxHashMap<Lifetime, FxHashSet<GenericBound>>,
353     ) -> Vec<WherePredicate> {
354         ty_to_bounds
355             .into_iter()
356             .flat_map(|(ty, mut bounds)| {
357                 if let Some(data) = ty_to_fn.get(&ty) {
358                     let (poly_trait, output) =
359                         (data.0.as_ref().expect("as_ref failed").clone(), data.1.as_ref().cloned());
360                     let new_ty = match poly_trait.trait_ {
361                         Type::ResolvedPath {
362                             ref path,
363                             ref param_names,
364                             ref did,
365                             ref is_generic,
366                         } => {
367                             let mut new_path = path.clone();
368                             let last_segment =
369                                 new_path.segments.pop().expect("segments were empty");
370
371                             let (old_input, old_output) = match last_segment.args {
372                                 GenericArgs::AngleBracketed { args, .. } => {
373                                     let types = args
374                                         .iter()
375                                         .filter_map(|arg| match arg {
376                                             GenericArg::Type(ty) => Some(ty.clone()),
377                                             _ => None,
378                                         })
379                                         .collect();
380                                     (types, None)
381                                 }
382                                 GenericArgs::Parenthesized { inputs, output, .. } => {
383                                     (inputs, output)
384                                 }
385                             };
386
387                             if old_output.is_some() && old_output != output {
388                                 panic!(
389                                     "Output mismatch for {:?} {:?} {:?}",
390                                     ty, old_output, data.1
391                                 );
392                             }
393
394                             let new_params =
395                                 GenericArgs::Parenthesized { inputs: old_input, output };
396
397                             new_path
398                                 .segments
399                                 .push(PathSegment { name: last_segment.name, args: new_params });
400
401                             Type::ResolvedPath {
402                                 path: new_path,
403                                 param_names: param_names.clone(),
404                                 did: *did,
405                                 is_generic: *is_generic,
406                             }
407                         }
408                         _ => panic!("Unexpected data: {:?}, {:?}", ty, data),
409                     };
410                     bounds.insert(GenericBound::TraitBound(
411                         PolyTrait { trait_: new_ty, generic_params: poly_trait.generic_params },
412                         hir::TraitBoundModifier::None,
413                     ));
414                 }
415                 if bounds.is_empty() {
416                     return None;
417                 }
418
419                 let mut bounds_vec = bounds.into_iter().collect();
420                 self.sort_where_bounds(&mut bounds_vec);
421
422                 Some(WherePredicate::BoundPredicate { ty, bounds: bounds_vec })
423             })
424             .chain(
425                 lifetime_to_bounds.into_iter().filter(|&(_, ref bounds)| !bounds.is_empty()).map(
426                     |(lifetime, bounds)| {
427                         let mut bounds_vec = bounds.into_iter().collect();
428                         self.sort_where_bounds(&mut bounds_vec);
429                         WherePredicate::RegionPredicate { lifetime, bounds: bounds_vec }
430                     },
431                 ),
432             )
433             .collect()
434     }
435
436     // Converts the calculated ParamEnv and lifetime information to a clean::Generics, suitable for
437     // display on the docs page. Cleaning the Predicates produces sub-optimal `WherePredicate`s,
438     // so we fix them up:
439     //
440     // * Multiple bounds for the same type are coalesced into one: e.g., 'T: Copy', 'T: Debug'
441     // becomes 'T: Copy + Debug'
442     // * Fn bounds are handled specially - instead of leaving it as 'T: Fn(), <T as Fn::Output> =
443     // K', we use the dedicated syntax 'T: Fn() -> K'
444     // * We explicitly add a '?Sized' bound if we didn't find any 'Sized' predicates for a type
445     fn param_env_to_generics(
446         &mut self,
447         tcx: TyCtxt<'tcx>,
448         param_env_def_id: DefId,
449         param_env: ty::ParamEnv<'tcx>,
450         mut existing_predicates: Vec<WherePredicate>,
451         vid_to_region: FxHashMap<ty::RegionVid, ty::Region<'tcx>>,
452     ) -> Generics {
453         debug!(
454             "param_env_to_generics(param_env_def_id={:?}, param_env={:?}, \
455              existing_predicates={:?})",
456             param_env_def_id, param_env, existing_predicates
457         );
458
459         // The `Sized` trait must be handled specially, since we only display it when
460         // it is *not* required (i.e., '?Sized')
461         let sized_trait = self.cx.tcx.require_lang_item(LangItem::Sized, None);
462
463         let mut replacer = RegionReplacer { vid_to_region: &vid_to_region, tcx };
464
465         let orig_bounds: FxHashSet<_> =
466             self.cx.tcx.param_env(param_env_def_id).caller_bounds().iter().collect();
467         let clean_where_predicates = param_env
468             .caller_bounds()
469             .iter()
470             .filter(|p| {
471                 !orig_bounds.contains(p)
472                     || match p.kind().skip_binder() {
473                         ty::PredicateKind::Trait(pred, _) => pred.def_id() == sized_trait,
474                         _ => false,
475                     }
476             })
477             .map(|p| p.fold_with(&mut replacer));
478
479         let mut generic_params =
480             (tcx.generics_of(param_env_def_id), tcx.explicit_predicates_of(param_env_def_id))
481                 .clean(self.cx)
482                 .params;
483
484         debug!(
485             "param_env_to_generics({:?}): generic_params={:?}",
486             param_env_def_id, generic_params
487         );
488
489         let mut has_sized = FxHashSet::default();
490         let mut ty_to_bounds: FxHashMap<_, FxHashSet<_>> = Default::default();
491         let mut lifetime_to_bounds: FxHashMap<_, FxHashSet<_>> = Default::default();
492         let mut ty_to_traits: FxHashMap<Type, FxHashSet<Type>> = Default::default();
493
494         let mut ty_to_fn: FxHashMap<Type, (Option<PolyTrait>, Option<Type>)> = Default::default();
495
496         for p in clean_where_predicates {
497             let (orig_p, p) = (p, p.clean(self.cx));
498             if p.is_none() {
499                 continue;
500             }
501             let p = p.unwrap();
502             match p {
503                 WherePredicate::BoundPredicate { ty, mut bounds } => {
504                     // Writing a projection trait bound of the form
505                     // <T as Trait>::Name : ?Sized
506                     // is illegal, because ?Sized bounds can only
507                     // be written in the (here, nonexistent) definition
508                     // of the type.
509                     // Therefore, we make sure that we never add a ?Sized
510                     // bound for projections
511                     if let Type::QPath { .. } = ty {
512                         has_sized.insert(ty.clone());
513                     }
514
515                     if bounds.is_empty() {
516                         continue;
517                     }
518
519                     let mut for_generics = self.extract_for_generics(tcx, orig_p);
520
521                     assert!(bounds.len() == 1);
522                     let mut b = bounds.pop().expect("bounds were empty");
523
524                     if b.is_sized_bound(self.cx) {
525                         has_sized.insert(ty.clone());
526                     } else if !b
527                         .get_trait_type()
528                         .and_then(|t| {
529                             ty_to_traits
530                                 .get(&ty)
531                                 .map(|bounds| bounds.contains(&strip_type(t.clone())))
532                         })
533                         .unwrap_or(false)
534                     {
535                         // If we've already added a projection bound for the same type, don't add
536                         // this, as it would be a duplicate
537
538                         // Handle any 'Fn/FnOnce/FnMut' bounds specially,
539                         // as we want to combine them with any 'Output' qpaths
540                         // later
541
542                         let is_fn = match &mut b {
543                             &mut GenericBound::TraitBound(ref mut p, _) => {
544                                 // Insert regions into the for_generics hash map first, to ensure
545                                 // that we don't end up with duplicate bounds (e.g., for<'b, 'b>)
546                                 for_generics.extend(p.generic_params.clone());
547                                 p.generic_params = for_generics.into_iter().collect();
548                                 self.is_fn_ty(tcx, &p.trait_)
549                             }
550                             _ => false,
551                         };
552
553                         let poly_trait = b.get_poly_trait().expect("Cannot get poly trait");
554
555                         if is_fn {
556                             ty_to_fn
557                                 .entry(ty.clone())
558                                 .and_modify(|e| *e = (Some(poly_trait.clone()), e.1.clone()))
559                                 .or_insert(((Some(poly_trait.clone())), None));
560
561                             ty_to_bounds.entry(ty.clone()).or_default();
562                         } else {
563                             ty_to_bounds.entry(ty.clone()).or_default().insert(b.clone());
564                         }
565                     }
566                 }
567                 WherePredicate::RegionPredicate { lifetime, bounds } => {
568                     lifetime_to_bounds.entry(lifetime).or_default().extend(bounds);
569                 }
570                 WherePredicate::EqPredicate { lhs, rhs } => {
571                     match lhs {
572                         Type::QPath { name: left_name, ref self_type, ref trait_ } => {
573                             let ty = &*self_type;
574                             match **trait_ {
575                                 Type::ResolvedPath {
576                                     path: ref trait_path,
577                                     ref param_names,
578                                     ref did,
579                                     ref is_generic,
580                                 } => {
581                                     let mut new_trait_path = trait_path.clone();
582
583                                     if self.is_fn_ty(tcx, trait_) && left_name == sym::Output {
584                                         ty_to_fn
585                                             .entry(*ty.clone())
586                                             .and_modify(|e| *e = (e.0.clone(), Some(rhs.clone())))
587                                             .or_insert((None, Some(rhs)));
588                                         continue;
589                                     }
590
591                                     let args = &mut new_trait_path
592                                         .segments
593                                         .last_mut()
594                                         .expect("segments were empty")
595                                         .args;
596
597                                     match args {
598                                         // Convert something like '<T as Iterator::Item> = u8'
599                                         // to 'T: Iterator<Item=u8>'
600                                         GenericArgs::AngleBracketed {
601                                             ref mut bindings, ..
602                                         } => {
603                                             bindings.push(TypeBinding {
604                                                 name: left_name,
605                                                 kind: TypeBindingKind::Equality { ty: rhs },
606                                             });
607                                         }
608                                         GenericArgs::Parenthesized { .. } => {
609                                             existing_predicates.push(WherePredicate::EqPredicate {
610                                                 lhs: lhs.clone(),
611                                                 rhs,
612                                             });
613                                             continue; // If something other than a Fn ends up
614                                             // with parenthesis, leave it alone
615                                         }
616                                     }
617
618                                     let bounds = ty_to_bounds.entry(*ty.clone()).or_default();
619
620                                     bounds.insert(GenericBound::TraitBound(
621                                         PolyTrait {
622                                             trait_: Type::ResolvedPath {
623                                                 path: new_trait_path,
624                                                 param_names: param_names.clone(),
625                                                 did: *did,
626                                                 is_generic: *is_generic,
627                                             },
628                                             generic_params: Vec::new(),
629                                         },
630                                         hir::TraitBoundModifier::None,
631                                     ));
632
633                                     // Remove any existing 'plain' bound (e.g., 'T: Iterator`) so
634                                     // that we don't see a
635                                     // duplicate bound like `T: Iterator + Iterator<Item=u8>`
636                                     // on the docs page.
637                                     bounds.remove(&GenericBound::TraitBound(
638                                         PolyTrait {
639                                             trait_: *trait_.clone(),
640                                             generic_params: Vec::new(),
641                                         },
642                                         hir::TraitBoundModifier::None,
643                                     ));
644                                     // Avoid creating any new duplicate bounds later in the outer
645                                     // loop
646                                     ty_to_traits
647                                         .entry(*ty.clone())
648                                         .or_default()
649                                         .insert(*trait_.clone());
650                                 }
651                                 _ => panic!(
652                                     "Unexpected trait {:?} for {:?}",
653                                     trait_, param_env_def_id,
654                                 ),
655                             }
656                         }
657                         _ => panic!("Unexpected LHS {:?} for {:?}", lhs, param_env_def_id),
658                     }
659                 }
660             };
661         }
662
663         let final_bounds = self.make_final_bounds(ty_to_bounds, ty_to_fn, lifetime_to_bounds);
664
665         existing_predicates.extend(final_bounds);
666
667         for param in generic_params.iter_mut() {
668             match param.kind {
669                 GenericParamDefKind::Type { ref mut default, ref mut bounds, .. } => {
670                     // We never want something like `impl<T=Foo>`.
671                     default.take();
672                     let generic_ty = Type::Generic(param.name);
673                     if !has_sized.contains(&generic_ty) {
674                         bounds.insert(0, GenericBound::maybe_sized(self.cx));
675                     }
676                 }
677                 GenericParamDefKind::Lifetime => {}
678                 GenericParamDefKind::Const { .. } => {}
679             }
680         }
681
682         self.sort_where_predicates(&mut existing_predicates);
683
684         Generics { params: generic_params, where_predicates: existing_predicates }
685     }
686
687     // Ensure that the predicates are in a consistent order. The precise
688     // ordering doesn't actually matter, but it's important that
689     // a given set of predicates always appears in the same order -
690     // both for visual consistency between 'rustdoc' runs, and to
691     // make writing tests much easier
692     #[inline]
693     fn sort_where_predicates(&self, mut predicates: &mut Vec<WherePredicate>) {
694         // We should never have identical bounds - and if we do,
695         // they're visually identical as well. Therefore, using
696         // an unstable sort is fine.
697         self.unstable_debug_sort(&mut predicates);
698     }
699
700     // Ensure that the bounds are in a consistent order. The precise
701     // ordering doesn't actually matter, but it's important that
702     // a given set of bounds always appears in the same order -
703     // both for visual consistency between 'rustdoc' runs, and to
704     // make writing tests much easier
705     #[inline]
706     fn sort_where_bounds(&self, mut bounds: &mut Vec<GenericBound>) {
707         // We should never have identical bounds - and if we do,
708         // they're visually identical as well. Therefore, using
709         // an unstable sort is fine.
710         self.unstable_debug_sort(&mut bounds);
711     }
712
713     // This might look horrendously hacky, but it's actually not that bad.
714     //
715     // For performance reasons, we use several different FxHashMaps
716     // in the process of computing the final set of where predicates.
717     // However, the iteration order of a HashMap is completely unspecified.
718     // In fact, the iteration of an FxHashMap can even vary between platforms,
719     // since FxHasher has different behavior for 32-bit and 64-bit platforms.
720     //
721     // Obviously, it's extremely undesirable for documentation rendering
722     // to be dependent on the platform it's run on. Apart from being confusing
723     // to end users, it makes writing tests much more difficult, as predicates
724     // can appear in any order in the final result.
725     //
726     // To solve this problem, we sort WherePredicates and GenericBounds
727     // by their Debug string. The thing to keep in mind is that we don't really
728     // care what the final order is - we're synthesizing an impl or bound
729     // ourselves, so any order can be considered equally valid. By sorting the
730     // predicates and bounds, however, we ensure that for a given codebase, all
731     // auto-trait impls always render in exactly the same way.
732     //
733     // Using the Debug implementation for sorting prevents us from needing to
734     // write quite a bit of almost entirely useless code (e.g., how should two
735     // Types be sorted relative to each other). It also allows us to solve the
736     // problem for both WherePredicates and GenericBounds at the same time. This
737     // approach is probably somewhat slower, but the small number of items
738     // involved (impls rarely have more than a few bounds) means that it
739     // shouldn't matter in practice.
740     fn unstable_debug_sort<T: Debug>(&self, vec: &mut Vec<T>) {
741         vec.sort_by_cached_key(|x| format!("{:?}", x))
742     }
743
744     fn is_fn_ty(&self, tcx: TyCtxt<'_>, ty: &Type) -> bool {
745         match ty {
746             &Type::ResolvedPath { did, .. } => {
747                 did == tcx.require_lang_item(LangItem::Fn, None)
748                     || did == tcx.require_lang_item(LangItem::FnMut, None)
749                     || did == tcx.require_lang_item(LangItem::FnOnce, None)
750             }
751             _ => false,
752         }
753     }
754 }
755
756 fn region_name(region: Region<'_>) -> Option<Symbol> {
757     match region {
758         &ty::ReEarlyBound(r) => Some(r.name),
759         _ => None,
760     }
761 }
762
763 // Replaces all ReVars in a type with ty::Region's, using the provided map
764 struct RegionReplacer<'a, 'tcx> {
765     vid_to_region: &'a FxHashMap<ty::RegionVid, ty::Region<'tcx>>,
766     tcx: TyCtxt<'tcx>,
767 }
768
769 impl<'a, 'tcx> TypeFolder<'tcx> for RegionReplacer<'a, 'tcx> {
770     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
771         self.tcx
772     }
773
774     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
775         (match r {
776             &ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned(),
777             _ => None,
778         })
779         .unwrap_or_else(|| r.super_fold_with(self))
780     }
781 }