diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index e2424e7d7ad90..f9e19d30fcc7e 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -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>, -); +pub struct Trait { + pub unsafety: Unsafe, + pub is_auto: IsAuto, + pub generics: Generics, + pub bounds: GenericBounds, + pub items: Vec>, +} #[derive(Clone, Encodable, Decodable, Debug)] -pub struct TyAliasKind(pub Defaultness, pub Generics, pub GenericBounds, pub Option>); +pub struct TyAlias { + pub defaultness: Defaultness, + pub generics: Generics, + pub bounds: GenericBounds, + pub ty: Option>, +} #[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, - pub self_ty: P, pub items: Vec>, } #[derive(Clone, Encodable, Decodable, Debug)] -pub struct FnKind(pub Defaultness, pub FnSig, pub Generics, pub Option>); +pub struct Fn { + pub defaultness: Defaultness, + pub generics: Generics, + pub sig: FnSig, + pub body: Option>, +} #[derive(Clone, Encodable, Decodable, Debug)] pub enum ItemKind { @@ -2695,7 +2703,7 @@ pub enum ItemKind { /// A function declaration (`fn`). /// /// E.g., `fn foo(bar: usize) -> usize { .. }`. - Fn(Box), + Fn(Box), /// A module declaration (`mod`). /// /// E.g., `mod foo;` or `mod foo { .. }`. @@ -2711,7 +2719,7 @@ pub enum ItemKind { /// A type alias (`type`). /// /// E.g., `type Foo = Bar;`. - TyAlias(Box), + TyAlias(Box), /// An enum definition (`enum`). /// /// E.g., `enum Foo { C, D }`. @@ -2727,7 +2735,7 @@ pub enum ItemKind { /// A trait declaration (`trait`). /// /// E.g., `trait Foo { .. }`, `trait Foo { .. }` or `auto trait Foo {}`. - Trait(Box), + Trait(Box), /// Trait alias /// /// E.g., `trait Foo = Bar + Quux;`. @@ -2735,7 +2743,7 @@ pub enum ItemKind { /// An implementation. /// /// E.g., `impl Foo { .. }` or `impl Trait for Foo { .. }`. - Impl(Box), + Impl(Box), /// A macro invocation. /// /// E.g., `foo!(..)`. @@ -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, } } @@ -2812,9 +2820,9 @@ pub enum AssocItemKind { /// If `def` is parsed, then the constant is provided, and otherwise required. Const(Defaultness, P, Option>), /// An associated function. - Fn(Box), + Fn(Box), /// An associated type. - TyAlias(Box), + TyAlias(Box), /// A macro expanding to associated items. MacCall(MacCall), } @@ -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, } } @@ -2864,9 +2872,9 @@ pub enum ForeignItemKind { /// A foreign static item (`static FOO: u8`). Static(P, Mutability, Option>), /// An foreign function. - Fn(Box), + Fn(Box), /// An foreign type. - TyAlias(Box), + TyAlias(Box), /// A macro expanding to foreign items. MacCall(MacCall), } diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 74def2bab1bfa..fc5cc96399257 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -459,7 +459,8 @@ pub fn noop_visit_ty(ty: &mut P, 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); } @@ -488,7 +489,8 @@ pub fn noop_visit_ty(ty: &mut P, vis: &mut T) { } pub fn noop_visit_foreign_mod(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)); } @@ -788,6 +790,38 @@ pub fn visit_interpolated(nt: &mut token::Nonterminal, vis: &mut } } +// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`. +pub fn visit_defaultness(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(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(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(constness: &mut Const, vis: &mut T) { + match constness { + Const::Yes(span) => vis.visit_span(span), + Const::No => {} + } +} + pub fn noop_visit_asyncness(asyncness: &mut Async, vis: &mut T) { match asyncness { Async::Yes { span: _, closure_id, return_impl_trait_id } => { @@ -955,25 +989,35 @@ pub fn noop_visit_item_kind(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)); @@ -986,22 +1030,27 @@ pub fn noop_visit_item_kind(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)); @@ -1025,16 +1074,19 @@ pub fn noop_flat_map_assoc_item( 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)); @@ -1047,8 +1099,10 @@ pub fn noop_flat_map_assoc_item( } pub fn noop_visit_fn_header(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, @@ -1114,12 +1168,14 @@ pub fn noop_flat_map_foreign_item( 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)); diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index b38031042e0f0..be794ed221ae7 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -285,7 +285,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(box FnKind(_, ref sig, ref generics, ref body)) => { + ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, 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) @@ -300,7 +300,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 asm) => walk_inline_asm(visitor, asm), - ItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref ty)) => { + ItemKind::TyAlias(box TyAlias { defaultness: _, ref generics, ref bounds, ref ty }) => { visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, ty); @@ -309,12 +309,12 @@ 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(box ImplKind { - unsafety: _, - polarity: _, + ItemKind::Impl(box Impl { defaultness: _, - constness: _, + unsafety: _, ref generics, + constness: _, + polarity: _, ref of_trait, ref self_ty, ref items, @@ -329,7 +329,13 @@ 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(box TraitKind(.., ref generics, ref bounds, ref items)) => { + ItemKind::Trait(box Trait { + unsafety: _, + is_auto: _, + 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); @@ -547,12 +553,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(box FnKind(_, sig, generics, body)) => { + ForeignItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref 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(box TyAliasKind(_, generics, bounds, ty)) => { + ForeignItemKind::TyAlias(box TyAlias { defaultness: _, generics, bounds, ty }) => { visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, ty); @@ -653,12 +659,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(box FnKind(_, sig, generics, body)) => { + AssocItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref 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(box TyAliasKind(_, generics, bounds, ty)) => { + AssocItemKind::TyAlias(box TyAlias { defaultness: _, generics, bounds, ty }) => { visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_ty, ty); diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 63b20cd320b37..6a4571cf6d278 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -49,7 +49,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { self.lctx.with_parent_item_lifetime_defs(hir_id, |this| { let this = &mut ItemLowerer { lctx: this }; match item.kind { - ItemKind::Impl(box ImplKind { ref of_trait, .. }) => { + ItemKind::Impl(box Impl { ref of_trait, .. }) => { this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item)); } _ => visit::walk_item(this, item), @@ -218,12 +218,12 @@ impl<'hir> LoweringContext<'_, 'hir> { let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); hir::ItemKind::Const(ty, body_id) } - ItemKind::Fn(box FnKind( - _, - FnSig { ref decl, header, span: fn_sig_span }, + ItemKind::Fn(box Fn { + sig: FnSig { ref decl, header, span: fn_sig_span }, ref generics, ref body, - )) => { + .. + }) => { let fn_def_id = self.resolver.local_def_id(id); self.with_new_scopes(|this| { this.current_item = Some(ident.span); @@ -273,7 +273,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ItemKind::GlobalAsm(ref asm) => { hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm)) } - ItemKind::TyAlias(box TyAliasKind(_, ref gen, _, Some(ref ty))) => { + ItemKind::TyAlias(box TyAlias { ref generics, ty: Some(ref ty), .. }) => { // We lower // // type Foo = impl Trait @@ -288,10 +288,10 @@ impl<'hir> LoweringContext<'_, 'hir> { capturable_lifetimes: &mut FxHashSet::default(), }, ); - let generics = self.lower_generics(gen, ImplTraitContext::disallowed()); + let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); hir::ItemKind::TyAlias(ty, generics) } - ItemKind::TyAlias(box TyAliasKind(_, ref generics, _, None)) => { + ItemKind::TyAlias(box TyAlias { ref generics, ty: None, .. }) => { let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err)); let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); hir::ItemKind::TyAlias(ty, generics) @@ -318,7 +318,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_generics(generics, ImplTraitContext::disallowed()), ) } - ItemKind::Impl(box ImplKind { + ItemKind::Impl(box Impl { unsafety, polarity, defaultness, @@ -384,13 +384,13 @@ impl<'hir> LoweringContext<'_, 'hir> { items: new_impl_items, }) } - ItemKind::Trait(box TraitKind( + ItemKind::Trait(box Trait { is_auto, unsafety, ref generics, ref bounds, ref items, - )) => { + }) => { let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed()); let items = self .arena @@ -655,7 +655,7 @@ impl<'hir> LoweringContext<'_, 'hir> { def_id, ident: self.lower_ident(i.ident), kind: match i.kind { - ForeignItemKind::Fn(box FnKind(_, ref sig, ref generics, _)) => { + ForeignItemKind::Fn(box Fn { ref sig, ref generics, .. }) => { let fdec = &sig.decl; let (generics, (fn_dec, fn_args)) = self.add_in_band_defs( generics, @@ -772,13 +772,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x))); (hir::Generics::empty(), hir::TraitItemKind::Const(ty, body)) } - AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, None)) => { + AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => { let names = self.lower_fn_params_to_names(&sig.decl); let (generics, sig) = self.lower_method_sig(generics, sig, trait_item_def_id, false, None); (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names))) } - AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, Some(ref body))) => { + AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => { let asyncness = sig.header.asyncness; let body_id = self.lower_maybe_async_body(i.span, &sig.decl, asyncness, Some(&body)); @@ -791,8 +791,8 @@ impl<'hir> LoweringContext<'_, 'hir> { ); (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id))) } - AssocItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref default)) => { - let ty = default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed())); + AssocItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => { + let ty = ty.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed())); let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); let kind = hir::TraitItemKind::Type( self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), @@ -818,11 +818,11 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef { let (kind, has_default) = match &i.kind { AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()), - AssocItemKind::TyAlias(box TyAliasKind(_, _, _, default)) => { - (hir::AssocItemKind::Type, default.is_some()) + AssocItemKind::TyAlias(box TyAlias { ty, .. }) => { + (hir::AssocItemKind::Type, ty.is_some()) } - AssocItemKind::Fn(box FnKind(_, sig, _, default)) => { - (hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }, default.is_some()) + AssocItemKind::Fn(box Fn { sig, body, .. }) => { + (hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }, body.is_some()) } AssocItemKind::MacCall(..) => unimplemented!(), }; @@ -853,7 +853,7 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())), ) } - AssocItemKind::Fn(box FnKind(_, sig, generics, body)) => { + AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => { self.current_item = Some(i.span); let asyncness = sig.header.asyncness; let body_id = @@ -869,7 +869,7 @@ impl<'hir> LoweringContext<'_, 'hir> { (generics, hir::ImplItemKind::Fn(sig, body_id)) } - AssocItemKind::TyAlias(box TyAliasKind(_, generics, _, ty)) => { + AssocItemKind::TyAlias(box TyAlias { generics, ty, .. }) => { let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); let kind = match ty { None => { @@ -920,7 +920,7 @@ impl<'hir> LoweringContext<'_, 'hir> { kind: match &i.kind { AssocItemKind::Const(..) => hir::AssocItemKind::Const, AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type, - AssocItemKind::Fn(box FnKind(_, sig, ..)) => { + AssocItemKind::Fn(box Fn { sig, .. }) => { hir::AssocItemKind::Fn { has_self: sig.decl.has_self() } } AssocItemKind::MacCall(..) => unimplemented!(), diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 793f6504be6f7..1822ba6ec9964 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1064,7 +1064,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } match item.kind { - ItemKind::Impl(box ImplKind { + ItemKind::Impl(box Impl { unsafety, polarity, defaultness: _, @@ -1111,7 +1111,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { }); return; // Avoid visiting again. } - ItemKind::Impl(box ImplKind { + ItemKind::Impl(box Impl { unsafety, polarity, defaultness, @@ -1152,8 +1152,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .emit(); } } - ItemKind::Fn(box FnKind(def, ref sig, ref generics, ref body)) => { - self.check_defaultness(item.span, def); + ItemKind::Fn(box Fn { defaultness, ref sig, ref generics, ref body }) => { + self.check_defaultness(item.span, defaultness); if body.is_none() { let msg = "free function without a body"; @@ -1195,19 +1195,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } } } - ItemKind::Trait(box TraitKind( - is_auto, - _, - ref generics, - ref bounds, - ref trait_items, - )) => { + ItemKind::Trait(box Trait { is_auto, ref generics, ref bounds, ref items, .. }) => { if is_auto == IsAuto::Yes { // Auto traits cannot have generics, super traits nor contain items. self.deny_generic_params(generics, item.ident.span); self.deny_super_traits(bounds, item.ident.span); self.deny_where_clause(&generics.where_clause, item.ident.span); - self.deny_items(trait_items, item.ident.span); + self.deny_items(items, item.ident.span); } self.no_questions_in_bounds(bounds, "supertraits", true); @@ -1217,7 +1211,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.visit_ident(item.ident); self.visit_generics(generics); self.with_banned_tilde_const(|this| walk_list!(this, visit_param_bound, bounds)); - walk_list!(self, visit_assoc_item, trait_items, AssocCtxt::Trait); + walk_list!(self, visit_assoc_item, items, AssocCtxt::Trait); walk_list!(self, visit_attribute, &item.attrs); return; } @@ -1278,9 +1272,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> { let msg = "free static item without body"; self.error_item_without_body(item.span, "static", msg, " = ;"); } - ItemKind::TyAlias(box TyAliasKind(def, _, ref bounds, ref body)) => { - self.check_defaultness(item.span, def); - if body.is_none() { + ItemKind::TyAlias(box TyAlias { defaultness, ref bounds, ref ty, .. }) => { + self.check_defaultness(item.span, defaultness); + if ty.is_none() { let msg = "free type alias without body"; self.error_item_without_body(item.span, "type", msg, " = ;"); } @@ -1294,15 +1288,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> { fn visit_foreign_item(&mut self, fi: &'a ForeignItem) { match &fi.kind { - ForeignItemKind::Fn(box FnKind(def, sig, _, body)) => { - self.check_defaultness(fi.span, *def); + ForeignItemKind::Fn(box Fn { defaultness, sig, body, .. }) => { + self.check_defaultness(fi.span, *defaultness); self.check_foreign_fn_bodyless(fi.ident, body.as_deref()); self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header); self.check_foreign_item_ascii_only(fi.ident); } - ForeignItemKind::TyAlias(box TyAliasKind(def, generics, bounds, body)) => { - self.check_defaultness(fi.span, *def); - self.check_foreign_kind_bodyless(fi.ident, "type", body.as_ref().map(|b| b.span)); + ForeignItemKind::TyAlias(box TyAlias { defaultness, generics, bounds, ty, .. }) => { + self.check_defaultness(fi.span, *defaultness); + self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span)); self.check_type_no_bounds(bounds, "`extern` blocks"); self.check_foreign_ty_genericless(generics); self.check_foreign_item_ascii_only(fi.ident); @@ -1587,11 +1581,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> { AssocItemKind::Const(_, _, body) => { self.check_impl_item_provided(item.span, body, "constant", " = ;"); } - AssocItemKind::Fn(box FnKind(_, _, _, body)) => { + AssocItemKind::Fn(box Fn { body, .. }) => { self.check_impl_item_provided(item.span, body, "function", " { }"); } - AssocItemKind::TyAlias(box TyAliasKind(_, _, bounds, body)) => { - self.check_impl_item_provided(item.span, body, "type", " = ;"); + AssocItemKind::TyAlias(box TyAlias { bounds, ty, .. }) => { + self.check_impl_item_provided(item.span, ty, "type", " = ;"); self.check_type_no_bounds(bounds, "`impl`s"); } _ => {} @@ -1600,7 +1594,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if ctxt == AssocCtxt::Trait || self.in_trait_impl { self.invalid_visibility(&item.vis, None); - if let AssocItemKind::Fn(box FnKind(_, sig, _, _)) = &item.kind { + if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind { self.check_trait_fn_not_const(sig.header.constness); self.check_trait_fn_not_async(item.span, sig.header.asyncness); } @@ -1611,7 +1605,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } match item.kind { - AssocItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref ty)) + AssocItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) if ctxt == AssocCtxt::Trait => { self.visit_vis(&item.vis); @@ -1623,7 +1617,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { }); walk_list!(self, visit_ty, ty); } - AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body)) + AssocItemKind::Fn(box Fn { ref sig, ref generics, ref body, .. }) if self.in_const_trait_impl || ctxt == AssocCtxt::Trait || matches!(sig.header.constness, Const::Yes(_)) => diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 91b4597a9bb1f..a421a563c4655 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -423,9 +423,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } - ast::ItemKind::Impl(box ast::ImplKind { - polarity, defaultness, ref of_trait, .. - }) => { + ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, ref of_trait, .. }) => { if let ast::ImplPolarity::Negative(span) = polarity { gate_feature_post!( &self, @@ -441,7 +439,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } - ast::ItemKind::Trait(box ast::TraitKind(ast::IsAuto::Yes, ..)) => { + ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => { gate_feature_post!( &self, auto_traits, @@ -459,7 +457,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { gate_feature_post!(&self, decl_macro, i.span, msg); } - ast::ItemKind::TyAlias(box ast::TyAliasKind(_, _, _, Some(ref ty))) => { + ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ref ty), .. }) => { self.check_impl_trait(&ty) } @@ -634,7 +632,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) { let is_fn = match i.kind { ast::AssocItemKind::Fn(_) => true, - ast::AssocItemKind::TyAlias(box ast::TyAliasKind(_, ref generics, _, ref ty)) => { + ast::AssocItemKind::TyAlias(box ast::TyAlias { ref generics, ref ty, .. }) => { if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) { gate_feature_post!( &self, diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 6d0589b7ba1af..ee369517aeba9 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1044,15 +1044,27 @@ impl<'a> State<'a> { self.maybe_print_comment(span.lo()); self.print_outer_attributes(attrs); match kind { - ast::ForeignItemKind::Fn(box ast::FnKind(def, sig, gen, body)) => { - self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs); + ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => { + self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs); } ast::ForeignItemKind::Static(ty, mutbl, body) => { let def = ast::Defaultness::Final; self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def); } - ast::ForeignItemKind::TyAlias(box ast::TyAliasKind(def, generics, bounds, ty)) => { - self.print_associated_type(ident, generics, bounds, ty.as_deref(), vis, *def); + ast::ForeignItemKind::TyAlias(box ast::TyAlias { + defaultness, + generics, + bounds, + ty, + }) => { + self.print_associated_type( + ident, + generics, + bounds, + ty.as_deref(), + vis, + *defaultness, + ); } ast::ForeignItemKind::MacCall(m) => { self.print_mac(m); @@ -1156,9 +1168,17 @@ impl<'a> State<'a> { ast::ItemKind::Const(def, ref ty, ref body) => { self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def); } - ast::ItemKind::Fn(box ast::FnKind(def, ref sig, ref gen, ref body)) => { + ast::ItemKind::Fn(box ast::Fn { defaultness, ref sig, ref generics, ref body }) => { let body = body.as_deref(); - self.print_fn_full(sig, item.ident, gen, &item.vis, def, body, &item.attrs); + self.print_fn_full( + sig, + item.ident, + generics, + &item.vis, + defaultness, + body, + &item.attrs, + ); } ast::ItemKind::Mod(unsafety, ref mod_kind) => { self.head(self.to_string(|s| { @@ -1203,9 +1223,21 @@ impl<'a> State<'a> { self.print_inline_asm(asm); self.end(); } - ast::ItemKind::TyAlias(box ast::TyAliasKind(def, ref generics, ref bounds, ref ty)) => { + ast::ItemKind::TyAlias(box ast::TyAlias { + defaultness, + ref generics, + ref bounds, + ref ty, + }) => { let ty = ty.as_deref(); - self.print_associated_type(item.ident, generics, bounds, ty, &item.vis, def); + self.print_associated_type( + item.ident, + generics, + bounds, + ty, + &item.vis, + defaultness, + ); } ast::ItemKind::Enum(ref enum_definition, ref params) => { self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis); @@ -1218,7 +1250,7 @@ impl<'a> State<'a> { self.head(visibility_qualified(&item.vis, "union")); self.print_struct(struct_def, generics, item.ident, item.span, true); } - ast::ItemKind::Impl(box ast::ImplKind { + ast::ItemKind::Impl(box ast::Impl { unsafety, polarity, defaultness, @@ -1261,13 +1293,14 @@ impl<'a> State<'a> { } self.bclose(item.span); } - ast::ItemKind::Trait(box ast::TraitKind( + ast::ItemKind::Trait(box ast::Trait { is_auto, unsafety, ref generics, ref bounds, - ref trait_items, - )) => { + ref items, + .. + }) => { self.head(""); self.print_visibility(&item.vis); self.print_unsafety(unsafety); @@ -1290,7 +1323,7 @@ impl<'a> State<'a> { self.s.word(" "); self.bopen(); self.print_inner_attributes(&item.attrs); - for trait_item in trait_items { + for trait_item in items { self.print_assoc_item(trait_item); } self.bclose(item.span); @@ -1483,14 +1516,21 @@ impl<'a> State<'a> { self.maybe_print_comment(span.lo()); self.print_outer_attributes(attrs); match kind { - ast::AssocItemKind::Fn(box ast::FnKind(def, sig, gen, body)) => { - self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs); + ast::AssocItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => { + self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs); } ast::AssocItemKind::Const(def, ty, body) => { self.print_item_const(ident, None, ty, body.as_deref(), vis, *def); } - ast::AssocItemKind::TyAlias(box ast::TyAliasKind(def, generics, bounds, ty)) => { - self.print_associated_type(ident, generics, bounds, ty.as_deref(), vis, *def); + ast::AssocItemKind::TyAlias(box ast::TyAlias { defaultness, generics, bounds, ty }) => { + self.print_associated_type( + ident, + generics, + bounds, + ty.as_deref(), + vis, + *defaultness, + ); } ast::AssocItemKind::MacCall(m) => { self.print_mac(m); diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index a225b328ab6a4..994a74a5a9b9f 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -557,12 +557,12 @@ impl<'a> TraitDef<'a> { tokens: None, }, attrs: Vec::new(), - kind: ast::AssocItemKind::TyAlias(Box::new(ast::TyAliasKind( - ast::Defaultness::Final, - Generics::default(), - Vec::new(), - Some(type_def.to_ty(cx, self.span, type_ident, generics)), - ))), + kind: ast::AssocItemKind::TyAlias(Box::new(ast::TyAlias { + defaultness: ast::Defaultness::Final, + generics: Generics::default(), + bounds: Vec::new(), + ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)), + })), tokens: None, }) }); @@ -726,7 +726,7 @@ impl<'a> TraitDef<'a> { self.span, Ident::empty(), a, - ast::ItemKind::Impl(Box::new(ast::ImplKind { + ast::ItemKind::Impl(Box::new(ast::Impl { unsafety, polarity: ast::ImplPolarity::Positive, defaultness: ast::Defaultness::Final, @@ -955,7 +955,7 @@ impl<'a> MethodDef<'a> { decl: fn_decl, span: trait_.span, }; - let def = ast::Defaultness::Final; + let defaultness = ast::Defaultness::Final; // Create the method. P(ast::AssocItem { @@ -968,12 +968,12 @@ impl<'a> MethodDef<'a> { tokens: None, }, ident: method_ident, - kind: ast::AssocItemKind::Fn(Box::new(ast::FnKind( - def, + kind: ast::AssocItemKind::Fn(Box::new(ast::Fn { + defaultness, sig, - fn_generics, - Some(body_block), - ))), + generics: fn_generics, + body: Some(body_block), + })), tokens: None, }) } diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs index fa389a5111578..367a5aa732370 100644 --- a/compiler/rustc_builtin_macros/src/deriving/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/mod.rs @@ -2,7 +2,7 @@ use rustc_ast as ast; use rustc_ast::ptr::P; -use rustc_ast::{ImplKind, ItemKind, MetaItem}; +use rustc_ast::{Impl, ItemKind, MetaItem}; use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier}; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::Span; @@ -180,7 +180,7 @@ fn inject_impl_of_structural_trait( span, Ident::empty(), attrs, - ItemKind::Impl(Box::new(ImplKind { + ItemKind::Impl(Box::new(Impl { unsafety: ast::Unsafe::No, polarity: ast::ImplPolarity::Positive, defaultness: ast::Defaultness::Final, diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs index 3f71ee6f489a0..a433876147f8d 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -5,7 +5,7 @@ use rustc_ast::expand::allocator::{ }; use rustc_ast::ptr::P; use rustc_ast::{self as ast, Attribute, Expr, FnHeader, FnSig, Generics, Param, StmtKind}; -use rustc_ast::{FnKind, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe}; +use rustc_ast::{Fn, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe}; use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::Span; @@ -84,13 +84,13 @@ impl AllocFnFactory<'_, '_> { let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty)); let header = FnHeader { unsafety: Unsafe::Yes(self.span), ..FnHeader::default() }; let sig = FnSig { decl, header, span: self.span }; - let block = Some(self.cx.block_expr(output_expr)); - let kind = ItemKind::Fn(Box::new(FnKind( - ast::Defaultness::Final, + let body = Some(self.cx.block_expr(output_expr)); + let kind = ItemKind::Fn(Box::new(Fn { + defaultness: ast::Defaultness::Final, sig, - Generics::default(), - block, - ))); + generics: Generics::default(), + body, + })); let item = self.cx.item( self.span, Ident::from_str_and_span(&self.kind.fn_name(method.name), self.span), diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index bbca07085ea36..d2629926b51da 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -429,7 +429,7 @@ fn test_type(cx: &ExtCtxt<'_>) -> TestType { fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { let has_should_panic_attr = cx.sess.contains_name(&i.attrs, sym::should_panic); let sd = &cx.sess.parse_sess.span_diagnostic; - if let ast::ItemKind::Fn(box ast::FnKind(_, ref sig, ref generics, _)) = i.kind { + if let ast::ItemKind::Fn(box ast::Fn { ref sig, ref generics, .. }) = i.kind { if let ast::Unsafe::Yes(span) = sig.header.unsafety { sd.struct_span_err(i.span, "unsafe functions cannot be used for tests") .span_label(span, "`unsafe` because of this") @@ -478,7 +478,7 @@ fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { } fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool { - let has_sig = if let ast::ItemKind::Fn(box ast::FnKind(_, ref sig, _, _)) = i.kind { + let has_sig = if let ast::ItemKind::Fn(box ast::Fn { ref sig, .. }) = i.kind { // N.B., inadequate check, but we're running // well before resolve, can't get too deep. sig.decl.inputs.len() == 1 diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index d791677cb8ee1..64ccd4331e58a 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -313,13 +313,13 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P { let decl = ecx.fn_decl(vec![], ast::FnRetTy::Ty(main_ret_ty)); let sig = ast::FnSig { decl, header: ast::FnHeader::default(), span: sp }; - let def = ast::Defaultness::Final; - let main = ast::ItemKind::Fn(Box::new(ast::FnKind( - def, + let defaultness = ast::Defaultness::Final; + let main = ast::ItemKind::Fn(Box::new(ast::Fn { + defaultness, sig, - ast::Generics::default(), - Some(main_body), - ))); + generics: ast::Generics::default(), + body: Some(main_body), + })); // Honor the reexport_test_harness_main attribute let main_id = match cx.reexport_test_harness_main { diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index cffb087af187f..946502378732a 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -776,7 +776,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> { fn visit_item_kind(&mut self, i: &mut ast::ItemKind) { let is_const = match i { ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true, - ast::ItemKind::Fn(box ast::FnKind(_, ref sig, _, _)) => Self::is_sig_const(sig), + ast::ItemKind::Fn(box ast::Fn { ref sig, .. }) => Self::is_sig_const(sig), _ => false, }; self.run(is_const, |s| noop_visit_item_kind(i, s)) @@ -785,7 +785,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> { fn flat_map_trait_item(&mut self, i: P) -> SmallVec<[P; 1]> { let is_const = match i.kind { ast::AssocItemKind::Const(..) => true, - ast::AssocItemKind::Fn(box ast::FnKind(_, ref sig, _, _)) => Self::is_sig_const(sig), + ast::AssocItemKind::Fn(box ast::Fn { ref sig, .. }) => Self::is_sig_const(sig), _ => false, }; self.run(is_const, |s| noop_flat_map_assoc_item(i, s)) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index c228ecb03fdec..a56177847be0a 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -369,12 +369,12 @@ impl EarlyLintPass for UnsafeCode { fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) { match it.kind { - ast::ItemKind::Trait(box ast::TraitKind(_, ast::Unsafe::Yes(_), ..)) => self + ast::ItemKind::Trait(box ast::Trait { unsafety: ast::Unsafe::Yes(_), .. }) => self .report_unsafe(cx, it.span, |lint| { lint.build("declaration of an `unsafe` trait").emit() }), - ast::ItemKind::Impl(box ast::ImplKind { unsafety: ast::Unsafe::Yes(_), .. }) => self + ast::ItemKind::Impl(box ast::Impl { unsafety: ast::Unsafe::Yes(_), .. }) => self .report_unsafe(cx, it.span, |lint| { lint.build("implementation of an `unsafe` trait").emit() }), @@ -921,7 +921,7 @@ impl EarlyLintPass for AnonymousParameters { // This is a hard error in future editions; avoid linting and erroring return; } - if let ast::AssocItemKind::Fn(box FnKind(_, ref sig, _, _)) = it.kind { + if let ast::AssocItemKind::Fn(box Fn { ref sig, .. }) = it.kind { for arg in sig.decl.inputs.iter() { if let ast::PatKind::Ident(_, ident, None) = arg.pat.kind { if ident.name == kw::Empty { diff --git a/compiler/rustc_lint/src/internal.rs b/compiler/rustc_lint/src/internal.rs index 50a0d211a366a..b726a0624179c 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -238,8 +238,7 @@ declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]); impl EarlyLintPass for LintPassImpl { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { - if let ast::ItemKind::Impl(box ast::ImplKind { of_trait: Some(lint_pass), .. }) = &item.kind - { + if let ast::ItemKind::Impl(box ast::Impl { of_trait: Some(lint_pass), .. }) = &item.kind { if let Some(last) = lint_pass.path.segments.last() { if last.ident.name == sym::LintPass { let expn_data = lint_pass.path.span.ctxt().outer_expn_data(); diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 95b74fd5306e5..eb0a693226c48 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -29,6 +29,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple}; use proc_macro::bridge::client::ProcMacro; use std::collections::BTreeMap; +use std::ops::Fn; use std::path::Path; use std::{cmp, env}; use tracing::{debug, info}; diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index d2167c7a5db0c..73ca809ab1d3a 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -220,7 +220,7 @@ impl<'a> Parser<'a> { } else if self.check_fn_front_matter(def_final) { // FUNCTION ITEM let (ident, sig, generics, body) = self.parse_fn(attrs, req_name, lo)?; - (ident, ItemKind::Fn(Box::new(FnKind(def(), sig, generics, body)))) + (ident, ItemKind::Fn(Box::new(Fn { defaultness: def(), sig, generics, body }))) } else if self.eat_keyword(kw::Extern) { if self.eat_keyword(kw::Crate) { // EXTERN CRATE @@ -560,7 +560,7 @@ impl<'a> Parser<'a> { }; let trait_ref = TraitRef { path, ref_id: ty_first.id }; - ItemKind::Impl(Box::new(ImplKind { + ItemKind::Impl(Box::new(Impl { unsafety, polarity, defaultness, @@ -573,7 +573,7 @@ impl<'a> Parser<'a> { } None => { // impl Type - ItemKind::Impl(Box::new(ImplKind { + ItemKind::Impl(Box::new(Impl { unsafety, polarity, defaultness, @@ -682,7 +682,7 @@ impl<'a> Parser<'a> { self.expect_keyword(kw::Trait)?; let ident = self.parse_ident()?; - let mut tps = self.parse_generics()?; + let mut generics = self.parse_generics()?; // Parse optional colon and supertrait bounds. let had_colon = self.eat(&token::Colon); @@ -702,7 +702,7 @@ impl<'a> Parser<'a> { } let bounds = self.parse_generic_bounds(None)?; - tps.where_clause = self.parse_where_clause()?; + generics.where_clause = self.parse_where_clause()?; self.expect_semi()?; let whole_span = lo.to(self.prev_token.span); @@ -717,12 +717,15 @@ impl<'a> Parser<'a> { self.sess.gated_spans.gate(sym::trait_alias, whole_span); - Ok((ident, ItemKind::TraitAlias(tps, bounds))) + Ok((ident, ItemKind::TraitAlias(generics, bounds))) } else { // It's a normal trait. - tps.where_clause = self.parse_where_clause()?; + generics.where_clause = self.parse_where_clause()?; let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?; - Ok((ident, ItemKind::Trait(Box::new(TraitKind(is_auto, unsafety, tps, bounds, items))))) + Ok(( + ident, + ItemKind::Trait(Box::new(Trait { is_auto, unsafety, generics, bounds, items })), + )) } } @@ -769,7 +772,7 @@ impl<'a> Parser<'a> { /// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ; /// ``` /// The `"type"` has already been eaten. - fn parse_type_alias(&mut self, def: Defaultness) -> PResult<'a, ItemInfo> { + fn parse_type_alias(&mut self, defaultness: Defaultness) -> PResult<'a, ItemInfo> { let ident = self.parse_ident()?; let mut generics = self.parse_generics()?; @@ -778,10 +781,10 @@ impl<'a> Parser<'a> { if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() }; generics.where_clause = self.parse_where_clause()?; - let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None }; + let ty = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None }; self.expect_semi()?; - Ok((ident, ItemKind::TyAlias(Box::new(TyAliasKind(def, generics, bounds, default))))) + Ok((ident, ItemKind::TyAlias(Box::new(TyAlias { defaultness, generics, bounds, ty })))) } /// Parses a `UseTree`. @@ -1039,9 +1042,7 @@ impl<'a> Parser<'a> { }; match impl_info.1 { - ItemKind::Impl(box ImplKind { - of_trait: Some(ref trai), ref mut constness, .. - }) => { + ItemKind::Impl(box Impl { of_trait: Some(ref trai), ref mut constness, .. }) => { *constness = Const::Yes(const_span); let before_trait = trai.path.span.shrink_to_lo(); diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 33af9884cbb66..d77a70e532799 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -15,7 +15,7 @@ use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError use rustc_ast::visit::{self, AssocCtxt, Visitor}; use rustc_ast::{self as ast, AssocItem, AssocItemKind, MetaItemKind, StmtKind}; -use rustc_ast::{Block, FnKind, ForeignItem, ForeignItemKind, ImplKind, Item, ItemKind, NodeId}; +use rustc_ast::{Block, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId}; use rustc_ast_lowering::ResolverAstLowering; use rustc_attr as attr; use rustc_data_structures::sync::Lrc; @@ -880,7 +880,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { } // These items do not add names to modules. - ItemKind::Impl(box ImplKind { of_trait: Some(..), .. }) => { + ItemKind::Impl(box Impl { of_trait: Some(..), .. }) => { self.r.trait_impl_items.insert(local_def_id); } ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {} @@ -1380,7 +1380,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> { if ctxt == AssocCtxt::Trait { let (def_kind, ns) = match item.kind { AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS), - AssocItemKind::Fn(box FnKind(_, ref sig, _, _)) => { + AssocItemKind::Fn(box Fn { ref sig, .. }) => { if sig.decl.has_self() { self.r.has_self.insert(def_id); } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 0a24e00ee4bf5..e67f7f0351680 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -498,8 +498,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> { } fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) { match foreign_item.kind { - ForeignItemKind::Fn(box FnKind(_, _, ref generics, _)) - | ForeignItemKind::TyAlias(box TyAliasKind(_, ref generics, ..)) => { + ForeignItemKind::Fn(box Fn { ref generics, .. }) + | ForeignItemKind::TyAlias(box TyAlias { ref generics, .. }) => { self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { visit::walk_foreign_item(this, foreign_item); }); @@ -953,8 +953,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { debug!("(resolving item) resolving {} ({:?})", name, item.kind); match item.kind { - ItemKind::TyAlias(box TyAliasKind(_, ref generics, _, _)) - | ItemKind::Fn(box FnKind(_, _, ref generics, _)) => { + ItemKind::TyAlias(box TyAlias { ref generics, .. }) + | ItemKind::Fn(box Fn { ref generics, .. }) => { self.compute_num_lifetime_params(item.id, generics); self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { visit::walk_item(this, item) @@ -968,7 +968,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { self.resolve_adt(item, generics); } - ItemKind::Impl(box ImplKind { + ItemKind::Impl(box Impl { ref generics, ref of_trait, ref self_ty, @@ -979,7 +979,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items); } - ItemKind::Trait(box TraitKind(.., ref generics, ref bounds, ref trait_items)) => { + ItemKind::Trait(box Trait { ref generics, ref bounds, ref items, .. }) => { self.compute_num_lifetime_params(item.id, generics); // Create a new rib for the trait-wide type parameters. self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| { @@ -994,8 +994,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }); }; - this.with_trait_items(trait_items, |this| { - for item in trait_items { + this.with_trait_items(items, |this| { + for item in items { match &item.kind { AssocItemKind::Const(_, ty, default) => { this.visit_ty(ty); @@ -1015,10 +1015,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ); } } - AssocItemKind::Fn(box FnKind(_, _, generics, _)) => { + AssocItemKind::Fn(box Fn { generics, .. }) => { walk_assoc_item(this, generics, item); } - AssocItemKind::TyAlias(box TyAliasKind(_, generics, _, _)) => { + AssocItemKind::TyAlias(box TyAlias { generics, .. }) => { walk_assoc_item(this, generics, item); } AssocItemKind::MacCall(_) => { @@ -1338,7 +1338,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }, ); } - AssocItemKind::Fn(box FnKind(.., generics, _)) => { + AssocItemKind::Fn(box Fn { generics, .. }) => { debug!("resolve_implementation AssocItemKind::Fn"); // We also need a new scope for the impl item type parameters. this.with_generic_param_rib( @@ -1363,12 +1363,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }, ); } - AssocItemKind::TyAlias(box TyAliasKind( - _, - generics, - _, - _, - )) => { + AssocItemKind::TyAlias(box TyAlias { + generics, .. + }) => { debug!("resolve_implementation AssocItemKind::TyAlias"); // We also need a new scope for the impl item type parameters. this.with_generic_param_rib( diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index b51e93a429dff..1d5b36155f46c 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1235,9 +1235,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { if assoc_item.ident == ident { return Some(match &assoc_item.kind { ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst, - ast::AssocItemKind::Fn(box ast::FnKind(_, sig, ..)) - if sig.decl.has_self() => - { + ast::AssocItemKind::Fn(box ast::Fn { sig, .. }) if sig.decl.has_self() => { AssocSuggestion::MethodWithSelf } ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn, diff --git a/src/test/run-make-fulldeps/print-cfg/Makefile b/src/test/run-make-fulldeps/print-cfg/Makefile index 013bf3baca45f..5472baae3f2b0 100644 --- a/src/test/run-make-fulldeps/print-cfg/Makefile +++ b/src/test/run-make-fulldeps/print-cfg/Makefile @@ -1,3 +1,5 @@ +# needs-llvm-components: x86 arm + -include ../tools.mk all: default diff --git a/src/tools/clippy/clippy_lints/src/doc.rs b/src/tools/clippy/clippy_lints/src/doc.rs index 87ad5178ff088..d4ba072807f8f 100644 --- a/src/tools/clippy/clippy_lints/src/doc.rs +++ b/src/tools/clippy/clippy_lints/src/doc.rs @@ -5,7 +5,7 @@ use clippy_utils::ty::{implements_trait, is_type_diagnostic_item}; use clippy_utils::{is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty}; use if_chain::if_chain; use itertools::Itertools; -use rustc_ast::ast::{Async, AttrKind, Attribute, FnKind, FnRetTy, ItemKind}; +use rustc_ast::ast::{Async, AttrKind, Attribute, Fn, FnRetTy, ItemKind}; use rustc_ast::token::CommentKind; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::Lrc; @@ -639,7 +639,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) { | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) => return false, // We found a main function ... - ItemKind::Fn(box FnKind(_, sig, _, Some(block))) if item.ident.name == sym::main => { + ItemKind::Fn(box Fn { sig, body: Some(block), .. }) if item.ident.name == sym::main => { let is_async = matches!(sig.header.asyncness, Async::Yes { .. }); let returns_nothing = match &sig.decl.output { FnRetTy::Default(..) => true, diff --git a/src/tools/clippy/clippy_lints/src/excessive_bools.rs b/src/tools/clippy/clippy_lints/src/excessive_bools.rs index 476e6d23f1215..09b6e20083889 100644 --- a/src/tools/clippy/clippy_lints/src/excessive_bools.rs +++ b/src/tools/clippy/clippy_lints/src/excessive_bools.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::in_macro; -use rustc_ast::ast::{AssocItemKind, Extern, FnKind, FnSig, ImplKind, Item, ItemKind, TraitKind, Ty, TyKind}; +use rustc_ast::ast::{AssocItemKind, Extern, Fn, FnSig, Impl, Item, ItemKind, Trait, Ty, TyKind}; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{sym, Span}; @@ -162,17 +162,17 @@ impl EarlyLintPass for ExcessiveBools { ); } }, - ItemKind::Impl(box ImplKind { + ItemKind::Impl(box Impl { of_trait: None, items, .. }) - | ItemKind::Trait(box TraitKind(.., items)) => { + | ItemKind::Trait(box Trait { items, .. }) => { for item in items { - if let AssocItemKind::Fn(box FnKind(_, fn_sig, _, _)) = &item.kind { - self.check_fn_sig(cx, fn_sig, item.span); + if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind { + self.check_fn_sig(cx, sig, item.span); } } }, - ItemKind::Fn(box FnKind(_, fn_sig, _, _)) => self.check_fn_sig(cx, fn_sig, item.span), + ItemKind::Fn(box Fn { sig, .. }) => self.check_fn_sig(cx, sig, item.span), _ => (), } } diff --git a/src/tools/clippy/clippy_lints/src/non_expressive_names.rs b/src/tools/clippy/clippy_lints/src/non_expressive_names.rs index 5b254bc8133d2..e28cc49bf2a1a 100644 --- a/src/tools/clippy/clippy_lints/src/non_expressive_names.rs +++ b/src/tools/clippy/clippy_lints/src/non_expressive_names.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; use rustc_ast::ast::{ - Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, FnKind, Item, ItemKind, Local, Pat, PatKind, + self, Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, Pat, PatKind, }; use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor}; use rustc_lint::{EarlyContext, EarlyLintPass}; @@ -357,7 +357,7 @@ impl EarlyLintPass for NonExpressiveNames { return; } - if let ItemKind::Fn(box FnKind(_, ref sig, _, Some(ref blk))) = item.kind { + if let ItemKind::Fn(box ast::Fn { ref sig, body: Some(ref blk), .. }) = item.kind { do_check(self, cx, &item.attrs, &sig.decl, blk); } } @@ -367,7 +367,7 @@ impl EarlyLintPass for NonExpressiveNames { return; } - if let AssocItemKind::Fn(box FnKind(_, ref sig, _, Some(ref blk))) = item.kind { + if let AssocItemKind::Fn(box ast::Fn { ref sig, body: Some(ref blk), .. }) = item.kind { do_check(self, cx, &item.attrs, &sig.decl, blk); } } diff --git a/src/tools/clippy/clippy_lints/src/write.rs b/src/tools/clippy/clippy_lints/src/write.rs index 85d1f65c51f09..b412e15ae4f82 100644 --- a/src/tools/clippy/clippy_lints/src/write.rs +++ b/src/tools/clippy/clippy_lints/src/write.rs @@ -4,7 +4,7 @@ use std::ops::{Deref, Range}; use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then}; use clippy_utils::source::{snippet_opt, snippet_with_applicability}; -use rustc_ast::ast::{Expr, ExprKind, ImplKind, Item, ItemKind, MacCall, Path, StrLit, StrStyle}; +use rustc_ast::ast::{Expr, ExprKind, Impl, Item, ItemKind, MacCall, Path, StrLit, StrStyle}; use rustc_ast::token::{self, LitKind}; use rustc_ast::tokenstream::TokenStream; use rustc_errors::Applicability; @@ -243,7 +243,7 @@ impl_lint_pass!(Write => [ impl EarlyLintPass for Write { fn check_item(&mut self, _: &EarlyContext<'_>, item: &Item) { - if let ItemKind::Impl(box ImplKind { + if let ItemKind::Impl(box Impl { of_trait: Some(trait_ref), .. }) = &item.kind diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 2fa98831c7740..1b05a8a35046e 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -250,7 +250,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { (Use(l), Use(r)) => eq_use_tree(l, r), (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), - (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => { + (Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }), + Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => { eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) }, (Mod(lu, lmk), Mod(ru, rmk)) => { @@ -266,7 +267,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { (ForeignMod(l), ForeignMod(r)) => { both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind)) }, - (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => { + (TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }), + TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => { eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && over(lb, rb, eq_generic_bound) @@ -276,7 +278,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => { eq_variant_data(lv, rv) && eq_generics(lg, rg) }, - (Trait(box TraitKind(la, lu, lg, lb, li)), Trait(box TraitKind(ra, ru, rg, rb, ri))) => { + (Trait(box ast::Trait { is_auto: la, unsafety: lu, generics: lg, bounds: lb, items: li }), + Trait(box ast::Trait { is_auto: ra, unsafety: ru, generics: rg, bounds: rb, items: ri })) => { la == ra && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No) && eq_generics(lg, rg) @@ -285,7 +288,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { }, (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound), ( - Impl(box ImplKind { + Impl(box ast::Impl { unsafety: lu, polarity: lp, defaultness: ld, @@ -295,7 +298,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { self_ty: lst, items: li, }), - Impl(box ImplKind { + Impl(box ast::Impl { unsafety: ru, polarity: rp, defaultness: rd, @@ -325,10 +328,12 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool { use ForeignItemKind::*; match (l, r) { (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), - (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => { + (Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }), + Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => { eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) }, - (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => { + (TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }), + TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => { eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && over(lb, rb, eq_generic_bound) @@ -343,10 +348,12 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool { use AssocItemKind::*; match (l, r) { (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), - (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => { + (Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }), + Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => { eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r)) }, - (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => { + (TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }), + TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => { eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && over(lb, rb, eq_generic_bound) diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 1cb1a2701c36b..c828798d93311 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -622,7 +622,7 @@ impl<'a> FmtVisitor<'a> { fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool { match (a, b) { (TyAlias(lty), TyAlias(rty)) - if both_type(<y.3, &rty.3) || both_opaque(<y.3, &rty.3) => + if both_type(<y.ty, &rty.ty) || both_opaque(<y.ty, &rty.ty) => { false } @@ -633,7 +633,7 @@ impl<'a> FmtVisitor<'a> { buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) { (TyAlias(lty), TyAlias(rty)) - if both_type(<y.3, &rty.3) || both_opaque(<y.3, &rty.3) => + if both_type(<y.ty, &rty.ty) || both_opaque(<y.ty, &rty.ty) => { a.ident.as_str().cmp(&b.ident.as_str()) } @@ -641,8 +641,8 @@ impl<'a> FmtVisitor<'a> { a.ident.as_str().cmp(&b.ident.as_str()) } (Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()), - (TyAlias(ty), _) if is_type(&ty.3) => Ordering::Less, - (_, TyAlias(ty)) if is_type(&ty.3) => Ordering::Greater, + (TyAlias(ty), _) if is_type(&ty.ty) => Ordering::Less, + (_, TyAlias(ty)) if is_type(&ty.ty) => Ordering::Greater, (TyAlias(..), _) => Ordering::Less, (_, TyAlias(..)) => Ordering::Greater, (Const(..), _) => Ordering::Less, @@ -679,7 +679,7 @@ pub(crate) fn format_impl( offset: Indent, ) -> Option { if let ast::ItemKind::Impl(impl_kind) = &item.kind { - let ast::ImplKind { + let ast::Impl { ref generics, ref self_ty, ref items, @@ -833,7 +833,7 @@ fn format_impl_ref_and_type( offset: Indent, ) -> Option { if let ast::ItemKind::Impl(impl_kind) = &item.kind { - let ast::ImplKind { + let ast::Impl { unsafety, polarity, defaultness, @@ -1029,8 +1029,13 @@ pub(crate) fn format_trait( offset: Indent, ) -> Option { if let ast::ItemKind::Trait(trait_kind) = &item.kind { - let ast::TraitKind(is_auto, unsafety, ref generics, ref generic_bounds, ref trait_items) = - **trait_kind; + let ast::Trait { + is_auto, + unsafety, + ref generics, + ref bounds, + ref items, + } = **trait_kind; let mut result = String::with_capacity(128); let header = format!( "{}{}{}trait ", @@ -1048,11 +1053,11 @@ pub(crate) fn format_trait( result.push_str(&generics_str); // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds. - if !generic_bounds.is_empty() { + if !bounds.is_empty() { let ident_hi = context .snippet_provider .span_after(item.span, &item.ident.as_str()); - let bound_hi = generic_bounds.last().unwrap().span().hi(); + let bound_hi = bounds.last().unwrap().span().hi(); let snippet = context.snippet(mk_sp(ident_hi, bound_hi)); if contains_comment(snippet) { return None; @@ -1061,7 +1066,7 @@ pub(crate) fn format_trait( result = rewrite_assign_rhs_with( context, result + ":", - generic_bounds, + bounds, shape, RhsTactics::ForceNextLineWithoutIndent, )?; @@ -1072,10 +1077,10 @@ pub(crate) fn format_trait( let where_on_new_line = context.config.indent_style() != IndentStyle::Block; let where_budget = context.budget(last_line_width(&result)); - let pos_before_where = if generic_bounds.is_empty() { + let pos_before_where = if bounds.is_empty() { generics.where_clause.span.lo() } else { - generic_bounds[generic_bounds.len() - 1].span().hi() + bounds[bounds.len() - 1].span().hi() }; let option = WhereClauseOption::snuggled(&generics_str); let where_clause_str = rewrite_where_clause( @@ -1134,7 +1139,7 @@ pub(crate) fn format_trait( BraceStyle::PreferSameLine => result.push(' '), BraceStyle::SameLineWhere => { if result.contains('\n') - || (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty()) + || (!generics.where_clause.predicates.is_empty() && !items.is_empty()) { result.push_str(&offset.to_string_with_newline(context.config)); } else { @@ -1149,12 +1154,12 @@ pub(crate) fn format_trait( let open_pos = snippet.find_uncommented("{")? + 1; let outer_indent_str = offset.block_only().to_string_with_newline(context.config); - if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) { + if !items.is_empty() || contains_comment(&snippet[open_pos..]) { let mut visitor = FmtVisitor::from_context(context); visitor.block_indent = offset.block_only().block_indent(context.config); visitor.last_pos = block_span.lo() + BytePos(open_pos as u32); - for item in trait_items { + for item in items { visitor.visit_trait_item(item); } @@ -3125,17 +3130,22 @@ impl Rewrite for ast::ForeignItem { let item_str = match self.kind { ast::ForeignItemKind::Fn(ref fn_kind) => { - let ast::FnKind(defaultness, ref fn_sig, ref generics, ref block) = **fn_kind; - if let Some(ref body) = block { + let ast::Fn { + defaultness, + ref sig, + ref generics, + ref body, + } = **fn_kind; + if let Some(ref body) = body { let mut visitor = FmtVisitor::from_context(context); visitor.block_indent = shape.indent; visitor.last_pos = self.span.lo(); let inner_attrs = inner_attributes(&self.attrs); let fn_ctxt = visit::FnCtxt::Foreign; visitor.visit_fn( - visit::FnKind::Fn(fn_ctxt, self.ident, &fn_sig, &self.vis, Some(body)), + visit::FnKind::Fn(fn_ctxt, self.ident, &sig, &self.vis, Some(body)), generics, - &fn_sig.decl, + &sig.decl, self.span, defaultness, Some(&inner_attrs), @@ -3146,7 +3156,7 @@ impl Rewrite for ast::ForeignItem { context, shape.indent, self.ident, - &FnSig::from_method_sig(&fn_sig, generics, &self.vis), + &FnSig::from_method_sig(&sig, generics, &self.vis), span, FnBraceStyle::None, ) @@ -3168,16 +3178,20 @@ impl Rewrite for ast::ForeignItem { rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";") } ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => { - let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) = - **ty_alias_kind; + let ast::TyAlias { + ref generics, + ref bounds, + ref ty, + .. + } = **ty_alias_kind; rewrite_type( &context, shape.indent, self.ident, &self.vis, generics, - Some(generic_bounds), - type_default.as_ref(), + Some(bounds), + ty.as_ref(), self.span, ) } diff --git a/src/tools/rustfmt/src/visitor.rs b/src/tools/rustfmt/src/visitor.rs index d854d90b40b6d..2cfd4e3f15c58 100644 --- a/src/tools/rustfmt/src/visitor.rs +++ b/src/tools/rustfmt/src/visitor.rs @@ -540,24 +540,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.visit_static(&StaticParts::from_item(item)); } ast::ItemKind::Fn(ref fn_kind) => { - let ast::FnKind(defaultness, ref fn_signature, ref generics, ref block) = - **fn_kind; - if let Some(ref body) = block { + let ast::Fn { + defaultness, + ref sig, + ref generics, + ref body, + } = **fn_kind; + if let Some(ref body) = body { let inner_attrs = inner_attributes(&item.attrs); - let fn_ctxt = match fn_signature.header.ext { + let fn_ctxt = match sig.header.ext { ast::Extern::None => visit::FnCtxt::Free, _ => visit::FnCtxt::Foreign, }; self.visit_fn( - visit::FnKind::Fn( - fn_ctxt, - item.ident, - &fn_signature, - &item.vis, - Some(body), - ), + visit::FnKind::Fn(fn_ctxt, item.ident, &sig, &item.vis, Some(body)), generics, - &fn_signature.decl, + &sig.decl, item.span, defaultness, Some(&inner_attrs), @@ -565,19 +563,18 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } else { let indent = self.block_indent; let rewrite = self.rewrite_required_fn( - indent, - item.ident, - &fn_signature, - &item.vis, - generics, - item.span, + indent, item.ident, &sig, &item.vis, generics, item.span, ); self.push_rewrite(item.span, rewrite); } } ast::ItemKind::TyAlias(ref alias_kind) => { - let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref ty) = - **alias_kind; + let ast::TyAlias { + ref generics, + ref bounds, + ref ty, + .. + } = **alias_kind; match ty { Some(ty) => { let rewrite = rewrite_type( @@ -586,7 +583,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { item.ident, &item.vis, generics, - Some(generic_bounds), + Some(bounds), Some(&*ty), item.span, ); @@ -597,7 +594,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { &self.get_context(), self.block_indent, item.ident, - generic_bounds, + bounds, generics, &item.vis, item.span, @@ -639,8 +636,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { match ti.kind { ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)), ast::AssocItemKind::Fn(ref fn_kind) => { - let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind; - if let Some(ref body) = block { + let ast::Fn { + defaultness, + ref sig, + ref generics, + ref body, + } = **fn_kind; + if let Some(ref body) = body { let inner_attrs = inner_attributes(&ti.attrs); let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait); self.visit_fn( @@ -659,16 +661,20 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } } ast::AssocItemKind::TyAlias(ref ty_alias_kind) => { - let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) = - **ty_alias_kind; + let ast::TyAlias { + ref generics, + ref bounds, + ref ty, + .. + } = **ty_alias_kind; let rewrite = rewrite_type( &self.get_context(), self.block_indent, ti.ident, &ti.vis, generics, - Some(generic_bounds), - type_default.as_ref(), + Some(bounds), + ty.as_ref(), ti.span, ); self.push_rewrite(ti.span, rewrite); @@ -689,8 +695,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { match ii.kind { ast::AssocItemKind::Fn(ref fn_kind) => { - let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind; - if let Some(ref body) = block { + let ast::Fn { + defaultness, + ref sig, + ref generics, + ref body, + } = **fn_kind; + if let Some(ref body) = body { let inner_attrs = inner_attributes(&ii.attrs); let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Impl); self.visit_fn( @@ -710,7 +721,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)), ast::AssocItemKind::TyAlias(ref ty_alias_kind) => { - let ast::TyAliasKind(defaultness, ref generics, _, ref ty) = **ty_alias_kind; + let ast::TyAlias { + defaultness, + ref generics, + ref ty, + .. + } = **ty_alias_kind; self.push_rewrite( ii.span, rewrite_impl_type(