From 0c736e92a3575927d693b839f7ab537db7d6f84e Mon Sep 17 00:00:00 2001 From: Tor Hovland Date: Mon, 5 Apr 2021 20:57:49 +0200 Subject: [PATCH 1/7] Add stability tags to ImportItem. --- src/librustdoc/html/render/print_item.rs | 26 ++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 42b795030171b..e2c7a24406458 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -282,11 +282,29 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl } clean::ImportItem(ref import) => { + let (stab, stab_tags) = if let Some(def_id) = import.source.did { + // Just need an item with the correct def_id + let import_item = clean::Item { def_id, ..myitem.clone() }; + let stab = import_item.stability_class(cx.tcx()); + let stab_tags = Some(extra_info_tags(&import_item, item, cx.tcx())); + (stab, stab_tags) + } else { + (None, None) + }; + + let add = if stab.is_some() { " " } else { "" }; + write!( w, - "{}{}", - myitem.visibility.print_with_space(myitem.def_id, cx), - import.print(cx), + "\ + {vis}{imp}\ + {stab_tags}\ + ", + stab = stab.unwrap_or_default(), + add = add, + vis = myitem.visibility.print_with_space(myitem.def_id, cx), + imp = import.print(cx), + stab_tags = stab_tags.unwrap_or_default(), ); } @@ -320,7 +338,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl docs = MarkdownSummaryLine(&doc_value, &myitem.links(cx)).into_string(), class = myitem.type_(), add = add, - stab = stab.unwrap_or_else(String::new), + stab = stab.unwrap_or_default(), unsafety_flag = unsafety_flag, href = item_path(myitem.type_(), &myitem.name.unwrap().as_str()), title = [full_path(cx, myitem), myitem.type_().to_string()] From 41f49aac4e5cdade7e41e89c4ea83c79b00a7dce Mon Sep 17 00:00:00 2001 From: Tor Hovland Date: Thu, 8 Apr 2021 23:03:39 +0200 Subject: [PATCH 2/7] Added a test. --- src/test/rustdoc/issue-83832.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/rustdoc/issue-83832.rs diff --git a/src/test/rustdoc/issue-83832.rs b/src/test/rustdoc/issue-83832.rs new file mode 100644 index 0000000000000..93dff7c6ce645 --- /dev/null +++ b/src/test/rustdoc/issue-83832.rs @@ -0,0 +1,21 @@ +#![crate_name = "foo"] + +pub mod io { + #[deprecated(since = "0.1.8", note = "Use bar() instead")] + pub trait Reader {} + pub trait Writer {} +} + +// @has foo/mod1/index.html +pub mod mod1 { + // @has - '//code' 'pub use io::Reader;' + // @has - '//span' 'Deprecated' + pub use io::Reader; +} + +// @has foo/mod2/index.html +pub mod mod2 { + // @has - '//code' 'pub use io::Writer;' + // @!has - '//span' 'Deprecated' + pub use io::Writer; +} From 1e2ab998c378e3c9b532e811a8f93b6a65711f92 Mon Sep 17 00:00:00 2001 From: Tor Hovland Date: Tue, 13 Apr 2021 17:23:27 +0200 Subject: [PATCH 3/7] Give import items their own CSS class. --- src/librustdoc/html/render/print_item.rs | 2 +- src/librustdoc/html/static/rustdoc.css | 6 ++++-- src/librustdoc/html/static/themes/ayu.css | 3 ++- src/librustdoc/html/static/themes/dark.css | 3 ++- src/librustdoc/html/static/themes/light.css | 3 ++- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index e2c7a24406458..c37a21d1adef3 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -296,7 +296,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl write!( w, - "\ + "\ {vis}{imp}\ {stab_tags}\ ", diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 427564cd7794a..776a2b25ed04e 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -868,7 +868,8 @@ body.blur > :not(#help) { 0 -1px 0 black; } -.module-item .stab { +.module-item .stab, +.import-item .stab { border-radius: 3px; display: inline-block; font-size: 80%; @@ -879,7 +880,8 @@ body.blur > :not(#help) { vertical-align: text-bottom; } -.module-item.unstable { +.module-item.unstable, +.import-item.unstable { opacity: 0.65; } diff --git a/src/librustdoc/html/static/themes/ayu.css b/src/librustdoc/html/static/themes/ayu.css index 5f6f3d66e5757..e45f95c2b9990 100644 --- a/src/librustdoc/html/static/themes/ayu.css +++ b/src/librustdoc/html/static/themes/ayu.css @@ -252,7 +252,8 @@ details.rustdoc-toggle > summary::before { color: #929292; } -.module-item .stab { +.module-item .stab, +.import-item .stab { color: #000; } diff --git a/src/librustdoc/html/static/themes/dark.css b/src/librustdoc/html/static/themes/dark.css index 2ce6cf4cc45ca..31ec87d741562 100644 --- a/src/librustdoc/html/static/themes/dark.css +++ b/src/librustdoc/html/static/themes/dark.css @@ -217,7 +217,8 @@ details.rustdoc-toggle > summary::before { box-shadow: 0 0 8px 4px #078dd8; } -.module-item .stab { +.module-item .stab, +.import-item .stab { color: #ddd; } diff --git a/src/librustdoc/html/static/themes/light.css b/src/librustdoc/html/static/themes/light.css index 31b3562cfcb06..e3e19d2ca0139 100644 --- a/src/librustdoc/html/static/themes/light.css +++ b/src/librustdoc/html/static/themes/light.css @@ -215,7 +215,8 @@ details.rustdoc-toggle > summary::before { box-shadow: 0 0 8px #078dd8; } -.module-item .stab { +.module-item .stab, +.import-item .stab { color: #000; } From fca088ae23cd2f3ea261e1d0c04e799a2918bb6f Mon Sep 17 00:00:00 2001 From: Tor Hovland Date: Wed, 14 Apr 2021 20:49:08 +0200 Subject: [PATCH 4/7] Now also displays portability tags. --- src/librustdoc/clean/inline.rs | 1 + src/librustdoc/clean/types.rs | 1 + src/librustdoc/clean/utils.rs | 8 +++---- src/librustdoc/html/render/print_item.rs | 11 ++++++--- src/test/rustdoc/issue-83832.rs | 29 ++++++++++++++++++------ 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 277ec91f15ed7..598d6da390a3b 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -477,6 +477,7 @@ fn build_module( }], }, did: None, + attrs: None, }, true, )), diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 2b25c6a26bcc4..75c541a50ed59 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -2081,6 +2081,7 @@ crate enum ImportKind { crate struct ImportSource { crate path: Path, crate did: Option, + crate attrs: Option, } #[derive(Clone, Debug)] diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index c2a971d637513..898a5b0df5db2 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -468,10 +468,10 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId { } crate fn resolve_use_source(cx: &mut DocContext<'_>, path: Path) -> ImportSource { - ImportSource { - did: if path.res.opt_def_id().is_none() { None } else { Some(register_res(cx, path.res)) }, - path, - } + let did = if path.res.opt_def_id().is_none() { None } else { Some(register_res(cx, path.res)) }; + let attrs = did.map(|did| cx.tcx.get_attrs(did).clean(cx)); + + ImportSource { did, path, attrs } } crate fn enter_impl_trait(cx: &mut DocContext<'_>, f: F) -> R diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index c37a21d1adef3..045ff5b4b897b 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -282,9 +282,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl } clean::ImportItem(ref import) => { - let (stab, stab_tags) = if let Some(def_id) = import.source.did { - // Just need an item with the correct def_id - let import_item = clean::Item { def_id, ..myitem.clone() }; + let (stab, stab_tags) = if let (Some(def_id), Some(attrs)) = + (import.source.did, import.source.attrs.clone()) + { + let attrs = Box::new(attrs); + + // Just need an item with the correct def_id and attrs + let import_item = clean::Item { def_id, attrs, ..myitem.clone() }; + let stab = import_item.stability_class(cx.tcx()); let stab_tags = Some(extra_info_tags(&import_item, item, cx.tcx())); (stab, stab_tags) diff --git a/src/test/rustdoc/issue-83832.rs b/src/test/rustdoc/issue-83832.rs index 93dff7c6ce645..a00401fbe09f0 100644 --- a/src/test/rustdoc/issue-83832.rs +++ b/src/test/rustdoc/issue-83832.rs @@ -1,21 +1,36 @@ #![crate_name = "foo"] +#![feature(doc_cfg)] -pub mod io { +pub mod tag { #[deprecated(since = "0.1.8", note = "Use bar() instead")] - pub trait Reader {} - pub trait Writer {} + pub trait Deprecated {} + + #[doc(cfg(feature = "sync"))] + pub trait Portability {} + + pub trait Unstable {} } // @has foo/mod1/index.html pub mod mod1 { - // @has - '//code' 'pub use io::Reader;' + // @has - '//code' 'pub use tag::Deprecated;' // @has - '//span' 'Deprecated' - pub use io::Reader; + // @!has - '//span' 'sync' + pub use tag::Deprecated; } // @has foo/mod2/index.html pub mod mod2 { - // @has - '//code' 'pub use io::Writer;' + // @has - '//code' 'pub use tag::Portability;' + // @!has - '//span' 'Deprecated' + // @has - '//span' 'sync' + pub use tag::Portability; +} + +// @has foo/mod3/index.html +pub mod mod3 { + // @has - '//code' 'pub use tag::Unstable;' // @!has - '//span' 'Deprecated' - pub use io::Writer; + // @!has - '//span' 'sync' + pub use tag::Unstable; } From e2a77b3d46e09a263fbba0ad7b964781659e6f40 Mon Sep 17 00:00:00 2001 From: Tor Hovland Date: Wed, 14 Apr 2021 21:41:46 +0200 Subject: [PATCH 5/7] Test Deprecated, Portability, and Unstable. --- ...bility-tags-deprecated-and-portability.rs} | 18 +++++- ...stability-tags-unstable-and-portability.rs | 61 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) rename src/test/rustdoc/{issue-83832.rs => reexport-stability-tags-deprecated-and-portability.rs} (64%) create mode 100644 src/test/rustdoc/reexport-stability-tags-unstable-and-portability.rs diff --git a/src/test/rustdoc/issue-83832.rs b/src/test/rustdoc/reexport-stability-tags-deprecated-and-portability.rs similarity index 64% rename from src/test/rustdoc/issue-83832.rs rename to src/test/rustdoc/reexport-stability-tags-deprecated-and-portability.rs index a00401fbe09f0..a79d05904e31c 100644 --- a/src/test/rustdoc/issue-83832.rs +++ b/src/test/rustdoc/reexport-stability-tags-deprecated-and-portability.rs @@ -8,7 +8,11 @@ pub mod tag { #[doc(cfg(feature = "sync"))] pub trait Portability {} - pub trait Unstable {} + #[deprecated(since = "0.1.8", note = "Use bar() instead")] + #[doc(cfg(feature = "sync"))] + pub trait Both {} + + pub trait None {} } // @has foo/mod1/index.html @@ -29,8 +33,16 @@ pub mod mod2 { // @has foo/mod3/index.html pub mod mod3 { - // @has - '//code' 'pub use tag::Unstable;' + // @has - '//code' 'pub use tag::Both;' + // @has - '//span' 'Deprecated' + // @has - '//span' 'sync' + pub use tag::Both; +} + +// @has foo/mod4/index.html +pub mod mod4 { + // @has - '//code' 'pub use tag::None;' // @!has - '//span' 'Deprecated' // @!has - '//span' 'sync' - pub use tag::Unstable; + pub use tag::None; } diff --git a/src/test/rustdoc/reexport-stability-tags-unstable-and-portability.rs b/src/test/rustdoc/reexport-stability-tags-unstable-and-portability.rs new file mode 100644 index 0000000000000..ff8a910f59f98 --- /dev/null +++ b/src/test/rustdoc/reexport-stability-tags-unstable-and-portability.rs @@ -0,0 +1,61 @@ +#![crate_name = "foo"] +#![feature(doc_cfg)] +#![feature(staged_api)] +#![stable(feature = "rust1", since = "1.0.0")] + +#[stable(feature = "rust1", since = "1.0.0")] +pub mod tag { + #[unstable(feature = "humans", issue = "none")] + pub trait Unstable {} + + #[stable(feature = "rust1", since = "1.0.0")] + #[doc(cfg(feature = "sync"))] + pub trait Portability {} + + #[unstable(feature = "humans", issue = "none")] + #[doc(cfg(feature = "sync"))] + pub trait Both {} + + #[stable(feature = "rust1", since = "1.0.0")] + pub trait None {} +} + +// @has foo/mod1/index.html +#[stable(feature = "rust1", since = "1.0.0")] +pub mod mod1 { + // @has - '//code' 'pub use tag::Unstable;' + // @has - '//span' 'Experimental' + // @!has - '//span' 'sync' + #[stable(feature = "rust1", since = "1.0.0")] + pub use tag::Unstable; +} + +// @has foo/mod2/index.html +#[stable(feature = "rust1", since = "1.0.0")] +pub mod mod2 { + // @has - '//code' 'pub use tag::Portability;' + // @!has - '//span' 'Experimental' + // @has - '//span' 'sync' + #[stable(feature = "rust1", since = "1.0.0")] + pub use tag::Portability; +} + +// @has foo/mod3/index.html +#[stable(feature = "rust1", since = "1.0.0")] +pub mod mod3 { + // @has - '//code' 'pub use tag::Both;' + // @has - '//span' 'Experimental' + // @has - '//span' 'sync' + #[stable(feature = "rust1", since = "1.0.0")] + pub use tag::Both; +} + +// @has foo/mod4/index.html +#[stable(feature = "rust1", since = "1.0.0")] +pub mod mod4 { + // @has - '//code' 'pub use tag::None;' + // @!has - '//span' 'Experimental' + // @!has - '//span' 'sync' + #[stable(feature = "rust1", since = "1.0.0")] + pub use tag::None; +} From c975fe25e4b5756b03033e4819a5c5044ca353f2 Mon Sep 17 00:00:00 2001 From: Tor Hovland Date: Sun, 18 Apr 2021 17:30:04 +0200 Subject: [PATCH 6/7] Put attrs in a Box for memory efficiency. --- src/librustdoc/clean/types.rs | 2 +- src/librustdoc/clean/utils.rs | 2 +- src/librustdoc/html/render/print_item.rs | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 75c541a50ed59..dc8784df9311c 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -2081,7 +2081,7 @@ crate enum ImportKind { crate struct ImportSource { crate path: Path, crate did: Option, - crate attrs: Option, + crate attrs: Option>, } #[derive(Clone, Debug)] diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 898a5b0df5db2..97dad8924b61a 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -469,7 +469,7 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId { crate fn resolve_use_source(cx: &mut DocContext<'_>, path: Path) -> ImportSource { let did = if path.res.opt_def_id().is_none() { None } else { Some(register_res(cx, path.res)) }; - let attrs = did.map(|did| cx.tcx.get_attrs(did).clean(cx)); + let attrs = did.map(|did| Box::new(cx.tcx.get_attrs(did).clean(cx))); ImportSource { did, path, attrs } } diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 045ff5b4b897b..d941095e5e17f 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -285,8 +285,6 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl let (stab, stab_tags) = if let (Some(def_id), Some(attrs)) = (import.source.did, import.source.attrs.clone()) { - let attrs = Box::new(attrs); - // Just need an item with the correct def_id and attrs let import_item = clean::Item { def_id, attrs, ..myitem.clone() }; From 64a68ae69adf508c73e642ff8739d3e547878467 Mon Sep 17 00:00:00 2001 From: Tor Hovland Date: Mon, 19 Apr 2021 20:39:04 +0200 Subject: [PATCH 7/7] No longer need to add attrs to ImportSource. --- src/librustdoc/clean/inline.rs | 1 - src/librustdoc/clean/types.rs | 1 - src/librustdoc/clean/utils.rs | 8 ++++---- src/librustdoc/html/render/print_item.rs | 17 +++++++++++++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 598d6da390a3b..277ec91f15ed7 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -477,7 +477,6 @@ fn build_module( }], }, did: None, - attrs: None, }, true, )), diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index dc8784df9311c..2b25c6a26bcc4 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -2081,7 +2081,6 @@ crate enum ImportKind { crate struct ImportSource { crate path: Path, crate did: Option, - crate attrs: Option>, } #[derive(Clone, Debug)] diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 97dad8924b61a..c2a971d637513 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -468,10 +468,10 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId { } crate fn resolve_use_source(cx: &mut DocContext<'_>, path: Path) -> ImportSource { - let did = if path.res.opt_def_id().is_none() { None } else { Some(register_res(cx, path.res)) }; - let attrs = did.map(|did| Box::new(cx.tcx.get_attrs(did).clean(cx))); - - ImportSource { did, path, attrs } + ImportSource { + did: if path.res.opt_def_id().is_none() { None } else { Some(register_res(cx, path.res)) }, + path, + } } crate fn enter_impl_trait(cx: &mut DocContext<'_>, f: F) -> R diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index d941095e5e17f..a303ca956d8fe 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -4,6 +4,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; use rustc_hir::def::CtorKind; use rustc_hir::def_id::DefId; +use rustc_middle::dep_graph::DepContext; use rustc_middle::middle::stability; use rustc_middle::ty::TyCtxt; use rustc_span::hygiene::MacroKind; @@ -282,11 +283,19 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl } clean::ImportItem(ref import) => { - let (stab, stab_tags) = if let (Some(def_id), Some(attrs)) = - (import.source.did, import.source.attrs.clone()) - { + let (stab, stab_tags) = if let Some(import_def_id) = import.source.did { + let import_attrs = Box::new(clean::Attributes::from_ast( + cx.tcx().sess().diagnostic(), + cx.tcx().get_attrs(import_def_id), + None, + )); + // Just need an item with the correct def_id and attrs - let import_item = clean::Item { def_id, attrs, ..myitem.clone() }; + let import_item = clean::Item { + def_id: import_def_id, + attrs: import_attrs, + ..myitem.clone() + }; let stab = import_item.stability_class(cx.tcx()); let stab_tags = Some(extra_info_tags(&import_item, item, cx.tcx()));