Skip to content

Commit 121e87b

Browse files
Remove rustc::existing_doc_keyword lint.
`CheckAttrVisitor::check_doc_keyword` checks `#[doc(keyword = "..")]` attributes to ensure they are on an empty module, and that the value is a non-empty identifier. The `rustc::existing_doc_keyword` lint checks these attributes to ensure that the value is the name of a keyword. It's silly to have two different checking mechanisms for these attributes. This commit does the following. - Changes `check_doc_keyword` to check that the value is the name of a keyword (avoiding the need for the identifier check, which removes a dependency on `rustc_lexer`). - Removes the lint. - Updates tests accordingly. There is one hack: the `SelfTy` FIXME case used to used to be handled by disabling the lint, but now is handled with a special case in `is_doc_keyword`. That hack will go away if/when the FIXME is fixed. Co-Authored-By: Guillaume Gomez <guillaume1.gomez@gmail.com>
1 parent f10169c commit 121e87b

25 files changed

+58
-115
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4273,7 +4273,6 @@ dependencies = [
42734273
"rustc_fluent_macro",
42744274
"rustc_hir",
42754275
"rustc_index",
4276-
"rustc_lexer",
42774276
"rustc_macros",
42784277
"rustc_middle",
42794278
"rustc_privacy",

compiler/rustc_lint/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,6 @@ lint_non_camel_case_type = {$sort} `{$name}` should have an upper camel case nam
536536
.suggestion = convert the identifier to upper camel case
537537
.label = should have an UpperCamelCase name
538538
539-
lint_non_existent_doc_keyword = found non-existing keyword `{$keyword}` used in `#[doc(keyword = "...")]`
540-
.help = only existing keywords are allowed in core/std
541-
542539
lint_non_fmt_panic = panic message is not a string literal
543540
.note = this usage of `{$name}!()` is deprecated; it will be a hard error in Rust 2021
544541
.more_info_note = for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>

compiler/rustc_lint/src/internal.rs

+2-42
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use rustc_middle::ty::{self, GenericArgsRef, Ty as MiddleTy};
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
1313
use rustc_span::Span;
1414
use rustc_span::hygiene::{ExpnKind, MacroKind};
15-
use rustc_span::symbol::{Symbol, kw, sym};
15+
use rustc_span::symbol::sym;
1616
use tracing::debug;
1717

1818
use crate::lints::{
19-
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistentDocKeyword,
19+
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand,
2020
NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, SpanUseEqCtxtDiag,
2121
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrInherentUsage,
2222
UntranslatableDiag,
@@ -375,46 +375,6 @@ impl EarlyLintPass for LintPassImpl {
375375
}
376376
}
377377

378-
declare_tool_lint! {
379-
/// The `existing_doc_keyword` lint detects use `#[doc()]` keywords
380-
/// that don't exist, e.g. `#[doc(keyword = "..")]`.
381-
pub rustc::EXISTING_DOC_KEYWORD,
382-
Allow,
383-
"Check that documented keywords in std and core actually exist",
384-
report_in_external_macro: true
385-
}
386-
387-
declare_lint_pass!(ExistingDocKeyword => [EXISTING_DOC_KEYWORD]);
388-
389-
fn is_doc_keyword(s: Symbol) -> bool {
390-
s <= kw::Union
391-
}
392-
393-
impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword {
394-
fn check_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::Item<'_>) {
395-
for attr in cx.tcx.hir().attrs(item.hir_id()) {
396-
if !attr.has_name(sym::doc) {
397-
continue;
398-
}
399-
if let Some(list) = attr.meta_item_list() {
400-
for nested in list {
401-
if nested.has_name(sym::keyword) {
402-
let keyword = nested
403-
.value_str()
404-
.expect("#[doc(keyword = \"...\")] expected a value!");
405-
if is_doc_keyword(keyword) {
406-
return;
407-
}
408-
cx.emit_span_lint(EXISTING_DOC_KEYWORD, attr.span, NonExistentDocKeyword {
409-
keyword,
410-
});
411-
}
412-
}
413-
}
414-
}
415-
}
416-
}
417-
418378
declare_tool_lint! {
419379
/// The `untranslatable_diagnostic` lint detects messages passed to functions with `impl
420380
/// Into<{D,Subd}iagMessage` parameters without using translatable Fluent strings.

compiler/rustc_lint/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,6 @@ fn register_internals(store: &mut LintStore) {
600600
store.register_late_mod_pass(|_| Box::new(DefaultHashTypes));
601601
store.register_lints(&QueryStability::lint_vec());
602602
store.register_late_mod_pass(|_| Box::new(QueryStability));
603-
store.register_lints(&ExistingDocKeyword::lint_vec());
604-
store.register_late_mod_pass(|_| Box::new(ExistingDocKeyword));
605603
store.register_lints(&TyTyKind::lint_vec());
606604
store.register_late_mod_pass(|_| Box::new(TyTyKind));
607605
store.register_lints(&TypeIr::lint_vec());
@@ -629,7 +627,6 @@ fn register_internals(store: &mut LintStore) {
629627
LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
630628
LintId::of(USAGE_OF_QUALIFIED_TY),
631629
LintId::of(NON_GLOB_IMPORT_OF_TYPE_IR_INHERENT),
632-
LintId::of(EXISTING_DOC_KEYWORD),
633630
LintId::of(BAD_OPT_ACCESS),
634631
LintId::of(SPAN_USE_EQ_CTXT),
635632
]);

compiler/rustc_lint/src/lints.rs

-7
Original file line numberDiff line numberDiff line change
@@ -950,13 +950,6 @@ pub(crate) struct NonGlobImportTypeIrInherent {
950950
#[help]
951951
pub(crate) struct LintPassByHand;
952952

953-
#[derive(LintDiagnostic)]
954-
#[diag(lint_non_existent_doc_keyword)]
955-
#[help]
956-
pub(crate) struct NonExistentDocKeyword {
957-
pub keyword: Symbol,
958-
}
959-
960953
#[derive(LintDiagnostic)]
961954
#[diag(lint_diag_out_of_impl)]
962955
pub(crate) struct DiagOutOfImpl;

compiler/rustc_passes/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ rustc_feature = { path = "../rustc_feature" }
1616
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1717
rustc_hir = { path = "../rustc_hir" }
1818
rustc_index = { path = "../rustc_index" }
19-
rustc_lexer = { path = "../rustc_lexer" }
2019
rustc_macros = { path = "../rustc_macros" }
2120
rustc_middle = { path = "../rustc_middle" }
2221
rustc_privacy = { path = "../rustc_privacy" }

compiler/rustc_passes/messages.ftl

+3-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ passes_doc_invalid =
211211
passes_doc_keyword_empty_mod =
212212
`#[doc(keyword = "...")]` should be used on empty modules
213213
214-
passes_doc_keyword_invalid_ident =
215-
`{$doc_keyword}` is not a valid identifier
214+
passes_doc_keyword_not_keyword =
215+
nonexistent keyword `{$keyword}` used in `#[doc(keyword = "...")]`
216+
.help = only existing keywords are allowed in core/std
216217
217218
passes_doc_keyword_not_mod =
218219
`#[doc(keyword = "...")]` should be used on modules

compiler/rustc_passes/src/check_attr.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
914914
}
915915

916916
fn check_doc_keyword(&self, meta: &MetaItemInner, hir_id: HirId) {
917+
fn is_doc_keyword(s: Symbol) -> bool {
918+
// FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we
919+
// can remove the `SelfTy` case here, remove `sym::SelfTy`, and update the
920+
// `#[doc(keyword = "SelfTy")` attribute in `library/std/src/keyword_docs.rs`.
921+
s <= kw::Union || s == sym::SelfTy
922+
}
923+
917924
let doc_keyword = meta.value_str().unwrap_or(kw::Empty);
918925
if doc_keyword == kw::Empty {
919926
self.doc_attr_str_error(meta, "keyword");
@@ -935,10 +942,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
935942
return;
936943
}
937944
}
938-
if !rustc_lexer::is_ident(doc_keyword.as_str()) {
939-
self.dcx().emit_err(errors::DocKeywordInvalidIdent {
945+
if !is_doc_keyword(doc_keyword) {
946+
self.dcx().emit_err(errors::DocKeywordNotKeyword {
940947
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
941-
doc_keyword,
948+
keyword: doc_keyword,
942949
});
943950
}
944951
}

compiler/rustc_passes/src/errors.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,19 @@ pub(crate) struct DocKeywordEmptyMod {
216216
}
217217

218218
#[derive(Diagnostic)]
219-
#[diag(passes_doc_keyword_not_mod)]
220-
pub(crate) struct DocKeywordNotMod {
219+
#[diag(passes_doc_keyword_not_keyword)]
220+
#[help]
221+
pub(crate) struct DocKeywordNotKeyword {
221222
#[primary_span]
222223
pub span: Span,
224+
pub keyword: Symbol,
223225
}
224226

225227
#[derive(Diagnostic)]
226-
#[diag(passes_doc_keyword_invalid_ident)]
227-
pub(crate) struct DocKeywordInvalidIdent {
228+
#[diag(passes_doc_keyword_not_mod)]
229+
pub(crate) struct DocKeywordNotMod {
228230
#[primary_span]
229231
pub span: Span,
230-
pub doc_keyword: Symbol,
231232
}
232233

233234
#[derive(Diagnostic)]

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ symbols! {
306306
RwLockWriteGuard,
307307
Saturating,
308308
SeekFrom,
309+
SelfTy,
309310
Send,
310311
SeqCst,
311312
Sized,

library/std/src/keyword_docs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1263,10 +1263,10 @@ mod return_keyword {}
12631263
/// [Reference]: ../reference/items/associated-items.html#methods
12641264
mod self_keyword {}
12651265

1266-
// FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we can remove the
1267-
// three next lines and put back: `#[doc(keyword = "Self")]`.
1266+
// FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we can replace
1267+
// these two lines with `#[doc(keyword = "Self")]` and update `is_doc_keyword` in
1268+
// `CheckAttrVisitor`.
12681269
#[doc(alias = "Self")]
1269-
#[allow(rustc::existing_doc_keyword)]
12701270
#[doc(keyword = "SelfTy")]
12711271
//
12721272
/// The implementing type within a [`trait`] or [`impl`] block, or the current type within a type

library/std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@
251251
#![allow(explicit_outlives_requirements)]
252252
#![allow(unused_lifetimes)]
253253
#![allow(internal_features)]
254-
#![deny(rustc::existing_doc_keyword)]
255254
#![deny(fuzzy_provenance_casts)]
256255
#![deny(unsafe_op_in_unsafe_fn)]
257256
#![allow(rustdoc::redundant_explicit_links)]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ To do so, the `#[doc(keyword = "...")]` attribute is used. Example:
207207
#![allow(internal_features)]
208208

209209
/// Some documentation about the keyword.
210-
#[doc(keyword = "keyword")]
210+
#[doc(keyword = "break")]
211211
mod empty_mod {}
212212
```
213213

tests/rustdoc-gui/search-result-color.goml

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ define-function: (
140140
},
141141
)
142142

143-
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=coo"
143+
// Searching for the `for` keyword
144+
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=fo"
144145

145146
// This is needed so that the text color is computed.
146147
show-text: true
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Checks that the "keyword" results have the expected text alongside them.
22
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
3-
write-into: (".search-input", "CookieMonster")
3+
write-into: (".search-input", "for")
44
// To be SURE that the search will be run.
55
press-key: 'Enter'
66
// Waiting for the search results to appear...
77
wait-for: "#search-tabs"
8-
assert-text: (".result-keyword .result-name", "keyword CookieMonster")
8+
assert-text: (".result-keyword .result-name", "keyword for")

tests/rustdoc-gui/search-tab.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ call-function: ("check-colors", {
7878
set-window-size: (851, 600)
7979

8080
// Check the size and count in tabs
81-
assert-text: ("#search-tabs > button:nth-child(1) > .count", " (25) ")
81+
assert-text: ("#search-tabs > button:nth-child(1) > .count", " (26) ")
8282
assert-text: ("#search-tabs > button:nth-child(2) > .count", " (6)  ")
8383
assert-text: ("#search-tabs > button:nth-child(3) > .count", " (0)  ")
8484
store-property: ("#search-tabs > button:nth-child(1)", {"offsetWidth": buttonWidth})

tests/rustdoc-gui/src/test_docs/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub enum AnEnum {
156156
WithVariants { and: usize, sub: usize, variants: usize },
157157
}
158158

159-
#[doc(keyword = "CookieMonster")]
159+
#[doc(keyword = "for")]
160160
/// Some keyword.
161161
pub mod keyword {}
162162

tests/rustdoc-json/keyword.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
/// this is a test!
1414
pub mod foo {}
1515

16-
//@ !has "$.index[*][?(@.name=='hello')]"
16+
//@ !has "$.index[*][?(@.name=='break')]"
1717
//@ !has "$.index[*][?(@.name=='bar')]"
18-
#[doc(keyword = "hello")]
18+
#[doc(keyword = "break")]
1919
/// hello
2020
mod bar {}

tests/rustdoc-json/keyword_private.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
/// this is a test!
1212
pub mod foo {}
1313

14-
//@ !has "$.index[*][?(@.name=='hello')]"
14+
//@ !has "$.index[*][?(@.name=='break')]"
1515
//@ has "$.index[*][?(@.name=='bar')]"
16-
//@ is "$.index[*][?(@.name=='bar')].attrs" '["#[doc(keyword = \"hello\")]"]'
16+
//@ is "$.index[*][?(@.name=='bar')].attrs" '["#[doc(keyword = \"break\")]"]'
1717
//@ is "$.index[*][?(@.name=='bar')].docs" '"hello"'
18-
#[doc(keyword = "hello")]
18+
#[doc(keyword = "break")]
1919
/// hello
2020
mod bar {}
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: `foo df` is not a valid identifier
1+
error: nonexistent keyword `foo df` used in `#[doc(keyword = "...")]`
22
--> $DIR/invalid-keyword.rs:3:17
33
|
44
LL | #[doc(keyword = "foo df")]
55
| ^^^^^^^^
6+
|
7+
= help: only existing keywords are allowed in core/std
68

79
error: aborting due to 1 previous error
810

tests/rustdoc/keyword.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// this is a test!
1717
mod foo{}
1818

19-
//@ has foo/keyword.foo.html '//section[@id="main-content"]//div[@class="docblock"]//p' 'hello'
20-
#[doc(keyword = "foo")]
19+
//@ has foo/keyword.break.html '//section[@id="main-content"]//div[@class="docblock"]//p' 'hello'
20+
#[doc(keyword = "break")]
2121
/// hello
2222
mod bar {}

tests/ui/internal-lints/existing_doc_keyword.rs

-10
This file was deleted.

tests/ui/internal-lints/existing_doc_keyword.stderr

-15
This file was deleted.

tests/ui/rustdoc/doc_keyword.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#![crate_type = "lib"]
22
#![feature(rustdoc_internals)]
33

4-
#![doc(keyword = "hello")] //~ ERROR
5-
6-
#[doc(keyword = "hell")] //~ ERROR
4+
#![doc(keyword = "hello")]
5+
//~^ ERROR `#![doc(keyword = "...")]` isn't allowed as a crate-level attribute
6+
#[doc(keyword = "hell")] //~ ERROR `#[doc(keyword = "...")]` should be used on empty modules
77
mod foo {
88
fn hell() {}
99
}
1010

11-
#[doc(keyword = "hall")] //~ ERROR
11+
#[doc(keyword = "hall")] //~ ERROR `#[doc(keyword = "...")]` should be used on modules
1212
fn foo() {}
1313

1414

@@ -18,3 +18,6 @@ trait Foo {
1818
//~^ ERROR: `#[doc(keyword = "...")]` should be used on modules
1919
fn quux() {}
2020
}
21+
22+
#[doc(keyword = "tadam")] //~ ERROR nonexistent keyword `tadam`
23+
mod tadam {}

tests/ui/rustdoc/doc_keyword.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ error: `#[doc(keyword = "...")]` should be used on modules
1010
LL | #[doc(keyword = "hall")]
1111
| ^^^^^^^^^^^^^^^^
1212

13+
error: nonexistent keyword `tadam` used in `#[doc(keyword = "...")]`
14+
--> $DIR/doc_keyword.rs:22:17
15+
|
16+
LL | #[doc(keyword = "tadam")]
17+
| ^^^^^^^
18+
|
19+
= help: only existing keywords are allowed in core/std
20+
1321
error: `#[doc(keyword = "...")]` should be used on modules
1422
--> $DIR/doc_keyword.rs:17:11
1523
|
@@ -22,5 +30,5 @@ error: `#![doc(keyword = "...")]` isn't allowed as a crate-level attribute
2230
LL | #![doc(keyword = "hello")]
2331
| ^^^^^^^^^^^^^^^^^
2432

25-
error: aborting due to 4 previous errors
33+
error: aborting due to 5 previous errors
2634

0 commit comments

Comments
 (0)