]> git.lizzy.rs Git - rust.git/blobdiff - src/librustdoc/clean/mod.rs
Auto merge of #79780 - camelid:use-summary_opts, r=GuillaumeGomez
[rust.git] / src / librustdoc / clean / mod.rs
index 221c818736ea283a37186a8e0def167fa7567956..16274430902e42486f3020117f4020e74b4c5efe 100644 (file)
@@ -162,9 +162,6 @@ 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);
@@ -172,19 +169,7 @@ fn clean(&self, cx: &DocContext<'_>) -> ExternalCrate {
                 for attr in attrs.lists(sym::doc) {
                     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);
-                            }
+                            keyword = Some(v.to_string());
                             break;
                         }
                     }
@@ -1015,7 +1000,7 @@ fn clean(&self, cx: &DocContext<'_>) -> FnDecl {
                     .iter()
                     .map(|t| Argument {
                         type_: t.clean(cx),
-                        name: names.next().map_or(String::new(), |name| name.to_string()),
+                        name: names.next().map_or_else(|| String::new(), |name| name.to_string()),
                     })
                     .collect(),
             },
@@ -1515,9 +1500,12 @@ fn clean(&self, cx: &DocContext<'_>) -> Type {
 }
 
 /// Returns `None` if the type could not be normalized
-#[allow(unreachable_code, unused_variables)]
 fn normalize(cx: &DocContext<'tcx>, ty: Ty<'_>) -> Option<Ty<'tcx>> {
-    return None; // HACK: low-churn fix for #79459 while we wait for a trait normalization fix
+    // HACK: low-churn fix for #79459 while we wait for a trait normalization fix
+    if !cx.tcx.sess.opts.debugging_opts.normalize_docs {
+        return None;
+    }
+
     use crate::rustc_trait_selection::infer::TyCtxtInferExt;
     use crate::rustc_trait_selection::traits::query::normalize::AtExt;
     use rustc_middle::traits::ObligationCause;
@@ -1986,16 +1974,13 @@ fn clean(&self, cx: &DocContext<'_>) -> BareFunctionDecl {
     }
 }
 
-impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
+impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
     fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
         use hir::ItemKind;
 
         let (item, renamed) = self;
         let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
-        let mut name = match renamed {
-            Some(ident) => ident.name,
-            None => cx.tcx.hir().name(item.hir_id),
-        };
+        let mut name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id));
         cx.with_param_env(def_id, || {
             let kind = match item.kind {
                 ItemKind::Static(ty, mutability, body_id) => StaticItem(Static {
@@ -2189,6 +2174,7 @@ fn clean_extern_crate(
         def_id: crate_def_id,
         visibility: krate.vis.clean(cx),
         stability: None,
+        const_stability: None,
         deprecation: None,
         kind: ExternCrateItem(name.clean(cx), path),
     }]
@@ -2259,6 +2245,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
                         def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
                         visibility: self.vis.clean(cx),
                         stability: None,
+                        const_stability: None,
                         deprecation: None,
                         kind: ImportItem(Import::new_simple(
                             self.name.clean(cx),
@@ -2276,16 +2263,17 @@ fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
             name: None,
             attrs: self.attrs.clean(cx),
             source: self.span.clean(cx),
-            def_id: DefId::local(CRATE_DEF_INDEX),
+            def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
             visibility: self.vis.clean(cx),
             stability: None,
+            const_stability: None,
             deprecation: None,
             kind: ImportItem(inner),
         }]
     }
 }
 
-impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
+impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
     fn clean(&self, cx: &DocContext<'_>) -> Item {
         let (item, renamed) = self;
         cx.with_param_env(cx.tcx.hir().local_def_id(item.hir_id).to_def_id(), || {
@@ -2319,7 +2307,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
 
             Item::from_hir_id_and_parts(
                 item.hir_id,
-                Some(renamed.unwrap_or(item.ident).name),
+                Some(renamed.unwrap_or(item.ident.name)),
                 kind,
                 cx,
             )
@@ -2327,22 +2315,49 @@ fn clean(&self, cx: &DocContext<'_>) -> Item {
     }
 }
 
-impl Clean<Item> for doctree::Macro {
+impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
     fn clean(&self, cx: &DocContext<'_>) -> Item {
-        Item::from_def_id_and_parts(
-            self.def_id,
-            Some(self.name.clean(cx)),
-            MacroItem(Macro {
-                source: format!(
-                    "macro_rules! {} {{\n{}}}",
-                    self.name,
-                    self.matchers
+        let (item, renamed) = self;
+        let name = renamed.unwrap_or(item.ident.name);
+        let tts = item.ast.body.inner_tokens().trees().collect::<Vec<_>>();
+        // Extract the spans of all matchers. They represent the "interface" of the macro.
+        let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect::<Vec<_>>();
+        let source = if item.ast.macro_rules {
+            format!(
+                "macro_rules! {} {{\n{}}}",
+                name,
+                matchers
+                    .iter()
+                    .map(|span| { format!("    {} => {{ ... }};\n", span.to_src(cx)) })
+                    .collect::<String>(),
+            )
+        } else {
+            let vis = item.vis.clean(cx);
+
+            if matchers.len() <= 1 {
+                format!(
+                    "{}macro {}{} {{\n    ...\n}}",
+                    vis.print_with_space(),
+                    name,
+                    matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
+                )
+            } else {
+                format!(
+                    "{}macro {} {{\n{}}}",
+                    vis.print_with_space(),
+                    name,
+                    matchers
                         .iter()
-                        .map(|span| { format!("    {} => {{ ... }};\n", span.to_src(cx)) })
-                        .collect::<String>()
-                ),
-                imported_from: self.imported_from.clean(cx),
-            }),
+                        .map(|span| { format!("    {} => {{ ... }},\n", span.to_src(cx)) })
+                        .collect::<String>(),
+                )
+            }
+        };
+
+        Item::from_hir_id_and_parts(
+            item.hir_id,
+            Some(name),
+            MacroItem(Macro { source, imported_from: None }),
             cx,
         )
     }