Skip to content

Commit 7087692

Browse files
authored
Do not document reexports (#33)
1 parent 59d0c64 commit 7087692

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
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: 22 additions & 9 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
},
@@ -53,9 +53,26 @@ impl UnstableAttribute {
5353
return item.into_token_stream();
5454
}
5555

56-
let feature_flag = self.feature_flag();
5756
self.add_doc(&mut item);
5857

58+
self.expand_item_without_doc(item)
59+
}
60+
61+
pub fn expand_use(&self, item: impl ItemLike + ToTokens + Clone) -> TokenStream {
62+
// We don't want to transform `pub use` items. Adding documentation has adverse effects.
63+
// The reexported type can signal its own stability, the reexport itself can really only
64+
// use the label that rustdoc renders.
65+
if !item.is_public() {
66+
// We only care about public items.
67+
return item.into_token_stream();
68+
}
69+
70+
self.expand_item_without_doc(item)
71+
}
72+
73+
fn expand_item_without_doc(&self, item: impl ItemLike + ToTokens + Clone) -> TokenStream {
74+
let feature_flag = self.feature_flag();
75+
5976
let mut hidden_item = item.clone();
6077
hidden_item.set_visibility(parse_quote! { pub(crate) });
6178

@@ -400,16 +417,14 @@ mod tests {
400417
let item: syn::ItemUse = parse_quote! {
401418
pub use crate::foo::bar;
402419
};
403-
let tokens = UnstableAttribute::default().expand(item);
420+
let tokens = UnstableAttribute::default().expand_use(item);
404421
let expected = quote! {
405422
#[cfg(any(doc, feature = "unstable"))]
406423
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
407-
#[doc = #DEFAULT_DOC]
408424
pub use crate::foo::bar;
409425

410426
#[cfg(not(any(doc, feature = "unstable")))]
411427
#[allow(unused_imports)]
412-
#[doc = #DEFAULT_DOC]
413428
pub(crate) use crate::foo::bar;
414429
};
415430
assert_eq!(tokens.to_string(), expected.to_string());
@@ -462,20 +477,18 @@ mod tests {
462477
}
463478

464479
#[test]
465-
fn expand_public_use() {
480+
fn expand_public_use_no_docs() {
466481
let item: syn::ItemUse = parse_quote! {
467482
pub use crate::foo::bar;
468483
};
469-
let tokens = UnstableAttribute::default().expand(item);
484+
let tokens = UnstableAttribute::default().expand_use(item);
470485
let expected = quote! {
471486
#[cfg(feature = "unstable")]
472487
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
473-
#[doc = #DEFAULT_DOC]
474488
pub use crate::foo::bar;
475489

476490
#[cfg(not(feature = "unstable"))]
477491
#[allow(unused_imports)]
478-
#[doc = #DEFAULT_DOC]
479492
pub(crate) use crate::foo::bar;
480493
};
481494
assert_eq!(tokens.to_string(), expected.to_string());

0 commit comments

Comments
 (0)