Skip to content

Commit 12dc24f

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 generics act stricter. 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)> * <https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/deciding.20on.20semantics.20of.20generics.20in.20rustdoc.20search/near/476841363>
1 parent 20a4b4f commit 12dc24f

40 files changed

Lines changed: 630 additions & 217 deletions

‎compiler/rustc_ast_passes/src/feature_gate.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
204204
"meant for internal use only"{
205205
keyword => rustdoc_internals
206206
fake_variadic => rustdoc_internals
207+
search_unbox => rustdoc_internals
207208
}
208209
);
209210
}

‎compiler/rustc_passes/messages.ftl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ passes_doc_masked_only_extern_crate =
234234
passes_doc_rust_logo =
235235
the `#[doc(rust_logo)]` attribute is used for Rust branding
236236
237+
passes_doc_search_unbox_invalid =
238+
`#[doc(search_unbox)]` should be used on generic structs and enums
239+
237240
passes_doc_test_literal = `#![doc(test(...)]` does not take a literal
238241
239242
passes_doc_test_takes_list =

‎compiler/rustc_passes/src/check_attr.rs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_feature::{AttributeDuplicates, AttributeType, BUILTIN_ATTRIBUTE_MAP, B
1616
use rustc_hir::def_id::LocalModDefId;
1717
use rustc_hir::intravisit::{self,Visitor};
1818
use rustc_hir::{
19-
selfas hir,self,CRATE_HIR_ID,CRATE_OWNER_ID,FnSig,ForeignItem,HirId,Item,ItemKind,
20-
MethodKind,Safety,Target,TraitItem,
19+
selfas hir,self,AssocItemKind,CRATE_HIR_ID,CRATE_OWNER_ID,FnSig,ForeignItem,HirId,
20+
Item,ItemKind,MethodKind,Safety,Target,TraitItem,
2121
};
2222
use rustc_macros::LintDiagnostic;
2323
use rustc_middle::hir::nested_filter;
@@ -940,6 +940,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
940940
}
941941
}
942942

943+
fncheck_doc_search_unbox(&self,meta:&MetaItemInner,hir_id:HirId){
944+
let hir::Node::Item(item) = self.tcx.hir_node(hir_id)else{
945+
self.dcx().emit_err(errors::DocSearchUnboxInvalid{span: meta.span()});
946+
return;
947+
};
948+
match item.kind{
949+
ItemKind::Enum(_, generics) | ItemKind::Struct(_, generics)
950+
if generics.params.len() != 0 => {}
951+
ItemKind::Trait(_, _, generics, _, items)
952+
if generics.params.len() != 0
953+
|| items.iter().any(|item| matches!(item.kind,AssocItemKind::Type)) => {}
954+
_ => {
955+
self.dcx().emit_err(errors::DocSearchUnboxInvalid{span: meta.span()});
956+
}
957+
}
958+
}
959+
943960
/// Checks `#[doc(inline)]`/`#[doc(no_inline)]` attributes.
944961
///
945962
/// A doc inlining attribute is invalid if it is applied to a non-`use` item, or
@@ -1152,6 +1169,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11521169
}
11531170
}
11541171

1172+
sym::search_unbox => {
1173+
ifself.check_attr_not_crate_level(meta, hir_id,"fake_variadic"){
1174+
self.check_doc_search_unbox(meta, hir_id);
1175+
}
1176+
}
1177+
11551178
sym::test => {
11561179
ifself.check_attr_crate_level(attr, meta, hir_id){
11571180
self.check_test_attr(meta, hir_id);

‎compiler/rustc_passes/src/errors.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ pub(crate) struct DocKeywordOnlyImpl {
244244
pubspan:Span,
245245
}
246246

247+
#[derive(Diagnostic)]
248+
#[diag(passes_doc_search_unbox_invalid)]
249+
pub(crate)structDocSearchUnboxInvalid{
250+
#[primary_span]
251+
pubspan:Span,
252+
}
253+
247254
#[derive(Diagnostic)]
248255
#[diag(passes_doc_inline_conflict)]
249256
#[help]

‎compiler/rustc_span/src/symbol.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,7 @@ symbols! {
17621762
saturating_add,
17631763
saturating_div,
17641764
saturating_sub,
1765+
search_unbox,
17651766
select_unpredictable,
17661767
self_in_typedefs,
17671768
self_struct_ctor,

‎library/alloc/src/boxed.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ pub use thin::ThinBox;
225225
#[fundamental]
226226
#[stable(feature = "rust1", since = "1.0.0")]
227227
#[rustc_insignificant_dtor]
228+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
228229
// The declaration of the `Box` struct must be kept in sync with the
229230
// compiler or ICEs will happen.
230231
pubstructBox<

‎library/alloc/src/rc.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ fn rc_inner_layout_for_value_layout(layout: Layout) -> Layout {
307307
/// `value.get_mut()`. This avoids conflicts with methods of the inner type `T`.
308308
///
309309
/// [get_mut]: Rc::get_mut
310+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
310311
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
311312
#[stable(feature = "rust1", since = "1.0.0")]
312313
#[rustc_insignificant_dtor]

‎library/alloc/src/sync.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ macro_rules! acquire {
235235
/// counting in general.
236236
///
237237
/// [rc_examples]: crate::rc#examples
238+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
238239
#[cfg_attr(not(test), rustc_diagnostic_item = "Arc")]
239240
#[stable(feature = "rust1", since = "1.0.0")]
240241
#[rustc_insignificant_dtor]

‎library/core/src/future/future.rs‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ use crate::pin::Pin;
563563
usecrate::{cmp, convert, hint, mem, slice};
564564

565565
/// The `Option` type. See [the module level documentation](self) for more.
566+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
566567
#[derive(Copy,Eq,Debug,Hash)]
567568
#[rustc_diagnostic_item = "Option"]
568569
#[lang = "Option"]

0 commit comments

Comments
 (0)