From dc10ccfe89d6c8b4ca7993afd66c5dd83f6712ee Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 1 Dec 2020 14:19:53 +0100 Subject: [PATCH] Add checks for #[doc(keyword = "...")] and move them into rustc_passes --- compiler/rustc_passes/src/check_attr.rs | 60 ++++++++++++++++++++++--- src/librustdoc/clean/mod.rs | 3 -- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 54621dae1ac..fc97ca035b9 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -287,24 +287,20 @@ impl CheckAttrVisitor<'tcx> { } } - fn doc_alias_str_error(&self, meta: &NestedMetaItem) { + fn doc_attr_str_error(&self, meta: &NestedMetaItem, attr_name: &str) { self.tcx .sess .struct_span_err( meta.span(), - "doc alias attribute expects a string: #[doc(alias = \"0\")]", + &format!("doc {0} attribute expects a string: #[doc({0} = \"a\")]", attr_name), ) .emit(); } fn check_doc_alias(&self, meta: &NestedMetaItem, hir_id: HirId, target: Target) -> bool { - if !meta.is_value_str() { - self.doc_alias_str_error(meta); - return false; - } let doc_alias = meta.value_str().map(|s| s.to_string()).unwrap_or_else(String::new); if doc_alias.is_empty() { - self.doc_alias_str_error(meta); + self.doc_attr_str_error(meta, "alias"); return false; } if let Some(c) = @@ -365,6 +361,49 @@ impl CheckAttrVisitor<'tcx> { true } + fn check_doc_keyword(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool { + let doc_keyword = meta.value_str().map(|s| s.to_string()).unwrap_or_else(String::new); + if doc_keyword.is_empty() { + self.doc_attr_str_error(meta, "keyword"); + return false; + } + match self.tcx.hir().expect_item(hir_id).kind { + ItemKind::Mod(ref module) => { + if !module.item_ids.is_empty() { + self.tcx + .sess + .struct_span_err( + meta.span(), + "`#[doc(keyword = \"...\")]` can only be used on empty modules", + ) + .emit(); + return false; + } + } + _ => { + self.tcx + .sess + .struct_span_err( + meta.span(), + "`#[doc(keyword = \"...\")]` can only be used on modules", + ) + .emit(); + return false; + } + } + if !rustc_lexer::is_ident(&doc_keyword) { + self.tcx + .sess + .struct_span_err( + meta.name_value_literal_span().unwrap_or_else(|| meta.span()), + &format!("`{}` is not a valid identifier", doc_keyword), + ) + .emit(); + return false; + } + true + } + fn check_attr_crate_level( &self, meta: &NestedMetaItem, @@ -384,6 +423,7 @@ impl CheckAttrVisitor<'tcx> { .emit(); return false; } + true } fn check_doc_attrs(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool { @@ -396,6 +436,12 @@ impl CheckAttrVisitor<'tcx> { { return false; } + } else if meta.has_name(sym::keyword) { + if !self.check_attr_crate_level(meta, hir_id, "keyword") + || !self.check_doc_keyword(meta, hir_id) + { + return false; + } } } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index d294d8f02a8..cd2700be5a7 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -162,9 +162,6 @@ impl Clean 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);