Skip to content

Commit 58f56a9

Browse files
committed
rustdoc: add #[doc(label_trait)] to render a trait badge
1 parent 39ec825 commit 58f56a9

25 files changed

Lines changed: 236 additions & 8 deletions

File tree

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
171171
auto_cfg => doc_cfg
172172
masked => doc_masked
173173
notable_trait => doc_notable_trait
174+
label_trait => doc_label_trait
174175
}
175176
"meant for internal use only" {
176177
attribute => rustdoc_internals

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ impl DocParser {
505505
match path.word_sym() {
506506
Some(sym::alias) => self.parse_alias(cx, path, args),
507507
Some(sym::hidden) => no_args!(hidden),
508+
Some(sym::label_trait) => no_args!(label_trait),
508509
Some(sym::html_favicon_url) => string_arg_and_crate_level!(html_favicon_url),
509510
Some(sym::html_logo_url) => string_arg_and_crate_level!(html_logo_url),
510511
Some(sym::html_no_source) => no_args_and_crate_level!(html_no_source),
@@ -690,6 +691,7 @@ impl AttributeParser for DocParser {
690691
"masked",
691692
"cfg",
692693
"notable_trait",
694+
"label_trait",
693695
"keyword",
694696
"fake_variadic",
695697
"search_unbox",

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ declare_features! (
313313
(unstable, box_patterns, "1.0.0", Some(29641)),
314314
/// Allows builtin # foo() syntax
315315
(internal, builtin_syntax, "1.71.0", Some(110680)),
316+
/// Allows `#[doc(label_trait)]`.
317+
(unstable, doc_label_trait, "CURRENT_RUSTC_VERSION", Some(156865)),
316318
/// Allows `#[doc(notable_trait)]`.
317319
/// Renamed from `doc_spotlight`.
318320
(unstable, doc_notable_trait, "1.52.0", Some(45040)),

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ pub struct DocAttribute {
528528

529529
pub aliases: FxIndexMap<Symbol, Span>,
530530
pub hidden: Option<Span>,
531+
pub label_trait: Option<Span>,
531532
// Because we need to emit the error if there is more than one `inline` attribute on an item
532533
// at the same time as the other doc attributes, we store a list instead of using `Option`.
533534
pub inline: ThinVec<(DocInline, Span)>,
@@ -566,6 +567,7 @@ impl<E: rustc_span::SpanEncoder> rustc_serialize::Encodable<E> for DocAttribute
566567
first_span,
567568
aliases,
568569
hidden,
570+
label_trait,
569571
inline,
570572
cfg,
571573
auto_cfg,
@@ -589,6 +591,7 @@ impl<E: rustc_span::SpanEncoder> rustc_serialize::Encodable<E> for DocAttribute
589591
rustc_serialize::Encodable::<E>::encode(first_span, encoder);
590592
rustc_serialize::Encodable::<E>::encode(aliases, encoder);
591593
rustc_serialize::Encodable::<E>::encode(hidden, encoder);
594+
rustc_serialize::Encodable::<E>::encode(label_trait, encoder);
592595

593596
// FIXME: The `doc(inline)` attribute is never encoded, but is it actually the right thing
594597
// to do? I suspect the condition was broken, should maybe instead not encode anything if we

compiler/rustc_middle/src/queries.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,11 @@ rustc_queries! {
14861486
separate_provide_extern
14871487
}
14881488

1489+
/// Determines whether an item is annotated with `#[doc(label_trait)]`.
1490+
query is_doc_label_trait(def_id: DefId) -> bool {
1491+
desc { "checking whether `{}` is `doc(label_trait)`", tcx.def_path_str(def_id) }
1492+
}
1493+
14891494
/// Determines whether an item is annotated with `#[doc(notable_trait)]`.
14901495
query is_doc_notable_trait(def_id: DefId) -> bool {
14911496
desc { "checking whether `{}` is `doc(notable_trait)`", tcx.def_path_str(def_id) }

compiler/rustc_middle/src/ty/util.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,11 @@ pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
16661666
find_attr!(tcx, def_id, Doc(doc) if doc.notable_trait.is_some())
16671667
}
16681668

1669+
/// Determines whether an item is annotated with `doc(notable_trait)`.
1670+
pub fn is_doc_label_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1671+
find_attr!(tcx, def_id, Doc(doc) if doc.label_trait.is_some())
1672+
}
1673+
16691674
/// Determines whether an item is an intrinsic (which may be via Abi or via the `rustc_intrinsic` attribute).
16701675
///
16711676
/// We double check the feature gate here because whether a function may be defined as an intrinsic causes
@@ -1693,6 +1698,7 @@ pub fn provide(providers: &mut Providers) {
16931698
*providers = Providers {
16941699
reveal_opaque_types_in_bounds,
16951700
is_doc_hidden,
1701+
is_doc_label_trait,
16961702
is_doc_notable_trait,
16971703
intrinsic_raw,
16981704
..*providers

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
989989
// valid pretty much anywhere, not checked here?
990990
// FIXME: should we?
991991
hidden: _,
992+
// FIXME: valid for traits, should be checked in attr_parsing
993+
label_trait: _,
992994
inline,
993995
// FIXME: currently unchecked
994996
cfg: _,

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ symbols! {
826826
doc_cfg,
827827
doc_cfg_hide,
828828
doc_keyword,
829+
doc_label_trait,
829830
doc_masked,
830831
doc_notable_trait,
831832
doc_primitive,
@@ -1140,6 +1141,7 @@ symbols! {
11401141
kreg0,
11411142
label,
11421143
label_break_value,
1144+
label_trait,
11431145
lahfsahf_target_feature,
11441146
lang,
11451147
lang_items,

src/doc/rustdoc/src/unstable-features.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ It is also not emitted for foreign items, aliases, extern crates and imports.
5656
These features operate by extending the `#[doc]` attribute, and thus can be caught by the compiler
5757
and enabled with a `#![feature(...)]` attribute in your crate.
5858

59+
### Making your trait more discoverable
60+
61+
* Tracking issue: [#156865](https://github.com/rust-lang/rust/issues/156865)
62+
63+
Important traits can be difficult to discover when lost in the noise.
64+
This `#![feature(doc_label_trait)]` allows you to tag traits important for your code base.
65+
66+
The traits with the attribute #![doc(label_trait)]` are rendered with a colored badge at the top of their dedicated page.
67+
68+
Consider lookint into the `notable_trait` unstable attribure, which help with
69+
discoverability in other ways.
70+
5971
### Adding your trait to the "Notable traits" dialog
6072

6173
* Tracking issue: [#45040](https://github.com/rust-lang/rust/issues/45040)

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,7 @@ fn add_without_unwanted_attributes<'hir>(
28142814
first_span: _,
28152815
aliases,
28162816
hidden,
2817+
label_trait: _,
28172818
inline,
28182819
cfg,
28192820
auto_cfg: _,

0 commit comments

Comments
 (0)