diff options
Diffstat (limited to 'src/librustdoc')
| -rw-r--r-- | src/librustdoc/clean/inline.rs | 2 | ||||
| -rw-r--r-- | src/librustdoc/clean/types.rs | 27 | ||||
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 58 | ||||
| -rw-r--r-- | src/librustdoc/html/render/print_item.rs | 36 | ||||
| -rw-r--r-- | src/librustdoc/html/static/css/rustdoc.css | 8 | ||||
| -rw-r--r-- | src/librustdoc/html/static/css/themes/ayu.css | 2 | ||||
| -rw-r--r-- | src/librustdoc/html/static/css/themes/dark.css | 2 | ||||
| -rw-r--r-- | src/librustdoc/html/static/css/themes/light.css | 2 | ||||
| -rw-r--r-- | src/librustdoc/html/templates/page.html | 8 | ||||
| -rw-r--r-- | src/librustdoc/passes/propagate_doc_cfg.rs | 39 |
10 files changed, 121 insertions, 63 deletions
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index a7048e788b6..f367edcbf5a 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -300,7 +300,7 @@ pub(crate) fn build_impls( } /// `parent_module` refers to the parent of the re-export, not the original item -fn merge_attrs( +pub(crate) fn merge_attrs( cx: &mut DocContext<'_>, parent_module: Option<DefId>, old_attrs: Attrs<'_>, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 4c39021903c..909a47d07b1 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -482,7 +482,7 @@ impl Item { cx: &mut DocContext<'_>, cfg: Option<Arc<Cfg>>, ) -> Item { - trace!("name={:?}, def_id={:?}", name, def_id); + trace!("name={:?}, def_id={:?} cfg={:?}", name, def_id, cfg); // Primitives and Keywords are written in the source code as private modules. // The modules need to be private so that nobody actually uses them, but the @@ -801,6 +801,31 @@ impl ItemKind { | KeywordItem => [].iter(), } } + + /// Returns `true` if this item does not appear inside an impl block. + pub(crate) fn is_non_assoc(&self) -> bool { + matches!( + self, + StructItem(_) + | UnionItem(_) + | EnumItem(_) + | TraitItem(_) + | ModuleItem(_) + | ExternCrateItem { .. } + | FunctionItem(_) + | TypedefItem(_) + | OpaqueTyItem(_) + | StaticItem(_) + | ConstantItem(_) + | TraitAliasItem(_) + | ForeignFunctionItem(_) + | ForeignStaticItem(_) + | ForeignTypeItem + | MacroItem(_) + | ProcMacroItem(_) + | PrimitiveItem(_) + ) + } } #[derive(Clone, Debug)] diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index f9d6b4619cc..6272f47f460 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -191,12 +191,6 @@ impl StylePath { } } -fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) { - if let Some(l) = cx.src_href(item) { - write!(buf, "<a class=\"srclink\" href=\"{}\">source</a>", l) - } -} - #[derive(Debug, Eq, PartialEq, Hash)] struct ItemEntry { url: String, @@ -522,7 +516,14 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<Strin (cfg, _) => cfg.as_deref().cloned(), }; - debug!("Portability {:?} - {:?} = {:?}", item.cfg, parent.and_then(|p| p.cfg.as_ref()), cfg); + debug!( + "Portability {:?} {:?} (parent: {:?}) - {:?} = {:?}", + item.name, + item.cfg, + parent, + parent.and_then(|p| p.cfg.as_ref()), + cfg + ); Some(format!("<div class=\"stab portability\">{}</div>", cfg?.render_long_html())) } @@ -840,12 +841,13 @@ fn assoc_method( /// Note that it is possible for an unstable function to be const-stable. In that case, the span /// will include the const-stable version, but no stable version will be emitted, as a natural /// consequence of the above rules. -fn render_stability_since_raw( +fn render_stability_since_raw_with_extra( w: &mut Buffer, ver: Option<Symbol>, const_stability: Option<ConstStability>, containing_ver: Option<Symbol>, containing_const_ver: Option<Symbol>, + extra_class: &str, ) -> bool { let stable_version = ver.filter(|inner| !inner.is_empty() && Some(*inner) != containing_ver); @@ -893,12 +895,30 @@ fn render_stability_since_raw( } if !stability.is_empty() { - write!(w, r#"<span class="since" title="{}">{}</span>"#, title, stability); + write!(w, r#"<span class="since{extra_class}" title="{title}">{stability}</span>"#); } !stability.is_empty() } +#[inline] +fn render_stability_since_raw( + w: &mut Buffer, + ver: Option<Symbol>, + const_stability: Option<ConstStability>, + containing_ver: Option<Symbol>, + containing_const_ver: Option<Symbol>, +) -> bool { + render_stability_since_raw_with_extra( + w, + ver, + const_stability, + containing_ver, + containing_const_ver, + "", + ) +} + fn render_assoc_item( w: &mut Buffer, item: &clean::Item, @@ -1681,23 +1701,29 @@ fn render_rightside( RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)), RenderMode::ForDeref { .. } => (None, None), }; + let src_href = cx.src_href(item); + let has_src_ref = src_href.is_some(); let mut rightside = Buffer::new(); - let has_stability = render_stability_since_raw( + let has_stability = render_stability_since_raw_with_extra( &mut rightside, item.stable_since(tcx), const_stability, containing_item.stable_since(tcx), const_stable_since, + if has_src_ref { "" } else { " rightside" }, ); - let mut srclink = Buffer::empty_from(w); - write_srclink(cx, item, &mut srclink); - if has_stability && !srclink.is_empty() { - rightside.write_str(" · "); + if let Some(l) = src_href { + if has_stability { + write!(rightside, " · <a class=\"srclink\" href=\"{}\">source</a>", l) + } else { + write!(rightside, "<a class=\"srclink rightside\" href=\"{}\">source</a>", l) + } } - rightside.push_buffer(srclink); - if !rightside.is_empty() { + if has_stability && has_src_ref { write!(w, "<span class=\"rightside\">{}</span>", rightside.into_inner()); + } else { + w.push_buffer(rightside); } } diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 6d0a825fec8..912601dda20 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -18,7 +18,7 @@ use std::rc::Rc; use super::{ collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section, notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code, - render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink, + render_attributes_in_pre, render_impl, render_rightside, render_stability_since_raw, AssocItemLink, Context, ImplRenderingParameters, }; use crate::clean; @@ -477,7 +477,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) -> (cfg, _) => cfg.as_deref().cloned(), }; - debug!("Portability {:?} - {:?} = {:?}", item.cfg, parent.cfg, cfg); + debug!("Portability name={:?} {:?} - {:?} = {:?}", item.name, item.cfg, parent.cfg, cfg); if let Some(ref cfg) = cfg { tags += &tag_html("portability", &cfg.render_long_plain(), &cfg.render_short_html()); } @@ -709,14 +709,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: write!(w, "<details class=\"rustdoc-toggle\" open><summary>"); } write!(w, "<div id=\"{}\" class=\"method has-srclink\">", id); - write!(w, "<div class=\"rightside\">"); - - let has_stability = render_stability_since(w, m, t, cx.tcx()); - if has_stability { - w.write_str(" · "); - } - write_srclink(cx, m, w); - write!(w, "</div>"); + render_rightside(w, cx, m, t, RenderMode::Normal); write!(w, "<h4 class=\"code-header\">"); render_assoc_item( w, @@ -1260,7 +1253,13 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean:: w.write_str(")"); } w.write_str("</code>"); - render_stability_since(w, variant, it, cx.tcx()); + render_stability_since_raw( + w, + variant.stable_since(cx.tcx()), + variant.const_stability(cx.tcx()), + it.stable_since(cx.tcx()), + it.const_stable_since(cx.tcx()), + ); w.write_str("</h3>"); use crate::clean::Variant; @@ -1591,21 +1590,6 @@ where w.write_str("</code></pre>"); } -fn render_stability_since( - w: &mut Buffer, - item: &clean::Item, - containing_item: &clean::Item, - tcx: TyCtxt<'_>, -) -> bool { - render_stability_since_raw( - w, - item.stable_since(tcx), - item.const_stability(tcx), - containing_item.stable_since(tcx), - containing_item.const_stable_since(tcx), - ) -} - fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cx: &Context<'_>) -> Ordering { let lhss = format!("{}", lhs.inner_impl().print(false, cx)); let rhss = format!("{}", rhs.inner_impl().print(false, cx)); diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index f6a7875fddc..38cdcd61e88 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1200,14 +1200,6 @@ so that we can apply CSS-filters to change the arrow color in themes */ float: right; } -.variants_table { - width: 100%; -} - -.variants_table tbody tr td:first-child { - width: 1%; /* make the variant name as small as possible */ -} - td.summary-column { width: 100%; } diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index 63009006b3f..8591f22d6de 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -199,7 +199,7 @@ details.rustdoc-toggle > summary::before { background: none; } -.rightside, +.rightside:not(a), .out-of-band { color: grey; } diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css index 1fcda22b6f4..d5cd47c3e19 100644 --- a/src/librustdoc/html/static/css/themes/dark.css +++ b/src/librustdoc/html/static/css/themes/dark.css @@ -165,7 +165,7 @@ details.rustdoc-toggle > summary::before { background: none; } -.rightside, +.rightside:not(a), .out-of-band { color: grey; } diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index 7139c199729..cff70268144 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -148,7 +148,7 @@ details.rustdoc-toggle > summary::before { .stab { background: #FFF5D6; border-color: #FFC600; } .stab.portability > code { background: none; } -.rightside, +.rightside:not(a), .out-of-band { color: grey; } diff --git a/src/librustdoc/html/templates/page.html b/src/librustdoc/html/templates/page.html index 8e25f6764a9..7caffeae3d6 100644 --- a/src/librustdoc/html/templates/page.html +++ b/src/librustdoc/html/templates/page.html @@ -13,13 +13,13 @@ <link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}SourceCodePro-Regular.ttf.woff2"> {#- -#} <link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}SourceSerif4-Bold.ttf.woff2"> {#- -#} <link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}SourceCodePro-Semibold.ttf.woff2"> {#- -#} - <link rel="stylesheet" type="text/css" {# -#} + <link rel="stylesheet" {# -#} href="{{static_root_path|safe}}normalize{{page.resource_suffix}}.css"> {#- -#} - <link rel="stylesheet" type="text/css" {# -#} + <link rel="stylesheet" {# -#} href="{{static_root_path|safe}}rustdoc{{page.resource_suffix}}.css" {# -#} id="mainThemeStyle"> {#- -#} {%- for theme in themes -%} - <link rel="stylesheet" type="text/css" {# -#} + <link rel="stylesheet" {# -#} href="{{static_root_path|safe}}{{theme}}{{page.resource_suffix}}.css" {# -#} {%- if theme == "light" -%} id="themeStyle" @@ -51,7 +51,7 @@ href="{{static_root_path|safe}}noscript{{page.resource_suffix}}.css"> {#- -#} </noscript> {#- -#} {%- if layout.css_file_extension.is_some() -%} - <link rel="stylesheet" type="text/css" {# -#} + <link rel="stylesheet" {# -#} href="{{static_root_path|safe}}theme{{page.resource_suffix}}.css"> {#- -#} {%- endif -%} {%- if !layout.favicon.is_empty() -%} diff --git a/src/librustdoc/passes/propagate_doc_cfg.rs b/src/librustdoc/passes/propagate_doc_cfg.rs index 0c5d8365518..21d295bb1f8 100644 --- a/src/librustdoc/passes/propagate_doc_cfg.rs +++ b/src/librustdoc/passes/propagate_doc_cfg.rs @@ -2,29 +2,53 @@ use std::sync::Arc; use crate::clean::cfg::Cfg; +use crate::clean::inline::{load_attrs, merge_attrs}; use crate::clean::{Crate, Item}; use crate::core::DocContext; use crate::fold::DocFolder; use crate::passes::Pass; +use rustc_hir::def_id::LocalDefId; + pub(crate) const PROPAGATE_DOC_CFG: Pass = Pass { name: "propagate-doc-cfg", run: propagate_doc_cfg, description: "propagates `#[doc(cfg(...))]` to child items", }; -pub(crate) fn propagate_doc_cfg(cr: Crate, _: &mut DocContext<'_>) -> Crate { - CfgPropagator { parent_cfg: None }.fold_crate(cr) +pub(crate) fn propagate_doc_cfg(cr: Crate, cx: &mut DocContext<'_>) -> Crate { + CfgPropagator { parent_cfg: None, parent: None, cx }.fold_crate(cr) } -struct CfgPropagator { +struct CfgPropagator<'a, 'tcx> { parent_cfg: Option<Arc<Cfg>>, + parent: Option<LocalDefId>, + cx: &'a mut DocContext<'tcx>, } -impl DocFolder for CfgPropagator { +impl<'a, 'tcx> DocFolder for CfgPropagator<'a, 'tcx> { fn fold_item(&mut self, mut item: Item) -> Option<Item> { let old_parent_cfg = self.parent_cfg.clone(); + if item.kind.is_non_assoc() && + let Some(def_id) = item.item_id.as_def_id().and_then(|def_id| def_id.as_local()) { + let hir = self.cx.tcx.hir(); + let hir_id = hir.local_def_id_to_hir_id(def_id); + let expected_parent = hir.get_parent_item(hir_id); + + // If parents are different, it means that `item` is a reexport and we need to compute + // the actual `cfg` by iterating through its "real" parents. + if self.parent != Some(expected_parent) { + let mut attrs = Vec::new(); + for (parent_hir_id, _) in hir.parent_iter(hir_id) { + let def_id = hir.local_def_id(parent_hir_id).to_def_id(); + attrs.extend_from_slice(load_attrs(self.cx, def_id)); + } + let (_, cfg) = + merge_attrs(self.cx, None, item.attrs.other_attrs.as_slice(), Some(&attrs)); + item.cfg = cfg; + } + } let new_cfg = match (self.parent_cfg.take(), item.cfg.take()) { (None, None) => None, (Some(rc), None) | (None, Some(rc)) => Some(rc), @@ -37,8 +61,15 @@ impl DocFolder for CfgPropagator { self.parent_cfg = new_cfg.clone(); item.cfg = new_cfg; + let old_parent = + if let Some(def_id) = item.item_id.as_def_id().and_then(|def_id| def_id.as_local()) { + self.parent.replace(def_id) + } else { + self.parent.take() + }; let result = self.fold_item_recur(item); self.parent_cfg = old_parent_cfg; + self.parent = old_parent; Some(result) } |
