Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve lang item generated docs #82641

Merged
merged 8 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_data_structures/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ macro_rules! static_assert_size {
#[macro_export]
macro_rules! enum_from_u32 {
($(#[$attr:meta])* pub enum $name:ident {
$($variant:ident = $e:expr,)*
$($(#[$var_attr:meta])* $variant:ident = $e:expr,)*
}) => {
$(#[$attr])*
pub enum $name {
$($variant = $e),*
$($(#[$var_attr])* $variant = $e),*
}

impl $name {
Expand All @@ -26,11 +26,11 @@ macro_rules! enum_from_u32 {
}
};
($(#[$attr:meta])* pub enum $name:ident {
$($variant:ident,)*
$($(#[$var_attr:meta])* $variant:ident,)*
}) => {
$(#[$attr])*
pub enum $name {
$($variant,)*
$($(#[$var_attr])* $variant,)*
}

impl $name {
Expand Down
46 changes: 27 additions & 19 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,34 @@ macro_rules! expand_group {
// So you probably just want to nip down to the end.
macro_rules! language_item_table {
(
$( $variant:ident $($group:expr)?, $name:expr, $method:ident, $target:expr; )*
$( $(#[$attr:meta])* $variant:ident $($group:expr)?, $module:ident :: $name:ident, $method:ident, $target:expr; )*
) => {

enum_from_u32! {
/// A representation of all the valid language items in Rust.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
pub enum LangItem {
$($variant,)*
$(
#[doc = concat!("The `", stringify!($name), "` lang item.")]
///
$(#[$attr])*
$variant,
)*
}
}

impl LangItem {
/// Returns the `name` symbol in `#[lang = "$name"]`.
/// For example, `LangItem::EqTraitLangItem`,
/// that is `#[lang = "eq"]` would result in `sym::eq`.
/// For example, [`LangItem::PartialEq`]`.name()`
/// would result in [`sym::eq`] since it is `#[lang = "eq"]`.
pub fn name(self) -> Symbol {
match self {
$( LangItem::$variant => $name, )*
$( LangItem::$variant => $module::$name, )*
}
}

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

/// All of the language items, defined or not.
/// Defined lang items can come from the current crate or its dependencies.
#[derive(HashStable_Generic, Debug)]
pub struct LanguageItems {
/// Mappings from lang items to their possibly found `DefId`s.
/// The index corresponds to the order in `LangItem`.
/// Mappings from lang items to their possibly found [`DefId`]s.
/// The index corresponds to the order in [`LangItem`].
pub items: Vec<Option<DefId>>,
/// Lang items that were not found during collection.
pub missing: Vec<LangItem>,
/// Mapping from `LangItemGroup` discriminants to all
/// `DefId`s of lang items in that group.
/// Mapping from [`LangItemGroup`] discriminants to all
/// [`DefId`]s of lang items in that group.
pub groups: [Vec<DefId>; NUM_GROUPS],
}

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

/// Returns the [`DefId`]s of all lang items in a group.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this include language items that aren't in the current crate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given #82641 (comment), I would assume yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, can you add that as a comment?

Copy link
Member Author

@camelid camelid Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can, but it seems redundant given that we say

Defined lang items can come from the current crate or its dependencies.

at the top of the docs. If you still think I should add a comment, could you suggest what you want it to say so we're on the same page?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, I missed that comment.

pub fn group(&self, group: LangItemGroup) -> &[DefId] {
self.groups[group as usize].as_ref()
}

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

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

Sized, sym::sized, sized_trait, Target::Trait;
Unsize, sym::unsize, unsize_trait, Target::Trait;
// Trait injected by #[derive(PartialEq)], (i.e. "Partial EQ").
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait;
// Trait injected by #[derive(Eq)], (i.e. "Total EQ"; no, I will not apologize).
/// Trait injected by `#[derive(Eq)]`, (i.e. "Total EQ"; no, I will not apologize).
StructuralTeq, sym::structural_teq, structural_teq_trait, Target::Trait;
Copy, sym::copy, copy_trait, Target::Trait;
Clone, sym::clone, clone_trait, Target::Trait;
Sync, sym::sync, sync_trait, Target::Trait;
DiscriminantKind, sym::discriminant_kind, discriminant_kind_trait, Target::Trait;
// The associated item of `trait DiscriminantKind`.
/// The associated item of the [`DiscriminantKind`] trait.
Discriminant, sym::discriminant_type, discriminant_type, Target::AssocTy;

PointeeTrait, sym::pointee_trait, pointee_trait, Target::Trait;
Expand Down Expand Up @@ -273,7 +281,7 @@ language_item_table! {
PanicInfo, sym::panic_info, panic_info, Target::Struct;
PanicLocation, sym::panic_location, panic_location, Target::Struct;
PanicImpl, sym::panic_impl, panic_impl, Target::Fn;
// libstd panic entry point. Necessary for const eval to be able to catch it
/// libstd panic entry point. Necessary for const eval to be able to catch it
BeginPanic, sym::begin_panic, begin_panic_fn, Target::Fn;

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

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

// Align offset for stride != 1; must not panic.
/// Align offset for stride != 1; must not panic.
AlignOffset, sym::align_offset, align_offset_fn, Target::Fn;

Termination, sym::termination, termination, Target::Trait;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![feature(crate_visibility_modifier)]
#![feature(const_fn)] // For the unsizing cast on `&[]`
#![feature(const_panic)]
#![feature(extended_key_value_attributes)]
#![feature(in_band_lifetimes)]
#![feature(once_cell)]
#![feature(or_patterns)]
Expand Down