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](https://github.com/rust-lang/rust/issues/51315#issuecomment-733879934), 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``
This commit is contained in:
commit
ca8a1b05c6
@ -1590,11 +1590,6 @@ fn is_unused_keyword_2018(self) -> bool {
|
||||
self == kw::Try
|
||||
}
|
||||
|
||||
/// Used for sanity checking rustdoc keyword sections.
|
||||
pub fn is_doc_keyword(self) -> bool {
|
||||
self <= kw::Union
|
||||
}
|
||||
|
||||
/// A keyword or reserved identifier that can be used as a path segment.
|
||||
pub fn is_path_segment_keyword(self) -> bool {
|
||||
self == kw::Super
|
||||
|
@ -162,18 +162,30 @@ fn clean(&self, cx: &DocContext<'_>) -> ExternalCrate {
|
||||
.collect()
|
||||
};
|
||||
|
||||
let get_span =
|
||||
|attr: &ast::NestedMetaItem| Some(attr.meta_item()?.name_value_literal()?.span);
|
||||
|
||||
let as_keyword = |res: Res| {
|
||||
if let Res::Def(DefKind::Mod, def_id) = res {
|
||||
let attrs = cx.tcx.get_attrs(def_id).clean(cx);
|
||||
let mut keyword = None;
|
||||
for attr in attrs.lists(sym::doc) {
|
||||
if let Some(v) = attr.value_str() {
|
||||
if attr.has_name(sym::keyword) {
|
||||
if v.is_doc_keyword() {
|
||||
keyword = Some(v.to_string());
|
||||
break;
|
||||
if attr.has_name(sym::keyword) {
|
||||
if let Some(v) = attr.value_str() {
|
||||
let k = v.to_string();
|
||||
if !rustc_lexer::is_ident(&k) {
|
||||
let sp = get_span(&attr).unwrap_or_else(|| attr.span());
|
||||
cx.tcx
|
||||
.sess
|
||||
.struct_span_err(
|
||||
sp,
|
||||
&format!("`{}` is not a valid identifier", v),
|
||||
)
|
||||
.emit();
|
||||
} else {
|
||||
keyword = Some(k);
|
||||
}
|
||||
// FIXME: should warn on unknown keywords?
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
src/test/rustdoc-ui/invalid-keyword.rs
Normal file
4
src/test/rustdoc-ui/invalid-keyword.rs
Normal file
@ -0,0 +1,4 @@
|
||||
#![feature(doc_keyword)]
|
||||
|
||||
#[doc(keyword = "foo df")] //~ ERROR
|
||||
mod foo {}
|
8
src/test/rustdoc-ui/invalid-keyword.stderr
Normal file
8
src/test/rustdoc-ui/invalid-keyword.stderr
Normal file
@ -0,0 +1,8 @@
|
||||
error: `foo df` is not a valid identifier
|
||||
--> $DIR/invalid-keyword.rs:3:17
|
||||
|
|
||||
LL | #[doc(keyword = "foo df")]
|
||||
| ^^^^^^^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
@ -14,3 +14,8 @@
|
||||
#[doc(keyword = "match")]
|
||||
/// this is a test!
|
||||
mod foo{}
|
||||
|
||||
// @has foo/keyword.foo.html '//section[@id="main"]//div[@class="docblock"]//p' 'hello'
|
||||
#[doc(keyword = "foo")]
|
||||
/// hello
|
||||
mod bar {}
|
||||
|
Loading…
Reference in New Issue
Block a user