Skip to content

Commit 0035d9d

Browse files
committedAug 16, 2021
Auto merge of #87050 - jyn514:no-doc-primitive, r=manishearth
Add future-incompat lint for `doc(primitive)` ## What is `doc(primitive)`? `doc(primitive)` is an attribute recognized by rustdoc which adds documentation for the built-in primitive types, such as `usize` and `()`. It has been stable since Rust 1.0. ## Why change anything? `doc(primitive)` is useless for anyone outside the standard library. Since rustdoc provides no way to combine the documentation on two different primitive items, you can only replace the docs, and since the standard library already provides extensive documentation there is no reason to do so. While fixing rustdoc's handling of primitive items (#87073) I discovered that even rustdoc's existing handling of primitive items was broken if you had more than two crates using it (it would pick randomly between them). That meant both: - Keeping rustdoc's existing treatment was nigh-impossible, because it was random. - doc(primitive) was even more useless than it would otherwise be. The only use-case for this outside the standard library is for no-std libraries which want to link to primitives (#73423) which is being fixed in #87073. #87073 makes various breaking changes to `doc(primitive)` (breaking in the sense that they change the semantics, not in that they cause code to fail to compile). It's not possible to avoid these and still fix rustdoc's issues. ## What can we do about it? As shown by the crater run (#87050 (comment)), no one is actually using doc(primitive), there wasn't a single true regression in the whole run. We can either: 1. Feature gate it completely, breaking anyone who crater missed. They can easily fix the breakage just by removing the attribute. 2. add it to the `INVALID_DOC_ATTRIBUTES` future-incompat lint, and at the same time make it a no-op unless you add a feature gate. That would mean rustdoc has to look at the features of dependent crates, because it needs to know where primitives are defined in order to link to them. 3. add it to `INVALID_DOC_ATTRIBUTES`, but still use it to determine where primitives come from 4. do nothing; the behavior will silently change in #87073. My preference is for 2, but I would also be happy with 1 or 3. I don't think we should silently change the behavior. This PR currently implements 3.
2 parents 73d96b0 + 03df654 commit 0035d9d

File tree

9 files changed

+49
-7
lines changed

9 files changed

+49
-7
lines changed
 

‎compiler/rustc_feature/src/active.rs

+3
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ declare_features! (
682682
/// Allows explicit generic arguments specification with `impl Trait` present.
683683
(active, explicit_generic_args_with_impl_trait, "1.56.0", Some(83701), None),
684684

685+
/// Allows using doc(primitive) without a future-incompat warning
686+
(active, doc_primitive, "1.56.0", Some(88070), None),
687+
685688
// -------------------------------------------------------------------------
686689
// feature-group-end: actual feature gates
687690
// -------------------------------------------------------------------------

‎compiler/rustc_passes/src/check_attr.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,24 @@ impl CheckAttrVisitor<'tcx> {
794794
| sym::notable_trait
795795
| sym::passes
796796
| sym::plugins
797-
| sym::primitive
798797
| sym::test => {}
799798

799+
sym::primitive => {
800+
if !self.tcx.features().doc_primitive {
801+
self.tcx.struct_span_lint_hir(
802+
INVALID_DOC_ATTRIBUTES,
803+
hir_id,
804+
i_meta.span,
805+
|lint| {
806+
let mut diag = lint.build(
807+
"`doc(primitive)` should never have been stable",
808+
);
809+
diag.emit();
810+
},
811+
);
812+
}
813+
}
814+
800815
_ => {
801816
self.tcx.struct_span_lint_hir(
802817
INVALID_DOC_ATTRIBUTES,

‎compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ symbols! {
538538
doc_keyword,
539539
doc_masked,
540540
doc_notable_trait,
541+
doc_primitive,
541542
doc_spotlight,
542543
doctest,
543544
document_private_items,

‎library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@
263263
#![feature(doc_keyword)]
264264
#![feature(doc_masked)]
265265
#![feature(doc_notable_trait)]
266+
#![cfg_attr(not(bootstrap), feature(doc_primitive))]
266267
#![feature(dropck_eyepatch)]
267268
#![feature(duration_checked_float)]
268269
#![feature(duration_constants)]

‎src/doc/rustdoc/src/the-doc-attribute.md

-6
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,3 @@ not eagerly inline it as a module unless you add `#[doc(inline)]`.
223223

224224
Any item annotated with `#[doc(hidden)]` will not appear in the documentation, unless
225225
the `strip-hidden` pass is removed.
226-
227-
## `#[doc(primitive)]`
228-
229-
Since primitive types are defined in the compiler, there's no place to attach documentation
230-
attributes. This attribute is used by the standard library to provide a way to generate
231-
documentation for primitive types.

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

+7
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ Book][unstable-masked] and [its tracking issue][issue-masked].
131131
[unstable-masked]: ../unstable-book/language-features/doc-masked.html
132132
[issue-masked]: https://github.com/rust-lang/rust/issues/44027
133133

134+
135+
## Document primitives
136+
137+
Since primitive types are defined in the compiler, there's no place to attach documentation
138+
attributes. The `#[doc(primitive)]` attribute is used by the standard library to provide a way to generate
139+
documentation for primitive types, and requires `#![feature(doc_primitive)]` to enable.
140+
134141
## Unstable command-line arguments
135142

136143
These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are

‎src/test/rustdoc-ui/coverage/exotic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// check-pass
33

44
#![feature(doc_keyword)]
5+
#![feature(doc_primitive)]
56

67
//! the features only used in std also have entries in the table, so make sure those get pulled out
78
//! properly as well
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// check-pass
2+
#[doc(primitive = "usize")]
3+
//~^ WARNING `doc(primitive)` should never have been stable
4+
//~| WARNING hard error in a future release
5+
/// Some docs
6+
mod usize {}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: `doc(primitive)` should never have been stable
2+
--> $DIR/feature-gate-doc_primitive.rs:2:7
3+
|
4+
LL | #[doc(primitive = "usize")]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(invalid_doc_attributes)]` on by default
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #82730 <https://github.com/rust-lang/rust/issues/82730>
10+
11+
warning: 1 warning emitted
12+

0 commit comments

Comments
 (0)