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

ast: Fix naming conventions in AST structures #90671

Merged
merged 1 commit into from
Nov 7, 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
68 changes: 38 additions & 30 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2645,34 +2645,42 @@ impl Default for FnHeader {
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct TraitKind(
pub IsAuto,
pub Unsafe,
pub Generics,
pub GenericBounds,
pub Vec<P<AssocItem>>,
);
pub struct Trait {
pub unsafety: Unsafe,
pub is_auto: IsAuto,
pub generics: Generics,
pub bounds: GenericBounds,
pub items: Vec<P<AssocItem>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct TyAliasKind(pub Defaultness, pub Generics, pub GenericBounds, pub Option<P<Ty>>);
pub struct TyAlias {
pub defaultness: Defaultness,
pub generics: Generics,
pub bounds: GenericBounds,
pub ty: Option<P<Ty>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct ImplKind {
pub unsafety: Unsafe,
pub polarity: ImplPolarity,
pub struct Impl {
pub defaultness: Defaultness,
pub constness: Const,
pub unsafety: Unsafe,
pub generics: Generics,

pub constness: Const,
pub polarity: ImplPolarity,
/// The trait being implemented, if any.
pub of_trait: Option<TraitRef>,

pub self_ty: P<Ty>,
pub items: Vec<P<AssocItem>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct FnKind(pub Defaultness, pub FnSig, pub Generics, pub Option<P<Block>>);
pub struct Fn {
pub defaultness: Defaultness,
pub generics: Generics,
pub sig: FnSig,
pub body: Option<P<Block>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ItemKind {
Expand All @@ -2695,7 +2703,7 @@ pub enum ItemKind {
/// A function declaration (`fn`).
///
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
Fn(Box<FnKind>),
Fn(Box<Fn>),
/// A module declaration (`mod`).
///
/// E.g., `mod foo;` or `mod foo { .. }`.
Expand All @@ -2711,7 +2719,7 @@ pub enum ItemKind {
/// A type alias (`type`).
///
/// E.g., `type Foo = Bar<u8>;`.
TyAlias(Box<TyAliasKind>),
TyAlias(Box<TyAlias>),
/// An enum definition (`enum`).
///
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
Expand All @@ -2727,15 +2735,15 @@ pub enum ItemKind {
/// A trait declaration (`trait`).
///
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
Trait(Box<TraitKind>),
Trait(Box<Trait>),
/// Trait alias
///
/// E.g., `trait Foo = Bar + Quux;`.
TraitAlias(Generics, GenericBounds),
/// An implementation.
///
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
Impl(Box<ImplKind>),
Impl(Box<Impl>),
/// A macro invocation.
///
/// E.g., `foo!(..)`.
Expand Down Expand Up @@ -2782,14 +2790,14 @@ impl ItemKind {

pub fn generics(&self) -> Option<&Generics> {
match self {
Self::Fn(box FnKind(_, _, generics, _))
| Self::TyAlias(box TyAliasKind(_, generics, ..))
Self::Fn(box Fn { generics, .. })
| Self::TyAlias(box TyAlias { generics, .. })
| Self::Enum(_, generics)
| Self::Struct(_, generics)
| Self::Union(_, generics)
| Self::Trait(box TraitKind(_, _, generics, ..))
| Self::Trait(box Trait { generics, .. })
| Self::TraitAlias(generics, _)
| Self::Impl(box ImplKind { generics, .. }) => Some(generics),
| Self::Impl(box Impl { generics, .. }) => Some(generics),
_ => None,
}
}
Expand All @@ -2812,9 +2820,9 @@ pub enum AssocItemKind {
/// If `def` is parsed, then the constant is provided, and otherwise required.
Const(Defaultness, P<Ty>, Option<P<Expr>>),
/// An associated function.
Fn(Box<FnKind>),
Fn(Box<Fn>),
/// An associated type.
TyAlias(Box<TyAliasKind>),
TyAlias(Box<TyAlias>),
/// A macro expanding to associated items.
MacCall(MacCall),
}
Expand All @@ -2825,9 +2833,9 @@ rustc_data_structures::static_assert_size!(AssocItemKind, 72);
impl AssocItemKind {
pub fn defaultness(&self) -> Defaultness {
match *self {
Self::Const(def, ..)
| Self::Fn(box FnKind(def, ..))
| Self::TyAlias(box TyAliasKind(def, ..)) => def,
Self::Const(defaultness, ..)
| Self::Fn(box Fn { defaultness, .. })
| Self::TyAlias(box TyAlias { defaultness, .. }) => defaultness,
Self::MacCall(..) => Defaultness::Final,
}
}
Expand Down Expand Up @@ -2864,9 +2872,9 @@ pub enum ForeignItemKind {
/// A foreign static item (`static FOO: u8`).
Static(P<Ty>, Mutability, Option<P<Expr>>),
/// An foreign function.
Fn(Box<FnKind>),
Fn(Box<Fn>),
/// An foreign type.
TyAlias(Box<TyAliasKind>),
TyAlias(Box<TyAlias>),
/// A macro expanding to foreign items.
MacCall(MacCall),
}
Expand Down
102 changes: 79 additions & 23 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
vis.visit_mt(mt);
}
TyKind::BareFn(bft) => {
let BareFnTy { unsafety: _, ext: _, generic_params, decl } = bft.deref_mut();
let BareFnTy { unsafety, ext: _, generic_params, decl } = bft.deref_mut();
visit_unsafety(unsafety, vis);
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
vis.visit_fn_decl(decl);
}
Expand Down Expand Up @@ -488,7 +489,8 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
}

pub fn noop_visit_foreign_mod<T: MutVisitor>(foreign_mod: &mut ForeignMod, vis: &mut T) {
let ForeignMod { unsafety: _, abi: _, items } = foreign_mod;
let ForeignMod { unsafety, abi: _, items } = foreign_mod;
visit_unsafety(unsafety, vis);
items.flat_map_in_place(|item| vis.flat_map_foreign_item(item));
}

Expand Down Expand Up @@ -788,6 +790,38 @@ pub fn visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut
}
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_defaultness<T: MutVisitor>(defaultness: &mut Defaultness, vis: &mut T) {
match defaultness {
Defaultness::Default(span) => vis.visit_span(span),
Defaultness::Final => {}
}
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_unsafety<T: MutVisitor>(unsafety: &mut Unsafe, vis: &mut T) {
match unsafety {
Unsafe::Yes(span) => vis.visit_span(span),
Unsafe::No => {}
}
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_polarity<T: MutVisitor>(polarity: &mut ImplPolarity, vis: &mut T) {
match polarity {
ImplPolarity::Positive => {}
ImplPolarity::Negative(span) => vis.visit_span(span),
}
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_constness<T: MutVisitor>(constness: &mut Const, vis: &mut T) {
match constness {
Const::Yes(span) => vis.visit_span(span),
Const::No => {}
}
}

pub fn noop_visit_asyncness<T: MutVisitor>(asyncness: &mut Async, vis: &mut T) {
match asyncness {
Async::Yes { span: _, closure_id, return_impl_trait_id } => {
Expand Down Expand Up @@ -955,25 +989,35 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
match kind {
ItemKind::ExternCrate(_orig_name) => {}
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
ItemKind::Static(ty, _, expr) | ItemKind::Const(_, ty, expr) => {
ItemKind::Static(ty, _, expr) => {
vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr));
}
ItemKind::Fn(box FnKind(_, sig, generics, body)) => {
ItemKind::Const(defaultness, ty, expr) => {
visit_defaultness(defaultness, vis);
vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr));
}
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(defaultness, vis);
visit_fn_sig(sig, vis);
vis.visit_generics(generics);
visit_opt(body, |body| vis.visit_block(body));
}
ItemKind::Mod(_unsafety, mod_kind) => match mod_kind {
ModKind::Loaded(items, _inline, inner_span) => {
vis.visit_span(inner_span);
items.flat_map_in_place(|item| vis.flat_map_item(item));
ItemKind::Mod(unsafety, mod_kind) => {
visit_unsafety(unsafety, vis);
match mod_kind {
ModKind::Loaded(items, _inline, inner_span) => {
vis.visit_span(inner_span);
items.flat_map_in_place(|item| vis.flat_map_item(item));
}
ModKind::Unloaded => {}
}
ModKind::Unloaded => {}
},
}
ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm),
ItemKind::GlobalAsm(asm) => noop_visit_inline_asm(asm, vis),
ItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
ItemKind::TyAlias(box TyAlias { defaultness, generics, bounds, ty }) => {
visit_defaultness(defaultness, vis);
vis.visit_generics(generics);
visit_bounds(bounds, vis);
visit_opt(ty, |ty| vis.visit_ty(ty));
Expand All @@ -986,22 +1030,27 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
vis.visit_variant_data(variant_data);
vis.visit_generics(generics);
}
ItemKind::Impl(box ImplKind {
unsafety: _,
polarity: _,
defaultness: _,
constness: _,
ItemKind::Impl(box Impl {
defaultness,
unsafety,
generics,
constness,
polarity,
of_trait,
self_ty,
items,
}) => {
visit_defaultness(defaultness, vis);
visit_unsafety(unsafety, vis);
vis.visit_generics(generics);
visit_constness(constness, vis);
visit_polarity(polarity, vis);
visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref));
vis.visit_ty(self_ty);
items.flat_map_in_place(|item| vis.flat_map_impl_item(item));
}
ItemKind::Trait(box TraitKind(.., generics, bounds, items)) => {
ItemKind::Trait(box Trait { unsafety, is_auto: _, generics, bounds, items }) => {
visit_unsafety(unsafety, vis);
vis.visit_generics(generics);
visit_bounds(bounds, vis);
items.flat_map_in_place(|item| vis.flat_map_trait_item(item));
Expand All @@ -1025,16 +1074,19 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
match kind {
AssocItemKind::Const(_, ty, expr) => {
AssocItemKind::Const(defaultness, ty, expr) => {
visit_defaultness(defaultness, visitor);
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
}
AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => {
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_generics(generics);
visit_fn_sig(sig, visitor);
visit_opt(body, |body| visitor.visit_block(body));
}
AssocItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
AssocItemKind::TyAlias(box TyAlias { defaultness, generics, bounds, ty }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_generics(generics);
visit_bounds(bounds, visitor);
visit_opt(ty, |ty| visitor.visit_ty(ty));
Expand All @@ -1047,8 +1099,10 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
}

pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
let FnHeader { unsafety: _, asyncness, constness: _, ext: _ } = header;
let FnHeader { unsafety, asyncness, constness, ext: _ } = header;
visit_constness(constness, vis);
vis.visit_asyncness(asyncness);
visit_unsafety(unsafety, vis);
}

// FIXME: Avoid visiting the crate as a `Mod` item, flat map only the inner items if possible,
Expand Down Expand Up @@ -1114,12 +1168,14 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
}
ForeignItemKind::Fn(box FnKind(_, sig, generics, body)) => {
ForeignItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_generics(generics);
visit_fn_sig(sig, visitor);
visit_opt(body, |body| visitor.visit_block(body));
}
ForeignItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => {
ForeignItemKind::TyAlias(box TyAlias { defaultness, generics, bounds, ty }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_generics(generics);
visit_bounds(bounds, visitor);
visit_opt(ty, |ty| visitor.visit_ty(ty));
Expand Down
Loading