Skip to content

Commit 36138dc

Browse files
Fix doc_cfg feature for extern items
1 parent 0424cc1 commit 36138dc

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/librustdoc/clean/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,14 +3260,22 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
32603260
hir::ForeignItemKind::Type => ForeignTypeItem,
32613261
};
32623262

3263-
generate_item_with_correct_attrs(
3263+
let mut clean_item = generate_item_with_correct_attrs(
32643264
cx,
32653265
kind,
32663266
item.owner_id.def_id.to_def_id(),
32673267
item.ident.name,
32683268
import_id.as_slice(),
32693269
renamed,
3270-
)
3270+
);
3271+
// We also need to take into account the `extern` block (doc_)cfg attributes.
3272+
let mut attrs = Attributes::from_hir(inline::load_attrs(
3273+
cx.tcx,
3274+
cx.tcx.hir_owner_parent(item.owner_id).owner.to_def_id(),
3275+
));
3276+
attrs.merge_with(std::mem::take(&mut clean_item.inner.attrs));
3277+
clean_item.inner.attrs = attrs;
3278+
clean_item
32713279
})
32723280
}
32733281

src/librustdoc/clean/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,12 @@ impl Attributes {
10931093
}
10941094
aliases.into_iter().collect::<Vec<_>>().into()
10951095
}
1096+
1097+
pub(crate) fn merge_with(&mut self, other: Self) {
1098+
let Self { doc_strings, other_attrs } = other;
1099+
self.doc_strings.extend(doc_strings);
1100+
self.other_attrs.extend(other_attrs);
1101+
}
10961102
}
10971103

10981104
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Ensure that the `cfg` on the extern blocks are correctly taken into account by
2+
// their children.
3+
// Regression test for <https://github.com/rust-lang/rust/issues/150268>.
4+
5+
#![feature(doc_cfg)]
6+
#![crate_name = "foo"]
7+
8+
//@has 'foo/index.html'
9+
//@count - '//*[@class="stab portability"]' 2
10+
//@has - '//*[@class="stab portability"]' 'Non-banana'
11+
12+
//@has 'foo/fn.doc_cfg_doesnt_work.html'
13+
//@has - '//*[@class="stab portability"]' 'Available on non-crate feature banana only.'
14+
15+
//@has 'foo/fn.doc_cfg_works.html'
16+
//@has - '//*[@class="stab portability"]' 'Available on non-crate feature banana only.'
17+
18+
unsafe extern "C" {
19+
#[cfg(not(feature = "banana"))]
20+
pub fn doc_cfg_works();
21+
}
22+
23+
#[cfg(not(feature = "banana"))]
24+
unsafe extern "C" {
25+
pub fn doc_cfg_doesnt_work();
26+
}

0 commit comments

Comments
 (0)