Skip to content

Commit 6386ffe

Browse files
authored
Rollup merge of rust-lang#103254 - fmease:fix-24183, r=GuillaumeGomez
rustdoc: do not filter out cross-crate `Self: Sized` bounds All type parameters **except `Self`** are implicitly `Sized` ([via](https://doc.rust-lang.org/nightly/std/marker/trait.Sized.html)). Previously, we disregarded the exception of `Self` and omitted cross-crate `Sized` bounds of *any* type parameter *including* `Self` when rendering. From now on, we *do* render cross-crate `Self: Sized` bounds. Most notably, in `std` we now finally properly render the `Sized` bound of the `Clone` trait as well as the `Self: Sized` bound on `Iterator::map`. Fixes rust-lang#24183. `@rustbot` label T-rustdoc A-cross-crate-reexports r? rustdoc
2 parents 6a3affb + f543770 commit 6386ffe

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

src/librustdoc/clean/mod.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -774,31 +774,36 @@ fn clean_ty_generics<'tcx>(
774774
let mut where_predicates =
775775
where_predicates.into_iter().flat_map(|p| clean_predicate(*p, cx)).collect::<Vec<_>>();
776776

777-
// Type parameters have a Sized bound by default unless removed with
778-
// ?Sized. Scan through the predicates and mark any type parameter with
779-
// a Sized bound, removing the bounds as we find them.
777+
// In the surface language, all type parameters except `Self` have an
778+
// implicit `Sized` bound unless removed with `?Sized`.
779+
// However, in the list of where-predicates below, `Sized` appears like a
780+
// normal bound: It's either present (the type is sized) or
781+
// absent (the type is unsized) but never *maybe* (i.e. `?Sized`).
780782
//
781-
// Note that associated types also have a sized bound by default, but we
783+
// This is unsuitable for rendering.
784+
// Thus, as a first step remove all `Sized` bounds that should be implicit.
785+
//
786+
// Note that associated types also have an implicit `Sized` bound but we
782787
// don't actually know the set of associated types right here so that's
783-
// handled in cleaning associated types
788+
// handled when cleaning associated types.
784789
let mut sized_params = FxHashSet::default();
785-
where_predicates.retain(|pred| match *pred {
786-
WherePredicate::BoundPredicate { ty: Generic(ref g), ref bounds, .. } => {
787-
if bounds.iter().any(|b| b.is_sized_bound(cx)) {
788-
sized_params.insert(*g);
789-
false
790-
} else {
791-
true
792-
}
790+
where_predicates.retain(|pred| {
791+
if let WherePredicate::BoundPredicate { ty: Generic(g), bounds, .. } = pred
792+
&& *g != kw::SelfUpper
793+
&& bounds.iter().any(|b| b.is_sized_bound(cx))
794+
{
795+
sized_params.insert(*g);
796+
false
797+
} else {
798+
true
793799
}
794-
_ => true,
795800
});
796801

797-
// Run through the type parameters again and insert a ?Sized
798-
// unbound for any we didn't find to be Sized.
802+
// As a final step, go through the type parameters again and insert a
803+
// `?Sized` bound for each one we didn't find to be `Sized`.
799804
for tp in &stripped_params {
800-
if matches!(tp.kind, types::GenericParamDefKind::Type { .. })
801-
&& !sized_params.contains(&tp.name)
805+
if let types::GenericParamDefKind::Type { .. } = tp.kind
806+
&& !sized_params.contains(&tp.name)
802807
{
803808
where_predicates.push(WherePredicate::BoundPredicate {
804809
ty: Type::Generic(tp.name),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![crate_type = "lib"]
2+
3+
pub trait U/*: ?Sized */ {
4+
fn modified(self) -> Self
5+
where
6+
Self: Sized
7+
{
8+
self
9+
}
10+
11+
fn touch(&self)/* where Self: ?Sized */{}
12+
}
13+
14+
pub trait S: Sized {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h4 class="code-header">fn <a href="#method.touch" class="fnname">touch</a>(&amp;self)</h4>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![crate_type = "lib"]
2+
#![crate_name = "usr"]
3+
4+
// aux-crate:issue_24183=issue-24183.rs
5+
// edition: 2021
6+
7+
// @has usr/trait.U.html
8+
// @has - '//*[@class="item-decl"]' "pub trait U {"
9+
// @has - '//*[@id="method.modified"]' \
10+
// "fn modified(self) -> Self\
11+
// where \
12+
// Self: Sized"
13+
// @snapshot method_no_where_self_sized - '//*[@id="method.touch"]/*[@class="code-header"]'
14+
pub use issue_24183::U;
15+
16+
// @has usr/trait.S.html
17+
// @has - '//*[@class="item-decl"]' 'pub trait S: Sized {'
18+
pub use issue_24183::S;

0 commit comments

Comments
 (0)