Skip to content

Commit 617d9de

Browse files
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.
1 parent ad4b935 commit 617d9de

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
@@ -205,7 +205,8 @@ fn generate_item_with_correct_attrs(
205205
attrs.extend(get_all_import_attributes(cx, import_id, def_id, is_inline));
206206
is_inline = is_inline || import_is_inline;
207207
}
208-
add_without_unwanted_attributes(&mut attrs, target_attrs, is_inline, None);
208+
let keep_target_cfg = is_inline || matches!(kind, ItemKind::TypeAliasItem(..));
209+
add_without_unwanted_attributes(&mut attrs, target_attrs, keep_target_cfg, None);
209210
attrs
210211
} else {
211212
// 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)