]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/clean/simplify.rs
New upstream version 1.63.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
c295e0f8 14use rustc_data_structures::fx::FxIndexMap;
dfeec247 15use rustc_hir::def_id::DefId;
ba9703b0 16use rustc_middle::ty;
fc512014 17use rustc_span::Symbol;
9346a6ac 18
dfeec247 19use crate::clean;
9fa01778
XL
20use crate::clean::GenericArgs as PP;
21use crate::clean::WherePredicate as WP;
9fa01778 22use crate::core::DocContext;
9346a6ac 23
923072b8 24pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
c295e0f8
XL
25 // First, partition the where clause into its separate components.
26 //
27 // We use `FxIndexMap` so that the insertion order is preserved to prevent messing up to
28 // the order of the generated bounds.
29 let mut params: FxIndexMap<Symbol, (Vec<_>, Vec<_>)> = FxIndexMap::default();
9346a6ac
AL
30 let mut lifetimes = Vec::new();
31 let mut equalities = Vec::new();
32 let mut tybounds = Vec::new();
ff7c6d11 33
9346a6ac
AL
34 for clause in clauses {
35 match clause {
136023e0
XL
36 WP::BoundPredicate { ty, bounds, bound_params } => match ty {
37 clean::Generic(s) => {
38 let (b, p) = params.entry(s).or_default();
39 b.extend(bounds);
40 p.extend(bound_params);
41 }
42 t => tybounds.push((t, (bounds, bound_params))),
dfeec247 43 },
9346a6ac
AL
44 WP::RegionPredicate { lifetime, bounds } => {
45 lifetimes.push((lifetime, bounds));
46 }
47 WP::EqPredicate { lhs, rhs } => equalities.push((lhs, rhs)),
48 }
49 }
50
9346a6ac
AL
51 // Look for equality predicates on associated types that can be merged into
52 // general bound predicates
53 equalities.retain(|&(ref lhs, ref rhs)| {
5099ac24 54 let Some((self_, trait_did, name)) = lhs.projection() else {
e1599b0c 55 return true;
9346a6ac 56 };
5e7ed085
FG
57 let clean::Generic(generic) = self_ else { return true };
58 let Some((bounds, _)) = params.get_mut(generic) else { return true };
e1599b0c
XL
59
60 merge_bounds(cx, bounds, trait_did, name, rhs)
9346a6ac
AL
61 });
62
63 // And finally, let's reassemble everything
64 let mut clauses = Vec::new();
dfeec247
XL
65 clauses.extend(
66 lifetimes.into_iter().map(|(lt, bounds)| WP::RegionPredicate { lifetime: lt, bounds }),
67 );
136023e0
XL
68 clauses.extend(params.into_iter().map(|(k, (bounds, params))| WP::BoundPredicate {
69 ty: clean::Generic(k),
70 bounds,
71 bound_params: params,
72 }));
73 clauses.extend(tybounds.into_iter().map(|(ty, (bounds, bound_params))| WP::BoundPredicate {
74 ty,
75 bounds,
76 bound_params,
77 }));
dfeec247 78 clauses.extend(equalities.into_iter().map(|(lhs, rhs)| WP::EqPredicate { lhs, rhs }));
9346a6ac
AL
79 clauses
80}
81
923072b8 82pub(crate) fn merge_bounds(
e1599b0c
XL
83 cx: &clean::DocContext<'_>,
84 bounds: &mut Vec<clean::GenericBound>,
85 trait_did: DefId,
5e7ed085 86 assoc: clean::PathSegment,
5099ac24 87 rhs: &clean::Term,
e1599b0c
XL
88) -> bool {
89 !bounds.iter_mut().any(|b| {
90 let trait_ref = match *b {
91 clean::GenericBound::TraitBound(ref mut tr, _) => tr,
92 clean::GenericBound::Outlives(..) => return false,
93 };
e1599b0c
XL
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.
c295e0f8 97 if !trait_is_same_or_supertrait(cx, trait_ref.trait_.def_id(), trait_did) {
dfeec247 98 return false;
e1599b0c 99 }
c295e0f8 100 let last = trait_ref.trait_.segments.last_mut().expect("segments were empty");
e1599b0c
XL
101 match last.args {
102 PP::AngleBracketed { ref mut bindings, .. } => {
103 bindings.push(clean::TypeBinding {
5e7ed085 104 assoc: assoc.clone(),
5099ac24 105 kind: clean::TypeBindingKind::Equality { term: rhs.clone() },
e1599b0c
XL
106 });
107 }
e74abb32 108 PP::Parenthesized { ref mut output, .. } => match output {
5099ac24 109 Some(o) => assert_eq!(&clean::Term::Type(o.as_ref().clone()), rhs),
dfeec247 110 None => {
5099ac24
FG
111 if *rhs != clean::Term::Type(clean::Type::Tuple(Vec::new())) {
112 *output = Some(Box::new(rhs.ty().unwrap().clone()));
dfeec247 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, _)| {
94222f64 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}