Skip to content

Commit c277c34

Browse files
authored
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 dbf246c + 617d9de commit c277c34

2 files changed

Lines changed: 34 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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#![crate_name = "foo"]
6+
#![feature(doc_cfg)]
7+
8+
mod inner {
9+
#[cfg(target_os = "linux")]
10+
pub type One = u32;
11+
12+
#[doc(cfg(target_os = "linux"))]
13+
pub type Two = u32;
14+
}
15+
16+
//@ has 'foo/index.html'
17+
// There should be two items in the type aliases table.
18+
//@ count - '//*[@class="item-table"]/dt' 2
19+
// Both of them should have the portability badge in the module index.
20+
//@ count - '//*[@class="item-table"]/dt/*[@class="stab portability"]' 2
21+
22+
//@ has 'foo/type.One.html'
23+
// Check that the individual type page has the portability badge.
24+
//@ count - '//*[@id="main-content"]/*[@class="item-info"]/*[@class="stab portability"]' 1
25+
//@ has - '//*[@id="main-content"]/*[@class="item-info"]/*[@class="stab portability"]' 'Linux'
26+
27+
//@ has 'foo/type.Two.html'
28+
// Check the explicit doc(cfg) type page as well.
29+
//@ count - '//*[@id="main-content"]/*[@class="item-info"]/*[@class="stab portability"]' 1
30+
//@ has - '//*[@id="main-content"]/*[@class="item-info"]/*[@class="stab portability"]' 'Linux'
31+
32+
pub use self::inner::{One, Two};

0 commit comments

Comments
 (0)