-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
rustdoc: properly support macros with multiple kinds #152449
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
Changes from all commits
3b053db
0299472
4f10038
72abf90
1ebef52
f2e8de7
afcada6
9b2fecb
28cfe80
d0ff143
daec3d8
1cbb922
794301a
376e0c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; | |
| use rustc_data_structures::thin_vec::ThinVec; | ||
| use rustc_hir as hir; | ||
| use rustc_hir::attrs::{AttributeKind, DeprecatedSince, Deprecation, DocAttribute}; | ||
| use rustc_hir::def::{CtorKind, DefKind, Res}; | ||
| use rustc_hir::def::{CtorKind, DefKind, MacroKinds, Res}; | ||
| use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId}; | ||
| use rustc_hir::lang_items::LangItem; | ||
| use rustc_hir::{Attribute, BodyId, ConstStability, Mutability, Stability, StableSince, find_attr}; | ||
|
|
@@ -758,11 +758,33 @@ impl Item { | |
| find_attr!(&self.attrs.other_attrs, NonExhaustive(..)) | ||
| } | ||
|
|
||
| /// Returns a documentation-level item type from the item. | ||
| /// Returns a documentation-level item type from the item. In case of a `macro_rules!` which | ||
| /// contains an attr/derive kind, it will always return `ItemType::Macro`. If you want all | ||
| /// kinds, you need to use [`Item::types`]. | ||
| pub(crate) fn type_(&self) -> ItemType { | ||
| ItemType::from(self) | ||
| } | ||
|
|
||
| /// Returns an item types. There is only one case where it can return more than one kind: | ||
| /// for `macro_rules!` items which contain an attr/derive kind. | ||
| pub(crate) fn types(&self) -> impl Iterator<Item = ItemType> { | ||
| if let ItemKind::MacroItem(_, macro_kinds) = self.kind { | ||
| Either::Right(macro_kinds.iter().map(|kind| match kind { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey using
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I know, the order never matters since it's the same item with potentially different types. |
||
| MacroKinds::ATTR => ItemType::DeclMacroAttribute, | ||
| MacroKinds::DERIVE => ItemType::DeclMacroDerive, | ||
| MacroKinds::BANG => ItemType::Macro, | ||
| _ => panic!("unsupported macro kind {kind:?}"), | ||
| })) | ||
| } else { | ||
| Either::Left(std::iter::once(self.type_())) | ||
| } | ||
| } | ||
|
GuillaumeGomez marked this conversation as resolved.
|
||
|
|
||
| /// Returns true if this a macro declared with the `macro` keyword or with `macro_rules!. | ||
| pub(crate) fn is_decl_macro(&self) -> bool { | ||
| matches!(self.kind, ItemKind::MacroItem(..)) | ||
| } | ||
|
|
||
| pub(crate) fn defaultness(&self) -> Option<Defaultness> { | ||
| match self.kind { | ||
| ItemKind::MethodItem(_, defaultness) | ItemKind::RequiredMethodItem(_, defaultness) => { | ||
|
|
@@ -772,6 +794,11 @@ impl Item { | |
| } | ||
| } | ||
|
|
||
| /// Generates the HTML file name based on the item kind. | ||
| pub(crate) fn html_filename(&self) -> String { | ||
| format!("{type_}.{name}.html", type_ = self.type_(), name = self.name.unwrap()) | ||
|
lolbinarycat marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /// Returns a `FnHeader` if `self` is a function item, otherwise returns `None`. | ||
| pub(crate) fn fn_header(&self, tcx: TyCtxt<'_>) -> Option<hir::FnHeader> { | ||
| fn build_fn_header( | ||
|
|
@@ -931,7 +958,13 @@ pub(crate) enum ItemKind { | |
| ForeignStaticItem(Static, hir::Safety), | ||
| /// `type`s from an extern block | ||
| ForeignTypeItem, | ||
| MacroItem(Macro), | ||
| /// A macro defined with `macro_rules` or the `macro` keyword. It can be multiple things (macro, | ||
| /// derive and attribute, potentially multiple at once). Don't forget to look into the | ||
| ///`MacroKinds` values. | ||
| /// | ||
| /// If a `macro_rules!` only contains a `attr`/`derive` branch, then it's not stored in this | ||
| /// variant but in the `ProcMacroItem` variant. | ||
| MacroItem(Macro, MacroKinds), | ||
| ProcMacroItem(ProcMacro), | ||
| PrimitiveItem(PrimitiveType), | ||
| /// A required associated constant in a trait declaration. | ||
|
|
@@ -986,7 +1019,7 @@ impl ItemKind { | |
| | ForeignFunctionItem(_, _) | ||
| | ForeignStaticItem(_, _) | ||
| | ForeignTypeItem | ||
| | MacroItem(_) | ||
| | MacroItem(..) | ||
| | ProcMacroItem(_) | ||
| | PrimitiveItem(_) | ||
| | RequiredAssocConstItem(..) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.