Skip to content

Commit 0c751b2

Browse files
committed
rustdoc-search: simplify rules for generics and type params
This commit is a response to feedback on the displayed type signatures results, by making type params looser and generics act stricter. Type params are loosened by allowing a single function type param to match multiple query params. In other words, the relationship is loosened to N:1 instead of the older 1:1 rule. This change also allows a type param to be unboxed in one spot and matched in another. Generics are tightened by making order significant. This means `Vec<Allocator>` now matches only with a true vector of allocators, instead of matching the second type param. It also makes unboxing within generics stricter, so `Result<A, B>` only matches if `B` is in the error type and `A` is in the success type. The top level of the function search is unaffected. Find the discussion on: * <https://rust-lang.zulipchat.com/#narrow/stream/393423-t-rustdoc.2Fmeetings/topic/meeting.202024-07-08/near/449965149> * <#124544 (comment)>
1 parent b051735 commit 0c751b2

12 files changed

+282
-130
lines changed

src/librustdoc/html/static/js/search.js

+227-66
Large diffs are not rendered by default.

tests/rustdoc-gui/search-about-this-result.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ assert: "//div[@class='tooltip popover']//strong[text()='bool']"
2626
assert: "//div[@class='tooltip popover']//strong[text()='Extend']"
2727

2828
assert-text: (".tooltip.popover h3", "About this result")
29-
assert-text: (".tooltip.popover pre code div:nth-child(1)", "Iterator::Item is T")
29+
assert-text: (".tooltip.popover pre code div:nth-child(1)", "T matches Iterator::Item")
3030
assert-text: (".tooltip.popover pre code div:nth-child(2)", "F: FnMut (&Iterator::Item) -> bool")
3131
assert-text: (".tooltip.popover pre code div:nth-child(3)", "B: Default + Extend<Iterator::Item>")
3232

tests/rustdoc-js-std/option-type-signatures.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ const EXPECTED = [
8080
'name': 'and',
8181
'displayType': '`Option`<`T`>, `Option`<`U`> -> `Option`<`U`>',
8282
},
83-
{
84-
'path': 'std::option::Option',
85-
'name': 'zip',
86-
'displayType': '`Option`<`T`>, `Option`<`U`> -> `Option`<(T, `U`)>',
87-
},
8883
],
8984
},
9085
{
@@ -103,12 +98,12 @@ const EXPECTED = [
10398
],
10499
},
105100
{
106-
'query': 'option<t>, option<u> -> option<t, u>',
101+
'query': 'option<t>, option<u> -> option<(t, u)>',
107102
'others': [
108103
{
109104
'path': 'std::option::Option',
110105
'name': 'zip',
111-
'displayType': '`Option`<`T`>, `Option`<`U`> -> `Option`<(`T`, `U`)>',
106+
'displayType': '`Option`<`T`>, `Option`<`U`> -> `Option`<`(T`, `U)`>',
112107
},
113108
],
114109
},
@@ -174,35 +169,35 @@ const EXPECTED = [
174169
'path': 'std::option::Option',
175170
'name': 'map',
176171
'displayType': '`Option`<`T`>, F -> `Option`<U>',
177-
'displayMappedNames': `T = t, U = u`,
172+
'displayMappedNames': `t = T, u = U`,
178173
'displayWhereClause': "F: `FnOnce` (T) -> `U`",
179174
},
180175
{
181176
'path': 'std::option::Option',
182177
'name': 'and_then',
183178
'displayType': '`Option`<`T`>, F -> `Option`<U>',
184-
'displayMappedNames': `T = t, U = u`,
179+
'displayMappedNames': `t = T, u = U`,
185180
'displayWhereClause': "F: `FnOnce` (T) -> Option<`U`>",
186181
},
187182
{
188183
'path': 'std::option::Option',
189184
'name': 'zip_with',
190185
'displayType': 'Option<T>, `Option`<`U`>, F -> `Option`<R>',
191-
'displayMappedNames': `U = t, R = u`,
186+
'displayMappedNames': `t = U, u = R`,
192187
'displayWhereClause': "F: `FnOnce` (T, U) -> `R`",
193188
},
194189
{
195190
'path': 'std::task::Poll',
196191
'name': 'map_ok',
197192
'displayType': 'Poll<`Option`<Result<`T`, E>>>, F -> Poll<`Option`<Result<U, E>>>',
198-
'displayMappedNames': `T = t, U = u`,
193+
'displayMappedNames': `t = T, u = U`,
199194
'displayWhereClause': "F: `FnOnce` (T) -> `U`",
200195
},
201196
{
202197
'path': 'std::task::Poll',
203198
'name': 'map_err',
204199
'displayType': 'Poll<`Option`<Result<`T`, E>>>, F -> Poll<`Option`<Result<T, U>>>',
205-
'displayMappedNames': `T = t, U = u`,
200+
'displayMappedNames': `t = T, u = U`,
206201
'displayWhereClause': "F: `FnOnce` (E) -> `U`",
207202
},
208203
],
@@ -214,7 +209,7 @@ const EXPECTED = [
214209
'path': 'std::option::Option',
215210
'name': 'and_then',
216211
'displayType': '`Option`<`T`>, F -> `Option`<U>',
217-
'displayMappedNames': `T = t, U = u`,
212+
'displayMappedNames': `t = T, u = U`,
218213
'displayWhereClause': "F: `FnOnce` (T) -> `Option`<`U`>",
219214
},
220215
],

tests/rustdoc-js-std/vec-type-signatures.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ const EXPECTED = [
2020
],
2121
},
2222
{
23-
'query': 'vec<Allocator> -> Box<[T]>',
23+
'query': 'vec<T, Allocator> -> Box<[T]>',
2424
'others': [
2525
{
2626
'path': 'std::boxed::Box',
2727
'name': 'from',
28-
'displayType': '`Vec`<T, `A`> -> `Box`<`[T]`, A>',
28+
'displayType': '`Vec`<`T`, `A`> -> `Box`<`[T]`, A>',
2929
'displayMappedNames': `T = T`,
3030
'displayWhereClause': 'A: `Allocator`',
3131
},

tests/rustdoc-js/assoc-type-backtrack.js

+25-13
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,21 @@ const EXPECTED = [
8989
],
9090
},
9191
{
92-
'query': 'myintofuture<myfuture<t>> -> myfuture<t>',
92+
'query': 'myintofuture<t, myfuture<t>> -> myfuture<t>',
9393
'correction': null,
9494
'others': [
9595
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future' },
9696
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
9797
],
9898
},
99-
// Invalid unboxing of the one-argument case.
100-
// If you unbox one of the myfutures, you need to unbox both of them.
99+
// Unboxings of the one-argument case.
101100
{
102101
'query': 'myintofuture<fut=t> -> myfuture<t>',
103102
'correction': null,
104-
'others': [],
103+
'others': [
104+
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future' },
105+
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
106+
],
105107
},
106108
// Unboxings of the two-argument case.
107109
{
@@ -119,7 +121,7 @@ const EXPECTED = [
119121
],
120122
},
121123
{
122-
'query': 'myintofuture<myfuture>, myintofuture<myfuture> -> myfuture',
124+
'query': 'myintofuture<t, myfuture>, myintofuture<t, myfuture> -> myfuture',
123125
'correction': null,
124126
'others': [
125127
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
@@ -132,32 +134,42 @@ const EXPECTED = [
132134
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
133135
],
134136
},
135-
// Invalid unboxings of the two-argument case.
136-
// If you unbox one of the myfutures, you need to unbox all of them.
137+
// If you unbox one of the myfutures, you don't need to unbox all of them.
137138
{
138139
'query': 'myintofuture<fut=t>, myintofuture<fut=myfuture<t>> -> myfuture<t>',
139140
'correction': null,
140-
'others': [],
141+
'others': [
142+
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
143+
],
141144
},
142145
{
143146
'query': 'myintofuture<fut=myfuture<t>>, myintofuture<fut=t> -> myfuture<t>',
144147
'correction': null,
145-
'others': [],
148+
'others': [
149+
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
150+
],
146151
},
147152
{
148153
'query': 'myintofuture<fut=myfuture<t>>, myintofuture<fut=myfuture<t>> -> t',
149154
'correction': null,
150-
'others': [],
155+
'others': [
156+
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
157+
],
151158
},
152-
// different generics don't match up either
159+
// different generics will match up (didn't used to, but does now)
153160
{
154161
'query': 'myintofuture<fut=myfuture<u>>, myintofuture<fut=myfuture<t>> -> myfuture<t>',
155162
'correction': null,
156-
'others': [],
163+
'others': [
164+
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
165+
],
157166
},
158167
{
159168
'query': 'myintofuture<output=t> -> myfuture<tt>',
160169
'correction': null,
161-
'others': [],
170+
'others': [
171+
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future' },
172+
{ 'path': 'assoc_type_backtrack::MyIntoFuture', 'name': 'into_future_2' },
173+
],
162174
},
163175
];

tests/rustdoc-js/assoc-type-unbound.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const EXPECTED = [
1313
'path': 'assoc_type_unbound::MyIter',
1414
'name': 'next',
1515
'displayType': '&mut `MyIter` -> `Option`<`MyIter::Item`>',
16-
'displayMappedNames': 'MyIter::Item = T',
16+
'displayMappedNames': 'T = MyIter::Item',
1717
'displayWhereClause': '',
1818
},
1919
],
@@ -26,7 +26,7 @@ const EXPECTED = [
2626
'path': 'assoc_type_unbound::MyIter',
2727
'name': 'next',
2828
'displayType': '&mut `MyIter` -> `Option`<`MyIter::Item`>',
29-
'displayMappedNames': 'MyIter::Item = T',
29+
'displayMappedNames': 'T = MyIter::Item',
3030
'displayWhereClause': '',
3131
},
3232
],

tests/rustdoc-js/generics-match-ambiguity.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,14 @@ const EXPECTED = [
6060
],
6161
},
6262
{
63+
// strict generics matching; W2<i32, u32> doesn't match W2<W3<i32, u32>>,
64+
// even though W2<i32> works just fine (ignoring the W3)
6365
'query': 'W2<i32>, W2<i32, u32>',
64-
'others': [
65-
{ 'path': 'generics_match_ambiguity', 'name': 'baag' },
66-
{ 'path': 'generics_match_ambiguity', 'name': 'baah' },
67-
],
66+
'others': [],
6867
},
6968
{
7069
'query': 'W2<i32, u32>, W2<i32>',
71-
'others': [
72-
{ 'path': 'generics_match_ambiguity', 'name': 'baag' },
73-
{ 'path': 'generics_match_ambiguity', 'name': 'baah' },
74-
],
70+
'others': [],
7571
},
7672
{
7773
'query': 'W2<i32>, W3<i32, u32>',

tests/rustdoc-js/generics-nested.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ const EXPECTED = [
1818
],
1919
},
2020
{
21+
// can't put generics out of order
2122
'query': '-> Out<Second, First>',
22-
'others': [
23-
{ 'path': 'generics_nested', 'name': 'bet' },
24-
],
23+
'others': [],
2524
},
2625
];

tests/rustdoc-js/generics-unbox.js

-4
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,24 @@ const EXPECTED = [
1111
'query': 'Inside<T> -> Out3<T>',
1212
'others': [
1313
{ 'path': 'generics_unbox', 'name': 'beta' },
14-
{ 'path': 'generics_unbox', 'name': 'gamma' },
1514
],
1615
},
1716
{
1817
'query': 'Inside<T> -> Out4<T>',
1918
'others': [
20-
{ 'path': 'generics_unbox', 'name': 'beta' },
2119
{ 'path': 'generics_unbox', 'name': 'gamma' },
2220
],
2321
},
2422
{
2523
'query': 'Inside<T> -> Out3<U, T>',
2624
'others': [
27-
{ 'path': 'generics_unbox', 'name': 'beta' },
2825
{ 'path': 'generics_unbox', 'name': 'gamma' },
2926
],
3027
},
3128
{
3229
'query': 'Inside<T> -> Out4<U, T>',
3330
'others': [
3431
{ 'path': 'generics_unbox', 'name': 'beta' },
35-
{ 'path': 'generics_unbox', 'name': 'gamma' },
3632
],
3733
},
3834
];

tests/rustdoc-js/nested-unboxed.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ const EXPECTED = [
3333
},
3434
{
3535
'query': '-> Result<i32, u32, bool>',
36-
'others': [
37-
{ 'path': 'nested_unboxed', 'name': 'something' },
38-
],
36+
// can't put nested generics out of order
37+
'others': [],
3938
},
4039
{
4140
'query': '-> Result<Object<i32>, bool>',
@@ -45,9 +44,7 @@ const EXPECTED = [
4544
},
4645
{
4746
'query': '-> Result<Object<u32>, bool>',
48-
'others': [
49-
{ 'path': 'nested_unboxed', 'name': 'something' },
50-
],
47+
'others': [],
5148
},
5249
{
5350
'query': '-> Result<Object<i32>, u32, bool>',

tests/rustdoc-js/reference.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ const EXPECTED = [
7979
},
8080
{
8181
'query': 'reference<ring>, reference<ring> -> ()',
82-
'others': [
83-
{ 'path': 'reference::Ring', 'name': 'wear' },
84-
],
82+
// can't leave out the `mut`, because can't reorder like that
83+
'others': [],
8584
},
8685
{
8786
'query': 'reference<mut, ring>, reference<ring> -> ()',
@@ -102,9 +101,8 @@ const EXPECTED = [
102101
},
103102
{
104103
'query': 'reference<middle>, reference<middle> -> ()',
105-
'others': [
106-
{ 'path': 'reference', 'name': 'show' },
107-
],
104+
// can't leave out the mut
105+
'others': [],
108106
},
109107
{
110108
'query': 'reference<mut, middle>, reference<mut, middle> -> ()',
@@ -203,9 +201,8 @@ const EXPECTED = [
203201
// middle with shorthand
204202
{
205203
'query': '&middle, &middle -> ()',
206-
'others': [
207-
{ 'path': 'reference', 'name': 'show' },
208-
],
204+
// can't leave out the mut
205+
'others': [],
209206
},
210207
{
211208
'query': '&mut middle, &mut middle -> ()',

tests/rustdoc-js/slice-array.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ const EXPECTED = [
4747
},
4848
{
4949
'query': 'primitive:array<TraitDog>',
50-
'in_args': [
51-
{ 'path': 'slice_array', 'name': 'gamma' },
52-
],
50+
// cat goes first
51+
'in_args': [],
5352
},
5453
{
5554
'query': '[TraitCat]',

0 commit comments

Comments
 (0)