Move ast::Item::ident
into ast::ItemKind
.
`ast::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, `Trait`, `TraitAlias`, `MacroDef`, `Delegation`. - It's always empty for these item kinds: `Use`, `ForeignMod`, `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`. There is a similar story for `AssocItemKind` and `ForeignItemKind`. Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out. The commit is large but it's mostly obvious plumbing work. Some notable things. - `ast::Item` got 8 bytes bigger. This could be avoided by boxing the fields within some of the `ast::ItemKind` variants (specifically: `Struct`, `Union`, `Enum`). I might do that in a follow-up; this commit is big enough already. - For the visitors: `FnKind` no longer needs an `ident` field because the `Fn` within how has one. - In the parser, the `ItemInfo` typedef is no longer needed. It was used in various places to return an `Ident` alongside an `ItemKind`, but now the `Ident` (if present) is within the `ItemKind`. - In a few places I renamed identifier variables called `name` (or `foo_name`) as `ident` (or `foo_ident`), to better match the type, and because `name` is normally used for `Symbol`s. It's confusing to see something like `foo_name.name`.
This commit is contained in:
parent
43018eacb6
commit
df247968f2
54 changed files with 1072 additions and 864 deletions
|
@ -3303,9 +3303,6 @@ pub struct Item<K = ItemKind> {
|
|||
pub id: NodeId,
|
||||
pub span: Span,
|
||||
pub vis: Visibility,
|
||||
/// The name of the item.
|
||||
/// It might be a dummy name in case of anonymous items.
|
||||
pub ident: Ident,
|
||||
|
||||
pub kind: K,
|
||||
|
||||
|
@ -3327,23 +3324,23 @@ impl Item {
|
|||
|
||||
pub fn opt_generics(&self) -> Option<&Generics> {
|
||||
match &self.kind {
|
||||
ItemKind::ExternCrate(_)
|
||||
ItemKind::ExternCrate(..)
|
||||
| ItemKind::Use(_)
|
||||
| ItemKind::Mod(_, _)
|
||||
| ItemKind::Mod(..)
|
||||
| ItemKind::ForeignMod(_)
|
||||
| ItemKind::GlobalAsm(_)
|
||||
| ItemKind::MacCall(_)
|
||||
| ItemKind::Delegation(_)
|
||||
| ItemKind::DelegationMac(_)
|
||||
| ItemKind::MacroDef(_) => None,
|
||||
| ItemKind::MacroDef(..) => None,
|
||||
ItemKind::Static(_) => None,
|
||||
ItemKind::Const(i) => Some(&i.generics),
|
||||
ItemKind::Fn(i) => Some(&i.generics),
|
||||
ItemKind::TyAlias(i) => Some(&i.generics),
|
||||
ItemKind::TraitAlias(generics, _)
|
||||
| ItemKind::Enum(_, generics)
|
||||
| ItemKind::Struct(_, generics)
|
||||
| ItemKind::Union(_, generics) => Some(&generics),
|
||||
ItemKind::TraitAlias(_, generics, _)
|
||||
| ItemKind::Enum(_, _, generics)
|
||||
| ItemKind::Struct(_, _, generics)
|
||||
| ItemKind::Union(_, _, generics) => Some(&generics),
|
||||
ItemKind::Trait(i) => Some(&i.generics),
|
||||
ItemKind::Impl(i) => Some(&i.generics),
|
||||
}
|
||||
|
@ -3420,6 +3417,7 @@ impl Default for FnHeader {
|
|||
pub struct Trait {
|
||||
pub safety: Safety,
|
||||
pub is_auto: IsAuto,
|
||||
pub ident: Ident,
|
||||
pub generics: Generics,
|
||||
pub bounds: GenericBounds,
|
||||
pub items: ThinVec<P<AssocItem>>,
|
||||
|
@ -3465,6 +3463,7 @@ pub struct TyAliasWhereClauses {
|
|||
#[derive(Clone, Encodable, Decodable, Debug)]
|
||||
pub struct TyAlias {
|
||||
pub defaultness: Defaultness,
|
||||
pub ident: Ident,
|
||||
pub generics: Generics,
|
||||
pub where_clauses: TyAliasWhereClauses,
|
||||
pub bounds: GenericBounds,
|
||||
|
@ -3493,6 +3492,7 @@ pub struct FnContract {
|
|||
#[derive(Clone, Encodable, Decodable, Debug)]
|
||||
pub struct Fn {
|
||||
pub defaultness: Defaultness,
|
||||
pub ident: Ident,
|
||||
pub generics: Generics,
|
||||
pub sig: FnSig,
|
||||
pub contract: Option<P<FnContract>>,
|
||||
|
@ -3506,6 +3506,7 @@ pub struct Delegation {
|
|||
pub id: NodeId,
|
||||
pub qself: Option<P<QSelf>>,
|
||||
pub path: Path,
|
||||
pub ident: Ident,
|
||||
pub rename: Option<Ident>,
|
||||
pub body: Option<P<Block>>,
|
||||
/// The item was expanded from a glob delegation item.
|
||||
|
@ -3523,6 +3524,7 @@ pub struct DelegationMac {
|
|||
|
||||
#[derive(Clone, Encodable, Decodable, Debug)]
|
||||
pub struct StaticItem {
|
||||
pub ident: Ident,
|
||||
pub ty: P<Ty>,
|
||||
pub safety: Safety,
|
||||
pub mutability: Mutability,
|
||||
|
@ -3533,6 +3535,7 @@ pub struct StaticItem {
|
|||
#[derive(Clone, Encodable, Decodable, Debug)]
|
||||
pub struct ConstItem {
|
||||
pub defaultness: Defaultness,
|
||||
pub ident: Ident,
|
||||
pub generics: Generics,
|
||||
pub ty: P<Ty>,
|
||||
pub expr: Option<P<Expr>>,
|
||||
|
@ -3545,7 +3548,7 @@ pub enum ItemKind {
|
|||
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
|
||||
///
|
||||
/// E.g., `extern crate foo` or `extern crate foo_bar as foo`.
|
||||
ExternCrate(Option<Symbol>),
|
||||
ExternCrate(Option<Symbol>, Ident),
|
||||
/// A use declaration item (`use`).
|
||||
///
|
||||
/// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
|
||||
|
@ -3567,7 +3570,7 @@ pub enum ItemKind {
|
|||
/// E.g., `mod foo;` or `mod foo { .. }`.
|
||||
/// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
|
||||
/// semantically by Rust.
|
||||
Mod(Safety, ModKind),
|
||||
Mod(Safety, Ident, ModKind),
|
||||
/// An external module (`extern`).
|
||||
///
|
||||
/// E.g., `extern {}` or `extern "C" {}`.
|
||||
|
@ -3581,15 +3584,15 @@ pub enum ItemKind {
|
|||
/// An enum definition (`enum`).
|
||||
///
|
||||
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
|
||||
Enum(EnumDef, Generics),
|
||||
Enum(Ident, EnumDef, Generics),
|
||||
/// A struct definition (`struct`).
|
||||
///
|
||||
/// E.g., `struct Foo<A> { x: A }`.
|
||||
Struct(VariantData, Generics),
|
||||
Struct(Ident, VariantData, Generics),
|
||||
/// A union definition (`union`).
|
||||
///
|
||||
/// E.g., `union Foo<A, B> { x: A, y: B }`.
|
||||
Union(VariantData, Generics),
|
||||
Union(Ident, VariantData, Generics),
|
||||
/// A trait declaration (`trait`).
|
||||
///
|
||||
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
|
||||
|
@ -3597,7 +3600,7 @@ pub enum ItemKind {
|
|||
/// Trait alias.
|
||||
///
|
||||
/// E.g., `trait Foo = Bar + Quux;`.
|
||||
TraitAlias(Generics, GenericBounds),
|
||||
TraitAlias(Ident, Generics, GenericBounds),
|
||||
/// An implementation.
|
||||
///
|
||||
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
|
||||
|
@ -3608,7 +3611,7 @@ pub enum ItemKind {
|
|||
MacCall(P<MacCall>),
|
||||
|
||||
/// A macro definition.
|
||||
MacroDef(MacroDef),
|
||||
MacroDef(Ident, MacroDef),
|
||||
|
||||
/// A single delegation item (`reuse`).
|
||||
///
|
||||
|
@ -3620,6 +3623,31 @@ pub enum ItemKind {
|
|||
}
|
||||
|
||||
impl ItemKind {
|
||||
pub fn ident(&self) -> Option<Ident> {
|
||||
match *self {
|
||||
ItemKind::ExternCrate(_, ident)
|
||||
| ItemKind::Static(box StaticItem { ident, .. })
|
||||
| ItemKind::Const(box ConstItem { ident, .. })
|
||||
| ItemKind::Fn(box Fn { ident, .. })
|
||||
| ItemKind::Mod(_, ident, _)
|
||||
| ItemKind::TyAlias(box TyAlias { ident, .. })
|
||||
| ItemKind::Enum(ident, ..)
|
||||
| ItemKind::Struct(ident, ..)
|
||||
| ItemKind::Union(ident, ..)
|
||||
| ItemKind::Trait(box Trait { ident, .. })
|
||||
| ItemKind::TraitAlias(ident, ..)
|
||||
| ItemKind::MacroDef(ident, _)
|
||||
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
|
||||
|
||||
ItemKind::Use(_)
|
||||
| ItemKind::ForeignMod(_)
|
||||
| ItemKind::GlobalAsm(_)
|
||||
| ItemKind::Impl(_)
|
||||
| ItemKind::MacCall(_)
|
||||
| ItemKind::DelegationMac(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// "a" or "an"
|
||||
pub fn article(&self) -> &'static str {
|
||||
use ItemKind::*;
|
||||
|
@ -3660,11 +3688,11 @@ impl ItemKind {
|
|||
Self::Fn(box Fn { generics, .. })
|
||||
| Self::TyAlias(box TyAlias { generics, .. })
|
||||
| Self::Const(box ConstItem { generics, .. })
|
||||
| Self::Enum(_, generics)
|
||||
| Self::Struct(_, generics)
|
||||
| Self::Union(_, generics)
|
||||
| Self::Enum(_, _, generics)
|
||||
| Self::Struct(_, _, generics)
|
||||
| Self::Union(_, _, generics)
|
||||
| Self::Trait(box Trait { generics, .. })
|
||||
| Self::TraitAlias(generics, _)
|
||||
| Self::TraitAlias(_, generics, _)
|
||||
| Self::Impl(box Impl { generics, .. }) => Some(generics),
|
||||
_ => None,
|
||||
}
|
||||
|
@ -3700,6 +3728,17 @@ pub enum AssocItemKind {
|
|||
}
|
||||
|
||||
impl AssocItemKind {
|
||||
pub fn ident(&self) -> Option<Ident> {
|
||||
match *self {
|
||||
AssocItemKind::Const(box ConstItem { ident, .. })
|
||||
| AssocItemKind::Fn(box Fn { ident, .. })
|
||||
| AssocItemKind::Type(box TyAlias { ident, .. })
|
||||
| AssocItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
|
||||
|
||||
AssocItemKind::MacCall(_) | AssocItemKind::DelegationMac(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn defaultness(&self) -> Defaultness {
|
||||
match *self {
|
||||
Self::Const(box ConstItem { defaultness, .. })
|
||||
|
@ -3746,14 +3785,26 @@ impl TryFrom<ItemKind> for AssocItemKind {
|
|||
pub enum ForeignItemKind {
|
||||
/// A foreign static item (`static FOO: u8`).
|
||||
Static(Box<StaticItem>),
|
||||
/// An foreign function.
|
||||
/// A foreign function.
|
||||
Fn(Box<Fn>),
|
||||
/// An foreign type.
|
||||
/// A foreign type.
|
||||
TyAlias(Box<TyAlias>),
|
||||
/// A macro expanding to foreign items.
|
||||
MacCall(P<MacCall>),
|
||||
}
|
||||
|
||||
impl ForeignItemKind {
|
||||
pub fn ident(&self) -> Option<Ident> {
|
||||
match *self {
|
||||
ForeignItemKind::Static(box StaticItem { ident, .. })
|
||||
| ForeignItemKind::Fn(box Fn { ident, .. })
|
||||
| ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => Some(ident),
|
||||
|
||||
ForeignItemKind::MacCall(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ForeignItemKind> for ItemKind {
|
||||
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
|
||||
match foreign_item_kind {
|
||||
|
@ -3790,21 +3841,21 @@ mod size_asserts {
|
|||
|
||||
use super::*;
|
||||
// tidy-alphabetical-start
|
||||
static_assert_size!(AssocItem, 88);
|
||||
static_assert_size!(AssocItem, 80);
|
||||
static_assert_size!(AssocItemKind, 16);
|
||||
static_assert_size!(Attribute, 32);
|
||||
static_assert_size!(Block, 32);
|
||||
static_assert_size!(Expr, 72);
|
||||
static_assert_size!(ExprKind, 40);
|
||||
static_assert_size!(Fn, 176);
|
||||
static_assert_size!(ForeignItem, 88);
|
||||
static_assert_size!(Fn, 184);
|
||||
static_assert_size!(ForeignItem, 80);
|
||||
static_assert_size!(ForeignItemKind, 16);
|
||||
static_assert_size!(GenericArg, 24);
|
||||
static_assert_size!(GenericBound, 88);
|
||||
static_assert_size!(Generics, 40);
|
||||
static_assert_size!(Impl, 136);
|
||||
static_assert_size!(Item, 136);
|
||||
static_assert_size!(ItemKind, 64);
|
||||
static_assert_size!(Item, 144);
|
||||
static_assert_size!(ItemKind, 80);
|
||||
static_assert_size!(LitKind, 24);
|
||||
static_assert_size!(Local, 80);
|
||||
static_assert_size!(MetaItemLit, 40);
|
||||
|
|
|
@ -41,7 +41,6 @@ pub trait WalkItemKind {
|
|||
&mut self,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &mut Ident,
|
||||
visibility: &mut Visibility,
|
||||
ctxt: Self::Ctxt,
|
||||
visitor: &mut impl MutVisitor,
|
||||
|
@ -963,10 +962,10 @@ fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
|
|||
match kind {
|
||||
FnKind::Fn(
|
||||
_ctxt,
|
||||
_ident,
|
||||
_vis,
|
||||
Fn {
|
||||
defaultness,
|
||||
ident,
|
||||
generics,
|
||||
contract,
|
||||
body,
|
||||
|
@ -974,8 +973,9 @@ fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
|
|||
define_opaque,
|
||||
},
|
||||
) => {
|
||||
// Identifier and visibility are visited as a part of the item.
|
||||
// Visibility is visited as a part of the item.
|
||||
visit_defaultness(vis, defaultness);
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_fn_header(header);
|
||||
vis.visit_generics(generics);
|
||||
vis.visit_fn_decl(decl);
|
||||
|
@ -1233,12 +1233,11 @@ pub fn walk_item_kind<K: WalkItemKind>(
|
|||
kind: &mut K,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &mut Ident,
|
||||
visibility: &mut Visibility,
|
||||
ctxt: K::Ctxt,
|
||||
vis: &mut impl MutVisitor,
|
||||
) {
|
||||
kind.walk(span, id, ident, visibility, ctxt, vis)
|
||||
kind.walk(span, id, visibility, ctxt, vis)
|
||||
}
|
||||
|
||||
impl WalkItemKind for ItemKind {
|
||||
|
@ -1247,21 +1246,22 @@ impl WalkItemKind for ItemKind {
|
|||
&mut self,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &mut Ident,
|
||||
visibility: &mut Visibility,
|
||||
_ctxt: Self::Ctxt,
|
||||
vis: &mut impl MutVisitor,
|
||||
) {
|
||||
match self {
|
||||
ItemKind::ExternCrate(_orig_name) => {}
|
||||
ItemKind::ExternCrate(_orig_name, ident) => vis.visit_ident(ident),
|
||||
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
|
||||
ItemKind::Static(box StaticItem {
|
||||
ident,
|
||||
ty,
|
||||
safety: _,
|
||||
mutability: _,
|
||||
expr,
|
||||
define_opaque,
|
||||
}) => {
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_ty(ty);
|
||||
visit_opt(expr, |expr| vis.visit_expr(expr));
|
||||
walk_define_opaques(vis, define_opaque);
|
||||
|
@ -1270,10 +1270,11 @@ impl WalkItemKind for ItemKind {
|
|||
walk_const_item(vis, item);
|
||||
}
|
||||
ItemKind::Fn(func) => {
|
||||
vis.visit_fn(FnKind::Fn(FnCtxt::Free, ident, visibility, &mut *func), span, id);
|
||||
vis.visit_fn(FnKind::Fn(FnCtxt::Free, visibility, &mut *func), span, id);
|
||||
}
|
||||
ItemKind::Mod(safety, mod_kind) => {
|
||||
ItemKind::Mod(safety, ident, mod_kind) => {
|
||||
visit_safety(vis, safety);
|
||||
vis.visit_ident(ident);
|
||||
match mod_kind {
|
||||
ModKind::Loaded(
|
||||
items,
|
||||
|
@ -1290,18 +1291,29 @@ impl WalkItemKind for ItemKind {
|
|||
}
|
||||
ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm),
|
||||
ItemKind::GlobalAsm(asm) => vis.visit_inline_asm(asm),
|
||||
ItemKind::TyAlias(box TyAlias { defaultness, generics, where_clauses, bounds, ty }) => {
|
||||
ItemKind::TyAlias(box TyAlias {
|
||||
defaultness,
|
||||
ident,
|
||||
generics,
|
||||
where_clauses,
|
||||
bounds,
|
||||
ty,
|
||||
}) => {
|
||||
visit_defaultness(vis, defaultness);
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_generics(generics);
|
||||
visit_bounds(vis, bounds, BoundKind::Bound);
|
||||
visit_opt(ty, |ty| vis.visit_ty(ty));
|
||||
walk_ty_alias_where_clauses(vis, where_clauses);
|
||||
}
|
||||
ItemKind::Enum(EnumDef { variants }, generics) => {
|
||||
ItemKind::Enum(ident, EnumDef { variants }, generics) => {
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_generics(generics);
|
||||
variants.flat_map_in_place(|variant| vis.flat_map_variant(variant));
|
||||
}
|
||||
ItemKind::Struct(variant_data, generics) | ItemKind::Union(variant_data, generics) => {
|
||||
ItemKind::Struct(ident, variant_data, generics)
|
||||
| ItemKind::Union(ident, variant_data, generics) => {
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_generics(generics);
|
||||
vis.visit_variant_data(variant_data);
|
||||
}
|
||||
|
@ -1326,22 +1338,28 @@ impl WalkItemKind for ItemKind {
|
|||
vis.flat_map_assoc_item(item, AssocCtxt::Impl { of_trait: of_trait.is_some() })
|
||||
});
|
||||
}
|
||||
ItemKind::Trait(box Trait { safety, is_auto: _, generics, bounds, items }) => {
|
||||
ItemKind::Trait(box Trait { safety, is_auto: _, ident, generics, bounds, items }) => {
|
||||
visit_safety(vis, safety);
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_generics(generics);
|
||||
visit_bounds(vis, bounds, BoundKind::Bound);
|
||||
items.flat_map_in_place(|item| vis.flat_map_assoc_item(item, AssocCtxt::Trait));
|
||||
}
|
||||
ItemKind::TraitAlias(generics, bounds) => {
|
||||
ItemKind::TraitAlias(ident, generics, bounds) => {
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_generics(generics);
|
||||
visit_bounds(vis, bounds, BoundKind::Bound);
|
||||
}
|
||||
ItemKind::MacCall(m) => vis.visit_mac_call(m),
|
||||
ItemKind::MacroDef(def) => vis.visit_macro_def(def),
|
||||
ItemKind::MacroDef(ident, def) => {
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_macro_def(def)
|
||||
}
|
||||
ItemKind::Delegation(box Delegation {
|
||||
id,
|
||||
qself,
|
||||
path,
|
||||
ident,
|
||||
rename,
|
||||
body,
|
||||
from_glob: _,
|
||||
|
@ -1349,6 +1367,7 @@ impl WalkItemKind for ItemKind {
|
|||
vis.visit_id(id);
|
||||
vis.visit_qself(qself);
|
||||
vis.visit_path(path);
|
||||
vis.visit_ident(ident);
|
||||
if let Some(rename) = rename {
|
||||
vis.visit_ident(rename);
|
||||
}
|
||||
|
@ -1381,7 +1400,6 @@ impl WalkItemKind for AssocItemKind {
|
|||
&mut self,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &mut Ident,
|
||||
visibility: &mut Visibility,
|
||||
ctxt: Self::Ctxt,
|
||||
visitor: &mut impl MutVisitor,
|
||||
|
@ -1391,20 +1409,18 @@ impl WalkItemKind for AssocItemKind {
|
|||
walk_const_item(visitor, item);
|
||||
}
|
||||
AssocItemKind::Fn(func) => {
|
||||
visitor.visit_fn(
|
||||
FnKind::Fn(FnCtxt::Assoc(ctxt), ident, visibility, &mut *func),
|
||||
span,
|
||||
id,
|
||||
);
|
||||
visitor.visit_fn(FnKind::Fn(FnCtxt::Assoc(ctxt), visibility, &mut *func), span, id);
|
||||
}
|
||||
AssocItemKind::Type(box TyAlias {
|
||||
defaultness,
|
||||
ident,
|
||||
generics,
|
||||
where_clauses,
|
||||
bounds,
|
||||
ty,
|
||||
}) => {
|
||||
visit_defaultness(visitor, defaultness);
|
||||
visitor.visit_ident(ident);
|
||||
visitor.visit_generics(generics);
|
||||
visit_bounds(visitor, bounds, BoundKind::Bound);
|
||||
visit_opt(ty, |ty| visitor.visit_ty(ty));
|
||||
|
@ -1415,6 +1431,7 @@ impl WalkItemKind for AssocItemKind {
|
|||
id,
|
||||
qself,
|
||||
path,
|
||||
ident,
|
||||
rename,
|
||||
body,
|
||||
from_glob: _,
|
||||
|
@ -1422,6 +1439,7 @@ impl WalkItemKind for AssocItemKind {
|
|||
visitor.visit_id(id);
|
||||
visitor.visit_qself(qself);
|
||||
visitor.visit_path(path);
|
||||
visitor.visit_ident(ident);
|
||||
if let Some(rename) = rename {
|
||||
visitor.visit_ident(rename);
|
||||
}
|
||||
|
@ -1449,8 +1467,9 @@ impl WalkItemKind for AssocItemKind {
|
|||
}
|
||||
|
||||
fn walk_const_item<T: MutVisitor>(vis: &mut T, item: &mut ConstItem) {
|
||||
let ConstItem { defaultness, generics, ty, expr, define_opaque } = item;
|
||||
let ConstItem { defaultness, ident, generics, ty, expr, define_opaque } = item;
|
||||
visit_defaultness(vis, defaultness);
|
||||
vis.visit_ident(ident);
|
||||
vis.visit_generics(generics);
|
||||
vis.visit_ty(ty);
|
||||
visit_opt(expr, |expr| vis.visit_expr(expr));
|
||||
|
@ -1487,12 +1506,11 @@ fn walk_item_ctxt<K: WalkItemKind>(
|
|||
item: &mut P<Item<K>>,
|
||||
ctxt: K::Ctxt,
|
||||
) {
|
||||
let Item { ident, attrs, id, kind, vis, span, tokens } = item.deref_mut();
|
||||
let Item { attrs, id, kind, vis, span, tokens } = item.deref_mut();
|
||||
visitor.visit_id(id);
|
||||
visit_attrs(visitor, attrs);
|
||||
visitor.visit_vis(vis);
|
||||
visitor.visit_ident(ident);
|
||||
kind.walk(*span, *id, ident, vis, ctxt, visitor);
|
||||
kind.walk(*span, *id, vis, ctxt, visitor);
|
||||
visit_lazy_tts(visitor, tokens);
|
||||
visitor.visit_span(span);
|
||||
}
|
||||
|
@ -1525,38 +1543,37 @@ impl WalkItemKind for ForeignItemKind {
|
|||
&mut self,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &mut Ident,
|
||||
visibility: &mut Visibility,
|
||||
_ctxt: Self::Ctxt,
|
||||
visitor: &mut impl MutVisitor,
|
||||
) {
|
||||
match self {
|
||||
ForeignItemKind::Static(box StaticItem {
|
||||
ident,
|
||||
ty,
|
||||
mutability: _,
|
||||
expr,
|
||||
safety: _,
|
||||
define_opaque,
|
||||
}) => {
|
||||
visitor.visit_ident(ident);
|
||||
visitor.visit_ty(ty);
|
||||
visit_opt(expr, |expr| visitor.visit_expr(expr));
|
||||
walk_define_opaques(visitor, define_opaque);
|
||||
}
|
||||
ForeignItemKind::Fn(func) => {
|
||||
visitor.visit_fn(
|
||||
FnKind::Fn(FnCtxt::Foreign, ident, visibility, &mut *func),
|
||||
span,
|
||||
id,
|
||||
);
|
||||
visitor.visit_fn(FnKind::Fn(FnCtxt::Foreign, visibility, &mut *func), span, id);
|
||||
}
|
||||
ForeignItemKind::TyAlias(box TyAlias {
|
||||
defaultness,
|
||||
ident,
|
||||
generics,
|
||||
where_clauses,
|
||||
bounds,
|
||||
ty,
|
||||
}) => {
|
||||
visit_defaultness(visitor, defaultness);
|
||||
visitor.visit_ident(ident);
|
||||
visitor.visit_generics(generics);
|
||||
visit_bounds(visitor, bounds, BoundKind::Bound);
|
||||
visit_opt(ty, |ty| visitor.visit_ty(ty));
|
||||
|
@ -1984,8 +2001,7 @@ impl DummyAstNode for Item {
|
|||
span: Default::default(),
|
||||
tokens: Default::default(),
|
||||
},
|
||||
ident: Ident::dummy(),
|
||||
kind: ItemKind::ExternCrate(None),
|
||||
kind: ItemKind::ExternCrate(None, Ident::dummy()),
|
||||
tokens: Default::default(),
|
||||
}
|
||||
}
|
||||
|
@ -2052,7 +2068,7 @@ impl<N: DummyAstNode, T: DummyAstNode> DummyAstNode for crate::ast_traits::AstNo
|
|||
#[derive(Debug)]
|
||||
pub enum FnKind<'a> {
|
||||
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
|
||||
Fn(FnCtxt, &'a mut Ident, &'a mut Visibility, &'a mut Fn),
|
||||
Fn(FnCtxt, &'a mut Visibility, &'a mut Fn),
|
||||
|
||||
/// E.g., `|x, y| body`.
|
||||
Closure(
|
||||
|
|
|
@ -66,7 +66,7 @@ impl BoundKind {
|
|||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum FnKind<'a> {
|
||||
/// E.g., `fn foo()`, `fn foo(&self)`, or `extern "Abi" fn foo()`.
|
||||
Fn(FnCtxt, &'a Ident, &'a Visibility, &'a Fn),
|
||||
Fn(FnCtxt, &'a Visibility, &'a Fn),
|
||||
|
||||
/// E.g., `|x, y| body`.
|
||||
Closure(&'a ClosureBinder, &'a Option<CoroutineKind>, &'a FnDecl, &'a Expr),
|
||||
|
@ -75,21 +75,21 @@ pub enum FnKind<'a> {
|
|||
impl<'a> FnKind<'a> {
|
||||
pub fn header(&self) -> Option<&'a FnHeader> {
|
||||
match *self {
|
||||
FnKind::Fn(_, _, _, Fn { sig, .. }) => Some(&sig.header),
|
||||
FnKind::Fn(_, _, Fn { sig, .. }) => Some(&sig.header),
|
||||
FnKind::Closure(..) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ident(&self) -> Option<&Ident> {
|
||||
match self {
|
||||
FnKind::Fn(_, ident, ..) => Some(ident),
|
||||
FnKind::Fn(_, _, Fn { ident, .. }) => Some(ident),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decl(&self) -> &'a FnDecl {
|
||||
match self {
|
||||
FnKind::Fn(_, _, _, Fn { sig, .. }) => &sig.decl,
|
||||
FnKind::Fn(_, _, Fn { sig, .. }) => &sig.decl,
|
||||
FnKind::Closure(_, _, decl, _) => decl,
|
||||
}
|
||||
}
|
||||
|
@ -118,7 +118,6 @@ pub trait WalkItemKind {
|
|||
&'a self,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &'a Ident,
|
||||
visibility: &'a Visibility,
|
||||
ctxt: Self::Ctxt,
|
||||
visitor: &mut V,
|
||||
|
@ -364,63 +363,72 @@ impl WalkItemKind for ItemKind {
|
|||
&'a self,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &'a Ident,
|
||||
vis: &'a Visibility,
|
||||
_ctxt: Self::Ctxt,
|
||||
visitor: &mut V,
|
||||
) -> V::Result {
|
||||
match self {
|
||||
ItemKind::ExternCrate(_rename) => {}
|
||||
ItemKind::ExternCrate(_rename, ident) => try_visit!(visitor.visit_ident(ident)),
|
||||
ItemKind::Use(use_tree) => try_visit!(visitor.visit_use_tree(use_tree, id, false)),
|
||||
ItemKind::Static(box StaticItem {
|
||||
ident,
|
||||
ty,
|
||||
safety: _,
|
||||
mutability: _,
|
||||
expr,
|
||||
define_opaque,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_ty(ty));
|
||||
visit_opt!(visitor, visit_expr, expr);
|
||||
try_visit!(walk_define_opaques(visitor, define_opaque));
|
||||
}
|
||||
ItemKind::Const(box ConstItem {
|
||||
defaultness: _,
|
||||
ident,
|
||||
generics,
|
||||
ty,
|
||||
expr,
|
||||
define_opaque,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
try_visit!(visitor.visit_ty(ty));
|
||||
visit_opt!(visitor, visit_expr, expr);
|
||||
try_visit!(walk_define_opaques(visitor, define_opaque));
|
||||
}
|
||||
ItemKind::Fn(func) => {
|
||||
let kind = FnKind::Fn(FnCtxt::Free, ident, vis, &*func);
|
||||
let kind = FnKind::Fn(FnCtxt::Free, vis, &*func);
|
||||
try_visit!(visitor.visit_fn(kind, span, id));
|
||||
}
|
||||
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
|
||||
ModKind::Loaded(items, _inline, _inner_span, _) => {
|
||||
walk_list!(visitor, visit_item, items);
|
||||
ItemKind::Mod(_unsafety, ident, mod_kind) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
match mod_kind {
|
||||
ModKind::Loaded(items, _inline, _inner_span, _) => {
|
||||
walk_list!(visitor, visit_item, items);
|
||||
}
|
||||
ModKind::Unloaded => {}
|
||||
}
|
||||
ModKind::Unloaded => {}
|
||||
},
|
||||
}
|
||||
ItemKind::ForeignMod(ForeignMod { extern_span: _, safety: _, abi: _, items }) => {
|
||||
walk_list!(visitor, visit_foreign_item, items);
|
||||
}
|
||||
ItemKind::GlobalAsm(asm) => try_visit!(visitor.visit_inline_asm(asm)),
|
||||
ItemKind::TyAlias(box TyAlias {
|
||||
generics,
|
||||
ident,
|
||||
bounds,
|
||||
ty,
|
||||
defaultness: _,
|
||||
where_clauses: _,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
|
||||
visit_opt!(visitor, visit_ty, ty);
|
||||
}
|
||||
ItemKind::Enum(enum_definition, generics) => {
|
||||
ItemKind::Enum(ident, enum_definition, generics) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
try_visit!(visitor.visit_enum_def(enum_definition));
|
||||
}
|
||||
|
@ -444,32 +452,47 @@ impl WalkItemKind for ItemKind {
|
|||
AssocCtxt::Impl { of_trait: of_trait.is_some() }
|
||||
);
|
||||
}
|
||||
ItemKind::Struct(struct_definition, generics)
|
||||
| ItemKind::Union(struct_definition, generics) => {
|
||||
ItemKind::Struct(ident, struct_definition, generics)
|
||||
| ItemKind::Union(ident, struct_definition, generics) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
try_visit!(visitor.visit_variant_data(struct_definition));
|
||||
}
|
||||
ItemKind::Trait(box Trait { safety: _, is_auto: _, generics, bounds, items }) => {
|
||||
ItemKind::Trait(box Trait {
|
||||
safety: _,
|
||||
is_auto: _,
|
||||
ident,
|
||||
generics,
|
||||
bounds,
|
||||
items,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits);
|
||||
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
|
||||
}
|
||||
ItemKind::TraitAlias(generics, bounds) => {
|
||||
ItemKind::TraitAlias(ident, generics, bounds) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
|
||||
}
|
||||
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
|
||||
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, id)),
|
||||
ItemKind::MacroDef(ident, ts) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_mac_def(ts, id))
|
||||
}
|
||||
ItemKind::Delegation(box Delegation {
|
||||
id,
|
||||
qself,
|
||||
path,
|
||||
ident,
|
||||
rename,
|
||||
body,
|
||||
from_glob: _,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_qself(qself));
|
||||
try_visit!(visitor.visit_path(path, *id));
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
visit_opt!(visitor, visit_ident, rename);
|
||||
visit_opt!(visitor, visit_block, body);
|
||||
}
|
||||
|
@ -743,34 +766,37 @@ impl WalkItemKind for ForeignItemKind {
|
|||
&'a self,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &'a Ident,
|
||||
vis: &'a Visibility,
|
||||
_ctxt: Self::Ctxt,
|
||||
visitor: &mut V,
|
||||
) -> V::Result {
|
||||
match self {
|
||||
ForeignItemKind::Static(box StaticItem {
|
||||
ident,
|
||||
ty,
|
||||
mutability: _,
|
||||
expr,
|
||||
safety: _,
|
||||
define_opaque,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_ty(ty));
|
||||
visit_opt!(visitor, visit_expr, expr);
|
||||
try_visit!(walk_define_opaques(visitor, define_opaque));
|
||||
}
|
||||
ForeignItemKind::Fn(func) => {
|
||||
let kind = FnKind::Fn(FnCtxt::Foreign, ident, vis, &*func);
|
||||
let kind = FnKind::Fn(FnCtxt::Foreign, vis, &*func);
|
||||
try_visit!(visitor.visit_fn(kind, span, id));
|
||||
}
|
||||
ForeignItemKind::TyAlias(box TyAlias {
|
||||
generics,
|
||||
ident,
|
||||
bounds,
|
||||
ty,
|
||||
defaultness: _,
|
||||
where_clauses: _,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
|
||||
visit_opt!(visitor, visit_ty, ty);
|
||||
|
@ -917,10 +943,10 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
|
|||
match kind {
|
||||
FnKind::Fn(
|
||||
_ctxt,
|
||||
_ident,
|
||||
_vis,
|
||||
Fn {
|
||||
defaultness: _,
|
||||
ident,
|
||||
sig: FnSig { header, decl, span: _ },
|
||||
generics,
|
||||
contract,
|
||||
|
@ -928,7 +954,8 @@ pub fn walk_fn<'a, V: Visitor<'a>>(visitor: &mut V, kind: FnKind<'a>) -> V::Resu
|
|||
define_opaque,
|
||||
},
|
||||
) => {
|
||||
// Identifier and visibility are visited as a part of the item.
|
||||
// Visibility is visited as a part of the item.
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_fn_header(header));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
try_visit!(visitor.visit_fn_decl(decl));
|
||||
|
@ -952,7 +979,6 @@ impl WalkItemKind for AssocItemKind {
|
|||
&'a self,
|
||||
span: Span,
|
||||
id: NodeId,
|
||||
ident: &'a Ident,
|
||||
vis: &'a Visibility,
|
||||
ctxt: Self::Ctxt,
|
||||
visitor: &mut V,
|
||||
|
@ -960,28 +986,32 @@ impl WalkItemKind for AssocItemKind {
|
|||
match self {
|
||||
AssocItemKind::Const(box ConstItem {
|
||||
defaultness: _,
|
||||
ident,
|
||||
generics,
|
||||
ty,
|
||||
expr,
|
||||
define_opaque,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
try_visit!(visitor.visit_ty(ty));
|
||||
visit_opt!(visitor, visit_expr, expr);
|
||||
try_visit!(walk_define_opaques(visitor, define_opaque));
|
||||
}
|
||||
AssocItemKind::Fn(func) => {
|
||||
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, vis, &*func);
|
||||
let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), vis, &*func);
|
||||
try_visit!(visitor.visit_fn(kind, span, id));
|
||||
}
|
||||
AssocItemKind::Type(box TyAlias {
|
||||
generics,
|
||||
ident,
|
||||
bounds,
|
||||
ty,
|
||||
defaultness: _,
|
||||
where_clauses: _,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_generics(generics));
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
|
||||
visit_opt!(visitor, visit_ty, ty);
|
||||
}
|
||||
|
@ -992,12 +1022,14 @@ impl WalkItemKind for AssocItemKind {
|
|||
id,
|
||||
qself,
|
||||
path,
|
||||
ident,
|
||||
rename,
|
||||
body,
|
||||
from_glob: _,
|
||||
}) => {
|
||||
try_visit!(visitor.visit_qself(qself));
|
||||
try_visit!(visitor.visit_path(path, *id));
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
visit_opt!(visitor, visit_ident, rename);
|
||||
visit_opt!(visitor, visit_block, body);
|
||||
}
|
||||
|
@ -1039,11 +1071,10 @@ fn walk_item_ctxt<'a, V: Visitor<'a>, K: WalkItemKind>(
|
|||
item: &'a Item<K>,
|
||||
ctxt: K::Ctxt,
|
||||
) -> V::Result {
|
||||
let Item { id, span, ident, vis, attrs, kind, tokens: _ } = item;
|
||||
let Item { id, span, vis, attrs, kind, tokens: _ } = item;
|
||||
walk_list!(visitor, visit_attribute, attrs);
|
||||
try_visit!(visitor.visit_vis(vis));
|
||||
try_visit!(visitor.visit_ident(ident));
|
||||
try_visit!(kind.walk(*span, *id, ident, vis, ctxt, visitor));
|
||||
try_visit!(kind.walk(*span, *id, vis, ctxt, visitor));
|
||||
V::Result::output()
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue