Skip to content

Commit 1d077d3

Browse files
committed
WIP: Add implied bounds to rustdoc
1 parent 14863ea commit 1d077d3

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,9 +2830,13 @@ fn clean_maybe_renamed_item<'tcx>(
28302830
generics: clean_generics(generics, cx),
28312831
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
28322832
}),
2833-
ItemKind::Struct(_, generics, variant_data) => StructItem(Struct {
2833+
ItemKind::Struct(_, _, variant_data) => StructItem(Struct {
28342834
ctor_kind: variant_data.ctor_kind(),
2835-
generics: clean_generics(generics, cx),
2835+
generics: clean_ty_generics(
2836+
cx,
2837+
cx.tcx.generics_of(def_id),
2838+
cx.tcx.predicates_of(def_id),
2839+
),
28362840
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
28372841
}),
28382842
ItemKind::Macro(_, macro_def, MacroKind::Bang) => MacroItem(Macro {

tests/rustdoc-json/implied-bounds.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ count '$.index[?(@.name=="Foo")].inner.struct.generics.params[*]' 2
2+
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.params[0].name' \"\'a\"
3+
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.params[0].kind.lifetime.outlives' []
4+
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.params[1].name' '"T"'
5+
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.params[1].kind.type.bounds' '[]'
6+
//@ count '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[*]' 1
7+
//@ count '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[*]' 1
8+
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[0].bound_predicate.type.generic' '"T"'
9+
//@ count '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[0].bound_predicate.bounds[*]' 2
10+
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[0].bound_predicate.bounds[0].trait_bound.trait.path' '"Copy"'
11+
//@ is '$.index[?(@.name=="Foo")].inner.struct.generics.where_predicates[0].bound_predicate.bounds[1].outlives' \"\'a\"
12+
// ^^^^ The last bound donesn't exist anywhere in the source code ^^^^
13+
14+
pub struct Foo<'a, T: Copy>(&'a T);
15+
// Desguars to:
16+
// pub struct Foo<'a, T>(&'a T) where T: Copy + 'a;
17+
18+
// FIXME: Needs more tests, at least
19+
// - Implied 'a: 'b

tests/rustdoc/assoc/normalize-assoc-item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ impl<Inner: Trait> Trait for Generic<Inner> {
4949
// These can't be normalized because they depend on a generic parameter.
5050
// However the user can choose whether the text should be displayed as `Inner::X` or `<Inner as Trait>::X`.
5151

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

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

5858
trait Lifetimes<'a> {

tests/rustdoc/const-generics/generic_const_exprs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
#![allow(incomplete_features)]
44
// make sure that `ConstEvaluatable` predicates dont cause rustdoc to ICE #77647
55
//@ has foo/struct.Ice.html '//pre[@class="rust item-decl"]' \
6-
// 'pub struct Ice<const N: usize> where [(); { _ }]:;'
6+
// 'pub struct Ice<const N: usize>;'
77
pub struct Ice<const N: usize> where [(); N + 1]:;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<pre class="rust item-decl"><code>pub struct Struct&lt;'a, B&gt;<div class="where">where
2-
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>{
2+
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>{
33
pub a: <a class="primitive" href="{{channel}}/std/primitive.reference.html">&amp;'a B</a>,
44
pub b: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>,
55
}</code></pre>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<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; {
1+
<pre class="rust item-decl"><code>pub struct Struct2&lt;'a, B&gt;<div class="where">where
2+
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>{
23
pub a: <a class="primitive" href="{{channel}}/std/primitive.reference.html">&amp;'a B</a>,
34
pub b: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>,
45
}</code></pre>

0 commit comments

Comments
 (0)