From f6630932220071a4ebe2fb3302d5af6a80ec8343 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 27 Nov 2020 14:27:59 +0100 Subject: [PATCH] Allow to have any valid ident used as keyword in doc_keyword feature --- src/librustdoc/clean/mod.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index e76ca1022a9..8babc90793a 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -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; } } }