Skip to content

Commit 4946c07

Browse files
committed
drustdoc-search: make unboxing a special-case type feature
1 parent 29d228b commit 4946c07

29 files changed

+305
-79
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

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

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ passes_doc_masked_only_extern_crate =
222222
.not_an_extern_crate_label = not an `extern crate` item
223223
.note = read <https://doc.rust-lang.org/unstable-book/language-features/doc-masked.html> for more information
224224
225+
passes_doc_search_unbox_invalid =
226+
`#[doc(search_unbox)]` should be used on generic structs and enums
227+
225228
passes_doc_test_literal = `#![doc(test(...)]` does not take a literal
226229
227230
passes_doc_test_takes_list =

compiler/rustc_passes/src/check_attr.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ use rustc_data_structures::fx::FxHashMap;
1111
use rustc_errors::{Applicability, IntoDiagArg, MultiSpan};
1212
use rustc_errors::{DiagCtxtHandle, StashKey};
1313
use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
14+
use rustc_hir as hir;
1415
use rustc_hir::def_id::LocalModDefId;
1516
use rustc_hir::intravisit::{self, Visitor};
16-
use rustc_hir::{self as hir};
1717
use rustc_hir::{
18-
self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID, CRATE_OWNER_ID,
18+
self, AssocItemKind, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID,
19+
CRATE_OWNER_ID,
1920
};
2021
use rustc_hir::{MethodKind, Safety, Target};
2122
use rustc_macros::LintDiagnostic;
@@ -875,6 +876,30 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
875876
true
876877
}
877878

879+
fn check_doc_search_unbox(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
880+
let hir::Node::Item(item) = self.tcx.hir_node(hir_id) else {
881+
self.dcx().emit_err(errors::DocSearchUnboxInvalid { span: meta.span() });
882+
return false;
883+
};
884+
match item.kind {
885+
ItemKind::Enum(_, generics) | ItemKind::Struct(_, generics)
886+
if generics.params.len() != 0 =>
887+
{
888+
true
889+
}
890+
ItemKind::Trait(_, _, generics, _, items)
891+
if generics.params.len() != 0
892+
|| items.iter().any(|item| matches!(item.kind, AssocItemKind::Type)) =>
893+
{
894+
true
895+
}
896+
_ => {
897+
self.dcx().emit_err(errors::DocSearchUnboxInvalid { span: meta.span() });
898+
false
899+
}
900+
}
901+
}
902+
878903
/// Checks `#[doc(inline)]`/`#[doc(no_inline)]` attributes. Returns `true` if valid.
879904
///
880905
/// A doc inlining attribute is invalid if it is applied to a non-`use` item, or
@@ -1108,6 +1133,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11081133
is_valid = false
11091134
}
11101135

1136+
sym::search_unbox
1137+
if !self.check_attr_not_crate_level(meta, hir_id, "search_unbox")
1138+
|| !self.check_doc_search_unbox(meta, hir_id) =>
1139+
{
1140+
is_valid = false
1141+
}
1142+
11111143
sym::html_favicon_url
11121144
| sym::html_logo_url
11131145
| sym::html_playground_url
@@ -1165,6 +1197,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11651197
| sym::notable_trait
11661198
| sym::passes
11671199
| sym::plugins
1200+
| sym::search_unbox
11681201
| sym::fake_variadic => {}
11691202

11701203
sym::rust_logo => {

compiler/rustc_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ pub struct DocKeywordOnlyImpl {
234234
pub span: Span,
235235
}
236236

237+
#[derive(Diagnostic)]
238+
#[diag(passes_doc_search_unbox_invalid)]
239+
pub struct DocSearchUnboxInvalid {
240+
#[primary_span]
241+
pub span: Span,
242+
}
243+
237244
#[derive(Diagnostic)]
238245
#[diag(passes_doc_inline_conflict)]
239246
#[help]

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,7 @@ symbols! {
17021702
saturating_add,
17031703
saturating_div,
17041704
saturating_sub,
1705+
search_unbox,
17051706
self_in_typedefs,
17061707
self_struct_ctor,
17071708
semitransparent,

library/alloc/src/boxed.rs

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ mod thin;
233233
#[lang = "owned_box"]
234234
#[fundamental]
235235
#[stable(feature = "rust1", since = "1.0.0")]
236+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
236237
// The declaration of the `Box` struct must be kept in sync with the
237238
// compiler or ICEs will happen.
238239
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
@@ -571,6 +571,7 @@ use crate::{
571571
#[lang = "Option"]
572572
#[stable(feature = "rust1", since = "1.0.0")]
573573
#[allow(clippy::derived_hash_with_manual_eq)] // PartialEq is manually implemented equivalently
574+
#[cfg_attr(not(bootstrap), doc(search_unbox))]
574575
pub enum Option<T> {
575576
/// No value.
576577
#[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"]

0 commit comments

Comments
 (0)