Skip to content

HACKY/INCOMPLETE: Add implied bounds to rustdoc-json #142264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/librustdoc/clean/mod.rs
Copy link
Member

@fmease fmease Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only works on structs. […]
I don't think this isn't fundamental, but should be doable with more work.

It is "fundamental" in that it doesn't work for functions which is the real question here. CC Jubilee's mention of implicit implied outlives-bounds.

For functions, predicates_of (more specifically, inferred_outlives_of which is called under the hood) does not include implied outlives-bounds. CC https://rustc-dev-guide.rust-lang.org/traits/implied-bounds.html#implicit-implied-bounds

Original file line number Diff line number Diff line change
Expand Up @@ -2830,9 +2830,13 @@ fn clean_maybe_renamed_item<'tcx>(
generics: clean_generics(generics, cx),
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
}),
ItemKind::Struct(_, generics, variant_data) => StructItem(Struct {
ItemKind::Struct(_, _, variant_data) => StructItem(Struct {
ctor_kind: variant_data.ctor_kind(),
generics: clean_generics(generics, cx),
generics: clean_ty_generics(
cx,
cx.tcx.generics_of(def_id),
cx.tcx.predicates_of(def_id),
Copy link
Member

@fmease fmease Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. predicates_of contains the (explicit) implied bounds because it's explicit_predicates_of + inferred_outlives_of.

I would just call inferred_outlives_of separately and not call predicates_of here. This way, implied bounds can be marked as implied.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d'oh that's much nicer, and solves all the problems with this approach, as we also get to keep using HIR for HTML!

),
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
}),
ItemKind::Macro(_, macro_def, MacroKind::Bang) => MacroItem(Macro {
Expand Down
19 changes: 19 additions & 0 deletions tests/rustdoc-json/implied-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@ count '$.index[?(@.name=="Foo")].inner.struct.generics.params[*]' 2
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.params[0].name' \"\'a\"
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.params[0].kind.lifetime.outlives' []
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.params[1].name' '"T"'
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.params[1].kind.type.bounds' '[]'
//@ count '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[*]' 1
//@ count '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[*]' 1
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[0].bound_predicate.type.generic' '"T"'
//@ count '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[0].bound_predicate.bounds[*]' 2
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[0].bound_predicate.bounds[0].trait_bound.trait.path' '"Copy"'
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[0].bound_predicate.bounds[1].outlives' \"\'a\"
// ^^^^ The last bound donesn't exist anywhere in the source code ^^^^

pub struct Foo<'a, T: Copy>(&'a T);
// Desguars to:
// pub struct Foo<'a, T>(&'a T) where T: Copy + 'a;

// FIXME: Needs more tests, at least
// - Implied 'a: 'b
4 changes: 2 additions & 2 deletions tests/rustdoc/assoc/normalize-assoc-item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl<Inner: Trait> Trait for Generic<Inner> {
// These can't be normalized because they depend on a generic parameter.
// However the user can choose whether the text should be displayed as `Inner::X` or `<Inner as Trait>::X`.

//@ has 'normalize_assoc_item/struct.Unknown.html' '//pre[@class="rust item-decl"]' 'pub struct Unknown<Inner: Trait>(pub <Inner as Trait>::X);'
//@ has 'normalize_assoc_item/struct.Unknown.html' '//pre[@class="rust item-decl"]' 'pub struct Unknown<Inner>(pub <Inner as Trait>::X) where Inner: Trait;'
pub struct Unknown<Inner: Trait>(pub <Inner as Trait>::X);

//@ has 'normalize_assoc_item/struct.Unknown2.html' '//pre[@class="rust item-decl"]' 'pub struct Unknown2<Inner: Trait>(pub Inner::X);'
//@ has 'normalize_assoc_item/struct.Unknown2.html' '//pre[@class="rust item-decl"]' 'pub struct Unknown2<Inner>(pub Inner::X) where Inner: Trait;'
pub struct Unknown2<Inner: Trait>(pub Inner::X);

trait Lifetimes<'a> {
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc/const-generics/generic_const_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
#![allow(incomplete_features)]
// make sure that `ConstEvaluatable` predicates dont cause rustdoc to ICE #77647
//@ has foo/struct.Ice.html '//pre[@class="rust item-decl"]' \
// 'pub struct Ice<const N: usize> where [(); { _ }]:;'
// 'pub struct Ice<const N: usize>;'
pub struct Ice<const N: usize> where [(); N + 1]:;
2 changes: 1 addition & 1 deletion tests/rustdoc/whitespace-after-where-clause.struct.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<pre class="rust item-decl"><code>pub struct Struct&lt;'a, B&gt;<div class="where">where
B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>&gt; + ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a,</div>{
B: 'a + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>&gt; + ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div>{
pub a: <a class="primitive" href="{{channel}}/std/primitive.reference.html">&amp;'a B</a>,
pub b: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>,
}</code></pre>
3 changes: 2 additions & 1 deletion tests/rustdoc/whitespace-after-where-clause.struct2.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<pre class="rust item-decl"><code>pub struct Struct2&lt;'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>&gt; + 'a&gt; {
<pre class="rust item-decl"><code>pub struct Struct2&lt;'a, B&gt;<div class="where">where
B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>&gt; + 'a + ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div>{
pub a: <a class="primitive" href="{{channel}}/std/primitive.reference.html">&amp;'a B</a>,
pub b: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>,
}</code></pre>
Loading