1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use rustc::traits::{self, auto_trait as auto};
13 use rustc::ty::{ToPredicate, TypeFoldable};
14 use rustc::ty::subst::Subst;
15 use rustc::infer::InferOk;
17 use syntax_pos::DUMMY_SP;
19 use core::DocAccessLevels;
23 pub struct AutoTraitFinder<'a, 'tcx: 'a, 'rcx: 'a> {
24 pub cx: &'a core::DocContext<'a, 'tcx, 'rcx>,
25 pub f: auto::AutoTraitFinder<'a, 'tcx>,
28 impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
29 pub fn new(cx: &'a core::DocContext<'a, 'tcx, 'rcx>) -> Self {
30 let f = auto::AutoTraitFinder::new(&cx.tcx);
32 AutoTraitFinder { cx, f }
35 pub fn get_with_def_id(&self, def_id: DefId) -> Vec<Item> {
36 let ty = self.cx.tcx.type_of(def_id);
38 let def_ctor: fn(DefId) -> Def = match ty.sty {
39 ty::TyAdt(adt, _) => match adt.adt_kind() {
40 AdtKind::Struct => Def::Struct,
41 AdtKind::Enum => Def::Enum,
42 AdtKind::Union => Def::Union,
49 ty::TyChar => return self.get_auto_trait_impls(def_id, &move |_: DefId| {
51 ty::TyInt(x) => Def::PrimTy(hir::TyInt(x)),
52 ty::TyUint(x) => Def::PrimTy(hir::TyUint(x)),
53 ty::TyFloat(x) => Def::PrimTy(hir::TyFloat(x)),
54 ty::TyStr => Def::PrimTy(hir::TyStr),
55 ty::TyBool => Def::PrimTy(hir::TyBool),
56 ty::TyChar => Def::PrimTy(hir::TyChar),
61 debug!("Unexpected type {:?}", def_id);
66 self.get_auto_trait_impls(def_id, &def_ctor, None)
69 pub fn get_with_node_id(&self, id: ast::NodeId, name: String) -> Vec<Item> {
70 let item = &self.cx.tcx.hir.expect_item(id).node;
71 let did = self.cx.tcx.hir.local_def_id(id);
73 let def_ctor = match *item {
74 hir::ItemKind::Struct(_, _) => Def::Struct,
75 hir::ItemKind::Union(_, _) => Def::Union,
76 hir::ItemKind::Enum(_, _) => Def::Enum,
77 _ => panic!("Unexpected type {:?} {:?}", item, id),
80 self.get_auto_trait_impls(did, &def_ctor, Some(name))
83 pub fn get_auto_trait_impls<F>(
89 where F: Fn(DefId) -> Def {
97 "get_auto_trait_impls(def_id={:?}, def_ctor=...): item has doc('hidden'), \
104 let tcx = self.cx.tcx;
105 let generics = self.cx.tcx.generics_of(def_id);
107 let ty = self.cx.tcx.type_of(def_id);
108 let mut traits = Vec::new();
109 if self.cx.crate_name != Some("core".to_string()) {
110 if let ty::TyAdt(_adt, _) = ty.sty {
111 let real_name = name.clone().map(|name| Ident::from_str(&name));
112 let param_env = self.cx.tcx.param_env(def_id);
113 for &trait_def_id in self.cx.all_traits.iter() {
114 if !self.cx.access_levels.borrow().is_doc_reachable(trait_def_id) ||
115 self.cx.generated_synthetics
117 .get(&(def_id, trait_def_id))
121 self.cx.tcx.for_each_relevant_impl(trait_def_id, ty, |impl_def_id| {
122 self.cx.tcx.infer_ctxt().enter(|infcx| {
123 let generics = infcx.tcx.generics_of(impl_def_id);
124 let trait_ref = infcx.tcx.impl_trait_ref(impl_def_id).unwrap();
126 if !match infcx.tcx.type_of(impl_def_id).sty {
127 ::rustc::ty::TypeVariants::TyParam(_) => true,
133 let substs = infcx.fresh_substs_for_item(DUMMY_SP, def_id);
134 let ty2 = ty.subst(infcx.tcx, substs);
135 let param_env = param_env.subst(infcx.tcx, substs);
137 let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
138 let trait_ref = trait_ref.subst(infcx.tcx, impl_substs);
140 // Require the type the impl is implemented on to match
141 // our type, and ignore the impl if there was a mismatch.
142 let cause = traits::ObligationCause::dummy();
143 let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty2);
144 if let Ok(InferOk { value: (), obligations }) = eq_result {
145 // FIXME(eddyb) ignoring `obligations` might cause false positives.
148 let may_apply = infcx.predicate_may_hold(&traits::Obligation::new(
151 trait_ref.to_predicate(),
156 self.cx.generated_synthetics.borrow_mut()
157 .insert((def_id, trait_def_id));
158 let trait_ = hir::TraitRef {
159 path: get_path_for_type(infcx.tcx, trait_def_id, hir::def::Def::Trait),
160 ref_id: ast::DUMMY_NODE_ID,
162 let provided_trait_methods = infcx.tcx.provided_trait_methods(impl_def_id)
164 .map(|meth| meth.ident.to_string())
167 let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
168 let mut segments = path.segments.into_vec();
169 let last = segments.pop().unwrap();
171 segments.push(hir::PathSegment::new(
172 real_name.unwrap_or(last.ident),
173 self.generics_to_path_params(generics.clone()),
177 let new_path = hir::Path {
180 segments: HirVec::from_vec(segments),
184 id: ast::DUMMY_NODE_ID,
185 node: hir::Ty_::TyPath(hir::QPath::Resolved(None, P(new_path))),
187 hir_id: hir::DUMMY_HIR_ID,
191 source: Span::empty(),
193 attrs: Default::default(),
195 def_id: self.next_def_id(impl_def_id.krate),
198 inner: ImplItem(Impl {
199 unsafety: hir::Unsafety::Normal,
201 &tcx.predicates_of(impl_def_id)).clean(self.cx),
202 provided_trait_methods,
203 trait_: Some(trait_.clean(self.cx)),
204 for_: ty.clean(self.cx),
205 items: infcx.tcx.associated_items(impl_def_id).collect::<Vec<_>>().clean(self.cx),
210 debug!("{:?} => {}", trait_ref, may_apply);
219 "get_auto_trait_impls(def_id={:?}, def_ctor=..., generics={:?}",
222 let auto_traits: Vec<_> =
224 .and_then(|send_trait| {
225 self.get_auto_trait_impl_for(
233 .chain(self.get_auto_trait_impl_for(
238 tcx.require_lang_item(lang_items::SyncTraitLangItem),
240 .chain(traits.into_iter())
244 "get_auto_traits: type {:?} auto_traits {:?}",
250 fn get_auto_trait_impl_for<F>(
253 name: Option<String>,
254 generics: ty::Generics,
258 where F: Fn(DefId) -> Def {
260 .generated_synthetics
262 .insert((def_id, trait_def_id))
265 "get_auto_trait_impl_for(def_id={:?}, generics={:?}, def_ctor=..., \
266 trait_def_id={:?}): already generated, aborting",
267 def_id, generics, trait_def_id
272 let result = self.find_auto_trait_generics(def_id, trait_def_id, &generics);
274 if result.is_auto() {
275 let trait_ = hir::TraitRef {
276 path: get_path_for_type(self.cx.tcx, trait_def_id, hir::def::Def::Trait),
277 ref_id: ast::DUMMY_NODE_ID,
282 let new_generics = match result {
283 AutoTraitResult::PositiveImpl(new_generics) => {
287 AutoTraitResult::NegativeImpl => {
288 polarity = Some(ImplPolarity::Negative);
290 // For negative impls, we use the generic params, but *not* the predicates,
291 // from the original type. Otherwise, the displayed impl appears to be a
292 // conditional negative impl, when it's really unconditional.
294 // For example, consider the struct Foo<T: Copy>(*mut T). Using
295 // the original predicates in our impl would cause us to generate
296 // `impl !Send for Foo<T: Copy>`, which makes it appear that Foo
297 // implements Send where T is not copy.
299 // Instead, we generate `impl !Send for Foo<T>`, which better
300 // expresses the fact that `Foo<T>` never implements `Send`,
301 // regardless of the choice of `T`.
302 let real_generics = (&generics, &Default::default());
304 // Clean the generics, but ignore the '?Sized' bounds generated
305 // by the `Clean` impl
306 let clean_generics = real_generics.clean(self.cx);
309 params: clean_generics.params,
310 where_predicates: Vec::new(),
316 let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
317 let mut segments = path.segments.into_vec();
318 let last = segments.pop().unwrap();
320 let real_name = name.map(|name| Ident::from_str(&name));
322 segments.push(hir::PathSegment::new(
323 real_name.unwrap_or(last.ident),
324 self.generics_to_path_params(generics.clone()),
328 let new_path = hir::Path {
331 segments: HirVec::from_vec(segments),
335 id: ast::DUMMY_NODE_ID,
336 node: hir::TyKind::Path(hir::QPath::Resolved(None, P(new_path))),
338 hir_id: hir::DUMMY_HIR_ID,
342 source: Span::empty(),
344 attrs: Default::default(),
346 def_id: self.next_def_id(def_id.krate),
349 inner: ImplItem(Impl {
350 unsafety: hir::Unsafety::Normal,
351 generics: new_generics,
352 provided_trait_methods: FxHashSet(),
353 trait_: Some(trait_.clean(self.cx)),
354 for_: ty.clean(self.cx),
364 fn generics_to_path_params(&self, generics: ty::Generics) -> hir::GenericArgs {
365 let mut args = vec![];
367 for param in generics.params.iter() {
369 ty::GenericParamDefKind::Lifetime => {
370 let name = if param.name == "" {
371 hir::ParamName::Plain(keywords::StaticLifetime.ident())
373 hir::ParamName::Plain(ast::Ident::from_interned_str(param.name))
376 args.push(hir::GenericArg::Lifetime(hir::Lifetime {
377 id: ast::DUMMY_NODE_ID,
379 name: hir::LifetimeName::Param(name),
382 ty::GenericParamDefKind::Type {..} => {
383 args.push(hir::GenericArg::Type(self.ty_param_to_ty(param.clone())));
389 args: HirVec::from_vec(args),
390 bindings: HirVec::new(),
391 parenthesized: false,
395 fn ty_param_to_ty(&self, param: ty::GenericParamDef) -> hir::Ty {
396 debug!("ty_param_to_ty({:?}) {:?}", param, param.def_id);
398 id: ast::DUMMY_NODE_ID,
399 node: hir::TyKind::Path(hir::QPath::Resolved(
403 def: Def::TyParam(param.def_id),
404 segments: HirVec::from_vec(vec![
405 hir::PathSegment::from_ident(Ident::from_interned_str(param.name))
410 hir_id: hir::DUMMY_HIR_ID,
414 fn find_auto_trait_generics(
418 generics: &ty::Generics,
419 ) -> AutoTraitResult {
420 match self.f.find_auto_trait_generics(did, trait_did, generics,
422 let region_data = info.region_data;
426 .map(|name| (name.clone(), Lifetime(name)))
428 let lifetime_predicates =
429 self.handle_lifetimes(®ion_data, &names_map);
430 let new_generics = self.param_env_to_generics(
440 "find_auto_trait_generics(did={:?}, trait_did={:?}, generics={:?}): \
442 did, trait_did, generics, new_generics
447 auto::AutoTraitResult::ExplicitImpl => AutoTraitResult::ExplicitImpl,
448 auto::AutoTraitResult::NegativeImpl => AutoTraitResult::NegativeImpl,
449 auto::AutoTraitResult::PositiveImpl(res) => AutoTraitResult::PositiveImpl(res),
453 fn get_lifetime(&self, region: Region, names_map: &FxHashMap<String, Lifetime>) -> Lifetime {
454 self.region_name(region)
456 names_map.get(&name).unwrap_or_else(|| {
457 panic!("Missing lifetime with name {:?} for {:?}", name, region)
460 .unwrap_or(&Lifetime::statik())
464 fn region_name(&self, region: Region) -> Option<String> {
466 &ty::ReEarlyBound(r) => Some(r.name.to_string()),
471 // This method calculates two things: Lifetime constraints of the form 'a: 'b,
472 // and region constraints of the form ReVar: 'a
474 // This is essentially a simplified version of lexical_region_resolve. However,
475 // handle_lifetimes determines what *needs be* true in order for an impl to hold.
476 // lexical_region_resolve, along with much of the rest of the compiler, is concerned
477 // with determining if a given set up constraints/predicates *are* met, given some
478 // starting conditions (e.g. user-provided code). For this reason, it's easier
479 // to perform the calculations we need on our own, rather than trying to make
480 // existing inference/solver code do what we want.
481 fn handle_lifetimes<'cx>(
483 regions: &RegionConstraintData<'cx>,
484 names_map: &FxHashMap<String, Lifetime>,
485 ) -> Vec<WherePredicate> {
486 // Our goal is to 'flatten' the list of constraints by eliminating
487 // all intermediate RegionVids. At the end, all constraints should
488 // be between Regions (aka region variables). This gives us the information
489 // we need to create the Generics.
490 let mut finished = FxHashMap();
492 let mut vid_map: FxHashMap<RegionTarget, RegionDeps> = FxHashMap();
494 // Flattening is done in two parts. First, we insert all of the constraints
495 // into a map. Each RegionTarget (either a RegionVid or a Region) maps
496 // to its smaller and larger regions. Note that 'larger' regions correspond
497 // to sub-regions in Rust code (e.g. in 'a: 'b, 'a is the larger region).
498 for constraint in regions.constraints.keys() {
500 &Constraint::VarSubVar(r1, r2) => {
503 .entry(RegionTarget::RegionVid(r1))
504 .or_insert_with(|| Default::default());
505 deps1.larger.insert(RegionTarget::RegionVid(r2));
509 .entry(RegionTarget::RegionVid(r2))
510 .or_insert_with(|| Default::default());
511 deps2.smaller.insert(RegionTarget::RegionVid(r1));
513 &Constraint::RegSubVar(region, vid) => {
515 .entry(RegionTarget::RegionVid(vid))
516 .or_insert_with(|| Default::default());
517 deps.smaller.insert(RegionTarget::Region(region));
519 &Constraint::VarSubReg(vid, region) => {
521 .entry(RegionTarget::RegionVid(vid))
522 .or_insert_with(|| Default::default());
523 deps.larger.insert(RegionTarget::Region(region));
525 &Constraint::RegSubReg(r1, r2) => {
526 // The constraint is already in the form that we want, so we're done with it
527 // Desired order is 'larger, smaller', so flip then
528 if self.region_name(r1) != self.region_name(r2) {
530 .entry(self.region_name(r2).unwrap())
531 .or_insert_with(|| Vec::new())
538 // Here, we 'flatten' the map one element at a time.
539 // All of the element's sub and super regions are connected
540 // to each other. For example, if we have a graph that looks like this:
542 // (A, B) - C - (D, E)
543 // Where (A, B) are subregions, and (D,E) are super-regions
545 // then after deleting 'C', the graph will look like this:
546 // ... - A - (D, E ...)
547 // ... - B - (D, E, ...)
548 // (A, B, ...) - D - ...
549 // (A, B, ...) - E - ...
551 // where '...' signifies the existing sub and super regions of an entry
552 // When two adjacent ty::Regions are encountered, we've computed a final
553 // constraint, and add it to our list. Since we make sure to never re-add
554 // deleted items, this process will always finish.
555 while !vid_map.is_empty() {
556 let target = vid_map.keys().next().expect("Keys somehow empty").clone();
557 let deps = vid_map.remove(&target).expect("Entry somehow missing");
559 for smaller in deps.smaller.iter() {
560 for larger in deps.larger.iter() {
561 match (smaller, larger) {
562 (&RegionTarget::Region(r1), &RegionTarget::Region(r2)) => {
563 if self.region_name(r1) != self.region_name(r2) {
565 .entry(self.region_name(r2).unwrap())
566 .or_insert_with(|| Vec::new())
567 .push(r1) // Larger, smaller
570 (&RegionTarget::RegionVid(_), &RegionTarget::Region(_)) => {
571 if let Entry::Occupied(v) = vid_map.entry(*smaller) {
572 let smaller_deps = v.into_mut();
573 smaller_deps.larger.insert(*larger);
574 smaller_deps.larger.remove(&target);
577 (&RegionTarget::Region(_), &RegionTarget::RegionVid(_)) => {
578 if let Entry::Occupied(v) = vid_map.entry(*larger) {
579 let deps = v.into_mut();
580 deps.smaller.insert(*smaller);
581 deps.smaller.remove(&target);
584 (&RegionTarget::RegionVid(_), &RegionTarget::RegionVid(_)) => {
585 if let Entry::Occupied(v) = vid_map.entry(*smaller) {
586 let smaller_deps = v.into_mut();
587 smaller_deps.larger.insert(*larger);
588 smaller_deps.larger.remove(&target);
591 if let Entry::Occupied(v) = vid_map.entry(*larger) {
592 let larger_deps = v.into_mut();
593 larger_deps.smaller.insert(*smaller);
594 larger_deps.smaller.remove(&target);
602 let lifetime_predicates = names_map
604 .flat_map(|(name, lifetime)| {
605 let empty = Vec::new();
606 let bounds: FxHashSet<GenericBound> = finished.get(name).unwrap_or(&empty).iter()
607 .map(|region| GenericBound::Outlives(self.get_lifetime(region, names_map)))
610 if bounds.is_empty() {
613 Some(WherePredicate::RegionPredicate {
614 lifetime: lifetime.clone(),
615 bounds: bounds.into_iter().collect(),
623 fn extract_for_generics<'b, 'c, 'd>(
625 tcx: TyCtxt<'b, 'c, 'd>,
626 pred: ty::Predicate<'d>,
627 ) -> FxHashSet<GenericParamDef> {
630 let mut regions = FxHashSet();
631 tcx.collect_regions(&t, &mut regions);
633 regions.into_iter().flat_map(|r| {
635 // We only care about late bound regions, as we need to add them
636 // to the 'for<>' section
637 &ty::ReLateBound(_, ty::BoundRegion::BrNamed(_, name)) => {
638 Some(GenericParamDef {
639 name: name.to_string(),
640 kind: GenericParamDefKind::Lifetime,
643 &ty::ReVar(_) | &ty::ReEarlyBound(_) => None,
644 _ => panic!("Unexpected region type {:?}", r),
651 fn make_final_bounds<'b, 'c, 'cx>(
653 ty_to_bounds: FxHashMap<Type, FxHashSet<GenericBound>>,
654 ty_to_fn: FxHashMap<Type, (Option<PolyTrait>, Option<Type>)>,
655 lifetime_to_bounds: FxHashMap<Lifetime, FxHashSet<GenericBound>>,
656 ) -> Vec<WherePredicate> {
659 .flat_map(|(ty, mut bounds)| {
660 if let Some(data) = ty_to_fn.get(&ty) {
661 let (poly_trait, output) =
662 (data.0.as_ref().unwrap().clone(), data.1.as_ref().cloned());
663 let new_ty = match &poly_trait.trait_ {
664 &Type::ResolvedPath {
670 let mut new_path = path.clone();
671 let last_segment = new_path.segments.pop().unwrap();
673 let (old_input, old_output) = match last_segment.args {
674 GenericArgs::AngleBracketed { types, .. } => (types, None),
675 GenericArgs::Parenthesized { inputs, output, .. } => {
680 if old_output.is_some() && old_output != output {
682 "Output mismatch for {:?} {:?} {:?}",
683 ty, old_output, data.1
687 let new_params = GenericArgs::Parenthesized {
692 new_path.segments.push(PathSegment {
693 name: last_segment.name,
699 typarams: typarams.clone(),
701 is_generic: *is_generic,
704 _ => panic!("Unexpected data: {:?}, {:?}", ty, data),
706 bounds.insert(GenericBound::TraitBound(
709 generic_params: poly_trait.generic_params,
711 hir::TraitBoundModifier::None,
714 if bounds.is_empty() {
718 let mut bounds_vec = bounds.into_iter().collect();
719 self.sort_where_bounds(&mut bounds_vec);
721 Some(WherePredicate::BoundPredicate {
729 .filter(|&(_, ref bounds)| !bounds.is_empty())
730 .map(|(lifetime, bounds)| {
731 let mut bounds_vec = bounds.into_iter().collect();
732 self.sort_where_bounds(&mut bounds_vec);
733 WherePredicate::RegionPredicate {
742 // Converts the calculated ParamEnv and lifetime information to a clean::Generics, suitable for
743 // display on the docs page. Cleaning the Predicates produces sub-optimal WherePredicate's,
744 // so we fix them up:
746 // * Multiple bounds for the same type are coalesced into one: e.g. 'T: Copy', 'T: Debug'
747 // becomes 'T: Copy + Debug'
748 // * Fn bounds are handled specially - instead of leaving it as 'T: Fn(), <T as Fn::Output> =
749 // K', we use the dedicated syntax 'T: Fn() -> K'
750 // * We explcitly add a '?Sized' bound if we didn't find any 'Sized' predicates for a type
751 fn param_env_to_generics<'b, 'c, 'cx>(
753 tcx: TyCtxt<'b, 'c, 'cx>,
755 param_env: ty::ParamEnv<'cx>,
756 type_generics: ty::Generics,
757 mut existing_predicates: Vec<WherePredicate>,
758 vid_to_region: FxHashMap<ty::RegionVid, ty::Region<'cx>>,
761 "param_env_to_generics(did={:?}, param_env={:?}, type_generics={:?}, \
762 existing_predicates={:?})",
763 did, param_env, type_generics, existing_predicates
766 // The `Sized` trait must be handled specially, since we only only display it when
767 // it is *not* required (i.e. '?Sized')
768 let sized_trait = self.cx
770 .require_lang_item(lang_items::SizedTraitLangItem);
772 let mut replacer = RegionReplacer {
773 vid_to_region: &vid_to_region,
777 let orig_bounds: FxHashSet<_> = self.cx.tcx.param_env(did).caller_bounds.iter().collect();
778 let clean_where_predicates = param_env
782 !orig_bounds.contains(p) || match p {
783 &&ty::Predicate::Trait(pred) => pred.def_id() == sized_trait,
788 let replaced = p.fold_with(&mut replacer);
789 (replaced.clone(), replaced.clean(self.cx))
792 let full_generics = (&type_generics, &tcx.predicates_of(did));
794 params: mut generic_params,
796 } = full_generics.clean(self.cx);
798 let mut has_sized = FxHashSet();
799 let mut ty_to_bounds = FxHashMap();
800 let mut lifetime_to_bounds = FxHashMap();
801 let mut ty_to_traits: FxHashMap<Type, FxHashSet<Type>> = FxHashMap();
803 let mut ty_to_fn: FxHashMap<Type, (Option<PolyTrait>, Option<Type>)> = FxHashMap();
805 for (orig_p, p) in clean_where_predicates {
807 WherePredicate::BoundPredicate { ty, mut bounds } => {
808 // Writing a projection trait bound of the form
809 // <T as Trait>::Name : ?Sized
810 // is illegal, because ?Sized bounds can only
811 // be written in the (here, nonexistant) definition
813 // Therefore, we make sure that we never add a ?Sized
814 // bound for projections
816 &Type::QPath { .. } => {
817 has_sized.insert(ty.clone());
822 if bounds.is_empty() {
826 let mut for_generics = self.extract_for_generics(tcx, orig_p.clone());
828 assert!(bounds.len() == 1);
829 let mut b = bounds.pop().unwrap();
831 if b.is_sized_bound(self.cx) {
832 has_sized.insert(ty.clone());
833 } else if !b.get_trait_type()
837 .map(|bounds| bounds.contains(&strip_type(t.clone())))
841 // If we've already added a projection bound for the same type, don't add
842 // this, as it would be a duplicate
844 // Handle any 'Fn/FnOnce/FnMut' bounds specially,
845 // as we want to combine them with any 'Output' qpaths
848 let is_fn = match &mut b {
849 &mut GenericBound::TraitBound(ref mut p, _) => {
850 // Insert regions into the for_generics hash map first, to ensure
851 // that we don't end up with duplicate bounds (e.g. for<'b, 'b>)
852 for_generics.extend(p.generic_params.clone());
853 p.generic_params = for_generics.into_iter().collect();
854 self.is_fn_ty(&tcx, &p.trait_)
859 let poly_trait = b.get_poly_trait().unwrap();
864 .and_modify(|e| *e = (Some(poly_trait.clone()), e.1.clone()))
865 .or_insert(((Some(poly_trait.clone())), None));
869 .or_insert_with(|| FxHashSet());
873 .or_insert_with(|| FxHashSet())
878 WherePredicate::RegionPredicate { lifetime, bounds } => {
881 .or_insert_with(|| FxHashSet())
884 WherePredicate::EqPredicate { lhs, rhs } => {
891 let ty = &*self_type;
894 path: ref trait_path,
899 let mut new_trait_path = trait_path.clone();
901 if self.is_fn_ty(&tcx, trait_) && left_name == FN_OUTPUT_NAME {
904 .and_modify(|e| *e = (e.0.clone(), Some(rhs.clone())))
905 .or_insert((None, Some(rhs)));
909 // FIXME: Remove this scope when NLL lands
912 &mut new_trait_path.segments.last_mut().unwrap().args;
915 // Convert somethiung like '<T as Iterator::Item> = u8'
916 // to 'T: Iterator<Item=u8>'
917 &mut GenericArgs::AngleBracketed {
921 bindings.push(TypeBinding {
922 name: left_name.clone(),
926 &mut GenericArgs::Parenthesized { .. } => {
927 existing_predicates.push(
928 WherePredicate::EqPredicate {
933 continue; // If something other than a Fn ends up
934 // with parenthesis, leave it alone
939 let bounds = ty_to_bounds
941 .or_insert_with(|| FxHashSet());
943 bounds.insert(GenericBound::TraitBound(
945 trait_: Type::ResolvedPath {
946 path: new_trait_path,
947 typarams: typarams.clone(),
949 is_generic: *is_generic,
951 generic_params: Vec::new(),
953 hir::TraitBoundModifier::None,
956 // Remove any existing 'plain' bound (e.g. 'T: Iterator`) so
957 // that we don't see a
958 // duplicate bound like `T: Iterator + Iterator<Item=u8>`
960 bounds.remove(&GenericBound::TraitBound(
962 trait_: *trait_.clone(),
963 generic_params: Vec::new(),
965 hir::TraitBoundModifier::None,
967 // Avoid creating any new duplicate bounds later in the outer
971 .or_insert_with(|| FxHashSet())
972 .insert(*trait_.clone());
974 _ => panic!("Unexpected trait {:?} for {:?}", trait_, did),
977 _ => panic!("Unexpected LHS {:?} for {:?}", lhs, did),
983 let final_bounds = self.make_final_bounds(ty_to_bounds, ty_to_fn, lifetime_to_bounds);
985 existing_predicates.extend(final_bounds);
987 for param in generic_params.iter_mut() {
989 GenericParamDefKind::Type { ref mut default, ref mut bounds, .. } => {
990 // We never want something like `impl<T=Foo>`.
992 let generic_ty = Type::Generic(param.name.clone());
993 if !has_sized.contains(&generic_ty) {
994 bounds.insert(0, GenericBound::maybe_sized(self.cx));
997 GenericParamDefKind::Lifetime => {}
1001 self.sort_where_predicates(&mut existing_predicates);
1004 params: generic_params,
1005 where_predicates: existing_predicates,
1009 // Ensure that the predicates are in a consistent order. The precise
1010 // ordering doesn't actually matter, but it's important that
1011 // a given set of predicates always appears in the same order -
1012 // both for visual consistency between 'rustdoc' runs, and to
1013 // make writing tests much easier
1015 fn sort_where_predicates(&self, mut predicates: &mut Vec<WherePredicate>) {
1016 // We should never have identical bounds - and if we do,
1017 // they're visually identical as well. Therefore, using
1018 // an unstable sort is fine.
1019 self.unstable_debug_sort(&mut predicates);
1022 // Ensure that the bounds are in a consistent order. The precise
1023 // ordering doesn't actually matter, but it's important that
1024 // a given set of bounds always appears in the same order -
1025 // both for visual consistency between 'rustdoc' runs, and to
1026 // make writing tests much easier
1028 fn sort_where_bounds(&self, mut bounds: &mut Vec<GenericBound>) {
1029 // We should never have identical bounds - and if we do,
1030 // they're visually identical as well. Therefore, using
1031 // an unstable sort is fine.
1032 self.unstable_debug_sort(&mut bounds);
1035 // This might look horrendously hacky, but it's actually not that bad.
1037 // For performance reasons, we use several different FxHashMaps
1038 // in the process of computing the final set of where predicates.
1039 // However, the iteration order of a HashMap is completely unspecified.
1040 // In fact, the iteration of an FxHashMap can even vary between platforms,
1041 // since FxHasher has different behavior for 32-bit and 64-bit platforms.
1043 // Obviously, it's extremely undesireable for documentation rendering
1044 // to be depndent on the platform it's run on. Apart from being confusing
1045 // to end users, it makes writing tests much more difficult, as predicates
1046 // can appear in any order in the final result.
1048 // To solve this problem, we sort WherePredicates and GenericBounds
1049 // by their Debug string. The thing to keep in mind is that we don't really
1050 // care what the final order is - we're synthesizing an impl or bound
1051 // ourselves, so any order can be considered equally valid. By sorting the
1052 // predicates and bounds, however, we ensure that for a given codebase, all
1053 // auto-trait impls always render in exactly the same way.
1055 // Using the Debug impementation for sorting prevents us from needing to
1056 // write quite a bit of almost entirely useless code (e.g. how should two
1057 // Types be sorted relative to each other). It also allows us to solve the
1058 // problem for both WherePredicates and GenericBounds at the same time. This
1059 // approach is probably somewhat slower, but the small number of items
1060 // involved (impls rarely have more than a few bounds) means that it
1061 // shouldn't matter in practice.
1062 fn unstable_debug_sort<T: Debug>(&self, vec: &mut Vec<T>) {
1063 vec.sort_by_cached_key(|x| format!("{:?}", x))
1066 fn is_fn_ty(&self, tcx: &TyCtxt, ty: &Type) -> bool {
1068 &&Type::ResolvedPath { ref did, .. } => {
1069 *did == tcx.require_lang_item(lang_items::FnTraitLangItem)
1070 || *did == tcx.require_lang_item(lang_items::FnMutTraitLangItem)
1071 || *did == tcx.require_lang_item(lang_items::FnOnceTraitLangItem)
1077 // This is an ugly hack, but it's the simplest way to handle synthetic impls without greatly
1078 // refactoring either librustdoc or librustc. In particular, allowing new DefIds to be
1079 // registered after the AST is constructed would require storing the defid mapping in a
1080 // RefCell, decreasing the performance for normal compilation for very little gain.
1082 // Instead, we construct 'fake' def ids, which start immediately after the last DefId in
1083 // DefIndexAddressSpace::Low. In the Debug impl for clean::Item, we explicitly check for fake
1084 // def ids, as we'll end up with a panic if we use the DefId Debug impl for fake DefIds
1085 fn next_def_id(&self, crate_num: CrateNum) -> DefId {
1086 let start_def_id = {
1087 let next_id = if crate_num == LOCAL_CRATE {
1093 .next_id(DefIndexAddressSpace::Low)
1097 .def_path_table(crate_num)
1098 .next_id(DefIndexAddressSpace::Low)
1107 let mut fake_ids = self.cx.fake_def_ids.borrow_mut();
1109 let def_id = fake_ids.entry(crate_num).or_insert(start_def_id).clone();
1114 index: DefIndex::from_array_index(
1115 def_id.index.as_array_index() + 1,
1116 def_id.index.address_space(),
1121 MAX_DEF_ID.with(|m| {
1123 .entry(def_id.krate.clone())
1124 .or_insert(start_def_id);
1127 self.cx.all_fake_def_ids.borrow_mut().insert(def_id);
1133 // Replaces all ReVars in a type with ty::Region's, using the provided map
1134 struct RegionReplacer<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
1135 vid_to_region: &'a FxHashMap<ty::RegionVid, ty::Region<'tcx>>,
1136 tcx: TyCtxt<'a, 'gcx, 'tcx>,
1139 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionReplacer<'a, 'gcx, 'tcx> {
1140 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
1144 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
1146 &ty::ReVar(vid) => self.vid_to_region.get(&vid).cloned(),
1148 }).unwrap_or_else(|| r.super_fold_with(self))