diff options
Diffstat (limited to 'compiler/rustc_ast/src')
| -rw-r--r-- | compiler/rustc_ast/src/ast.rs | 89 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/lib.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/mut_visit.rs | 18 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/visit.rs | 18 |
4 files changed, 73 insertions, 53 deletions
diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index ff89982a4ef..ea79782ece1 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2656,6 +2656,35 @@ impl Default for FnHeader { } #[derive(Clone, Encodable, Decodable, Debug)] +pub struct TraitKind( + pub IsAuto, + pub Unsafe, + pub Generics, + pub GenericBounds, + pub Vec<P<AssocItem>>, +); +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct TyAliasKind(pub Defaultness, pub Generics, pub GenericBounds, pub Option<P<Ty>>); + +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct ImplKind { + pub unsafety: Unsafe, + pub polarity: ImplPolarity, + pub defaultness: Defaultness, + pub constness: Const, + pub generics: Generics, + + /// 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>>); + +#[derive(Clone, Encodable, Decodable, Debug)] pub enum ItemKind { /// An `extern crate` item, with the optional *original* crate name if the crate was renamed. /// @@ -2676,7 +2705,7 @@ pub enum ItemKind { /// A function declaration (`fn`). /// /// E.g., `fn foo(bar: usize) -> usize { .. }`. - Fn(Defaultness, FnSig, Generics, Option<P<Block>>), + Fn(Box<FnKind>), /// A module declaration (`mod`). /// /// E.g., `mod foo;` or `mod foo { .. }`. @@ -2690,7 +2719,7 @@ pub enum ItemKind { /// A type alias (`type`). /// /// E.g., `type Foo = Bar<u8>;`. - TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>), + TyAlias(Box<TyAliasKind>), /// An enum definition (`enum`). /// /// E.g., `enum Foo<A, B> { C<A>, D<B> }`. @@ -2706,7 +2735,7 @@ pub enum ItemKind { /// A trait declaration (`trait`). /// /// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`. - Trait(IsAuto, Unsafe, Generics, GenericBounds, Vec<P<AssocItem>>), + Trait(Box<TraitKind>), /// Trait alias /// /// E.g., `trait Foo = Bar + Quux;`. @@ -2714,19 +2743,7 @@ pub enum ItemKind { /// An implementation. /// /// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`. - Impl { - unsafety: Unsafe, - polarity: ImplPolarity, - defaultness: Defaultness, - constness: Const, - generics: Generics, - - /// The trait being implemented, if any. - of_trait: Option<TraitRef>, - - self_ty: P<Ty>, - items: Vec<P<AssocItem>>, - }, + Impl(Box<ImplKind>), /// A macro invocation. /// /// E.g., `foo!(..)`. @@ -2770,14 +2787,14 @@ impl ItemKind { pub fn generics(&self) -> Option<&Generics> { match self { - Self::Fn(_, _, generics, _) - | Self::TyAlias(_, generics, ..) + Self::Fn(box FnKind(_, _, generics, _)) + | Self::TyAlias(box TyAliasKind(_, generics, ..)) | Self::Enum(_, generics) | Self::Struct(_, generics) | Self::Union(_, generics) - | Self::Trait(_, _, generics, ..) + | Self::Trait(box TraitKind(_, _, generics, ..)) | Self::TraitAlias(generics, _) - | Self::Impl { generics, .. } => Some(generics), + | Self::Impl(box ImplKind { generics, .. }) => Some(generics), _ => None, } } @@ -2800,9 +2817,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(Defaultness, FnSig, Generics, Option<P<Block>>), + Fn(Box<FnKind>), /// An associated type. - TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>), + TyAlias(Box<TyAliasKind>), /// A macro expanding to associated items. MacCall(MacCall), } @@ -2810,7 +2827,9 @@ pub enum AssocItemKind { impl AssocItemKind { pub fn defaultness(&self) -> Defaultness { match *self { - Self::Const(def, ..) | Self::Fn(def, ..) | Self::TyAlias(def, ..) => def, + Self::Const(def, ..) + | Self::Fn(box FnKind(def, ..)) + | Self::TyAlias(box TyAliasKind(def, ..)) => def, Self::MacCall(..) => Defaultness::Final, } } @@ -2820,8 +2839,8 @@ impl From<AssocItemKind> for ItemKind { fn from(assoc_item_kind: AssocItemKind) -> ItemKind { match assoc_item_kind { AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c), - AssocItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d), - AssocItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d), + AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), + AssocItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), AssocItemKind::MacCall(a) => ItemKind::MacCall(a), } } @@ -2833,8 +2852,8 @@ impl TryFrom<ItemKind> for AssocItemKind { fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> { Ok(match item_kind { ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c), - ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d), - ItemKind::TyAlias(a, b, c, d) => AssocItemKind::TyAlias(a, b, c, d), + ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind), + ItemKind::TyAlias(ty_alias_kind) => AssocItemKind::TyAlias(ty_alias_kind), ItemKind::MacCall(a) => AssocItemKind::MacCall(a), _ => return Err(item_kind), }) @@ -2846,10 +2865,10 @@ impl TryFrom<ItemKind> for AssocItemKind { pub enum ForeignItemKind { /// A foreign static item (`static FOO: u8`). Static(P<Ty>, Mutability, Option<P<Expr>>), - /// A foreign function. - Fn(Defaultness, FnSig, Generics, Option<P<Block>>), - /// A foreign type. - TyAlias(Defaultness, Generics, GenericBounds, Option<P<Ty>>), + /// An foreign function. + Fn(Box<FnKind>), + /// An foreign type. + TyAlias(Box<TyAliasKind>), /// A macro expanding to foreign items. MacCall(MacCall), } @@ -2858,8 +2877,8 @@ impl From<ForeignItemKind> for ItemKind { fn from(foreign_item_kind: ForeignItemKind) -> ItemKind { match foreign_item_kind { ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c), - ForeignItemKind::Fn(a, b, c, d) => ItemKind::Fn(a, b, c, d), - ForeignItemKind::TyAlias(a, b, c, d) => ItemKind::TyAlias(a, b, c, d), + ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), + ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), ForeignItemKind::MacCall(a) => ItemKind::MacCall(a), } } @@ -2871,8 +2890,8 @@ impl TryFrom<ItemKind> for ForeignItemKind { fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> { Ok(match item_kind { ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c), - ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d), - ItemKind::TyAlias(a, b, c, d) => ForeignItemKind::TyAlias(a, b, c, d), + ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind), + ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind), ItemKind::MacCall(a) => ForeignItemKind::MacCall(a), _ => return Err(item_kind), }) diff --git a/compiler/rustc_ast/src/lib.rs b/compiler/rustc_ast/src/lib.rs index 08695491de7..0f411eda49d 100644 --- a/compiler/rustc_ast/src/lib.rs +++ b/compiler/rustc_ast/src/lib.rs @@ -9,6 +9,7 @@ test(attr(deny(warnings))) )] #![feature(box_syntax)] +#![feature(box_patterns)] #![feature(const_fn)] // For the `transmute` in `P::new` #![feature(const_fn_transmute)] #![feature(const_panic)] diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 35de744d274..90d31d4801f 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -912,7 +912,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) { vis.visit_ty(ty); visit_opt(expr, |expr| vis.visit_expr(expr)); } - ItemKind::Fn(_, sig, generics, body) => { + ItemKind::Fn(box FnKind(_, sig, generics, body)) => { visit_fn_sig(sig, vis); vis.visit_generics(generics); visit_opt(body, |body| vis.visit_block(body)); @@ -920,7 +920,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) { ItemKind::Mod(m) => vis.visit_mod(m), ItemKind::ForeignMod(nm) => vis.visit_foreign_mod(nm), ItemKind::GlobalAsm(_ga) => {} - ItemKind::TyAlias(_, generics, bounds, ty) => { + ItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { vis.visit_generics(generics); visit_bounds(bounds, vis); visit_opt(ty, |ty| vis.visit_ty(ty)); @@ -933,7 +933,7 @@ 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 { + ItemKind::Impl(box ImplKind { unsafety: _, polarity: _, defaultness: _, @@ -942,13 +942,13 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) { of_trait, self_ty, items, - } => { + }) => { vis.visit_generics(generics); 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(_is_auto, _unsafety, generics, bounds, items) => { + ItemKind::Trait(box TraitKind(.., generics, bounds, items)) => { vis.visit_generics(generics); visit_bounds(bounds, vis); items.flat_map_in_place(|item| vis.flat_map_trait_item(item)); @@ -976,12 +976,12 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>( visitor.visit_ty(ty); visit_opt(expr, |expr| visitor.visit_expr(expr)); } - AssocItemKind::Fn(_, sig, generics, body) => { + AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => { visitor.visit_generics(generics); visit_fn_sig(sig, visitor); visit_opt(body, |body| visitor.visit_block(body)); } - AssocItemKind::TyAlias(_, generics, bounds, ty) => { + AssocItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { visitor.visit_generics(generics); visit_bounds(bounds, visitor); visit_opt(ty, |ty| visitor.visit_ty(ty)); @@ -1066,12 +1066,12 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>( visitor.visit_ty(ty); visit_opt(expr, |expr| visitor.visit_expr(expr)); } - ForeignItemKind::Fn(_, sig, generics, body) => { + ForeignItemKind::Fn(box FnKind(_, sig, generics, body)) => { visitor.visit_generics(generics); visit_fn_sig(sig, visitor); visit_opt(body, |body| visitor.visit_block(body)); } - ForeignItemKind::TyAlias(_, generics, bounds, ty) => { + ForeignItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { visitor.visit_generics(generics); visit_bounds(bounds, visitor); visit_opt(ty, |ty| visitor.visit_ty(ty)); diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 2ba1c49edfa..c37d4cd9f79 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -292,7 +292,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { visitor.visit_ty(typ); walk_list!(visitor, visit_expr, expr); } - ItemKind::Fn(_, ref sig, ref generics, ref body) => { + ItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body)) => { visitor.visit_generics(generics); let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref()); visitor.visit_fn(kind, item.span, item.id) @@ -302,7 +302,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { walk_list!(visitor, visit_foreign_item, &foreign_module.items); } ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga), - ItemKind::TyAlias(_, ref generics, ref bounds, ref ty) => { + ItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref ty)) => { visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, ty); @@ -311,7 +311,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { visitor.visit_generics(generics); visitor.visit_enum_def(enum_definition, generics, item.id, item.span) } - ItemKind::Impl { + ItemKind::Impl(box ImplKind { unsafety: _, polarity: _, defaultness: _, @@ -320,7 +320,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { ref of_trait, ref self_ty, ref items, - } => { + }) => { visitor.visit_generics(generics); walk_list!(visitor, visit_trait_ref, of_trait); visitor.visit_ty(self_ty); @@ -331,7 +331,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { visitor.visit_generics(generics); visitor.visit_variant_data(struct_definition); } - ItemKind::Trait(.., ref generics, ref bounds, ref items) => { + ItemKind::Trait(box TraitKind(.., ref generics, ref bounds, ref items)) => { visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait); @@ -543,12 +543,12 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } - ForeignItemKind::Fn(_, sig, generics, body) => { + ForeignItemKind::Fn(box FnKind(_, sig, generics, body)) => { visitor.visit_generics(generics); let kind = FnKind::Fn(FnCtxt::Foreign, ident, sig, vis, body.as_deref()); visitor.visit_fn(kind, span, id); } - ForeignItemKind::TyAlias(_, generics, bounds, ty) => { + ForeignItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, ty); @@ -653,12 +653,12 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } - AssocItemKind::Fn(_, sig, generics, body) => { + AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => { visitor.visit_generics(generics); let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), ident, sig, vis, body.as_deref()); visitor.visit_fn(kind, span, id); } - AssocItemKind::TyAlias(_, generics, bounds, ty) => { + AssocItemKind::TyAlias(box TyAliasKind(_, generics, bounds, ty)) => { visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, ty); |
