Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend doc keyword feature by allowing any ident #79464

Merged
merged 3 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,11 +1589,6 @@ impl Symbol {
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
Expand Down
24 changes: 18 additions & 6 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,30 @@ impl Clean<ExternalCrate> for CrateNum {
.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() {
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/rustdoc-ui/invalid-keyword.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![feature(doc_keyword)]

#[doc(keyword = "foo df")] //~ ERROR
mod foo {}
8 changes: 8 additions & 0 deletions src/test/rustdoc-ui/invalid-keyword.stderr
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions src/test/rustdoc/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}