Skip to main content

rustdoc/clean/
auto_trait.rs

1use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
2use rustc_data_structures::thin_vec::ThinVec;
3use rustc_hir as hir;
4use rustc_infer::infer::region_constraints::{ConstraintKind, RegionConstraintData};
5use rustc_middle::ty::{self, CallerBoundsIterator, Region, Ty, fold_regions};
6use rustc_span::bug;
7use rustc_span::def_id::DefId;
8use rustc_span::symbol::{Symbol, kw};
9use rustc_trait_selection::traits::auto_trait::{self, RegionTarget};
10use tracing::{debug, instrument};
11
12use crate::clean::{
13    self, Lifetime, clean_clause, clean_generic_param_def, clean_middle_ty,
14    clean_trait_ref_with_constraints, clean_ty_generics_inner, simplify,
15};
16use crate::core::DocContext;
17
18#[instrument(level = "debug", skip(cx))]
19pub(crate) fn synthesize_auto_trait_impls<'tcx>(
20    cx: &mut DocContext<'tcx>,
21    item_def_id: DefId,
22) -> Vec<clean::Item> {
23    let tcx = cx.tcx;
24    let typing_env = ty::TypingEnv::non_body_analysis(tcx, item_def_id);
25    let ty = tcx.type_of(item_def_id).instantiate_identity().skip_norm_wip();
26
27    let finder = auto_trait::AutoTraitFinder::new(tcx);
28    let mut auto_trait_impls: Vec<_> = cx
29        .auto_traits
30        .clone()
31        .into_iter()
32        .filter_map(|trait_def_id| {
33            synthesize_auto_trait_impl(
34                cx,
35                ty,
36                trait_def_id,
37                typing_env,
38                item_def_id,
39                &finder,
40                DiscardPositiveImpls::No,
41            )
42        })
43        .collect();
44    // We are only interested in case the type *doesn't* implement the `Sized` trait.
45    if !ty.is_sized(tcx, typing_env)
46        && let Some(sized_trait_def_id) = tcx.lang_items().sized_trait()
47        && let Some(impl_item) = synthesize_auto_trait_impl(
48            cx,
49            ty,
50            sized_trait_def_id,
51            typing_env,
52            item_def_id,
53            &finder,
54            DiscardPositiveImpls::Yes,
55        )
56    {
57        auto_trait_impls.push(impl_item);
58    }
59    auto_trait_impls
60}
61
62#[instrument(level = "debug", skip(cx, finder))]
63fn synthesize_auto_trait_impl<'tcx>(
64    cx: &mut DocContext<'tcx>,
65    ty: Ty<'tcx>,
66    trait_def_id: DefId,
67    typing_env: ty::TypingEnv<'tcx>,
68    item_def_id: DefId,
69    finder: &auto_trait::AutoTraitFinder<'tcx>,
70    discard_positive_impls: DiscardPositiveImpls,
71) -> Option<clean::Item> {
72    let tcx = cx.tcx;
73    let trait_ref = ty::Binder::dummy(ty::TraitRef::new(tcx, trait_def_id, [ty]));
74    if !cx.synthetic_auto_trait_impls.insert((ty, trait_def_id)) {
75        debug!("already generated, aborting");
76        return None;
77    }
78
79    let result = finder.find_auto_trait_generics(ty, typing_env, trait_def_id, |info| {
80        clean_param_env(cx, item_def_id, info.full_user_env, info.region_data, info.vid_to_region)
81    });
82
83    let (generics, polarity) = match result {
84        auto_trait::AutoTraitResult::PositiveImpl(generics) => {
85            if let DiscardPositiveImpls::Yes = discard_positive_impls {
86                return None;
87            }
88
89            (generics, ty::ImplPolarity::Positive)
90        }
91        auto_trait::AutoTraitResult::NegativeImpl => {
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 mut generics = clean_ty_generics_inner(
105                cx,
106                tcx.generics_of(item_def_id),
107                ty::GenericClauses::default(),
108            );
109            generics.where_predicates.clear();
110
111            (generics, ty::ImplPolarity::Negative)
112        }
113        auto_trait::AutoTraitResult::NoImpl => return None,
114        auto_trait::AutoTraitResult::ExplicitImpl => return None,
115    };
116
117    super::inline::record_extern_trait(cx, trait_def_id);
118
119    Some(clean::Item {
120        inner: Box::new(clean::ItemInner {
121            name: None,
122            attrs: Default::default(),
123            stability: None,
124            kind: clean::ItemKind::Impl(Box::new(clean::Impl {
125                safety: hir::Safety::Safe,
126                generics,
127                trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
128                for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
129                items: Vec::new(),
130                polarity,
131                kind: clean::ImplKind::Auto,
132                is_deprecated: false,
133            })),
134            item_id: clean::ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
135            cfg: None,
136            inline_stmt_id: None,
137        }),
138    })
139}
140
141#[derive(Debug)]
142enum DiscardPositiveImpls {
143    Yes,
144    No,
145}
146
147#[instrument(level = "debug", skip(cx, region_data, vid_to_region))]
148fn clean_param_env<'tcx>(
149    cx: &mut DocContext<'tcx>,
150    item_def_id: DefId,
151    param_env: ty::ParamEnv<'tcx>,
152    region_data: RegionConstraintData<'tcx>,
153    vid_to_region: FxIndexMap<ty::RegionVid, ty::Region<'tcx>>,
154) -> clean::Generics {
155    let tcx = cx.tcx;
156    let generics = tcx.generics_of(item_def_id);
157
158    let params: ThinVec<_> = generics
159        .own_params
160        .iter()
161        .inspect(|param| {
162            if cfg!(debug_assertions) {
163                debug_assert!(!param.is_anonymous_lifetime());
164                if let ty::GenericParamDefKind::Type { synthetic, .. } = param.kind {
165                    debug_assert!(!synthetic && param.name != kw::SelfUpper);
166                }
167            }
168        })
169        // We're basing the generics of the synthetic auto trait impl off of the generics of the
170        // implementing type. Its generic parameters may have defaults, don't copy them over:
171        // Generic parameter defaults are meaningless in impls.
172        .map(|param| clean_generic_param_def(param, clean::ParamDefaults::No, cx))
173        .collect();
174
175    // FIXME(#111101): Incorporate the explicit predicates of the item here...
176    let item_clauses: FxIndexSet<_> =
177        tcx.param_env(item_def_id).caller_bounds().all_clauses(tcx).iter().collect();
178    let sized_trait = tcx.lang_items().sized_trait();
179    let where_predicates = cx.with_exact_param_env(param_env, |cx| {
180        param_env
181            .caller_bounds()
182            .all_clauses(tcx)
183            .iter()
184            // FIXME: ...which hopefully allows us to simplify this:
185            .filter(|clause| {
186                !item_clauses.contains(clause)
187                    || matches!(
188                        clause.skip_binder(),
189                        ty::ClauseKind::Trait(pred) if Some(pred.def_id()) == sized_trait
190                    )
191            })
192            .map(|clause| {
193                fold_regions(tcx, clause, |r, _| match r.kind() {
194                    // FIXME: Don't `unwrap_or`, I think we should panic if we encounter an infer var that
195                    // we can't map to a concrete region. However, `AutoTraitFinder` *does* leak those kinds
196                    // of `ReVar`s for some reason at the time of writing. See `rustdoc-ui/` tests.
197                    // This is in dire need of an investigation into `AutoTraitFinder`.
198                    ty::ReVar(vid) => vid_to_region.get(&vid).copied().unwrap_or(r),
199                    ty::ReEarlyParam(_) | ty::ReStatic | ty::ReBound(..) | ty::ReError(_) => r,
200                    // FIXME(#120606): `AutoTraitFinder` can actually leak placeholder regions which feels
201                    // incorrect. Needs investigation.
202                    ty::ReLateParam(_) | ty::RePlaceholder(_) | ty::ReErased => {
203                        bug!("unexpected region kind: {r:?}")
204                    }
205                })
206            })
207            .flat_map(|clause| clean_clause(clause, cx))
208            .chain(clean_region_outlives_constraints(&region_data, generics))
209            .collect()
210    });
211
212    let mut generics = clean::Generics { params, where_predicates };
213    simplify::sizedness_bounds(cx, &mut generics);
214    generics.where_predicates = simplify::where_clauses(cx.tcx, generics.where_predicates);
215    generics
216}
217
218/// Clean region outlives constraints to where-predicates.
219///
220/// This is essentially a simplified version of `lexical_region_resolve`.
221///
222/// However, here we determine what *needs to be* true in order for an impl to hold.
223/// `lexical_region_resolve`, along with much of the rest of the compiler, is concerned
224/// with determining if a given set up constraints / predicates *are* met, given some
225/// starting conditions like user-provided code.
226///
227/// For this reason, it's easier to perform the calculations we need on our own,
228/// rather than trying to make existing inference/solver code do what we want.
229fn clean_region_outlives_constraints<'tcx>(
230    regions: &RegionConstraintData<'tcx>,
231    generics: &'tcx ty::Generics,
232) -> ThinVec<clean::WherePredicate> {
233    // Our goal is to "flatten" the list of constraints by eliminating all intermediate
234    // `RegionVids` (region inference variables). At the end, all constraints should be
235    // between `Region`s. This gives us the information we need to create the where-predicates.
236    // This flattening is done in two parts.
237
238    let mut outlives_predicates = FxIndexMap::<_, Vec<_>>::default();
239    let mut map = FxIndexMap::<RegionTarget<'_>, auto_trait::RegionDeps<'_>>::default();
240
241    // (1)  We insert all of the constraints into a map.
242    // Each `RegionTarget` (a `RegionVid` or a `Region`) maps to its smaller and larger regions.
243    // Note that "larger" regions correspond to sub regions in the surface language.
244    // E.g., in `'a: 'b`, `'a` is the larger region.
245    for c in regions.constraints.iter().flat_map(|(c, _)| c.iter_outlives()) {
246        match c.kind {
247            ConstraintKind::VarSubVar => {
248                let sub_vid = c.sub.as_var();
249                let sup_vid = c.sup.as_var();
250                let deps1 = map.entry(RegionTarget::RegionVid(sub_vid)).or_default();
251                deps1.larger.insert(RegionTarget::RegionVid(sup_vid));
252
253                let deps2 = map.entry(RegionTarget::RegionVid(sup_vid)).or_default();
254                deps2.smaller.insert(RegionTarget::RegionVid(sub_vid));
255            }
256            ConstraintKind::RegSubVar => {
257                let sup_vid = c.sup.as_var();
258                let deps = map.entry(RegionTarget::RegionVid(sup_vid)).or_default();
259                deps.smaller.insert(RegionTarget::Region(c.sub));
260            }
261            ConstraintKind::VarSubReg => {
262                let sub_vid = c.sub.as_var();
263                let deps = map.entry(RegionTarget::RegionVid(sub_vid)).or_default();
264                deps.larger.insert(RegionTarget::Region(c.sup));
265            }
266            ConstraintKind::RegSubReg => {
267                // The constraint is already in the form that we want, so we're done with it
268                // The desired order is [larger, smaller], so flip them.
269                if early_bound_region_name(c.sub) != early_bound_region_name(c.sup) {
270                    outlives_predicates
271                        .entry(early_bound_region_name(c.sup).expect("no region_name found"))
272                        .or_default()
273                        .push(c.sub);
274                }
275            }
276            ConstraintKind::VarEqVar | ConstraintKind::VarEqReg | ConstraintKind::RegEqReg => {
277                unreachable!()
278            }
279        }
280    }
281
282    // (2)  Here, we "flatten" the map one element at a time. All of the elements' sub and super
283    // regions are connected to each other. For example, if we have a graph that looks like this:
284    //
285    //     (A, B) - C - (D, E)
286    //
287    // where (A, B) are sub regions, and (D,E) are super regions.
288    // Then, after deleting 'C', the graph will look like this:
289    //
290    //             ... - A - (D, E, ...)
291    //             ... - B - (D, E, ...)
292    //     (A, B, ...) - D - ...
293    //     (A, B, ...) - E - ...
294    //
295    // where '...' signifies the existing sub and super regions of an entry. When two adjacent
296    // `Region`s are encountered, we've computed a final constraint, and add it to our list.
297    // Since we make sure to never re-add deleted items, this process will always finish.
298    while !map.is_empty() {
299        let target = *map.keys().next().unwrap();
300        let deps = map.swap_remove(&target).unwrap();
301
302        for smaller in &deps.smaller {
303            for larger in &deps.larger {
304                match (smaller, larger) {
305                    (&RegionTarget::Region(smaller), &RegionTarget::Region(larger)) => {
306                        if early_bound_region_name(smaller) != early_bound_region_name(larger) {
307                            outlives_predicates
308                                .entry(
309                                    early_bound_region_name(larger).expect("no region name found"),
310                                )
311                                .or_default()
312                                .push(smaller)
313                        }
314                    }
315                    (&RegionTarget::RegionVid(_), &RegionTarget::Region(_)) => {
316                        if let IndexEntry::Occupied(v) = map.entry(*smaller) {
317                            let smaller_deps = v.into_mut();
318                            smaller_deps.larger.insert(*larger);
319                            smaller_deps.larger.swap_remove(&target);
320                        }
321                    }
322                    (&RegionTarget::Region(_), &RegionTarget::RegionVid(_)) => {
323                        if let IndexEntry::Occupied(v) = map.entry(*larger) {
324                            let deps = v.into_mut();
325                            deps.smaller.insert(*smaller);
326                            deps.smaller.swap_remove(&target);
327                        }
328                    }
329                    (&RegionTarget::RegionVid(_), &RegionTarget::RegionVid(_)) => {
330                        if let IndexEntry::Occupied(v) = map.entry(*smaller) {
331                            let smaller_deps = v.into_mut();
332                            smaller_deps.larger.insert(*larger);
333                            smaller_deps.larger.swap_remove(&target);
334                        }
335                        if let IndexEntry::Occupied(v) = map.entry(*larger) {
336                            let larger_deps = v.into_mut();
337                            larger_deps.smaller.insert(*smaller);
338                            larger_deps.smaller.swap_remove(&target);
339                        }
340                    }
341                }
342            }
343        }
344    }
345
346    let region_params: FxIndexSet<_> = generics
347        .own_params
348        .iter()
349        .filter_map(|param| match param.kind {
350            ty::GenericParamDefKind::Lifetime => Some(param.name),
351            _ => None,
352        })
353        .collect();
354
355    region_params
356        .iter()
357        .filter_map(|&name| {
358            let bounds: FxIndexSet<_> = outlives_predicates
359                .get(&name)?
360                .iter()
361                .map(|&region| {
362                    let lifetime = early_bound_region_name(region)
363                        .inspect(|name| assert!(region_params.contains(name)))
364                        .map(Lifetime)
365                        .unwrap_or(Lifetime::statik());
366                    clean::GenericBound::Outlives(lifetime)
367                })
368                .collect();
369            if bounds.is_empty() {
370                return None;
371            }
372            Some(clean::WherePredicate::RegionPredicate {
373                lifetime: Lifetime(name),
374                bounds: bounds.into_iter().collect(),
375            })
376        })
377        .collect()
378}
379
380fn early_bound_region_name(region: Region<'_>) -> Option<Symbol> {
381    match region.kind() {
382        ty::ReEarlyParam(r) => Some(r.name),
383        _ => None,
384    }
385}