]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/clean/simplify.rs
New upstream version 1.54.0+dfsg1
[rustc.git] / src / librustdoc / clean / simplify.rs
CommitLineData
9fa01778 1//! Simplification of where-clauses and parameter bounds into a prettier and
9346a6ac
AL
2//! more canonical form.
3//!
ba9703b0 4//! Currently all cross-crate-inlined function use `rustc_middle::ty` to reconstruct
0731742a 5//! the AST (e.g., see all of `clean::inline`), but this is not always a
9fa01778 6//! non-lossy transformation. The current format of storage for where-clauses
9346a6ac 7//! for functions and such is simply a list of predicates. One example of this
9fa01778 8//! is that the AST predicate of: `where T: Trait<Foo = Bar>` is encoded as:
ea8adc8c 9//! `where T: Trait, <T as Trait>::Foo = Bar`.
9346a6ac
AL
10//!
11//! This module attempts to reconstruct the original where and/or parameter
12//! bounds by special casing scenarios such as these. Fun!
13
a7813a04 14use std::collections::BTreeMap;
9346a6ac 15
dfeec247 16use rustc_hir::def_id::DefId;
ba9703b0 17use rustc_middle::ty;
fc512014 18use rustc_span::Symbol;
9346a6ac 19
dfeec247 20use crate::clean;
9fa01778
XL
21use crate::clean::GenericArgs as PP;
22use crate::clean::WherePredicate as WP;
9fa01778 23use crate::core::DocContext;
9346a6ac 24
fc512014 25crate fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
9346a6ac 26 // First, partition the where clause into its separate components
b7449926 27 let mut params: BTreeMap<_, Vec<_>> = BTreeMap::new();
9346a6ac
AL
28 let mut lifetimes = Vec::new();
29 let mut equalities = Vec::new();
30 let mut tybounds = Vec::new();
ff7c6d11 31
9346a6ac
AL
32 for clause in clauses {
33 match clause {
dfeec247
XL
34 WP::BoundPredicate { ty, bounds } => match ty {
35 clean::Generic(s) => params.entry(s).or_default().extend(bounds),
36 t => tybounds.push((t, bounds)),
37 },
9346a6ac
AL
38 WP::RegionPredicate { lifetime, bounds } => {
39 lifetimes.push((lifetime, bounds));
40 }
41 WP::EqPredicate { lhs, rhs } => equalities.push((lhs, rhs)),
42 }
43 }
44
9346a6ac
AL
45 // Look for equality predicates on associated types that can be merged into
46 // general bound predicates
47 equalities.retain(|&(ref lhs, ref rhs)| {
e1599b0c
XL
48 let (self_, trait_did, name) = if let Some(p) = lhs.projection() {
49 p
50 } else {
51 return true;
9346a6ac 52 };
e1599b0c
XL
53 let generic = match self_ {
54 clean::Generic(s) => s,
9346a6ac
AL
55 _ => return true,
56 };
57 let bounds = match params.get_mut(generic) {
58 Some(bound) => bound,
59 None => return true,
60 };
e1599b0c
XL
61
62 merge_bounds(cx, bounds, trait_did, name, rhs)
9346a6ac
AL
63 });
64
65 // And finally, let's reassemble everything
66 let mut clauses = Vec::new();
dfeec247
XL
67 clauses.extend(
68 lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }),
69 );
70 clauses.extend(
71 params.into_iter().map(|(k, v)| WP::BoundPredicate { ty: clean::Generic(k), bounds: v }),
72 );
73 clauses.extend(tybounds.into_iter().map(|(ty, bounds)| WP::BoundPredicate { ty, bounds }));
74 clauses.extend(equalities.into_iter().map(|(lhs, rhs)| WP::EqPredicate { lhs, rhs }));
9346a6ac
AL
75 clauses
76}
77
fc512014 78crate fn merge_bounds(
e1599b0c
XL
79 cx: &clean::DocContext<'_>,
80 bounds: &mut Vec<clean::GenericBound>,
81 trait_did: DefId,
fc512014 82 name: Symbol,
e1599b0c
XL
83 rhs: &clean::Type,
84) -> bool {
85 !bounds.iter_mut().any(|b| {
86 let trait_ref = match *b {
87 clean::GenericBound::TraitBound(ref mut tr, _) => tr,
88 clean::GenericBound::Outlives(..) => return false,
89 };
90 let (did, path) = match trait_ref.trait_ {
dfeec247 91 clean::ResolvedPath { did, ref mut path, .. } => (did, path),
e1599b0c
XL
92 _ => return false,
93 };
94 // If this QPath's trait `trait_did` is the same as, or a supertrait
95 // of, the bound's trait `did` then we can keep going, otherwise
96 // this is just a plain old equality bound.
97 if !trait_is_same_or_supertrait(cx, did, trait_did) {
dfeec247 98 return false;
e1599b0c
XL
99 }
100 let last = path.segments.last_mut().expect("segments were empty");
101 match last.args {
102 PP::AngleBracketed { ref mut bindings, .. } => {
103 bindings.push(clean::TypeBinding {
fc512014 104 name,
dfeec247 105 kind: clean::TypeBindingKind::Equality { ty: rhs.clone() },
e1599b0c
XL
106 });
107 }
e74abb32
XL
108 PP::Parenthesized { ref mut output, .. } => match output {
109 Some(o) => assert_eq!(o, rhs),
dfeec247
XL
110 None => {
111 if *rhs != clean::Type::Tuple(Vec::new()) {
112 *output = Some(rhs.clone());
113 }
e1599b0c 114 }
dfeec247 115 },
e1599b0c
XL
116 };
117 true
118 })
119}
120
dfeec247 121fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) -> bool {
9346a6ac 122 if child == trait_ {
dfeec247 123 return true;
9346a6ac 124 }
a1dfa0c6 125 let predicates = cx.tcx.super_predicates_of(child);
e1599b0c
XL
126 debug_assert!(cx.tcx.generics_of(child).has_self);
127 let self_ty = cx.tcx.types.self_param;
dfeec247
XL
128 predicates
129 .predicates
130 .iter()
131 .filter_map(|(pred, _)| {
5869c6ff 132 if let ty::PredicateKind::Trait(pred, _) = pred.kind().skip_binder() {
3dfed10e 133 if pred.trait_ref.self_ty() == self_ty { Some(pred.def_id()) } else { None }
9e0c209e
SL
134 } else {
135 None
9346a6ac 136 }
dfeec247
XL
137 })
138 .any(|did| trait_is_same_or_supertrait(cx, did, trait_))
9346a6ac 139}