Skip to content

Commit 34d39eb

Browse files
authored
Rollup merge of #142275 - aDotInTheVoid:gen-ty-of, r=fmease
rustdoc: Refractor `clean_ty_generics` Refactoring towards #142226 [Zulip Discussion](https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Why.20sometimes.20.60predicates_of.60.20.28vs.20.60explicit_predicates_.2E.2E.2E/near/523182529) The old `clean_ty_generics` was almost always called with the same args, so rename it to `clean_ty_generics_inner`, and add a new wrapper that generates those args from a `DefId`. Having this be the main entrypoint to `clean_ty_generics` should make it easier to start calling `inferred_outlives_of` #142264 (comment) (and is more readable even if we don't) Also, replaces all calls in rustdoc to `tcx.predicates_of` to `tcx.explicit_predicates_of`, which lets us remove `filter_non_trait_generics` r? ```@fmease```
2 parents a2badeb + 856c997 commit 34d39eb

File tree

4 files changed

+19
-74
lines changed

4 files changed

+19
-74
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tracing::{debug, instrument};
1111

1212
use crate::clean::{
1313
self, Lifetime, clean_generic_param_def, clean_middle_ty, clean_predicate,
14-
clean_trait_ref_with_constraints, clean_ty_generics, simplify,
14+
clean_trait_ref_with_constraints, clean_ty_generics_inner, simplify,
1515
};
1616
use crate::core::DocContext;
1717

@@ -101,7 +101,7 @@ fn synthesize_auto_trait_impl<'tcx>(
101101
// Instead, we generate `impl !Send for Foo<T>`, which better
102102
// expresses the fact that `Foo<T>` never implements `Send`,
103103
// regardless of the choice of `T`.
104-
let mut generics = clean_ty_generics(
104+
let mut generics = clean_ty_generics_inner(
105105
cx,
106106
tcx.generics_of(item_def_id),
107107
ty::GenericPredicates::default(),

src/librustdoc/clean/blanket_impl.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ pub(crate) fn synthesize_blanket_impls(
9090
stability: None,
9191
kind: clean::ImplItem(Box::new(clean::Impl {
9292
safety: hir::Safety::Safe,
93-
generics: clean_ty_generics(
94-
cx,
95-
tcx.generics_of(impl_def_id),
96-
tcx.explicit_predicates_of(impl_def_id),
97-
),
93+
generics: clean_ty_generics(cx, impl_def_id),
9894
// FIXME(eddyb) compute both `trait_` and `for_` from
9995
// the post-inference `trait_ref`, as it's more accurate.
10096
trait_: Some(clean_trait_ref_with_constraints(

src/librustdoc/clean/inline.rs

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -264,25 +264,21 @@ pub(crate) fn build_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
264264
.map(|item| clean_middle_assoc_item(item, cx))
265265
.collect();
266266

267-
let predicates = cx.tcx.predicates_of(did);
268-
let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
269-
let generics = filter_non_trait_generics(did, generics);
267+
let generics = clean_ty_generics(cx, did);
270268
let (generics, supertrait_bounds) = separate_self_bounds(generics);
271269
clean::Trait { def_id: did, generics, items: trait_items, bounds: supertrait_bounds }
272270
}
273271

274272
fn build_trait_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::TraitAlias {
275-
let predicates = cx.tcx.predicates_of(did);
276-
let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
273+
let generics = clean_ty_generics(cx, did);
277274
let (generics, bounds) = separate_self_bounds(generics);
278275
clean::TraitAlias { generics, bounds }
279276
}
280277

281278
pub(super) fn build_function(cx: &mut DocContext<'_>, def_id: DefId) -> Box<clean::Function> {
282279
let sig = cx.tcx.fn_sig(def_id).instantiate_identity();
283280
// The generics need to be cleaned before the signature.
284-
let mut generics =
285-
clean_ty_generics(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id));
281+
let mut generics = clean_ty_generics(cx, def_id);
286282
let bound_vars = clean_bound_vars(sig.bound_vars());
287283

288284
// At the time of writing early & late-bound params are stored separately in rustc,
@@ -311,30 +307,26 @@ pub(super) fn build_function(cx: &mut DocContext<'_>, def_id: DefId) -> Box<clea
311307
}
312308

313309
fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
314-
let predicates = cx.tcx.explicit_predicates_of(did);
315-
316310
clean::Enum {
317-
generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
311+
generics: clean_ty_generics(cx, did),
318312
variants: cx.tcx.adt_def(did).variants().iter().map(|v| clean_variant_def(v, cx)).collect(),
319313
}
320314
}
321315

322316
fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
323-
let predicates = cx.tcx.explicit_predicates_of(did);
324317
let variant = cx.tcx.adt_def(did).non_enum_variant();
325318

326319
clean::Struct {
327320
ctor_kind: variant.ctor_kind(),
328-
generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
321+
generics: clean_ty_generics(cx, did),
329322
fields: variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(),
330323
}
331324
}
332325

333326
fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
334-
let predicates = cx.tcx.explicit_predicates_of(did);
335327
let variant = cx.tcx.adt_def(did).non_enum_variant();
336328

337-
let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
329+
let generics = clean_ty_generics(cx, did);
338330
let fields = variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect();
339331
clean::Union { generics, fields }
340332
}
@@ -344,14 +336,13 @@ fn build_type_alias(
344336
did: DefId,
345337
ret: &mut Vec<Item>,
346338
) -> Box<clean::TypeAlias> {
347-
let predicates = cx.tcx.explicit_predicates_of(did);
348339
let ty = cx.tcx.type_of(did).instantiate_identity();
349340
let type_ = clean_middle_ty(ty::Binder::dummy(ty), cx, Some(did), None);
350341
let inner_type = clean_ty_alias_inner_type(ty, cx, ret);
351342

352343
Box::new(clean::TypeAlias {
353344
type_,
354-
generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
345+
generics: clean_ty_generics(cx, did),
355346
inner_type,
356347
item_type: None,
357348
})
@@ -483,7 +474,6 @@ pub(crate) fn build_impl(
483474
}
484475

485476
let document_hidden = cx.render_options.document_hidden;
486-
let predicates = tcx.explicit_predicates_of(did);
487477
let (trait_items, generics) = match impl_item {
488478
Some(impl_) => (
489479
impl_
@@ -549,9 +539,7 @@ pub(crate) fn build_impl(
549539
})
550540
.map(|item| clean_middle_assoc_item(item, cx))
551541
.collect::<Vec<_>>(),
552-
clean::enter_impl_trait(cx, |cx| {
553-
clean_ty_generics(cx, tcx.generics_of(did), predicates)
554-
}),
542+
clean::enter_impl_trait(cx, |cx| clean_ty_generics(cx, did)),
555543
),
556544
};
557545
let polarity = tcx.impl_polarity(did);
@@ -713,8 +701,7 @@ pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
713701
}
714702

715703
fn build_const_item(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
716-
let mut generics =
717-
clean_ty_generics(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id));
704+
let mut generics = clean_ty_generics(cx, def_id);
718705
clean::simplify::move_bounds_to_generic_parameters(&mut generics);
719706
let ty = clean_middle_ty(
720707
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
@@ -761,44 +748,6 @@ fn build_macro(
761748
}
762749
}
763750

764-
/// A trait's generics clause actually contains all of the predicates for all of
765-
/// its associated types as well. We specifically move these clauses to the
766-
/// associated types instead when displaying, so when we're generating the
767-
/// generics for the trait itself we need to be sure to remove them.
768-
/// We also need to remove the implied "recursive" Self: Trait bound.
769-
///
770-
/// The inverse of this filtering logic can be found in the `Clean`
771-
/// implementation for `AssociatedType`
772-
fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean::Generics {
773-
for pred in &mut g.where_predicates {
774-
if let clean::WherePredicate::BoundPredicate { ty: clean::SelfTy, ref mut bounds, .. } =
775-
*pred
776-
{
777-
bounds.retain(|bound| match bound {
778-
clean::GenericBound::TraitBound(clean::PolyTrait { trait_, .. }, _) => {
779-
trait_.def_id() != trait_did
780-
}
781-
_ => true,
782-
});
783-
}
784-
}
785-
786-
g.where_predicates.retain(|pred| match pred {
787-
clean::WherePredicate::BoundPredicate {
788-
ty:
789-
clean::QPath(box clean::QPathData {
790-
self_type: clean::Generic(_),
791-
trait_: Some(trait_),
792-
..
793-
}),
794-
bounds,
795-
..
796-
} => !bounds.is_empty() && trait_.def_id() != trait_did,
797-
_ => true,
798-
});
799-
g
800-
}
801-
802751
fn separate_self_bounds(mut g: clean::Generics) -> (clean::Generics, Vec<clean::GenericBound>) {
803752
let mut ty_bounds = Vec::new();
804753
g.where_predicates.retain(|pred| match *pred {

src/librustdoc/clean/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,11 @@ pub(crate) fn clean_generics<'tcx>(
793793
}
794794
}
795795

796-
fn clean_ty_generics<'tcx>(
796+
fn clean_ty_generics<'tcx>(cx: &mut DocContext<'tcx>, def_id: DefId) -> Generics {
797+
clean_ty_generics_inner(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id))
798+
}
799+
800+
fn clean_ty_generics_inner<'tcx>(
797801
cx: &mut DocContext<'tcx>,
798802
gens: &ty::Generics,
799803
preds: ty::GenericPredicates<'tcx>,
@@ -1297,11 +1301,7 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
12971301
None,
12981302
);
12991303

1300-
let mut generics = clean_ty_generics(
1301-
cx,
1302-
tcx.generics_of(assoc_item.def_id),
1303-
tcx.explicit_predicates_of(assoc_item.def_id),
1304-
);
1304+
let mut generics = clean_ty_generics(cx, assoc_item.def_id);
13051305
simplify::move_bounds_to_generic_parameters(&mut generics);
13061306

13071307
match assoc_item.container {
@@ -1389,7 +1389,7 @@ pub(crate) fn clean_middle_assoc_item(assoc_item: &ty::AssocItem, cx: &mut DocCo
13891389
let bounds = tcx.explicit_item_bounds(assoc_item.def_id).iter_identity_copied();
13901390
predicates = tcx.arena.alloc_from_iter(bounds.chain(predicates.iter().copied()));
13911391
}
1392-
let mut generics = clean_ty_generics(
1392+
let mut generics = clean_ty_generics_inner(
13931393
cx,
13941394
tcx.generics_of(assoc_item.def_id),
13951395
ty::GenericPredicates { parent: None, predicates },

0 commit comments

Comments
 (0)