Skip to content

Commit fc09817

Browse files
authored
Rollup merge of #74370 - Manishearth:re-spotlight, r=GuillaumeGomez
Reintroduce spotlight / "important traits" feature (Reopened version of #74111 because Github is broken, see discussion there) Fixes #73785 This PR reintroduces the "spotlight" ("important traits") feature. A couple changes have been made: As there were concerns about its visibility, it has been moved to be next to the return type, as opposed to being on the side. It also no longer produces a modal, it shows the traits on hover, and it can be clicked on to pin the hover bubble. ![image](https://user-images.githubusercontent.com/1617736/86674555-a82d2600-bfad-11ea-9a4a-a1a9ffd66ae5.png) ![image](https://user-images.githubusercontent.com/1617736/86674533-a1061800-bfad-11ea-9e8a-c62ad86ed0d7.png) It also works fine on mobile: ![image](https://user-images.githubusercontent.com/1617736/86674638-bda25000-bfad-11ea-8d8d-1798b608923e.png)
2 parents 61fccf0 + c621a54 commit fc09817

File tree

23 files changed

+284
-15
lines changed

23 files changed

+284
-15
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ Book][unstable-doc-cfg] and [its tracking issue][issue-doc-cfg].
150150
[unstable-doc-cfg]: ../unstable-book/language-features/doc-cfg.html
151151
[issue-doc-cfg]: https://github.com/rust-lang/rust/issues/43781
152152

153+
### Adding your trait to the "Important Traits" dialog
154+
155+
Rustdoc keeps a list of a few traits that are believed to be "fundamental" to a given type when
156+
implemented on it. These traits are intended to be the primary interface for their types, and are
157+
often the only thing available to be documented on their types. For this reason, Rustdoc will track
158+
when a given type implements one of these traits and call special attention to it when a function
159+
returns one of these types. This is the "Important Traits" dialog, visible as a circle-i button next
160+
to the function, which, when clicked, shows the dialog.
161+
162+
In the standard library, the traits that qualify for inclusion are `Iterator`, `io::Read`, and
163+
`io::Write`. However, rather than being implemented as a hard-coded list, these traits have a
164+
special marker attribute on them: `#[doc(spotlight)]`. This means that you could apply this
165+
attribute to your own trait to include it in the "Important Traits" dialog in documentation.
166+
167+
The `#[doc(spotlight)]` attribute currently requires the `#![feature(doc_spotlight)]` feature gate.
168+
For more information, see [its chapter in the Unstable Book][unstable-spotlight] and [its tracking
169+
issue][issue-spotlight].
170+
171+
[unstable-spotlight]: ../unstable-book/language-features/doc-spotlight.html
172+
[issue-spotlight]: https://github.com/rust-lang/rust/issues/45040
173+
153174
### Exclude certain dependencies from documentation
154175

155176
The standard library uses several dependencies which, in turn, use several types and traits from the
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# `doc_spotlight`
2+
3+
The tracking issue for this feature is: [#45040]
4+
5+
The `doc_spotlight` feature allows the use of the `spotlight` parameter to the `#[doc]` attribute,
6+
to "spotlight" a specific trait on the return values of functions. Adding a `#[doc(spotlight)]`
7+
attribute to a trait definition will make rustdoc print extra information for functions which return
8+
a type that implements that trait. This attribute is applied to the `Iterator`, `io::Read`, and
9+
`io::Write` traits in the standard library.
10+
11+
You can do this on your own traits, like this:
12+
13+
```
14+
#![feature(doc_spotlight)]
15+
16+
#[doc(spotlight)]
17+
pub trait MyTrait {}
18+
19+
pub struct MyStruct;
20+
impl MyTrait for MyStruct {}
21+
22+
/// The docs for this function will have an extra line about `MyStruct` implementing `MyTrait`,
23+
/// without having to write that yourself!
24+
pub fn my_fn() -> MyStruct { MyStruct }
25+
```
26+
27+
This feature was originally implemented in PR [#45039].
28+
29+
[#45040]: https://github.com/rust-lang/rust/issues/45040
30+
[#45039]: https://github.com/rust-lang/rust/pull/45039

src/libcore/future/future.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::task::{Context, Poll};
2424
/// `.await` the value.
2525
///
2626
/// [`Waker`]: ../task/struct.Waker.html
27+
#[doc(spotlight)]
2728
#[must_use = "futures do nothing unless you `.await` or poll them"]
2829
#[stable(feature = "futures_api", since = "1.36.0")]
2930
#[lang = "future_trait"]

src/libcore/iter/traits/iterator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item = ()>) {}
9292
label = "`{Self}` is not an iterator",
9393
message = "`{Self}` is not an iterator"
9494
)]
95+
#[doc(spotlight)]
9596
#[must_use = "iterators are lazy and do nothing unless consumed"]
9697
pub trait Iterator {
9798
/// The type of the elements being iterated over.

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#![feature(custom_inner_attributes)]
9797
#![feature(decl_macro)]
9898
#![feature(doc_cfg)]
99+
#![cfg_attr(not(bootstrap), feature(doc_spotlight))]
99100
#![feature(duration_consts_2)]
100101
#![feature(extern_types)]
101102
#![feature(fundamental)]

src/librustc_ast_passes/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
253253
include => external_doc
254254
cfg => doc_cfg
255255
masked => doc_masked
256+
spotlight => doc_spotlight
256257
alias => doc_alias
257258
keyword => doc_keyword
258259
);

src/librustc_feature/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ declare_features! (
368368
/// Allows `#[doc(masked)]`.
369369
(active, doc_masked, "1.21.0", Some(44027), None),
370370

371+
/// Allows `#[doc(spotlight)]`.
372+
(active, doc_spotlight, "1.22.0", Some(45040), None),
373+
371374
/// Allows `#[doc(include = "some-file")]`.
372375
(active, external_doc, "1.22.0", Some(44732), None),
373376

src/librustc_span/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ symbols! {
400400
doc_cfg,
401401
doc_keyword,
402402
doc_masked,
403+
doc_spotlight,
403404
doctest,
404405
document_private_items,
405406
dotdot_in_tuple_patterns,
@@ -968,6 +969,7 @@ symbols! {
968969
soft,
969970
specialization,
970971
speed,
972+
spotlight,
971973
sqrtf32,
972974
sqrtf64,
973975
sse4a_target_feature,

src/librustdoc/clean/inline.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_metadata::creader::LoadedMacro;
1212
use rustc_middle::ty;
1313
use rustc_mir::const_eval::is_min_const_fn;
1414
use rustc_span::hygiene::MacroKind;
15-
use rustc_span::symbol::Symbol;
15+
use rustc_span::symbol::{sym, Symbol};
1616
use rustc_span::Span;
1717

1818
use crate::clean::{self, GetDefId, ToSource, TypeKind};
@@ -194,13 +194,15 @@ pub fn build_external_trait(cx: &DocContext<'_>, did: DefId) -> clean::Trait {
194194
let generics = (cx.tcx.generics_of(did), predicates).clean(cx);
195195
let generics = filter_non_trait_generics(did, generics);
196196
let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
197+
let is_spotlight = load_attrs(cx, did).clean(cx).has_doc_flag(sym::spotlight);
197198
let is_auto = cx.tcx.trait_is_auto(did);
198199
clean::Trait {
199200
auto: auto_trait,
200201
unsafety: cx.tcx.trait_def(did).unsafety,
201202
generics,
202203
items: trait_items,
203204
bounds: supertrait_bounds,
205+
is_spotlight,
204206
is_auto,
205207
}
206208
}

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ impl Clean<FnRetTy> for hir::FnRetTy<'_> {
10071007
impl Clean<Item> for doctree::Trait<'_> {
10081008
fn clean(&self, cx: &DocContext<'_>) -> Item {
10091009
let attrs = self.attrs.clean(cx);
1010+
let is_spotlight = attrs.has_doc_flag(sym::spotlight);
10101011
Item {
10111012
name: Some(self.name.clean(cx)),
10121013
attrs,
@@ -1021,6 +1022,7 @@ impl Clean<Item> for doctree::Trait<'_> {
10211022
items: self.items.iter().map(|ti| ti.clean(cx)).collect(),
10221023
generics: self.generics.clean(cx),
10231024
bounds: self.bounds.clean(cx),
1025+
is_spotlight,
10241026
is_auto: self.is_auto.clean(cx),
10251027
}),
10261028
}

0 commit comments

Comments
 (0)