Skip to content

Commit eb5487e

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> * <rust-lang#124544 (comment)>
1 parent 36e26ab commit eb5487e

38 files changed

+580
-208
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
217217
"meant for internal use only" {
218218
keyword => rustdoc_internals
219219
fake_variadic => rustdoc_internals
220+
search_unbox => rustdoc_internals
220221
}
221222
);
222223
}

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ passes_doc_masked_only_extern_crate =
226226
passes_doc_rust_logo =
227227
the `#[doc(rust_logo)]` attribute is used for Rust branding
228228
229+
passes_doc_search_unbox_invalid =
230+
`#[doc(search_unbox)]` should be used on generic structs and enums
231+
229232
passes_doc_test_literal = `#![doc(test(...)]` does not take a literal
230233
231234
passes_doc_test_takes_list =

compiler/rustc_passes/src/check_attr.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTI
1616
use rustc_hir::def_id::LocalModDefId;
1717
use rustc_hir::intravisit::{self, Visitor};
1818
use rustc_hir::{
19-
self as hir, self, FnSig, ForeignItem, HirId, Item, ItemKind, MethodKind, Safety, Target,
20-
TraitItem, CRATE_HIR_ID, CRATE_OWNER_ID,
19+
self as hir, self, AssocItemKind, FnSig, ForeignItem, HirId, Item, ItemKind, MethodKind,
20+
Safety, Target, TraitItem, CRATE_HIR_ID, CRATE_OWNER_ID,
2121
};
2222
use rustc_macros::LintDiagnostic;
2323
use rustc_middle::hir::nested_filter;
@@ -962,6 +962,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
962962
}
963963
}
964964

965+
fn check_doc_search_unbox(&self, meta: &NestedMetaItem, hir_id: HirId) {
966+
let hir::Node::Item(item) = self.tcx.hir_node(hir_id) else {
967+
self.dcx().emit_err(errors::DocSearchUnboxInvalid { span: meta.span() });
968+
return;
969+
};
970+
match item.kind {
971+
ItemKind::Enum(_, generics) | ItemKind::Struct(_, generics)
972+
if generics.params.len() != 0 => {}
973+
ItemKind::Trait(_, _, generics, _, items)
974+
if generics.params.len() != 0
975+
|| items.iter().any(|item| matches!(item.kind, AssocItemKind::Type)) => {}
976+
_ => {
977+
self.dcx().emit_err(errors::DocSearchUnboxInvalid { span: meta.span() });
978+
}
979+
}
980+
}
981+
965982
/// Checks `#[doc(inline)]`/`#[doc(no_inline)]` attributes.
966983
///
967984
/// A doc inlining attribute is invalid if it is applied to a non-`use` item, or
@@ -1180,6 +1197,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11801197
}
11811198
}
11821199

1200+
sym::search_unbox => {
1201+
if self.check_attr_not_crate_level(meta, hir_id, "fake_variadic") {
1202+
self.check_doc_search_unbox(meta, hir_id);
1203+
}
1204+
}
1205+
11831206
sym::html_favicon_url
11841207
| sym::html_logo_url
11851208
| sym::html_playground_url

compiler/rustc_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ pub struct DocKeywordOnlyImpl {
230230
pub span: Span,
231231
}
232232

233+
#[derive(Diagnostic)]
234+
#[diag(passes_doc_search_unbox_invalid)]
235+
pub struct DocSearchUnboxInvalid {
236+
#[primary_span]
237+
pub span: Span,
238+
}
239+
233240
#[derive(Diagnostic)]
234241
#[diag(passes_doc_inline_conflict)]
235242
#[help]

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,7 @@ symbols! {
17041704
saturating_add,
17051705
saturating_div,
17061706
saturating_sub,
1707+
search_unbox,
17071708
select_unpredictable,
17081709
self_in_typedefs,
17091710
self_struct_ctor,

library/alloc/src/boxed.rs

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ mod thin;
230230
#[lang = "owned_box"]
231231
#[fundamental]
232232
#[stable(feature = "rust1", since = "1.0.0")]
233+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
233234
// The declaration of the `Box` struct must be kept in sync with the
234235
// compiler or ICEs will happen.
235236
pub struct Box<

library/core/src/future/future.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::task::{Context, Poll};
2525
/// [`async`]: ../../std/keyword.async.html
2626
/// [`Waker`]: crate::task::Waker
2727
#[doc(notable_trait)]
28+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
2829
#[must_use = "futures do nothing unless you `.await` or poll them"]
2930
#[stable(feature = "futures_api", since = "1.36.0")]
3031
#[lang = "future_trait"]

library/core/src/option.rs

+1
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ use crate::{cmp, convert, hint, mem, slice};
568568
#[lang = "Option"]
569569
#[stable(feature = "rust1", since = "1.0.0")]
570570
#[allow(clippy::derived_hash_with_manual_eq)] // PartialEq is manually implemented equivalently
571+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
571572
pub enum Option<T> {
572573
/// No value.
573574
#[lang = "None"]

library/core/src/result.rs

+1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ use crate::{convert, fmt, hint};
524524
#[must_use = "this `Result` may be an `Err` variant, which should be handled"]
525525
#[rustc_diagnostic_item = "Result"]
526526
#[stable(feature = "rust1", since = "1.0.0")]
527+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
527528
pub enum Result<T, E> {
528529
/// Contains the success value
529530
#[lang = "Ok"]

src/doc/rustdoc/src/read-documentation/search.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,31 @@ pub trait MyTrait {
130130
/// This function can be found using the following search queries:
131131
///
132132
/// MyTrait<First=u8, Second=u32> -> bool
133-
/// MyTrait<u32, First=u8> -> bool
134133
/// MyTrait<Second=u32> -> bool
135-
/// MyTrait<u32, u8> -> bool
136134
///
137135
/// The following queries, however, will *not* match it:
138136
///
139137
/// MyTrait<First=u32> -> bool
140138
/// MyTrait<u32, u32> -> bool
139+
/// MyTrait<u32, First=u8> -> bool
140+
/// MyTrait<u32, u8> -> bool
141141
pub fn my_fn(x: impl MyTrait<First=u8, Second=u32>) -> bool { true }
142142
```
143143

144-
Generics and function parameters are order-agnostic, but sensitive to nesting
144+
Function parameters are order-agnostic, but sensitive to nesting
145145
and number of matches. For example, a function with the signature
146146
`fn read_all(&mut self: impl Read) -> Result<Vec<u8>, Error>`
147147
will match these queries:
148148

149149
* `&mut Read -> Result<Vec<u8>, Error>`
150150
* `Read -> Result<Vec<u8>, Error>`
151-
* `Read -> Result<Error, Vec>`
152151
* `Read -> Result<Vec<u8>>`
153152
* `Read -> u8`
154153

155-
But it *does not* match `Result<Vec, u8>` or `Result<u8<Vec>>`.
154+
But it *does not* match `Result<Vec, u8>` or `Result<u8<Vec>>`,
155+
because those are nested incorrectly, and it does not match
156+
`Result<Error, Vec<u8>>` or `Result<Error>`, because those are
157+
in the wrong order.
156158

157159
To search for a function that accepts a function as a parameter,
158160
like `Iterator::all`, wrap the nested signature in parenthesis,

0 commit comments

Comments
 (0)