Skip to content

Commit 04fce73

Browse files
committed
Auto merge of #82641 - camelid:lang-item-docs, r=jyn514
Improve lang item generated docs cc https://rust-lang.zulipchat.com/#narrow/stream/146229-wg-secure-code/topic/Is.20.60core.60.20part.20of.20the.20compiler.3F/near/226738260 r? `@jyn514`
2 parents b3ac526 + ab42f96 commit 04fce73

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

compiler/rustc_data_structures/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ macro_rules! static_assert_size {
99
#[macro_export]
1010
macro_rules! enum_from_u32 {
1111
($(#[$attr:meta])* pub enum $name:ident {
12-
$($variant:ident = $e:expr,)*
12+
$($(#[$var_attr:meta])* $variant:ident = $e:expr,)*
1313
}) => {
1414
$(#[$attr])*
1515
pub enum $name {
16-
$($variant = $e),*
16+
$($(#[$var_attr])* $variant = $e),*
1717
}
1818

1919
impl $name {
@@ -26,11 +26,11 @@ macro_rules! enum_from_u32 {
2626
}
2727
};
2828
($(#[$attr:meta])* pub enum $name:ident {
29-
$($variant:ident,)*
29+
$($(#[$var_attr:meta])* $variant:ident,)*
3030
}) => {
3131
$(#[$attr])*
3232
pub enum $name {
33-
$($variant,)*
33+
$($(#[$var_attr])* $variant,)*
3434
}
3535

3636
impl $name {

compiler/rustc_hir/src/lang_items.rs

+27-19
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,34 @@ macro_rules! expand_group {
3838
// So you probably just want to nip down to the end.
3939
macro_rules! language_item_table {
4040
(
41-
$( $variant:ident $($group:expr)?, $name:expr, $method:ident, $target:expr; )*
41+
$( $(#[$attr:meta])* $variant:ident $($group:expr)?, $module:ident :: $name:ident, $method:ident, $target:expr; )*
4242
) => {
4343

4444
enum_from_u32! {
4545
/// A representation of all the valid language items in Rust.
4646
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
4747
pub enum LangItem {
48-
$($variant,)*
48+
$(
49+
#[doc = concat!("The `", stringify!($name), "` lang item.")]
50+
///
51+
$(#[$attr])*
52+
$variant,
53+
)*
4954
}
5055
}
5156

5257
impl LangItem {
5358
/// Returns the `name` symbol in `#[lang = "$name"]`.
54-
/// For example, `LangItem::EqTraitLangItem`,
55-
/// that is `#[lang = "eq"]` would result in `sym::eq`.
59+
/// For example, [`LangItem::PartialEq`]`.name()`
60+
/// would result in [`sym::eq`] since it is `#[lang = "eq"]`.
5661
pub fn name(self) -> Symbol {
5762
match self {
58-
$( LangItem::$variant => $name, )*
63+
$( LangItem::$variant => $module::$name, )*
5964
}
6065
}
6166

67+
/// The [group](LangItemGroup) that this lang item belongs to,
68+
/// or `None` if it doesn't belong to a group.
6269
pub fn group(self) -> Option<LangItemGroup> {
6370
use LangItemGroup::*;
6471
match self {
@@ -67,15 +74,17 @@ macro_rules! language_item_table {
6774
}
6875
}
6976

77+
/// All of the language items, defined or not.
78+
/// Defined lang items can come from the current crate or its dependencies.
7079
#[derive(HashStable_Generic, Debug)]
7180
pub struct LanguageItems {
72-
/// Mappings from lang items to their possibly found `DefId`s.
73-
/// The index corresponds to the order in `LangItem`.
81+
/// Mappings from lang items to their possibly found [`DefId`]s.
82+
/// The index corresponds to the order in [`LangItem`].
7483
pub items: Vec<Option<DefId>>,
7584
/// Lang items that were not found during collection.
7685
pub missing: Vec<LangItem>,
77-
/// Mapping from `LangItemGroup` discriminants to all
78-
/// `DefId`s of lang items in that group.
86+
/// Mapping from [`LangItemGroup`] discriminants to all
87+
/// [`DefId`]s of lang items in that group.
7988
pub groups: [Vec<DefId>; NUM_GROUPS],
8089
}
8190

@@ -103,14 +112,13 @@ macro_rules! language_item_table {
103112
self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
104113
}
105114

115+
/// Returns the [`DefId`]s of all lang items in a group.
106116
pub fn group(&self, group: LangItemGroup) -> &[DefId] {
107117
self.groups[group as usize].as_ref()
108118
}
109119

110120
$(
111-
/// Returns the corresponding `DefId` for the lang item if it
112-
/// exists.
113-
#[allow(dead_code)]
121+
#[doc = concat!("Returns the [`DefId`] of the `", stringify!($name), "` lang item if it is defined.")]
114122
pub fn $method(&self) -> Option<DefId> {
115123
self.items[LangItem::$variant as usize]
116124
}
@@ -120,7 +128,7 @@ macro_rules! language_item_table {
120128
/// A mapping from the name of the lang item to its order and the form it must be of.
121129
pub static ITEM_REFS: SyncLazy<FxHashMap<Symbol, (usize, Target)>> = SyncLazy::new(|| {
122130
let mut item_refs = FxHashMap::default();
123-
$( item_refs.insert($name, (LangItem::$variant as usize, $target)); )*
131+
$( item_refs.insert($module::$name, (LangItem::$variant as usize, $target)); )*
124132
item_refs
125133
});
126134

@@ -140,7 +148,7 @@ impl<CTX> HashStable<CTX> for LangItem {
140148
///
141149
/// About the `check_name` argument: passing in a `Session` would be simpler,
142150
/// because then we could call `Session::check_name` directly. But we want to
143-
/// avoid the need for `librustc_hir` to depend on `librustc_session`, so we
151+
/// avoid the need for `rustc_hir` to depend on `rustc_session`, so we
144152
/// use a closure instead.
145153
pub fn extract<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option<(Symbol, Span)>
146154
where
@@ -190,15 +198,15 @@ language_item_table! {
190198

191199
Sized, sym::sized, sized_trait, Target::Trait;
192200
Unsize, sym::unsize, unsize_trait, Target::Trait;
193-
// Trait injected by #[derive(PartialEq)], (i.e. "Partial EQ").
201+
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
194202
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait;
195-
// Trait injected by #[derive(Eq)], (i.e. "Total EQ"; no, I will not apologize).
203+
/// Trait injected by `#[derive(Eq)]`, (i.e. "Total EQ"; no, I will not apologize).
196204
StructuralTeq, sym::structural_teq, structural_teq_trait, Target::Trait;
197205
Copy, sym::copy, copy_trait, Target::Trait;
198206
Clone, sym::clone, clone_trait, Target::Trait;
199207
Sync, sym::sync, sync_trait, Target::Trait;
200208
DiscriminantKind, sym::discriminant_kind, discriminant_kind_trait, Target::Trait;
201-
// The associated item of `trait DiscriminantKind`.
209+
/// The associated item of the [`DiscriminantKind`] trait.
202210
Discriminant, sym::discriminant_type, discriminant_type, Target::AssocTy;
203211

204212
PointeeTrait, sym::pointee_trait, pointee_trait, Target::Trait;
@@ -273,7 +281,7 @@ language_item_table! {
273281
PanicInfo, sym::panic_info, panic_info, Target::Struct;
274282
PanicLocation, sym::panic_location, panic_location, Target::Struct;
275283
PanicImpl, sym::panic_impl, panic_impl, Target::Fn;
276-
// libstd panic entry point. Necessary for const eval to be able to catch it
284+
/// libstd panic entry point. Necessary for const eval to be able to catch it
277285
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn;
278286

279287
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn;
@@ -295,7 +303,7 @@ language_item_table! {
295303

296304
MaybeUninit, sym::maybe_uninit, maybe_uninit, Target::Union;
297305

298-
// Align offset for stride != 1; must not panic.
306+
/// Align offset for stride != 1; must not panic.
299307
AlignOffset, sym::align_offset, align_offset_fn, Target::Fn;
300308

301309
Termination, sym::termination, termination, Target::Trait;

compiler/rustc_hir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(crate_visibility_modifier)]
66
#![feature(const_fn)] // For the unsizing cast on `&[]`
77
#![feature(const_panic)]
8+
#![feature(extended_key_value_attributes)]
89
#![feature(in_band_lifetimes)]
910
#![feature(once_cell)]
1011
#![feature(or_patterns)]

0 commit comments

Comments
 (0)