Skip to content

Commit 3c52d85

Browse files
Rollup merge of rust-lang#154970 - shivendra02467:fix-rustdoc-154921, r=GuillaumeGomez
rustdoc: preserve `doc(cfg)` on locally re-exported type aliases When a type alias is locally re-exported from a private module (an implicit inline), rustdoc drops its `cfg` attributes because it treats it like a standard un-inlined re-export. Since type aliases have no inner fields to carry the `cfg` badge (unlike structs or enums), the portability info is lost entirely. This patch explicitly preserves the target's `cfg` metadata when the generated item is a `TypeAliasItem`, ensuring the portability badge renders correctly without breaking standard cross-crate re-export behavior. Fixes rust-lang#154921
2 parents b6c8b5c + 52ad8c0 commit 3c52d85

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ fn generate_item_with_correct_attrs(
235235
attrs.extend(get_all_import_attributes(cx, import_id, def_id, is_inline));
236236
is_inline = is_inline || import_is_inline;
237237
}
238-
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
238+
let keep_target_cfg = is_inline || matches!(kind, ItemKind::TypeAliasItem(..));
239+
add_without_unwanted_attributes(&mut attrs, target_attrs, keep_target_cfg, None);
239240
attrs
240241
} else {
241242
// We only keep the item's attributes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/154921>.
2+
// This test ensures that auto-generated and explicit `doc(cfg)` attributes are correctly
3+
// preserved for locally re-exported type aliases.
4+
5+
//@ compile-flags: --cfg feature="foo"
6+
7+
#![crate_name = "foo"]
8+
#![feature(doc_cfg)]
9+
10+
mod inner {
11+
#[cfg(feature = "foo")]
12+
pub type One = u32;
13+
14+
#[doc(cfg(feature = "foo"))]
15+
pub type Two = u32;
16+
}
17+
18+
//@ has 'foo/index.html'
19+
// There should be two items in the type aliases table.
20+
//@ count - '//*[@class="item-table"]/dt' 2
21+
// Both of them should have the portability badge in the module index.
22+
//@ count - '//*[@class="item-table"]/dt/*[@class="stab portability"]' 2
23+
24+
//@ has 'foo/type.One.html'
25+
// Check that the individual type page has the portability badge.
26+
//@ count - '//*[@id="main-content"]/*[@class="item-info"]/*[@class="stab portability"]' 1
27+
//@ has - '//*[@id="main-content"]/*[@class="item-info"]/*[@class="stab portability"]' 'foo'
28+
29+
//@ has 'foo/type.Two.html'
30+
// Check the explicit doc(cfg) type page as well.
31+
//@ count - '//*[@id="main-content"]/*[@class="item-info"]/*[@class="stab portability"]' 1
32+
//@ has - '//*[@id="main-content"]/*[@class="item-info"]/*[@class="stab portability"]' 'foo'
33+
34+
pub use self::inner::{One, Two};

0 commit comments

Comments
 (0)