Skip to content

Commit ca8a1b0

Browse files
authored
Rollup merge of #79464 - GuillaumeGomez:doc-keyword-ident, r=jyn514
Extend doc keyword feature by allowing any ident Part of #51315. As suggested by ``@danielhenrymantilla`` in [this comment](#51315 (comment)), this PR extends `#[doc(keyword = "...")]` to allow any ident to be used as keyword. The final goal is to allow (proc-)macro crates' owners to write documentation of the keywords they might introduce. r? ``@jyn514``
2 parents d5d6036 + 482b3ac commit ca8a1b0

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

compiler/rustc_span/src/symbol.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1590,11 +1590,6 @@ impl Symbol {
15901590
self == kw::Try
15911591
}
15921592

1593-
/// Used for sanity checking rustdoc keyword sections.
1594-
pub fn is_doc_keyword(self) -> bool {
1595-
self <= kw::Union
1596-
}
1597-
15981593
/// A keyword or reserved identifier that can be used as a path segment.
15991594
pub fn is_path_segment_keyword(self) -> bool {
16001595
self == kw::Super

src/librustdoc/clean/mod.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,30 @@ impl Clean<ExternalCrate> for CrateNum {
162162
.collect()
163163
};
164164

165+
let get_span =
166+
|attr: &ast::NestedMetaItem| Some(attr.meta_item()?.name_value_literal()?.span);
167+
165168
let as_keyword = |res: Res| {
166169
if let Res::Def(DefKind::Mod, def_id) = res {
167170
let attrs = cx.tcx.get_attrs(def_id).clean(cx);
168171
let mut keyword = None;
169172
for attr in attrs.lists(sym::doc) {
170-
if let Some(v) = attr.value_str() {
171-
if attr.has_name(sym::keyword) {
172-
if v.is_doc_keyword() {
173-
keyword = Some(v.to_string());
174-
break;
173+
if attr.has_name(sym::keyword) {
174+
if let Some(v) = attr.value_str() {
175+
let k = v.to_string();
176+
if !rustc_lexer::is_ident(&k) {
177+
let sp = get_span(&attr).unwrap_or_else(|| attr.span());
178+
cx.tcx
179+
.sess
180+
.struct_span_err(
181+
sp,
182+
&format!("`{}` is not a valid identifier", v),
183+
)
184+
.emit();
185+
} else {
186+
keyword = Some(k);
175187
}
176-
// FIXME: should warn on unknown keywords?
188+
break;
177189
}
178190
}
179191
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![feature(doc_keyword)]
2+
3+
#[doc(keyword = "foo df")] //~ ERROR
4+
mod foo {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `foo df` is not a valid identifier
2+
--> $DIR/invalid-keyword.rs:3:17
3+
|
4+
LL | #[doc(keyword = "foo df")]
5+
| ^^^^^^^^
6+
7+
error: aborting due to previous error
8+

src/test/rustdoc/keyword.rs

+5
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
#[doc(keyword = "match")]
1515
/// this is a test!
1616
mod foo{}
17+
18+
// @has foo/keyword.foo.html '//section[@id="main"]//div[@class="docblock"]//p' 'hello'
19+
#[doc(keyword = "foo")]
20+
/// hello
21+
mod bar {}

0 commit comments

Comments
 (0)