Skip to content

Commit f400364

Browse files
committed
Do not document reexports
1 parent 8b311cd commit f400364

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ pub fn unstable(args: TokenStream, input: TokenStream) -> TokenStream {
198198
/// This attribute does not change the visibility of the annotated item. You should ensure that the
199199
/// item's visibility is set to `pub` if you want it to be part of your crate's public API.
200200
///
201+
/// Re-exports (`pub use`) do not modify the re-exported item's stability or documentation, and they have
202+
/// no way to display the `since` and `issue` information.
203+
///
201204
/// # See also
202205
///
203206
/// - The [`unstable`] attribute for marking an API as unstable.

src/stable.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn stable_macro(args: TokenStream, input: TokenStream) -> TokenStream {
2525
Item::Trait(item_trait) => unstable_attribute.expand(item_trait),
2626
Item::Const(item_const) => unstable_attribute.expand(item_const),
2727
Item::Static(item_static) => unstable_attribute.expand(item_static),
28-
Item::Use(item_use) => unstable_attribute.expand(item_use),
28+
Item::Use(item_use) => unstable_attribute.expand_use(item_use),
2929
Item::Impl(item_impl) => unstable_attribute.expand_impl(item_impl),
3030
_ => panic!("unsupported item type"),
3131
},
@@ -51,6 +51,13 @@ impl StableAttribute {
5151
self.expand_impl(item)
5252
}
5353

54+
pub fn expand_use(&self, item: impl ItemLike + ToTokens + Clone) -> TokenStream {
55+
// We don't want to transform `pub use` items. Adding documentation has adverse effects.
56+
// The reexported type can signal its own stability, the reexport itself can really only
57+
// use the label that rustdoc renders.
58+
item.into_token_stream()
59+
}
60+
5461
pub fn expand_impl(&self, mut item: impl Stability + ToTokens) -> TokenStream {
5562
let doc = if let Some(ref version) = self.since {
5663
formatdoc! {"
@@ -273,14 +280,13 @@ mod tests {
273280
}
274281

275282
#[test]
276-
fn expand_public_use() {
283+
fn public_use_is_noop() {
277284
let item: syn::ItemUse = parse_quote! {
278285
pub use crate::foo::bar;
279286
};
280287
let stable = StableAttribute::default();
281-
let tokens = stable.expand(item);
288+
let tokens = stable.expand_use(item);
282289
let expected = quote! {
283-
#[doc = #STABLE_DOC]
284290
pub use crate::foo::bar;
285291
};
286292
assert_eq!(tokens.to_string(), expected.to_string());

src/unstable.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn unstable_macro(args: TokenStream, input: TokenStream) -> TokenStream {
2525
Item::Trait(item_trait) => unstable_attribute.expand(item_trait),
2626
Item::Const(item_const) => unstable_attribute.expand(item_const),
2727
Item::Static(item_static) => unstable_attribute.expand(item_static),
28-
Item::Use(item_use) => unstable_attribute.expand(item_use),
28+
Item::Use(item_use) => unstable_attribute.expand_use(item_use),
2929
Item::Impl(item_impl) => unstable_attribute.expand_impl(item_impl),
3030
_ => panic!("unsupported item type"),
3131
},
@@ -87,6 +87,25 @@ impl UnstableAttribute {
8787
}
8888
}
8989

90+
pub fn expand_use(&self, item: impl ItemLike + ToTokens + Clone) -> TokenStream {
91+
// We don't want to transform `pub use` items. Adding documentation has adverse effects.
92+
// The reexported type can signal its own stability, the reexport itself can really only
93+
// use the label that rustdoc renders.
94+
let feature_flag = self.feature_flag();
95+
96+
#[cfg(not(instability_disable_unstable_docs))]
97+
let cfg_gate_unstable = quote! { #[cfg(any(doc, feature = #feature_flag))] };
98+
99+
#[cfg(instability_disable_unstable_docs)]
100+
let cfg_gate_unstable = quote! { #[cfg(feature = #feature_flag)] };
101+
102+
quote! {
103+
#cfg_gate_unstable
104+
#[cfg_attr(docsrs, doc(cfg(feature = #feature_flag)))]
105+
#item
106+
}
107+
}
108+
90109
pub fn expand_impl(&self, mut item: impl Stability + ToTokens) -> TokenStream {
91110
let feature_flag = self.feature_flag();
92111
self.add_doc(&mut item);
@@ -400,16 +419,14 @@ mod tests {
400419
let item: syn::ItemUse = parse_quote! {
401420
pub use crate::foo::bar;
402421
};
403-
let tokens = UnstableAttribute::default().expand(item);
422+
let tokens = UnstableAttribute::default().expand_use(item);
404423
let expected = quote! {
405424
#[cfg(any(doc, feature = "unstable"))]
406425
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
407-
#[doc = #DEFAULT_DOC]
408426
pub use crate::foo::bar;
409427

410428
#[cfg(not(any(doc, feature = "unstable")))]
411429
#[allow(unused_imports)]
412-
#[doc = #DEFAULT_DOC]
413430
pub(crate) use crate::foo::bar;
414431
};
415432
assert_eq!(tokens.to_string(), expected.to_string());
@@ -462,20 +479,18 @@ mod tests {
462479
}
463480

464481
#[test]
465-
fn expand_public_use() {
482+
fn expand_public_use_no_docs() {
466483
let item: syn::ItemUse = parse_quote! {
467484
pub use crate::foo::bar;
468485
};
469-
let tokens = UnstableAttribute::default().expand(item);
486+
let tokens = UnstableAttribute::default().expand_use(item);
470487
let expected = quote! {
471488
#[cfg(feature = "unstable")]
472489
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
473-
#[doc = #DEFAULT_DOC]
474490
pub use crate::foo::bar;
475491

476492
#[cfg(not(feature = "unstable"))]
477493
#[allow(unused_imports)]
478-
#[doc = #DEFAULT_DOC]
479494
pub(crate) use crate::foo::bar;
480495
};
481496
assert_eq!(tokens.to_string(), expected.to_string());

0 commit comments

Comments
 (0)