From 574ba831d426b7c72279906a3dfd38ae5af53dce Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 09:47:59 +1000 Subject: [PATCH 01/14] Avoid repeating qualifiers on `static_assert_size` calls. Some of these don't need a `use` statement because there is already a `#[macro_use] extern crate rustc_data_structures` item in the crate. --- compiler/rustc_ast/src/ast.rs | 35 ++++++++++--------- .../rustc_const_eval/src/interpret/operand.rs | 9 ++--- .../rustc_const_eval/src/interpret/place.rs | 11 +++--- compiler/rustc_hir/src/hir.rs | 24 ++++++------- compiler/rustc_middle/src/thir.rs | 8 ++--- src/librustdoc/clean/types.rs | 19 +++++----- 6 files changed, 55 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 870a7c0be33dc..5e8fd508f56fb 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -3030,22 +3030,23 @@ pub type ForeignItem = Item; #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] mod size_asserts { use super::*; + use rustc_data_structures::static_assert_size; // These are in alphabetical order, which is easy to maintain. - rustc_data_structures::static_assert_size!(AssocItemKind, 72); - rustc_data_structures::static_assert_size!(Attribute, 152); - rustc_data_structures::static_assert_size!(Block, 48); - rustc_data_structures::static_assert_size!(Expr, 104); - rustc_data_structures::static_assert_size!(Fn, 192); - rustc_data_structures::static_assert_size!(ForeignItemKind, 72); - rustc_data_structures::static_assert_size!(GenericBound, 88); - rustc_data_structures::static_assert_size!(Generics, 72); - rustc_data_structures::static_assert_size!(Impl, 200); - rustc_data_structures::static_assert_size!(Item, 200); - rustc_data_structures::static_assert_size!(ItemKind, 112); - rustc_data_structures::static_assert_size!(Lit, 48); - rustc_data_structures::static_assert_size!(Pat, 120); - rustc_data_structures::static_assert_size!(Path, 40); - rustc_data_structures::static_assert_size!(PathSegment, 24); - rustc_data_structures::static_assert_size!(Stmt, 32); - rustc_data_structures::static_assert_size!(Ty, 96); + static_assert_size!(AssocItemKind, 72); + static_assert_size!(Attribute, 152); + static_assert_size!(Block, 48); + static_assert_size!(Expr, 104); + static_assert_size!(Fn, 192); + static_assert_size!(ForeignItemKind, 72); + static_assert_size!(GenericBound, 88); + static_assert_size!(Generics, 72); + static_assert_size!(Impl, 200); + static_assert_size!(Item, 200); + static_assert_size!(ItemKind, 112); + static_assert_size!(Lit, 48); + static_assert_size!(Pat, 120); + static_assert_size!(Path, 40); + static_assert_size!(PathSegment, 24); + static_assert_size!(Stmt, 32); + static_assert_size!(Ty, 96); } diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 94ba62c160c98..fe80a55dfd28c 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -823,9 +823,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] mod size_asserts { use super::*; + use rustc_data_structures::static_assert_size; // These are in alphabetical order, which is easy to maintain. - rustc_data_structures::static_assert_size!(Immediate, 56); - rustc_data_structures::static_assert_size!(ImmTy<'_>, 72); - rustc_data_structures::static_assert_size!(Operand, 64); - rustc_data_structures::static_assert_size!(OpTy<'_>, 88); + static_assert_size!(Immediate, 56); + static_assert_size!(ImmTy<'_>, 72); + static_assert_size!(Operand, 64); + static_assert_size!(OpTy<'_>, 88); } diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index f4571a1ca3d8a..97fe23cb5bcf3 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -891,10 +891,11 @@ where #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] mod size_asserts { use super::*; + use rustc_data_structures::static_assert_size; // These are in alphabetical order, which is easy to maintain. - rustc_data_structures::static_assert_size!(MemPlaceMeta, 24); - rustc_data_structures::static_assert_size!(MemPlace, 40); - rustc_data_structures::static_assert_size!(MPlaceTy<'_>, 64); - rustc_data_structures::static_assert_size!(Place, 48); - rustc_data_structures::static_assert_size!(PlaceTy<'_>, 72); + static_assert_size!(MemPlaceMeta, 24); + static_assert_size!(MemPlace, 40); + static_assert_size!(MPlaceTy<'_>, 64); + static_assert_size!(Place, 48); + static_assert_size!(PlaceTy<'_>, 72); } diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 617433a9803d5..7a87a3e488260 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -3491,16 +3491,16 @@ impl<'hir> Node<'hir> { mod size_asserts { use super::*; // These are in alphabetical order, which is easy to maintain. - rustc_data_structures::static_assert_size!(Block<'static>, 48); - rustc_data_structures::static_assert_size!(Expr<'static>, 56); - rustc_data_structures::static_assert_size!(ForeignItem<'static>, 72); - rustc_data_structures::static_assert_size!(GenericBound<'_>, 48); - rustc_data_structures::static_assert_size!(Generics<'static>, 56); - rustc_data_structures::static_assert_size!(ImplItem<'static>, 88); - rustc_data_structures::static_assert_size!(Impl<'static>, 80); - rustc_data_structures::static_assert_size!(Item<'static>, 80); - rustc_data_structures::static_assert_size!(Pat<'static>, 88); - rustc_data_structures::static_assert_size!(QPath<'static>, 24); - rustc_data_structures::static_assert_size!(TraitItem<'static>, 96); - rustc_data_structures::static_assert_size!(Ty<'static>, 72); + static_assert_size!(Block<'static>, 48); + static_assert_size!(Expr<'static>, 56); + static_assert_size!(ForeignItem<'static>, 72); + static_assert_size!(GenericBound<'_>, 48); + static_assert_size!(Generics<'static>, 56); + static_assert_size!(ImplItem<'static>, 88); + static_assert_size!(Impl<'static>, 80); + static_assert_size!(Item<'static>, 80); + static_assert_size!(Pat<'static>, 88); + static_assert_size!(QPath<'static>, 24); + static_assert_size!(TraitItem<'static>, 96); + static_assert_size!(Ty<'static>, 72); } diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index b856af1d8f835..5e26a52900eaf 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -814,8 +814,8 @@ impl<'tcx> fmt::Display for Pat<'tcx> { mod size_asserts { use super::*; // These are in alphabetical order, which is easy to maintain. - rustc_data_structures::static_assert_size!(Block, 56); - rustc_data_structures::static_assert_size!(Expr<'_>, 104); - rustc_data_structures::static_assert_size!(Pat<'_>, 24); - rustc_data_structures::static_assert_size!(Stmt<'_>, 120); + static_assert_size!(Block, 56); + static_assert_size!(Expr<'_>, 104); + static_assert_size!(Pat<'_>, 24); + static_assert_size!(Stmt<'_>, 120); } diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 0e6de842cc267..91d5758077c94 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -2495,14 +2495,15 @@ impl SubstParam { #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] mod size_asserts { use super::*; + use rustc_data_structures::static_assert_size; // These are in alphabetical order, which is easy to maintain. - rustc_data_structures::static_assert_size!(Crate, 72); // frequently moved by-value - rustc_data_structures::static_assert_size!(DocFragment, 32); - rustc_data_structures::static_assert_size!(GenericArg, 80); - rustc_data_structures::static_assert_size!(GenericArgs, 32); - rustc_data_structures::static_assert_size!(GenericParamDef, 56); - rustc_data_structures::static_assert_size!(Item, 56); - rustc_data_structures::static_assert_size!(ItemKind, 112); - rustc_data_structures::static_assert_size!(PathSegment, 40); - rustc_data_structures::static_assert_size!(Type, 72); + static_assert_size!(Crate, 72); // frequently moved by-value + static_assert_size!(DocFragment, 32); + static_assert_size!(GenericArg, 80); + static_assert_size!(GenericArgs, 32); + static_assert_size!(GenericParamDef, 56); + static_assert_size!(Item, 56); + static_assert_size!(ItemKind, 112); + static_assert_size!(PathSegment, 40); + static_assert_size!(Type, 72); } From 0658e8cbcaa6f5d3952a0958ab7657fc8bba32d9 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 10:06:38 +1000 Subject: [PATCH 02/14] Add a couple more AST node size assertions. --- compiler/rustc_ast/src/ast.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 5e8fd508f56fb..28862dbb2678b 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -3032,11 +3032,13 @@ mod size_asserts { use super::*; use rustc_data_structures::static_assert_size; // These are in alphabetical order, which is easy to maintain. + static_assert_size!(AssocItem, 160); static_assert_size!(AssocItemKind, 72); static_assert_size!(Attribute, 152); static_assert_size!(Block, 48); static_assert_size!(Expr, 104); static_assert_size!(Fn, 192); + static_assert_size!(ForeignItem, 160); static_assert_size!(ForeignItemKind, 72); static_assert_size!(GenericBound, 88); static_assert_size!(Generics, 72); From 8ec3d0dece4c1342d08186e210fa289eaf96c765 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 11:22:01 +1000 Subject: [PATCH 03/14] Simplify `rustc_hir::intravisit::Visitor::visit_variant_data`. It has four arguments that are never used. This avoids lots of argument passing in functions that feed into `visit_variant_data`. --- compiler/rustc_ast_lowering/src/index.rs | 4 +- compiler/rustc_hir/src/intravisit.rs | 51 +++++------------------- compiler/rustc_lint/src/late.rs | 19 ++------- compiler/rustc_lint/src/levels.rs | 9 +---- compiler/rustc_mir_transform/src/lib.rs | 10 +---- compiler/rustc_passes/src/check_attr.rs | 9 +---- compiler/rustc_passes/src/dead.rs | 9 +---- compiler/rustc_passes/src/hir_stats.rs | 9 +---- compiler/rustc_passes/src/stability.rs | 10 ++--- compiler/rustc_privacy/src/lib.rs | 9 +---- src/librustdoc/doctest.rs | 9 +---- 11 files changed, 32 insertions(+), 116 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index d5af74d47fd03..e08c1d063c105 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -295,14 +295,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime)); } - fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) { + fn visit_variant(&mut self, v: &'hir Variant<'hir>) { self.insert(v.span, v.id, Node::Variant(v)); self.with_parent(v.id, |this| { // Register the constructor of this variant. if let Some(ctor_hir_id) = v.data.ctor_hir_id() { this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data)); } - intravisit::walk_variant(this, v, g, item_id); + intravisit::walk_variant(this, v); }); } diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index e676acebe3598..cd283e7651b30 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -385,30 +385,17 @@ pub trait Visitor<'v>: Sized { fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef<'v>, m: TraitBoundModifier) { walk_poly_trait_ref(self, t, m) } - fn visit_variant_data( - &mut self, - s: &'v VariantData<'v>, - _: Symbol, - _: &'v Generics<'v>, - _parent_id: HirId, - _: Span, - ) { + fn visit_variant_data(&mut self, s: &'v VariantData<'v>) { walk_struct_def(self, s) } fn visit_field_def(&mut self, s: &'v FieldDef<'v>) { walk_field_def(self, s) } - fn visit_enum_def( - &mut self, - enum_definition: &'v EnumDef<'v>, - generics: &'v Generics<'v>, - item_id: HirId, - _: Span, - ) { - walk_enum_def(self, enum_definition, generics, item_id) + fn visit_enum_def(&mut self, enum_definition: &'v EnumDef<'v>, item_id: HirId, _: Span) { + walk_enum_def(self, enum_definition, item_id) } - fn visit_variant(&mut self, v: &'v Variant<'v>, g: &'v Generics<'v>, item_id: HirId) { - walk_variant(self, v, g, item_id) + fn visit_variant(&mut self, v: &'v Variant<'v>) { + walk_variant(self, v) } fn visit_label(&mut self, label: &'v Label) { walk_label(self, label) @@ -572,7 +559,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { ItemKind::Enum(ref enum_definition, ref generics) => { visitor.visit_generics(generics); // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`. - visitor.visit_enum_def(enum_definition, generics, item.hir_id(), item.span) + visitor.visit_enum_def(enum_definition, item.hir_id(), item.span) } ItemKind::Impl(Impl { unsafety: _, @@ -595,13 +582,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { | ItemKind::Union(ref struct_definition, ref generics) => { visitor.visit_generics(generics); visitor.visit_id(item.hir_id()); - visitor.visit_variant_data( - struct_definition, - item.ident.name, - generics, - item.hir_id(), - item.span, - ); + visitor.visit_variant_data(struct_definition); } ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => { visitor.visit_id(item.hir_id()); @@ -649,28 +630,16 @@ pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>, hir_id: pub fn walk_enum_def<'v, V: Visitor<'v>>( visitor: &mut V, enum_definition: &'v EnumDef<'v>, - generics: &'v Generics<'v>, item_id: HirId, ) { visitor.visit_id(item_id); - walk_list!(visitor, visit_variant, enum_definition.variants, generics, item_id); + walk_list!(visitor, visit_variant, enum_definition.variants); } -pub fn walk_variant<'v, V: Visitor<'v>>( - visitor: &mut V, - variant: &'v Variant<'v>, - generics: &'v Generics<'v>, - parent_item_id: HirId, -) { +pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V, variant: &'v Variant<'v>) { visitor.visit_ident(variant.ident); visitor.visit_id(variant.id); - visitor.visit_variant_data( - &variant.data, - variant.ident.name, - generics, - parent_item_id, - variant.span, - ); + visitor.visit_variant_data(&variant.data); walk_list!(visitor, visit_anon_const, &variant.disr_expr); } diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index a329b37519d98..5188ac633d392 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -24,7 +24,6 @@ use rustc_hir::intravisit::Visitor; use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, TyCtxt}; use rustc_session::lint::LintPass; -use rustc_span::symbol::Symbol; use rustc_span::Span; use std::any::Any; @@ -194,14 +193,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas self.context.cached_typeck_results.set(old_cached_typeck_results); } - fn visit_variant_data( - &mut self, - s: &'tcx hir::VariantData<'tcx>, - _: Symbol, - _: &'tcx hir::Generics<'tcx>, - _: hir::HirId, - _: Span, - ) { + fn visit_variant_data(&mut self, s: &'tcx hir::VariantData<'tcx>) { lint_callback!(self, check_struct_def, s); hir_visit::walk_struct_def(self, s); } @@ -213,15 +205,10 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas }) } - fn visit_variant( - &mut self, - v: &'tcx hir::Variant<'tcx>, - g: &'tcx hir::Generics<'tcx>, - item_id: hir::HirId, - ) { + fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) { self.with_lint_attrs(v.id, |cx| { lint_callback!(cx, check_variant, v); - hir_visit::walk_variant(cx, v, g, item_id); + hir_visit::walk_variant(cx, v); }) } diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 00e96f20d1aaa..7ab9302d835a2 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -772,14 +772,9 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'tcx> { }) } - fn visit_variant( - &mut self, - v: &'tcx hir::Variant<'tcx>, - g: &'tcx hir::Generics<'tcx>, - item_id: hir::HirId, - ) { + fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) { self.with_lint_attrs(v.id, |builder| { - intravisit::walk_variant(builder, v, g, item_id); + intravisit::walk_variant(builder, v); }) } diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 40dc9fe9a05bb..2a51af582f585 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -29,7 +29,6 @@ use rustc_middle::mir::visit::Visitor as _; use rustc_middle::mir::{traversal, Body, ConstQualifs, MirPass, MirPhase, Promoted}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, TyCtxt, TypeVisitable}; -use rustc_span::{Span, Symbol}; #[macro_use] mod pass_manager; @@ -159,14 +158,7 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet { set: &'a mut FxIndexSet, } impl<'tcx> Visitor<'tcx> for GatherCtors<'_, 'tcx> { - fn visit_variant_data( - &mut self, - v: &'tcx hir::VariantData<'tcx>, - _: Symbol, - _: &'tcx hir::Generics<'tcx>, - _: hir::HirId, - _: Span, - ) { + fn visit_variant_data(&mut self, v: &'tcx hir::VariantData<'tcx>) { if let hir::VariantData::Tuple(_, hir_id) = *v { self.set.insert(self.tcx.hir().local_def_id(hir_id)); } diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 6fea68ce8b3d1..f75fffb6871f7 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -2066,14 +2066,9 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> { intravisit::walk_expr(self, expr) } - fn visit_variant( - &mut self, - variant: &'tcx hir::Variant<'tcx>, - generics: &'tcx hir::Generics<'tcx>, - item_id: HirId, - ) { + fn visit_variant(&mut self, variant: &'tcx hir::Variant<'tcx>) { self.check_attributes(variant.id, variant.span, Target::Variant, None); - intravisit::walk_variant(self, variant, generics, item_id) + intravisit::walk_variant(self, variant) } fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) { diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 1e2fbeb384cee..625c854ea77a5 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -368,14 +368,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> { self.maybe_typeck_results = old_maybe_typeck_results; } - fn visit_variant_data( - &mut self, - def: &'tcx hir::VariantData<'tcx>, - _: Symbol, - _: &hir::Generics<'_>, - _: hir::HirId, - _: rustc_span::Span, - ) { + fn visit_variant_data(&mut self, def: &'tcx hir::VariantData<'tcx>) { let tcx = self.tcx; let has_repr_c = self.repr_has_repr_c; let has_repr_simd = self.repr_has_repr_simd; diff --git a/compiler/rustc_passes/src/hir_stats.rs b/compiler/rustc_passes/src/hir_stats.rs index a3be827a7ccec..4148d9728060b 100644 --- a/compiler/rustc_passes/src/hir_stats.rs +++ b/compiler/rustc_passes/src/hir_stats.rs @@ -203,14 +203,9 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_field_def(self, s) } - fn visit_variant( - &mut self, - v: &'v hir::Variant<'v>, - g: &'v hir::Generics<'v>, - item_id: hir::HirId, - ) { + fn visit_variant(&mut self, v: &'v hir::Variant<'v>) { self.record("Variant", Id::None, v); - hir_visit::walk_variant(self, v, g, item_id) + hir_visit::walk_variant(self, v) } fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) { diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index ca6a2ac3db34c..e7ab47d7d282a 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -10,7 +10,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; use rustc_hir::hir_id::CRATE_HIR_ID; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::{FieldDef, Generics, HirId, Item, ItemKind, TraitRef, Ty, TyKind, Variant}; +use rustc_hir::{FieldDef, Item, ItemKind, TraitRef, Ty, TyKind, Variant}; use rustc_middle::hir::nested_filter; use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::middle::stability::{AllowUnstable, DeprecationEntry, Index}; @@ -434,7 +434,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> { ); } - fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) { + fn visit_variant(&mut self, var: &'tcx Variant<'tcx>) { self.annotate( self.tcx.hir().local_def_id(var.id), var.span, @@ -457,7 +457,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> { ); } - intravisit::walk_variant(v, var, g, item_id) + intravisit::walk_variant(v, var) }, ) } @@ -590,9 +590,9 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> { intravisit::walk_impl_item(self, ii); } - fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) { + fn visit_variant(&mut self, var: &'tcx Variant<'tcx>) { self.check_missing_stability(self.tcx.hir().local_def_id(var.id), var.span); - intravisit::walk_variant(self, var, g, item_id); + intravisit::walk_variant(self, var); } fn visit_field_def(&mut self, s: &'tcx FieldDef<'tcx>) { diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index c28d0569d4ce9..f7c28eff55b76 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -1625,15 +1625,10 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { intravisit::walk_ty(self, t) } - fn visit_variant( - &mut self, - v: &'tcx hir::Variant<'tcx>, - g: &'tcx hir::Generics<'tcx>, - item_id: hir::HirId, - ) { + fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) { if self.access_levels.is_reachable(self.tcx.hir().local_def_id(v.id)) { self.in_variant = true; - intravisit::walk_variant(self, v, g, item_id); + intravisit::walk_variant(self, v); self.in_variant = false; } } diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 35964e3ba38e4..20ae102bc27d3 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -1289,14 +1289,9 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx> }); } - fn visit_variant( - &mut self, - v: &'hir hir::Variant<'_>, - g: &'hir hir::Generics<'_>, - item_id: hir::HirId, - ) { + fn visit_variant(&mut self, v: &'hir hir::Variant<'_>) { self.visit_testable(v.ident.to_string(), v.id, v.span, |this| { - intravisit::walk_variant(this, v, g, item_id); + intravisit::walk_variant(this, v); }); } From 694dbc8eca52e778da7163e5d10eab6fb34ddf02 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 12:02:48 +1000 Subject: [PATCH 04/14] Simplify `rustc_ast::visit::Visitor::visit_enum_def`. It's passed three arguments that are never used. --- compiler/rustc_ast/src/visit.rs | 19 ++++--------------- .../rustc_ast_passes/src/ast_validation.rs | 12 ++---------- compiler/rustc_ast_passes/src/node_count.rs | 12 +++--------- 3 files changed, 9 insertions(+), 34 deletions(-) diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index d9594b323dd0d..c21d3246bd89c 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -177,14 +177,8 @@ pub trait Visitor<'ast>: Sized { fn visit_field_def(&mut self, s: &'ast FieldDef) { walk_field_def(self, s) } - fn visit_enum_def( - &mut self, - enum_definition: &'ast EnumDef, - generics: &'ast Generics, - item_id: NodeId, - _: Span, - ) { - walk_enum_def(self, enum_definition, generics, item_id) + fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef) { + walk_enum_def(self, enum_definition) } fn visit_variant(&mut self, v: &'ast Variant) { walk_variant(self, v) @@ -334,7 +328,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { } ItemKind::Enum(ref enum_definition, ref generics) => { visitor.visit_generics(generics); - visitor.visit_enum_def(enum_definition, generics, item.id, item.span) + visitor.visit_enum_def(enum_definition) } ItemKind::Impl(box Impl { defaultness: _, @@ -377,12 +371,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { walk_list!(visitor, visit_attribute, &item.attrs); } -pub fn walk_enum_def<'a, V: Visitor<'a>>( - visitor: &mut V, - enum_definition: &'a EnumDef, - _: &'a Generics, - _: NodeId, -) { +pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V, enum_definition: &'a EnumDef) { walk_list!(visitor, visit_variant, &enum_definition.variants); } diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 60560b1c00e25..31e961336a55d 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1547,16 +1547,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s)) } - fn visit_enum_def( - &mut self, - enum_definition: &'a EnumDef, - generics: &'a Generics, - item_id: NodeId, - _: Span, - ) { - self.with_banned_assoc_ty_bound(|this| { - visit::walk_enum_def(this, enum_definition, generics, item_id) - }) + fn visit_enum_def(&mut self, enum_definition: &'a EnumDef) { + self.with_banned_assoc_ty_bound(|this| visit::walk_enum_def(this, enum_definition)) } fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) { diff --git a/compiler/rustc_ast_passes/src/node_count.rs b/compiler/rustc_ast_passes/src/node_count.rs index 9c7369c83e2da..be44795b1399b 100644 --- a/compiler/rustc_ast_passes/src/node_count.rs +++ b/compiler/rustc_ast_passes/src/node_count.rs @@ -91,15 +91,9 @@ impl<'ast> Visitor<'ast> for NodeCounter { self.count += 1; walk_field_def(self, s) } - fn visit_enum_def( - &mut self, - enum_definition: &EnumDef, - generics: &Generics, - item_id: NodeId, - _: Span, - ) { - self.count += 1; - walk_enum_def(self, enum_definition, generics, item_id) + fn visit_enum_def(&mut self, enum_definition: &EnumDef) { + self.count += 1; + walk_enum_def(self, enum_definition) } fn visit_variant(&mut self, v: &Variant) { self.count += 1; From 07a2eb1f445a6cab8b012963228361f3ce3edde0 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 10:44:30 +1000 Subject: [PATCH 05/14] Box the `Generics` in `ItemKind::{Enum,Struct,Union,TraitAlias}`. --- compiler/rustc_ast/src/ast.rs | 20 +++++++++---------- .../src/deriving/clone.rs | 4 ++-- .../rustc_builtin_macros/src/deriving/mod.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 8 ++++---- compiler/rustc_resolve/src/late.rs | 8 ++++---- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 28862dbb2678b..ff1b476261893 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2849,15 +2849,15 @@ pub enum ItemKind { /// An enum definition (`enum`). /// /// E.g., `enum Foo { C, D }`. - Enum(EnumDef, Generics), + Enum(EnumDef, Box), /// A struct definition (`struct`). /// /// E.g., `struct Foo { x: A }`. - Struct(VariantData, Generics), + Struct(VariantData, Box), /// A union definition (`union`). /// /// E.g., `union Foo { x: A, y: B }`. - Union(VariantData, Generics), + Union(VariantData, Box), /// A trait declaration (`trait`). /// /// E.g., `trait Foo { .. }`, `trait Foo { .. }` or `auto trait Foo {}`. @@ -2865,7 +2865,7 @@ pub enum ItemKind { /// Trait alias /// /// E.g., `trait Foo = Bar + Quux;`. - TraitAlias(Generics, GenericBounds), + TraitAlias(Box, GenericBounds), /// An implementation. /// /// E.g., `impl Foo { .. }` or `impl Trait for Foo { .. }`. @@ -2915,11 +2915,11 @@ impl ItemKind { match self { Self::Fn(box Fn { generics, .. }) | Self::TyAlias(box TyAlias { generics, .. }) - | Self::Enum(_, generics) - | Self::Struct(_, generics) - | Self::Union(_, generics) + | Self::Enum(_, box generics) + | Self::Struct(_, box generics) + | Self::Union(_, box generics) | Self::Trait(box Trait { generics, .. }) - | Self::TraitAlias(generics, _) + | Self::TraitAlias(box generics, _) | Self::Impl(box Impl { generics, .. }) => Some(generics), _ => None, } @@ -3043,8 +3043,8 @@ mod size_asserts { static_assert_size!(GenericBound, 88); static_assert_size!(Generics, 72); static_assert_size!(Impl, 200); - static_assert_size!(Item, 200); - static_assert_size!(ItemKind, 112); + static_assert_size!(Item, 176); + static_assert_size!(ItemKind, 88); static_assert_size!(Lit, 48); static_assert_size!(Pat, 120); static_assert_size!(Path, 40); diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index 7755ff779c4d9..05c6ac4bc3a27 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -33,8 +33,8 @@ pub fn expand_deriving_clone( let is_simple; match *item { Annotatable::Item(ref annitem) => match annitem.kind { - ItemKind::Struct(_, Generics { ref params, .. }) - | ItemKind::Enum(_, Generics { ref params, .. }) => { + ItemKind::Struct(_, box Generics { ref params, .. }) + | ItemKind::Enum(_, box Generics { ref params, .. }) => { let container_id = cx.current_expansion.id.expn_data().parent.expect_local(); let has_derive_copy = cx.resolver.has_derive_copy(container_id); if has_derive_copy diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs index c1ca089da221f..b6c225af65c87 100644 --- a/compiler/rustc_builtin_macros/src/deriving/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/mod.rs @@ -184,7 +184,7 @@ fn inject_impl_of_structural_trait( polarity: ast::ImplPolarity::Positive, defaultness: ast::Defaultness::Final, constness: ast::Const::No, - generics, + generics: *generics, of_trait: Some(trait_ref), self_ty: self_type, items: Vec::new(), diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 72c23776d3399..4fdbd4cfd6952 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -782,7 +782,7 @@ impl<'a> Parser<'a> { self.sess.gated_spans.gate(sym::trait_alias, whole_span); - Ok((ident, ItemKind::TraitAlias(generics, bounds))) + Ok((ident, ItemKind::TraitAlias(Box::new(generics), bounds))) } else { // It's a normal trait. generics.where_clause = self.parse_where_clause()?; @@ -1258,7 +1258,7 @@ impl<'a> Parser<'a> { })?; let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() }; - Ok((id, ItemKind::Enum(enum_definition, generics))) + Ok((id, ItemKind::Enum(enum_definition, Box::new(generics)))) } fn parse_enum_variant(&mut self) -> PResult<'a, Option> { @@ -1359,7 +1359,7 @@ impl<'a> Parser<'a> { return Err(err); }; - Ok((class_name, ItemKind::Struct(vdata, generics))) + Ok((class_name, ItemKind::Struct(vdata, Box::new(generics)))) } /// Parses `union Foo { ... }`. @@ -1385,7 +1385,7 @@ impl<'a> Parser<'a> { return Err(err); }; - Ok((class_name, ItemKind::Union(vdata, generics))) + Ok((class_name, ItemKind::Union(vdata, Box::new(generics)))) } fn parse_record_struct_body( diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index ac8d26fb0724a..44017b7833b9b 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -3943,12 +3943,12 @@ impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> { match &item.kind { ItemKind::TyAlias(box TyAlias { ref generics, .. }) | ItemKind::Fn(box Fn { ref generics, .. }) - | ItemKind::Enum(_, ref generics) - | ItemKind::Struct(_, ref generics) - | ItemKind::Union(_, ref generics) + | ItemKind::Enum(_, box ref generics) + | ItemKind::Struct(_, box ref generics) + | ItemKind::Union(_, box ref generics) | ItemKind::Impl(box Impl { ref generics, .. }) | ItemKind::Trait(box Trait { ref generics, .. }) - | ItemKind::TraitAlias(ref generics, _) => { + | ItemKind::TraitAlias(box ref generics, _) => { let def_id = self.r.local_def_id(item.id); let count = generics .params From 5c7b4e7c9efdef8bfcf2534b7f9ec773106490a7 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 12:30:51 +1000 Subject: [PATCH 06/14] Box the `UseTree` in `ItemKind::UseTree`. --- compiler/rustc_ast/src/ast.rs | 6 +++--- compiler/rustc_builtin_macros/src/assert/context.rs | 4 ++-- .../rustc_builtin_macros/src/standard_library_imports.rs | 5 +++-- compiler/rustc_parse/src/parser/item.rs | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index ff1b476261893..359ea3250a031 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2817,7 +2817,7 @@ pub enum ItemKind { /// A use declaration item (`use`). /// /// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`. - Use(UseTree), + Use(P), /// A static item (`static`). /// /// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`. @@ -3043,8 +3043,8 @@ mod size_asserts { static_assert_size!(GenericBound, 88); static_assert_size!(Generics, 72); static_assert_size!(Impl, 200); - static_assert_size!(Item, 176); - static_assert_size!(ItemKind, 88); + static_assert_size!(Item, 160); + static_assert_size!(ItemKind, 72); static_assert_size!(Lit, 48); static_assert_size!(Pat, 120); static_assert_size!(Path, 40); diff --git a/compiler/rustc_builtin_macros/src/assert/context.rs b/compiler/rustc_builtin_macros/src/assert/context.rs index dcea883a5a378..1541c7c502a49 100644 --- a/compiler/rustc_builtin_macros/src/assert/context.rs +++ b/compiler/rustc_builtin_macros/src/assert/context.rs @@ -120,14 +120,14 @@ impl<'cx, 'a> Context<'cx, 'a> { Ident::new(sym::allow, self.span), vec![attr::mk_nested_word_item(Ident::new(sym::unused_imports, self.span))], ))], - ItemKind::Use(UseTree { + ItemKind::Use(P(UseTree { prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])), kind: UseTreeKind::Nested(vec![ nested_tree(self, sym::TryCaptureGeneric), nested_tree(self, sym::TryCapturePrintable), ]), span: self.span, - }), + })), ), ) } diff --git a/compiler/rustc_builtin_macros/src/standard_library_imports.rs b/compiler/rustc_builtin_macros/src/standard_library_imports.rs index 09ad5f9b3eaa9..6a05bf25d10ca 100644 --- a/compiler/rustc_builtin_macros/src/standard_library_imports.rs +++ b/compiler/rustc_builtin_macros/src/standard_library_imports.rs @@ -1,4 +1,5 @@ use rustc_ast as ast; +use rustc_ast::ptr::P; use rustc_expand::base::{ExtCtxt, ResolverExpand}; use rustc_expand::expand::ExpansionConfig; use rustc_session::Session; @@ -79,11 +80,11 @@ pub fn inject( span, Ident::empty(), vec![cx.attribute(cx.meta_word(span, sym::prelude_import))], - ast::ItemKind::Use(ast::UseTree { + ast::ItemKind::Use(P(ast::UseTree { prefix: cx.path(span, import_path), kind: ast::UseTreeKind::Glob, span, - }), + })), ); krate.items.insert(0, use_item); diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 4fdbd4cfd6952..d5169a41051be 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -328,7 +328,7 @@ impl<'a> Parser<'a> { } return Err(e); } - Ok((Ident::empty(), ItemKind::Use(tree))) + Ok((Ident::empty(), ItemKind::Use(P(tree)))) } /// When parsing a statement, would the start of a path be an item? From d665e9cbd8f1ffdb9959413e4e6888c09f4910fa Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 12:38:04 +1000 Subject: [PATCH 07/14] Box the `ForeignMod` in `ItemKind::ForeignMod`. --- compiler/rustc_ast/src/ast.rs | 2 +- compiler/rustc_ast_passes/src/ast_validation.rs | 6 +++--- compiler/rustc_parse/src/parser/item.rs | 4 ++-- compiler/rustc_resolve/src/access_levels.rs | 5 ++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 359ea3250a031..4dd40e1f94ef4 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2839,7 +2839,7 @@ pub enum ItemKind { /// An external module (`extern`). /// /// E.g., `extern {}` or `extern "C" {}`. - ForeignMod(ForeignMod), + ForeignMod(P), /// Module-level inline assembly (from `global_asm!()`). GlobalAsm(Box), /// A type alias (`type`). diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 31e961336a55d..3419847dc6ba4 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1234,16 +1234,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> { walk_list!(self, visit_attribute, &item.attrs); return; // Avoid visiting again. } - ItemKind::ForeignMod(ForeignMod { abi, unsafety, .. }) => { + ItemKind::ForeignMod(ref foreign_mod) => { let old_item = mem::replace(&mut self.extern_mod, Some(item)); self.invalid_visibility( &item.vis, Some("place qualifiers on individual foreign items instead"), ); - if let Unsafe::Yes(span) = unsafety { + if let Unsafe::Yes(span) = foreign_mod.unsafety { self.err_handler().span_err(span, "extern block cannot be declared unsafe"); } - if abi.is_none() { + if foreign_mod.abi.is_none() { self.maybe_lint_missing_abi(item.span, item.id); } visit::walk_item(self, item); diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index d5169a41051be..f6e7949b5a8dd 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1039,11 +1039,11 @@ impl<'a> Parser<'a> { unsafety = Unsafe::Yes(self.token.span); self.eat_keyword(kw::Unsafe); } - let module = ast::ForeignMod { + let module = P(ast::ForeignMod { unsafety, abi, items: self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?, - }; + }); Ok((Ident::empty(), ItemKind::ForeignMod(module))) } diff --git a/compiler/rustc_resolve/src/access_levels.rs b/compiler/rustc_resolve/src/access_levels.rs index 3fba923d9fdf4..4fdf8f689b844 100644 --- a/compiler/rustc_resolve/src/access_levels.rs +++ b/compiler/rustc_resolve/src/access_levels.rs @@ -3,7 +3,6 @@ use rustc_ast::visit; use rustc_ast::visit::Visitor; use rustc_ast::Crate; use rustc_ast::EnumDef; -use rustc_ast::ForeignMod; use rustc_ast::NodeId; use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::CRATE_DEF_ID; @@ -184,8 +183,8 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> { self.prev_level = orig_level; } - ast::ItemKind::ForeignMod(ForeignMod { ref items, .. }) => { - for nested in items { + ast::ItemKind::ForeignMod(ref foreign_mod) => { + for nested in foreign_mod.items.iter() { if nested.vis.kind.is_pub() { self.set_access_level(nested.id, access_level); } From cb6230e12822bcef6a22fc786c70c668858330d8 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 12:42:03 +1000 Subject: [PATCH 08/14] Box the `MacCall` in various types. --- compiler/rustc_ast/src/ast.rs | 26 +++++++++---------- compiler/rustc_builtin_macros/src/assert.rs | 4 +-- .../src/assert/context.rs | 4 +-- .../rustc_builtin_macros/src/edition_panic.rs | 4 +-- compiler/rustc_expand/src/expand.rs | 24 ++++++++--------- compiler/rustc_expand/src/placeholders.rs | 6 ++--- compiler/rustc_parse/src/parser/expr.rs | 4 +-- compiler/rustc_parse/src/parser/item.rs | 2 +- compiler/rustc_parse/src/parser/pat.rs | 2 +- compiler/rustc_parse/src/parser/stmt.rs | 2 +- compiler/rustc_parse/src/parser/ty.rs | 4 +-- 11 files changed, 41 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 4dd40e1f94ef4..ade055642d6ab 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -771,7 +771,7 @@ pub enum PatKind { Paren(P), /// A macro pattern; pre-expansion. - MacCall(MacCall), + MacCall(P), } #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)] @@ -981,7 +981,7 @@ pub enum StmtKind { #[derive(Clone, Encodable, Decodable, Debug)] pub struct MacCallStmt { - pub mac: MacCall, + pub mac: P, pub style: MacStmtStyle, pub attrs: AttrVec, pub tokens: Option, @@ -1439,7 +1439,7 @@ pub enum ExprKind { InlineAsm(P), /// A macro invocation; pre-expansion. - MacCall(MacCall), + MacCall(P), /// A struct literal expression. /// @@ -2042,7 +2042,7 @@ pub enum TyKind { /// Inferred type of a `self` or `&self` argument in a method. ImplicitSelf, /// A macro in the type position. - MacCall(MacCall), + MacCall(P), /// Placeholder for a kind that has failed to be defined. Err, /// Placeholder for a `va_list`. @@ -2873,7 +2873,7 @@ pub enum ItemKind { /// A macro invocation. /// /// E.g., `foo!(..)`. - MacCall(MacCall), + MacCall(P), /// A macro definition. MacroDef(MacroDef), @@ -2947,7 +2947,7 @@ pub enum AssocItemKind { /// An associated type. TyAlias(Box), /// A macro expanding to associated items. - MacCall(MacCall), + MacCall(P), } impl AssocItemKind { @@ -2996,7 +2996,7 @@ pub enum ForeignItemKind { /// An foreign type. TyAlias(Box), /// A macro expanding to foreign items. - MacCall(MacCall), + MacCall(P), } impl From for ItemKind { @@ -3032,19 +3032,19 @@ mod size_asserts { use super::*; use rustc_data_structures::static_assert_size; // These are in alphabetical order, which is easy to maintain. - static_assert_size!(AssocItem, 160); - static_assert_size!(AssocItemKind, 72); + static_assert_size!(AssocItem, 120); + static_assert_size!(AssocItemKind, 32); static_assert_size!(Attribute, 152); static_assert_size!(Block, 48); static_assert_size!(Expr, 104); static_assert_size!(Fn, 192); - static_assert_size!(ForeignItem, 160); - static_assert_size!(ForeignItemKind, 72); + static_assert_size!(ForeignItem, 112); + static_assert_size!(ForeignItemKind, 24); static_assert_size!(GenericBound, 88); static_assert_size!(Generics, 72); static_assert_size!(Impl, 200); - static_assert_size!(Item, 160); - static_assert_size!(ItemKind, 72); + static_assert_size!(Item, 152); + static_assert_size!(ItemKind, 64); static_assert_size!(Lit, 48); static_assert_size!(Pat, 120); static_assert_size!(Path, 40); diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index 925c36edb5166..119724b50493e 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -52,7 +52,7 @@ pub fn expand_assert<'cx>( let expr = if let Some(tokens) = custom_message { let then = cx.expr( call_site_span, - ExprKind::MacCall(MacCall { + ExprKind::MacCall(P(MacCall { path: panic_path(), args: P(MacArgs::Delimited( DelimSpan::from_single(call_site_span), @@ -60,7 +60,7 @@ pub fn expand_assert<'cx>( tokens, )), prior_type_ascription: None, - }), + })), ); expr_if_not(cx, call_site_span, cond_expr, then, None) } diff --git a/compiler/rustc_builtin_macros/src/assert/context.rs b/compiler/rustc_builtin_macros/src/assert/context.rs index 1541c7c502a49..0a229f1bb1ee9 100644 --- a/compiler/rustc_builtin_macros/src/assert/context.rs +++ b/compiler/rustc_builtin_macros/src/assert/context.rs @@ -177,7 +177,7 @@ impl<'cx, 'a> Context<'cx, 'a> { }); self.cx.expr( self.span, - ExprKind::MacCall(MacCall { + ExprKind::MacCall(P(MacCall { path: panic_path, args: P(MacArgs::Delimited( DelimSpan::from_single(self.span), @@ -185,7 +185,7 @@ impl<'cx, 'a> Context<'cx, 'a> { initial.into_iter().chain(captures).collect::(), )), prior_type_ascription: None, - }), + })), ) } diff --git a/compiler/rustc_builtin_macros/src/edition_panic.rs b/compiler/rustc_builtin_macros/src/edition_panic.rs index ea0e768a58f48..3f1a8b3bc2cf7 100644 --- a/compiler/rustc_builtin_macros/src/edition_panic.rs +++ b/compiler/rustc_builtin_macros/src/edition_panic.rs @@ -48,7 +48,7 @@ fn expand<'cx>( MacEager::expr( cx.expr( sp, - ExprKind::MacCall(MacCall { + ExprKind::MacCall(P(MacCall { path: Path { span: sp, segments: cx @@ -64,7 +64,7 @@ fn expand<'cx>( tts, )), prior_type_ascription: None, - }), + })), ), ) } diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 93eeca5b2892b..cc72dab84afa2 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -306,7 +306,7 @@ pub struct Invocation { pub enum InvocationKind { Bang { - mac: ast::MacCall, + mac: P, span: Span, }, Attr { @@ -1017,7 +1017,7 @@ trait InvocationCollectorNode: HasAttrs + HasNodeId + Sized { fn is_mac_call(&self) -> bool { false } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { unreachable!() } fn pre_flat_map_node_collect_attr(_cfg: &StripUnconfigured<'_>, _attr: &ast::Attribute) {} @@ -1046,7 +1046,7 @@ impl InvocationCollectorNode for P { fn is_mac_call(&self) -> bool { matches!(self.kind, ItemKind::MacCall(..)) } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { let node = self.into_inner(); match node.kind { ItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No), @@ -1154,7 +1154,7 @@ impl InvocationCollectorNode for AstNodeWrapper, TraitItemTag> fn is_mac_call(&self) -> bool { matches!(self.wrapped.kind, AssocItemKind::MacCall(..)) } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { let item = self.wrapped.into_inner(); match item.kind { AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No), @@ -1179,7 +1179,7 @@ impl InvocationCollectorNode for AstNodeWrapper, ImplItemTag> fn is_mac_call(&self) -> bool { matches!(self.wrapped.kind, AssocItemKind::MacCall(..)) } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { let item = self.wrapped.into_inner(); match item.kind { AssocItemKind::MacCall(mac) => (mac, item.attrs, AddSemicolon::No), @@ -1202,7 +1202,7 @@ impl InvocationCollectorNode for P { fn is_mac_call(&self) -> bool { matches!(self.kind, ForeignItemKind::MacCall(..)) } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { let node = self.into_inner(); match node.kind { ForeignItemKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No), @@ -1323,7 +1323,7 @@ impl InvocationCollectorNode for ast::Stmt { StmtKind::Local(..) | StmtKind::Empty => false, } } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { // We pull macro invocations (both attributes and fn-like macro calls) out of their // `StmtKind`s and treat them as statement macro invocations, not as items or expressions. let (add_semicolon, mac, attrs) = match self.kind { @@ -1387,7 +1387,7 @@ impl InvocationCollectorNode for P { fn is_mac_call(&self) -> bool { matches!(self.kind, ast::TyKind::MacCall(..)) } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { let node = self.into_inner(); match node.kind { TyKind::MacCall(mac) => (mac, Vec::new(), AddSemicolon::No), @@ -1411,7 +1411,7 @@ impl InvocationCollectorNode for P { fn is_mac_call(&self) -> bool { matches!(self.kind, PatKind::MacCall(..)) } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { let node = self.into_inner(); match node.kind { PatKind::MacCall(mac) => (mac, Vec::new(), AddSemicolon::No), @@ -1439,7 +1439,7 @@ impl InvocationCollectorNode for P { fn is_mac_call(&self) -> bool { matches!(self.kind, ExprKind::MacCall(..)) } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { let node = self.into_inner(); match node.kind { ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No), @@ -1466,7 +1466,7 @@ impl InvocationCollectorNode for AstNodeWrapper, OptExprTag> { fn is_mac_call(&self) -> bool { matches!(self.wrapped.kind, ast::ExprKind::MacCall(..)) } - fn take_mac_call(self) -> (ast::MacCall, Self::AttrsTy, AddSemicolon) { + fn take_mac_call(self) -> (P, Self::AttrsTy, AddSemicolon) { let node = self.wrapped.into_inner(); match node.kind { ExprKind::MacCall(mac) => (mac, node.attrs, AddSemicolon::No), @@ -1512,7 +1512,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis) } - fn collect_bang(&mut self, mac: ast::MacCall, kind: AstFragmentKind) -> AstFragment { + fn collect_bang(&mut self, mac: P, kind: AstFragmentKind) -> AstFragment { // cache the macro call span so that it can be // easily adjusted for incremental compilation let span = mac.span(); diff --git a/compiler/rustc_expand/src/placeholders.rs b/compiler/rustc_expand/src/placeholders.rs index 0d5d6ee07944f..48918541e72fc 100644 --- a/compiler/rustc_expand/src/placeholders.rs +++ b/compiler/rustc_expand/src/placeholders.rs @@ -15,12 +15,12 @@ pub fn placeholder( id: ast::NodeId, vis: Option, ) -> AstFragment { - fn mac_placeholder() -> ast::MacCall { - ast::MacCall { + fn mac_placeholder() -> P { + P(ast::MacCall { path: ast::Path { span: DUMMY_SP, segments: Vec::new(), tokens: None }, args: P(ast::MacArgs::Empty), prior_type_ascription: None, - } + }) } let ident = Ident::empty(); diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index e473f4d30cf09..401bf54beb293 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1513,11 +1513,11 @@ impl<'a> Parser<'a> { if qself.is_some() { self.struct_span_err(path.span, "macros cannot use qualified paths").emit(); } - let mac = MacCall { + let mac = P(MacCall { path, args: self.parse_mac_args()?, prior_type_ascription: self.last_type_ascription, - }; + }); (self.prev_token.span, ExprKind::MacCall(mac)) } else if self.check(&token::OpenDelim(Delimiter::Brace)) { if let Some(expr) = self.maybe_parse_struct_expr(qself.as_ref(), &path, &attrs) { diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index f6e7949b5a8dd..30fcc2a842805 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -282,7 +282,7 @@ impl<'a> Parser<'a> { return Ok(None); } else if macros_allowed && self.check_path() { // MACRO INVOCATION ITEM - (Ident::empty(), ItemKind::MacCall(self.parse_item_macro(vis)?)) + (Ident::empty(), ItemKind::MacCall(P(self.parse_item_macro(vis)?))) } else { return Ok(None); }; diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index ba77a39584093..3369ba9ab1830 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -665,7 +665,7 @@ impl<'a> Parser<'a> { fn parse_pat_mac_invoc(&mut self, path: Path) -> PResult<'a, PatKind> { self.bump(); let args = self.parse_mac_args()?; - let mac = MacCall { path, args, prior_type_ascription: self.last_type_ascription }; + let mac = P(MacCall { path, args, prior_type_ascription: self.last_type_ascription }); Ok(PatKind::MacCall(mac)) } diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 51bd9d2d386ad..86caaa109b432 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -168,7 +168,7 @@ impl<'a> Parser<'a> { None => unreachable!(), }; - let mac = MacCall { path, args, prior_type_ascription: self.last_type_ascription }; + let mac = P(MacCall { path, args, prior_type_ascription: self.last_type_ascription }); let kind = if (style == MacStmtStyle::Braces && self.token != token::Dot diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 31b40a83e6052..76b710095d798 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -598,11 +598,11 @@ impl<'a> Parser<'a> { let path = self.parse_path_inner(PathStyle::Type, ty_generics)?; if self.eat(&token::Not) { // Macro invocation in type position - Ok(TyKind::MacCall(MacCall { + Ok(TyKind::MacCall(P(MacCall { path, args: self.parse_mac_args()?, prior_type_ascription: self.last_type_ascription, - })) + }))) } else if allow_plus == AllowPlus::Yes && self.check_plus() { // `Trait1 + Trait2 + 'a` self.parse_remaining_bounds_path(Vec::new(), path, lo, true) From e71c37537c7ce4a09113e1fd1f206ba1a8a4122f Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 13:58:27 +1000 Subject: [PATCH 09/14] Box the `ForeignMod` in `ItemKind::ForeignMod`. --- compiler/rustc_ast/src/ast.rs | 6 +++--- compiler/rustc_ast/src/mut_visit.rs | 2 +- compiler/rustc_ast/src/visit.rs | 2 +- compiler/rustc_ast_lowering/src/item.rs | 2 +- compiler/rustc_ast_passes/src/ast_validation.rs | 2 +- compiler/rustc_ast_pretty/src/pprust/state/item.rs | 2 +- compiler/rustc_builtin_macros/src/test_harness.rs | 4 +++- compiler/rustc_expand/src/expand.rs | 12 +++++------- compiler/rustc_expand/src/parse/tests.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 4 ++-- compiler/rustc_resolve/src/diagnostics.rs | 4 +++- compiler/rustc_resolve/src/macros.rs | 4 +++- src/tools/clippy/clippy_lints/src/duplicate_mod.rs | 3 ++- .../src/single_component_path_imports.rs | 8 +++++--- src/tools/clippy/clippy_utils/src/ast_utils.rs | 2 +- src/tools/rustfmt/src/items.rs | 10 ++++++---- src/tools/rustfmt/src/modules.rs | 4 ++-- 17 files changed, 41 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index ade055642d6ab..02a8fba873f0f 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2835,7 +2835,7 @@ pub enum ItemKind { /// E.g., `mod foo;` or `mod foo { .. }`. /// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not /// semantically by Rust. - Mod(Unsafe, ModKind), + Mod(Unsafe, P), /// An external module (`extern`). /// /// E.g., `extern {}` or `extern "C" {}`. @@ -3043,8 +3043,8 @@ mod size_asserts { static_assert_size!(GenericBound, 88); static_assert_size!(Generics, 72); static_assert_size!(Impl, 200); - static_assert_size!(Item, 152); - static_assert_size!(ItemKind, 64); + static_assert_size!(Item, 136); + static_assert_size!(ItemKind, 48); static_assert_size!(Lit, 48); static_assert_size!(Pat, 120); static_assert_size!(Path, 40); diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 01bd498b37780..8e13d2d7c3d8d 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1027,7 +1027,7 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { } ItemKind::Mod(unsafety, mod_kind) => { visit_unsafety(unsafety, vis); - match mod_kind { + match &mut **mod_kind { ModKind::Loaded(items, _inline, ModSpans { inner_span, inject_use_span }) => { vis.visit_span(inner_span); vis.visit_span(inject_use_span); diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index c21d3246bd89c..f292445810ca2 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -311,7 +311,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, generics, body.as_deref()); visitor.visit_fn(kind, item.span, item.id) } - ItemKind::Mod(_unsafety, ref mod_kind) => match mod_kind { + ItemKind::Mod(_unsafety, ref mod_kind) => match &**mod_kind { ModKind::Loaded(items, _inline, _inner_span) => { walk_list!(visitor, visit_item, items) } diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index ee4c0036f7698..cdb45cf915404 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -272,7 +272,7 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ItemKind::Fn(sig, generics, body_id) }) } - ItemKind::Mod(_, ref mod_kind) => match mod_kind { + ItemKind::Mod(_, ref mod_kind) => match &**mod_kind { ModKind::Loaded(items, _, spans) => { hir::ItemKind::Mod(self.lower_mod(items, spans)) } diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 3419847dc6ba4..ecb62d8b7ad51 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1284,7 +1284,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.err_handler().span_err(span, "module cannot be declared unsafe"); } // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584). - if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) + if !matches!(&**mod_kind, ModKind::Loaded(_, Inline::Yes, _)) && !self.session.contains_name(&item.attrs, sym::path) { self.check_mod_file_item_asciionly(item.ident); diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index f1caf22f3640f..64160cd75b80a 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -183,7 +183,7 @@ impl<'a> State<'a> { })); self.print_ident(item.ident); - match mod_kind { + match &**mod_kind { ModKind::Loaded(items, ..) => { self.nbsp(); self.bopen(); diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index 0ebe29df95f20..7b5de4e3e598f 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -129,7 +129,9 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> { // We don't want to recurse into anything other than mods, since // mods or tests inside of functions will break things - if let ast::ItemKind::Mod(_, ModKind::Loaded(.., ref spans)) = item.kind { + if let ast::ItemKind::Mod(_, ref mod_kind) = item.kind + && let ModKind::Loaded(.., ref spans) = &**mod_kind + { let ast::ModSpans { inner_span: span, inject_use_span: _ } = *spans; let prev_tests = mem::take(&mut self.tests); noop_visit_item_kind(&mut item.kind, self); diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index cc72dab84afa2..3ac377d1737e2 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -673,10 +673,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { if matches!(attr.style, AttrStyle::Inner) && matches!( item_inner.kind, - ItemKind::Mod( - _, - ModKind::Unloaded | ModKind::Loaded(_, Inline::No, _), - ) + ItemKind::Mod(_, ref mod_kind) if + matches!(&**mod_kind, ModKind::Unloaded | ModKind::Loaded(_, Inline::No, _)), ) => { rustc_parse::fake_token_stream_for_item( @@ -800,7 +798,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { fn visit_item(&mut self, item: &'ast ast::Item) { match &item.kind { ItemKind::Mod(_, mod_kind) - if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) => + if !matches!(&**mod_kind, ModKind::Loaded(_, Inline::Yes, _)) => { feature_err( self.parse_sess, @@ -1069,7 +1067,7 @@ impl InvocationCollectorNode for P { }; let ecx = &mut collector.cx; - let (file_path, dir_path, dir_ownership) = match mod_kind { + let (file_path, dir_path, dir_ownership) = match &**mod_kind { ModKind::Loaded(_, inline, _) => { // Inline `mod foo { ... }`, but we still need to push directories. let (dir_path, dir_ownership) = mod_dir_path( @@ -1107,7 +1105,7 @@ impl InvocationCollectorNode for P { ); } - *mod_kind = ModKind::Loaded(items, Inline::No, spans); + **mod_kind = ModKind::Loaded(items, Inline::No, spans); node.attrs = attrs; if node.attrs.len() > old_attrs_len { // If we loaded an out-of-line module and added some inner attributes, diff --git a/compiler/rustc_expand/src/parse/tests.rs b/compiler/rustc_expand/src/parse/tests.rs index a3c631d3318a0..9c53b768c0f8d 100644 --- a/compiler/rustc_expand/src/parse/tests.rs +++ b/compiler/rustc_expand/src/parse/tests.rs @@ -319,7 +319,7 @@ fn out_of_line_mod() { .unwrap(); if let ast::ItemKind::Mod(_, ref mod_kind) = item.kind { - assert!(matches!(mod_kind, ast::ModKind::Loaded(items, ..) if items.len() == 2)); + assert!(matches!(&**mod_kind, ast::ModKind::Loaded(items, ..) if items.len() == 2)); } else { panic!(); } diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 30fcc2a842805..0a257ea68c718 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -36,7 +36,7 @@ impl<'a> Parser<'a> { let unsafety = self.parse_unsafety(); self.expect_keyword(kw::Mod)?; let id = self.parse_ident()?; - let mod_kind = if self.eat(&token::Semi) { + let mod_kind = P(if self.eat(&token::Semi) { ModKind::Unloaded } else { self.expect(&token::OpenDelim(Delimiter::Brace))?; @@ -44,7 +44,7 @@ impl<'a> Parser<'a> { self.parse_mod(&token::CloseDelim(Delimiter::Brace))?; attrs.append(&mut inner_attrs); ModKind::Loaded(items, Inline::Yes, inner_span) - }; + }); Ok((id, ItemKind::Mod(unsafety, mod_kind))) } diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 8839fb1a15120..1bd6bda8b47a6 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -2671,7 +2671,9 @@ impl<'tcx> visit::Visitor<'tcx> for UsePlacementFinder { fn visit_item(&mut self, item: &'tcx ast::Item) { if self.target_module == item.id { - if let ItemKind::Mod(_, ModKind::Loaded(items, _inline, mod_spans)) = &item.kind { + if let ItemKind::Mod(_, mod_kind) = &item.kind + && let ModKind::Loaded(items, _inline, mod_spans) = &**mod_kind + { let inject = mod_spans.inject_use_span; if is_span_suitable_for_use_injection(inject) { self.first_legal_span = Some(inject); diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 070fb9c721b40..67a03c3532ba0 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -164,7 +164,9 @@ fn soft_custom_inner_attributes_gate(path: &ast::Path, invoc: &Invocation) -> bo [seg1, seg2] if seg1.ident.name == sym::rustfmt && seg2.ident.name == sym::skip => { if let InvocationKind::Attr { item, .. } = &invoc.kind { if let Annotatable::Item(item) = item { - if let ItemKind::Mod(_, ModKind::Loaded(_, Inline::No, _)) = item.kind { + if let ItemKind::Mod(_, ref mod_kind) = item.kind + && let ModKind::Loaded(_, Inline::No, _) = &**mod_kind + { return true; } } diff --git a/src/tools/clippy/clippy_lints/src/duplicate_mod.rs b/src/tools/clippy/clippy_lints/src/duplicate_mod.rs index e1eb3b6324c78..0911c4da9eadc 100644 --- a/src/tools/clippy/clippy_lints/src/duplicate_mod.rs +++ b/src/tools/clippy/clippy_lints/src/duplicate_mod.rs @@ -63,7 +63,8 @@ impl_lint_pass!(DuplicateMod => [DUPLICATE_MOD]); impl EarlyLintPass for DuplicateMod { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { - if let ItemKind::Mod(_, ModKind::Loaded(_, Inline::No, mod_spans)) = &item.kind + if let ItemKind::Mod(_, mod_kind) = &item.kind + && let ModKind::Loaded(_, Inline::No, mod_spans) = &**mod_kind && let FileName::Real(real) = cx.sess().source_map().span_to_filename(mod_spans.inner_span) && let Some(local_path) = real.into_local_path() && let Ok(absolute_path) = local_path.canonicalize() diff --git a/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs b/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs index 66b79513032f6..81485f5d80547 100644 --- a/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs +++ b/src/tools/clippy/clippy_lints/src/single_component_path_imports.rs @@ -114,9 +114,11 @@ fn track_uses( } match &item.kind { - ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => { - check_mod(cx, items); - }, + ItemKind::Mod(_, mod_kind) => { + if let ModKind::Loaded(ref items, ..) = **mod_kind { + check_mod(cx, items); + } + } ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => { macros.push(item.ident.name); }, diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index b226026323be8..4b873c30858d1 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -273,7 +273,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { }, (Mod(lu, lmk), Mod(ru, rmk)) => { lu == ru - && match (lmk, rmk) { + && match (&**lmk, &**rmk) { (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => { linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind)) }, diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 8f35068e35f04..c030b04abeabd 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -3320,10 +3320,12 @@ pub(crate) fn rewrite_extern_crate( /// Returns `true` for `mod foo;`, false for `mod foo { .. }`. pub(crate) fn is_mod_decl(item: &ast::Item) -> bool { - !matches!( - item.kind, - ast::ItemKind::Mod(_, ast::ModKind::Loaded(_, ast::Inline::Yes, _)) - ) + if let ast::ItemKind::Mod(_, ref mod_kind) = item.kind { + if let ast::ModKind::Loaded(_, ast::Inline::Yes, _) = &**mod_kind { + return false; + } + } + true } pub(crate) fn is_use_item(item: &ast::Item) -> bool { diff --git a/src/tools/rustfmt/src/modules.rs b/src/tools/rustfmt/src/modules.rs index 81da724329f02..64506d053bf5a 100644 --- a/src/tools/rustfmt/src/modules.rs +++ b/src/tools/rustfmt/src/modules.rs @@ -156,7 +156,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { &module_item.item, Module::new( module_item.item.span, - Some(Cow::Owned(sub_mod_kind.clone())), + Some(Cow::Owned((**sub_mod_kind).clone())), Cow::Owned(vec![]), Cow::Owned(vec![]), ), @@ -183,7 +183,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { &item, Module::new( span, - Some(Cow::Owned(sub_mod_kind.clone())), + Some(Cow::Owned((**sub_mod_kind).clone())), Cow::Owned(vec![]), Cow::Owned(vec![]), ), From 19db44f8f25b4c75f89982595352d42f3fe376a0 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 14:13:12 +1000 Subject: [PATCH 10/14] XXX: redoing Enum --- compiler/rustc_ast/src/ast.rs | 14 ++++--- compiler/rustc_ast/src/mut_visit.rs | 2 +- compiler/rustc_ast/src/visit.rs | 11 +----- compiler/rustc_ast_lowering/src/item.rs | 6 +-- .../rustc_ast_passes/src/ast_validation.rs | 8 +--- compiler/rustc_ast_passes/src/feature_gate.rs | 2 +- compiler/rustc_ast_passes/src/node_count.rs | 4 -- .../rustc_ast_pretty/src/pprust/state/item.rs | 10 ++--- .../src/deriving/clone.rs | 8 ++-- .../src/deriving/cmp/eq.rs | 4 +- .../src/deriving/default.rs | 29 +++++++------- .../src/deriving/generic/mod.rs | 38 +++++++++---------- .../rustc_builtin_macros/src/deriving/mod.rs | 8 ++-- compiler/rustc_expand/src/base.rs | 4 +- compiler/rustc_expand/src/expand.rs | 2 +- compiler/rustc_expand/src/lib.rs | 1 + compiler/rustc_parse/src/parser/item.rs | 6 +-- compiler/rustc_resolve/src/access_levels.rs | 3 +- .../rustc_resolve/src/build_reduced_graph.rs | 2 +- compiler/rustc_resolve/src/late.rs | 8 ++-- .../clippy/clippy_utils/src/ast_utils.rs | 5 ++- src/tools/rustfmt/src/items.rs | 17 ++++----- src/tools/rustfmt/src/visitor.rs | 10 ++++- 23 files changed, 96 insertions(+), 106 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 02a8fba873f0f..b876684ab3707 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2440,10 +2440,6 @@ pub struct ForeignMod { pub items: Vec>, } -#[derive(Clone, Encodable, Decodable, Debug)] -pub struct EnumDef { - pub variants: Vec, -} /// Enum variant. #[derive(Clone, Encodable, Decodable, Debug)] pub struct Variant { @@ -2808,6 +2804,12 @@ pub struct Fn { pub body: Option>, } +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct Enum { + pub variants: Vec, + pub generics: Generics, +} + #[derive(Clone, Encodable, Decodable, Debug)] pub enum ItemKind { /// An `extern crate` item, with the optional *original* crate name if the crate was renamed. @@ -2849,7 +2851,7 @@ pub enum ItemKind { /// An enum definition (`enum`). /// /// E.g., `enum Foo { C, D }`. - Enum(EnumDef, Box), + Enum(Box), /// A struct definition (`struct`). /// /// E.g., `struct Foo { x: A }`. @@ -2915,7 +2917,7 @@ impl ItemKind { match self { Self::Fn(box Fn { generics, .. }) | Self::TyAlias(box TyAlias { generics, .. }) - | Self::Enum(_, box generics) + | Self::Enum(box Enum { generics, .. }) | Self::Struct(_, box generics) | Self::Union(_, box generics) | Self::Trait(box Trait { generics, .. }) diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 8e13d2d7c3d8d..84b4d17b2c63e 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1048,7 +1048,7 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { visit_bounds(bounds, vis); visit_opt(ty, |ty| vis.visit_ty(ty)); } - ItemKind::Enum(EnumDef { variants }, generics) => { + ItemKind::Enum(box Enum { variants, generics }) => { variants.flat_map_in_place(|variant| vis.flat_map_variant(variant)); vis.visit_generics(generics); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index f292445810ca2..3c2900c574341 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -177,9 +177,6 @@ pub trait Visitor<'ast>: Sized { fn visit_field_def(&mut self, s: &'ast FieldDef) { walk_field_def(self, s) } - fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef) { - walk_enum_def(self, enum_definition) - } fn visit_variant(&mut self, v: &'ast Variant) { walk_variant(self, v) } @@ -326,9 +323,9 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound); walk_list!(visitor, visit_ty, ty); } - ItemKind::Enum(ref enum_definition, ref generics) => { + ItemKind::Enum(box Enum { ref variants, ref generics }) => { visitor.visit_generics(generics); - visitor.visit_enum_def(enum_definition) + walk_list!(visitor, visit_variant, variants); } ItemKind::Impl(box Impl { defaultness: _, @@ -371,10 +368,6 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { walk_list!(visitor, visit_attribute, &item.attrs); } -pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V, enum_definition: &'a EnumDef) { - walk_list!(visitor, visit_variant, &enum_definition.variants); -} - pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant) where V: Visitor<'a>, diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index cdb45cf915404..250cd41e05cbf 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -324,15 +324,13 @@ impl<'hir> LoweringContext<'_, 'hir> { ); hir::ItemKind::TyAlias(ty, generics) } - ItemKind::Enum(ref enum_definition, ref generics) => { + ItemKind::Enum(box Enum { ref variants, ref generics }) => { let (generics, variants) = self.lower_generics( generics, id, ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| { - this.arena.alloc_from_iter( - enum_definition.variants.iter().map(|x| this.lower_variant(x)), - ) + this.arena.alloc_from_iter(variants.iter().map(|x| this.lower_variant(x))) }, ); hir::ItemKind::Enum(hir::EnumDef { variants }, generics) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index ecb62d8b7ad51..2c6376c61ffb3 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1250,8 +1250,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.extern_mod = old_item; return; // Avoid visiting again. } - ItemKind::Enum(ref def, _) => { - for variant in &def.variants { + ItemKind::Enum(box Enum { ref variants, .. }) => { + for variant in variants { self.invalid_visibility(&variant.vis, None); for field in variant.data.fields() { self.invalid_visibility(&field.vis, None); @@ -1547,10 +1547,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s)) } - fn visit_enum_def(&mut self, enum_definition: &'a EnumDef) { - self.with_banned_assoc_ty_bound(|this| visit::walk_enum_def(this, enum_definition)) - } - fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) { // Only associated `fn`s can have `self` parameters. let self_semantic = match fk.ctxt() { diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 6e33c261a03b0..8728e18a9ffb0 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -465,7 +465,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } - ast::ItemKind::Enum(ast::EnumDef { ref variants, .. }, ..) => { + ast::ItemKind::Enum(box ast::Enum { ref variants, .. }) => { for variant in variants { match (&variant.data, &variant.disr_expr) { (ast::VariantData::Unit(..), _) => {} diff --git a/compiler/rustc_ast_passes/src/node_count.rs b/compiler/rustc_ast_passes/src/node_count.rs index be44795b1399b..b7ea783c471d7 100644 --- a/compiler/rustc_ast_passes/src/node_count.rs +++ b/compiler/rustc_ast_passes/src/node_count.rs @@ -91,10 +91,6 @@ impl<'ast> Visitor<'ast> for NodeCounter { self.count += 1; walk_field_def(self, s) } - fn visit_enum_def(&mut self, enum_definition: &EnumDef) { - self.count += 1; - walk_enum_def(self, enum_definition) - } fn visit_variant(&mut self, v: &Variant) { self.count += 1; walk_variant(self, v) diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 64160cd75b80a..b592c3112e3ac 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -240,8 +240,8 @@ impl<'a> State<'a> { defaultness, ); } - ast::ItemKind::Enum(ref enum_definition, ref params) => { - self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis); + ast::ItemKind::Enum(box ast::Enum { ref variants, ref generics }) => { + self.print_enum(variants, generics, item.ident, item.span, &item.vis); } ast::ItemKind::Struct(ref struct_def, ref generics) => { self.head(visibility_qualified(&item.vis, "struct")); @@ -377,9 +377,9 @@ impl<'a> State<'a> { self.ann.post(self, AnnNode::Item(item)) } - fn print_enum_def( + fn print_enum( &mut self, - enum_definition: &ast::EnumDef, + variants: &[ast::Variant], generics: &ast::Generics, ident: Ident, span: rustc_span::Span, @@ -390,7 +390,7 @@ impl<'a> State<'a> { self.print_generic_params(&generics.params); self.print_where_clause(&generics.where_clause); self.space(); - self.print_variants(&enum_definition.variants, span) + self.print_variants(variants, span) } fn print_variants(&mut self, variants: &[ast::Variant], span: rustc_span::Span) { diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index 05c6ac4bc3a27..1e5ac49ccb3e6 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -2,7 +2,7 @@ use crate::deriving::generic::ty::*; use crate::deriving::generic::*; use crate::deriving::path_std; -use rustc_ast::{self as ast, Generics, ItemKind, MetaItem, VariantData}; +use rustc_ast::{self as ast, Enum, Generics, ItemKind, MetaItem, VariantData}; use rustc_data_structures::fx::FxHashSet; use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_span::symbol::{kw, sym, Ident}; @@ -34,7 +34,7 @@ pub fn expand_deriving_clone( match *item { Annotatable::Item(ref annitem) => match annitem.kind { ItemKind::Struct(_, box Generics { ref params, .. }) - | ItemKind::Enum(_, box Generics { ref params, .. }) => { + | ItemKind::Enum(box Enum { variants: _, generics: Generics { ref params, .. } }) => { let container_id = cx.current_expansion.id.expn_data().parent.expect_local(); let has_derive_copy = cx.resolver.has_derive_copy(container_id); if has_derive_copy @@ -137,8 +137,8 @@ fn cs_clone_simple( StaticStruct(vdata, ..) => { process_variant(vdata); } - StaticEnum(enum_def, ..) => { - for variant in &enum_def.variants { + StaticEnum(variants, ..) => { + for variant in variants { process_variant(&variant.data); } } diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs index 4e798bf6acb10..49d1ddfeda3e7 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs @@ -79,8 +79,8 @@ fn cs_total_eq_assert( StaticStruct(vdata, ..) => { process_variant(vdata); } - StaticEnum(enum_def, ..) => { - for variant in &enum_def.variants { + StaticEnum(variants, ..) => { + for variant in variants { process_variant(&variant.data); } } diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs index 5177690917f21..652f103e0eb4f 100644 --- a/compiler/rustc_builtin_macros/src/deriving/default.rs +++ b/compiler/rustc_builtin_macros/src/deriving/default.rs @@ -3,7 +3,6 @@ use crate::deriving::generic::*; use rustc_ast as ast; use rustc_ast::walk_list; -use rustc_ast::EnumDef; use rustc_ast::VariantData; use rustc_errors::Applicability; use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt}; @@ -43,7 +42,7 @@ pub fn expand_deriving_default( StaticStruct(_, fields) => { default_struct_substructure(cx, trait_span, substr, fields) } - StaticEnum(enum_def, _) => default_enum_substructure(cx, trait_span, enum_def), + StaticEnum(variants, _) => default_enum_substructure(cx, trait_span, variants), _ => cx.span_bug(trait_span, "method in `derive(Default)`"), } })), @@ -86,9 +85,9 @@ fn default_struct_substructure( fn default_enum_substructure( cx: &mut ExtCtxt<'_>, trait_span: Span, - enum_def: &EnumDef, + variants: &[ast::Variant], ) -> BlockOrExpr { - let expr = if let Ok(default_variant) = extract_default_variant(cx, enum_def, trait_span) + let expr = if let Ok(default_variant) = extract_default_variant(cx, variants, trait_span) && let Ok(_) = validate_default_attribute(cx, default_variant) { // We now know there is exactly one unit variant with exactly one `#[default]` attribute. @@ -104,11 +103,10 @@ fn default_enum_substructure( fn extract_default_variant<'a>( cx: &mut ExtCtxt<'_>, - enum_def: &'a EnumDef, + variants: &'a [ast::Variant], trait_span: Span, -) -> Result<&'a rustc_ast::Variant, ()> { - let default_variants: SmallVec<[_; 1]> = enum_def - .variants +) -> Result<&'a ast::Variant, ()> { + let default_variants: SmallVec<[_; 1]> = variants .iter() .filter(|variant| cx.sess.contains_name(&variant.attrs, kw::Default)) .collect(); @@ -116,8 +114,7 @@ fn extract_default_variant<'a>( let variant = match default_variants.as_slice() { [variant] => variant, [] => { - let possible_defaults = enum_def - .variants + let possible_defaults = variants .iter() .filter(|variant| matches!(variant.data, VariantData::Unit(..))) .filter(|variant| !cx.sess.contains_name(&variant.attrs, sym::non_exhaustive)); @@ -192,7 +189,7 @@ fn extract_default_variant<'a>( fn validate_default_attribute( cx: &mut ExtCtxt<'_>, - default_variant: &rustc_ast::Variant, + default_variant: &ast::Variant, ) -> Result<(), ()> { let attrs: SmallVec<[_; 1]> = cx.sess.filter_by_name(&default_variant.attrs, kw::Default).collect(); @@ -242,8 +239,8 @@ struct DetectNonVariantDefaultAttr<'a, 'b> { cx: &'a ExtCtxt<'b>, } -impl<'a, 'b> rustc_ast::visit::Visitor<'a> for DetectNonVariantDefaultAttr<'a, 'b> { - fn visit_attribute(&mut self, attr: &'a rustc_ast::Attribute) { +impl<'a, 'b> ast::visit::Visitor<'a> for DetectNonVariantDefaultAttr<'a, 'b> { + fn visit_attribute(&mut self, attr: &'a ast::Attribute) { if attr.has_name(kw::Default) { self.cx .struct_span_err( @@ -253,15 +250,15 @@ impl<'a, 'b> rustc_ast::visit::Visitor<'a> for DetectNonVariantDefaultAttr<'a, ' .emit(); } - rustc_ast::visit::walk_attribute(self, attr); + ast::visit::walk_attribute(self, attr); } - fn visit_variant(&mut self, v: &'a rustc_ast::Variant) { + fn visit_variant(&mut self, v: &'a ast::Variant) { self.visit_ident(v.ident); self.visit_vis(&v.vis); self.visit_variant_data(&v.data); walk_list!(self, visit_anon_const, &v.disr_expr); for attr in &v.attrs { - rustc_ast::visit::walk_attribute(self, attr); + ast::visit::walk_attribute(self, attr); } } } diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 735017aa5a850..d52b033297f2d 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -154,7 +154,7 @@ //! //! StaticStruct(, Unnamed(vec![])) //! -//! StaticEnum(, +//! StaticEnum(<&[ast::Variants] of C>, //! vec![(, , Unnamed(vec![])), //! (, , Named(vec![(, )]))]) //! ``` @@ -167,7 +167,7 @@ use std::iter; use std::vec; use rustc_ast::ptr::P; -use rustc_ast::{self as ast, EnumDef, Expr, Generics, PatKind}; +use rustc_ast::{self as ast, Expr, Generics, PatKind}; use rustc_ast::{GenericArg, GenericParamKind, VariantData}; use rustc_attr as attr; use rustc_expand::base::{Annotatable, ExtCtxt}; @@ -280,7 +280,7 @@ pub enum SubstructureFields<'a> { StaticStruct(&'a ast::VariantData, StaticFields), /// A static method where `Self` is an enum. - StaticEnum(&'a ast::EnumDef, Vec<(Ident, Span, StaticFields)>), + StaticEnum(&'a [ast::Variant], Vec<(Ident, Span, StaticFields)>), } /// Combine the values of all the fields together. The last argument is @@ -443,9 +443,9 @@ impl<'a> TraitDef<'a> { false }); let has_no_type_params = match item.kind { - ast::ItemKind::Struct(_, ref generics) - | ast::ItemKind::Enum(_, ref generics) - | ast::ItemKind::Union(_, ref generics) => !generics + ast::ItemKind::Struct(_, box ref generics) + | ast::ItemKind::Enum(box ast::Enum { ref generics, .. }) + | ast::ItemKind::Union(_, box ref generics) => !generics .params .iter() .any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. })), @@ -464,14 +464,14 @@ impl<'a> TraitDef<'a> { is_packed, always_copy, ), - ast::ItemKind::Enum(ref enum_def, ref generics) => { + ast::ItemKind::Enum(box ast::Enum { ref variants, ref generics }) => { // We ignore `is_packed`/`always_copy` here, because // `repr(packed)` enums cause an error later on. // // This can only cause further compilation errors // downstream in blatantly illegal code, so it // is fine. - self.expand_enum_def(cx, enum_def, item.ident, generics, from_scratch) + self.expand_enum_def(cx, variants, item.ident, generics, from_scratch) } ast::ItemKind::Union(ref struct_def, ref generics) => { if self.supports_unions { @@ -807,14 +807,14 @@ impl<'a> TraitDef<'a> { fn expand_enum_def( &self, cx: &mut ExtCtxt<'_>, - enum_def: &'a EnumDef, + variants: &'a [ast::Variant], type_ident: Ident, generics: &Generics, from_scratch: bool, ) -> P { let mut field_tys = Vec::new(); - for variant in &enum_def.variants { + for variant in variants { field_tys.extend(variant.data.fields().iter().map(|field| field.ty.clone())); } @@ -829,7 +829,7 @@ impl<'a> TraitDef<'a> { method_def.expand_static_enum_method_body( cx, self, - enum_def, + variants, type_ident, &nonselflike_args, ) @@ -837,7 +837,7 @@ impl<'a> TraitDef<'a> { method_def.expand_enum_method_body( cx, self, - enum_def, + variants, type_ident, selflike_args, &nonselflike_args, @@ -1145,13 +1145,12 @@ impl<'a> MethodDef<'a> { &self, cx: &mut ExtCtxt<'_>, trait_: &TraitDef<'b>, - enum_def: &'b EnumDef, + variants: &'b [ast::Variant], type_ident: Ident, selflike_args: Vec>, nonselflike_args: &[P], ) -> BlockOrExpr { let span = trait_.span; - let variants = &enum_def.variants; // Traits that unify fieldless variants always use the tag(s). let uses_tags = self.unify_fieldless_variants; @@ -1373,12 +1372,11 @@ impl<'a> MethodDef<'a> { &self, cx: &mut ExtCtxt<'_>, trait_: &TraitDef<'_>, - enum_def: &EnumDef, + variants: &[ast::Variant], type_ident: Ident, nonselflike_args: &[P], ) -> BlockOrExpr { - let summary = enum_def - .variants + let summary = variants .iter() .map(|v| { let sp = v.span.with_ctxt(trait_.span.ctxt()); @@ -1391,7 +1389,7 @@ impl<'a> MethodDef<'a> { trait_, type_ident, nonselflike_args, - &StaticEnum(enum_def, summary), + &StaticEnum(variants, summary), ) } } @@ -1643,8 +1641,8 @@ where pub fn is_type_without_fields(item: &Annotatable) -> bool { if let Annotatable::Item(ref item) = *item { match item.kind { - ast::ItemKind::Enum(ref enum_def, _) => { - enum_def.variants.iter().all(|v| v.data.fields().is_empty()) + ast::ItemKind::Enum(box ast::Enum { ref variants, .. }) => { + variants.iter().all(|v| v.data.fields().is_empty()) } ast::ItemKind::Struct(ref variant_data, _) => variant_data.fields().is_empty(), _ => false, diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs index b6c225af65c87..f31b69acca252 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::{GenericArg, Impl, ItemKind, MetaItem}; +use rustc_ast::{Enum, GenericArg, Impl, ItemKind, MetaItem}; use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier}; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::Span; @@ -121,7 +121,9 @@ fn inject_impl_of_structural_trait( }; let generics = match item.kind { - ItemKind::Struct(_, ref generics) | ItemKind::Enum(_, ref generics) => generics, + ItemKind::Struct(_, box ref generics) | ItemKind::Enum(box Enum { ref generics, .. }) => { + generics + } // Do not inject `impl Structural for Union`. (`PartialEq` does not // support unions, so we will see error downstream.) ItemKind::Union(..) => return, @@ -184,7 +186,7 @@ fn inject_impl_of_structural_trait( polarity: ast::ImplPolarity::Positive, defaultness: ast::Defaultness::Final, constness: ast::Const::No, - generics: *generics, + generics, of_trait: Some(trait_ref), self_ty: self_type, items: Vec::new(), diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 6e093811fcf6e..69305bcee9b88 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -1399,8 +1399,8 @@ pub fn parse_macro_name_and_helper_attrs( fn pretty_printing_compatibility_hack(item: &Item, sess: &ParseSess) -> bool { let name = item.ident.name; if name == sym::ProceduralMasqueradeDummyType { - if let ast::ItemKind::Enum(enum_def, _) = &item.kind { - if let [variant] = &*enum_def.variants { + if let ast::ItemKind::Enum(box ast::Enum { ref variants, .. }) = &item.kind { + if let [variant] = variants.as_slice() { if variant.ident.name == sym::Input { sess.buffer_lint_with_diagnostic( &PROC_MACRO_BACK_COMPAT, diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 3ac377d1737e2..de9568a838426 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -673,7 +673,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { if matches!(attr.style, AttrStyle::Inner) && matches!( item_inner.kind, - ItemKind::Mod(_, ref mod_kind) if + ItemKind::Mod(_, ref mod_kind) if matches!(&**mod_kind, ModKind::Unloaded | ModKind::Loaded(_, Inline::No, _)), ) => { diff --git a/compiler/rustc_expand/src/lib.rs b/compiler/rustc_expand/src/lib.rs index 20d01b6dc2688..e4cddc289640a 100644 --- a/compiler/rustc_expand/src/lib.rs +++ b/compiler/rustc_expand/src/lib.rs @@ -1,6 +1,7 @@ #![feature(array_windows)] #![feature(associated_type_bounds)] #![feature(associated_type_defaults)] +#![feature(box_patterns)] #![feature(if_let_guard)] #![cfg_attr(bootstrap, feature(let_chains))] #![feature(let_else)] diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 0a257ea68c718..c0b0d40a591ba 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -9,7 +9,7 @@ use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree}; use rustc_ast::{self as ast, AttrVec, Attribute, DUMMY_NODE_ID}; use rustc_ast::{Async, Const, Defaultness, IsAuto, Mutability, Unsafe, UseTree, UseTreeKind}; use rustc_ast::{BindingMode, Block, FnDecl, FnSig, Param, SelfKind}; -use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, VariantData}; +use rustc_ast::{Enum, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, VariantData}; use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind}; use rustc_ast::{MacArgs, MacCall, MacDelimiter}; use rustc_ast_pretty::pprust; @@ -1257,8 +1257,8 @@ impl<'a> Parser<'a> { e })?; - let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() }; - Ok((id, ItemKind::Enum(enum_definition, Box::new(generics)))) + let variants = variants.into_iter().flatten().collect(); + Ok((id, ItemKind::Enum(Box::new(Enum { variants, generics })))) } fn parse_enum_variant(&mut self) -> PResult<'a, Option> { diff --git a/compiler/rustc_resolve/src/access_levels.rs b/compiler/rustc_resolve/src/access_levels.rs index 4fdf8f689b844..5ebc48b9e785a 100644 --- a/compiler/rustc_resolve/src/access_levels.rs +++ b/compiler/rustc_resolve/src/access_levels.rs @@ -2,7 +2,6 @@ use rustc_ast::ast; use rustc_ast::visit; use rustc_ast::visit::Visitor; use rustc_ast::Crate; -use rustc_ast::EnumDef; use rustc_ast::NodeId; use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::CRATE_DEF_ID; @@ -190,7 +189,7 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> { } } } - ast::ItemKind::Enum(EnumDef { ref variants }, _) => { + ast::ItemKind::Enum(box ast::Enum { ref variants, .. }) => { for variant in variants { let variant_level = self.set_access_level(variant.id, access_level); if let Some(ctor_id) = variant.data.ctor_id() { diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index e955a1798b735..9563759e741d4 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -735,7 +735,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); } - ItemKind::Enum(_, _) => { + ItemKind::Enum(..) => { let module = self.r.new_module( Some(parent), ModuleKind::Def(DefKind::Enum, def_id, ident.name), diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 44017b7833b9b..7760898a53d80 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2164,9 +2164,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ); } - ItemKind::Enum(_, ref generics) - | ItemKind::Struct(_, ref generics) - | ItemKind::Union(_, ref generics) => { + ItemKind::Enum(box Enum { ref generics, .. }) + | ItemKind::Struct(_, box ref generics) + | ItemKind::Union(_, box ref generics) => { self.resolve_adt(item, generics); } @@ -3943,7 +3943,7 @@ impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> { match &item.kind { ItemKind::TyAlias(box TyAlias { ref generics, .. }) | ItemKind::Fn(box Fn { ref generics, .. }) - | ItemKind::Enum(_, box ref generics) + | ItemKind::Enum(box Enum { ref generics, .. }) | ItemKind::Struct(_, box ref generics) | ItemKind::Union(_, box ref generics) | ItemKind::Impl(box Impl { ref generics, .. }) diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 4b873c30858d1..622caa8098ccb 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -305,7 +305,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { && over(lb, rb, eq_generic_bound) && both(lt, rt, |l, r| eq_ty(l, r)) }, - (Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg), + ( + Enum(box ast::Enum { variants: lv, generics: lg }), + Enum(box ast::Enum { variants: rv, generics: rg }) + ) => over(lv, rv, eq_variant) && eq_generics(lg, rg), (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => { eq_variant_data(lv, rv) && eq_generics(lg, rg) }, diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index c030b04abeabd..15c09e2d22a67 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -411,7 +411,7 @@ impl<'a> FmtVisitor<'a> { &mut self, ident: symbol::Ident, vis: &ast::Visibility, - enum_def: &ast::EnumDef, + variants: &[ast::Variant], generics: &ast::Generics, span: Span, ) { @@ -426,7 +426,7 @@ impl<'a> FmtVisitor<'a> { &self.get_context(), generics, self.config.brace_style(), - if enum_def.variants.is_empty() { + if variants.is_empty() { BracePos::ForceSameLine } else { BracePos::Auto @@ -441,8 +441,8 @@ impl<'a> FmtVisitor<'a> { self.last_pos = body_start; - match self.format_variant_list(enum_def, body_start, span.hi()) { - Some(ref s) if enum_def.variants.is_empty() => self.push_str(s), + match self.format_variant_list(variants, body_start, span.hi()) { + Some(ref s) if variants.is_empty() => self.push_str(s), rw => { self.push_rewrite(mk_sp(body_start, span.hi()), rw); self.block_indent = self.block_indent.block_unindent(self.config); @@ -453,11 +453,11 @@ impl<'a> FmtVisitor<'a> { // Format the body of an enum definition fn format_variant_list( &mut self, - enum_def: &ast::EnumDef, + variants: &[ast::Variant], body_lo: BytePos, body_hi: BytePos, ) -> Option { - if enum_def.variants.is_empty() { + if variants.is_empty() { let mut buffer = String::with_capacity(128); // 1 = "}" let span = mk_sp(body_lo, body_hi - BytePos(1)); @@ -478,8 +478,7 @@ impl<'a> FmtVisitor<'a> { // If enum variants have discriminants, try to vertically align those, // provided the discrims are not shifted too much to the right let align_threshold: usize = self.config.enum_discrim_align_threshold(); - let discr_ident_lens: Vec = enum_def - .variants + let discr_ident_lens: Vec = variants .iter() .filter(|var| var.disr_expr.is_some()) .map(|var| rewrite_ident(&self.get_context(), var.ident).len()) @@ -495,7 +494,7 @@ impl<'a> FmtVisitor<'a> { let itemize_list_with = |one_line_width: usize| { itemize_list( self.snippet_provider, - enum_def.variants.iter(), + variants.iter(), "}", ",", |f| { diff --git a/src/tools/rustfmt/src/visitor.rs b/src/tools/rustfmt/src/visitor.rs index 9a0e0752c12f5..bfda5b78a3543 100644 --- a/src/tools/rustfmt/src/visitor.rs +++ b/src/tools/rustfmt/src/visitor.rs @@ -518,9 +518,15 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { ast::ItemKind::Struct(..) | ast::ItemKind::Union(..) => { self.visit_struct(&StructParts::from_item(item)); } - ast::ItemKind::Enum(ref def, ref generics) => { + ast::ItemKind::Enum(ref enum_) => { self.format_missing_with_indent(source!(self, item.span).lo()); - self.visit_enum(item.ident, &item.vis, def, generics, item.span); + self.visit_enum( + item.ident, + &item.vis, + &enum_.variants, + &enum_.generics, + item.span, + ); self.last_pos = source!(self, item.span).hi(); } ast::ItemKind::Mod(unsafety, ref mod_kind) => { From e71f89da28bea965bca4a4a3ece87334fff85f47 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 15:50:17 +1000 Subject: [PATCH 11/14] XXX: redoing Struct/Union/TraitAlias --- compiler/rustc_ast/src/ast.rs | 28 +++-- compiler/rustc_ast/src/mut_visit.rs | 7 +- compiler/rustc_ast/src/visit.rs | 8 +- compiler/rustc_ast_lowering/src/item.rs | 12 +- .../rustc_ast_passes/src/ast_validation.rs | 4 +- .../rustc_ast_pretty/src/pprust/state/item.rs | 18 +-- .../src/deriving/clone.rs | 6 +- .../src/deriving/generic/mod.rs | 110 +++++++++--------- .../rustc_builtin_macros/src/deriving/mod.rs | 7 +- compiler/rustc_parse/src/parser/item.rs | 6 +- compiler/rustc_resolve/src/access_levels.rs | 7 +- .../rustc_resolve/src/build_reduced_graph.rs | 6 +- compiler/rustc_resolve/src/def_collector.rs | 5 +- compiler/rustc_resolve/src/late.rs | 12 +- .../src/empty_structs_with_brackets.rs | 16 +-- .../clippy_lints/src/excessive_bools.rs | 8 +- .../clippy_lints/src/manual_non_exhaustive.rs | 4 +- .../clippy/clippy_utils/src/ast_utils.rs | 13 ++- src/tools/rustfmt/src/items.rs | 16 +-- src/tools/rustfmt/src/visitor.rs | 6 +- 20 files changed, 162 insertions(+), 137 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index b876684ab3707..a8a9db62430e9 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2810,6 +2810,18 @@ pub struct Enum { pub generics: Generics, } +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct Struct { + pub vdata: VariantData, + pub generics: Generics, +} + +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct TraitAlias { + pub generics: Generics, + pub bounds: GenericBounds, +} + #[derive(Clone, Encodable, Decodable, Debug)] pub enum ItemKind { /// An `extern crate` item, with the optional *original* crate name if the crate was renamed. @@ -2855,11 +2867,11 @@ pub enum ItemKind { /// A struct definition (`struct`). /// /// E.g., `struct Foo { x: A }`. - Struct(VariantData, Box), + Struct(Box), /// A union definition (`union`). /// /// E.g., `union Foo { x: A, y: B }`. - Union(VariantData, Box), + Union(Box), /// A trait declaration (`trait`). /// /// E.g., `trait Foo { .. }`, `trait Foo { .. }` or `auto trait Foo {}`. @@ -2867,7 +2879,7 @@ pub enum ItemKind { /// Trait alias /// /// E.g., `trait Foo = Bar + Quux;`. - TraitAlias(Box, GenericBounds), + TraitAlias(Box), /// An implementation. /// /// E.g., `impl Foo { .. }` or `impl Trait for Foo { .. }`. @@ -2918,10 +2930,10 @@ impl ItemKind { Self::Fn(box Fn { generics, .. }) | Self::TyAlias(box TyAlias { generics, .. }) | Self::Enum(box Enum { generics, .. }) - | Self::Struct(_, box generics) - | Self::Union(_, box generics) + | Self::Struct(box Struct { generics, .. }) + | Self::Union(box Struct { generics, .. }) | Self::Trait(box Trait { generics, .. }) - | Self::TraitAlias(box generics, _) + | Self::TraitAlias(box TraitAlias { generics, .. }) | Self::Impl(box Impl { generics, .. }) => Some(generics), _ => None, } @@ -3045,8 +3057,8 @@ mod size_asserts { static_assert_size!(GenericBound, 88); static_assert_size!(Generics, 72); static_assert_size!(Impl, 200); - static_assert_size!(Item, 136); - static_assert_size!(ItemKind, 48); + static_assert_size!(Item, 120); + static_assert_size!(ItemKind, 32); static_assert_size!(Lit, 48); static_assert_size!(Pat, 120); static_assert_size!(Path, 40); diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 84b4d17b2c63e..873bf7cacfe25 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1052,8 +1052,9 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { variants.flat_map_in_place(|variant| vis.flat_map_variant(variant)); vis.visit_generics(generics); } - ItemKind::Struct(variant_data, generics) | ItemKind::Union(variant_data, generics) => { - vis.visit_variant_data(variant_data); + ItemKind::Struct(box Struct { vdata, generics }) + | ItemKind::Union(box Struct { vdata, generics }) => { + vis.visit_variant_data(vdata); vis.visit_generics(generics); } ItemKind::Impl(box Impl { @@ -1081,7 +1082,7 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { visit_bounds(bounds, vis); items.flat_map_in_place(|item| vis.flat_map_trait_item(item)); } - ItemKind::TraitAlias(generics, bounds) => { + ItemKind::TraitAlias(box TraitAlias { generics, bounds }) => { vis.visit_generics(generics); visit_bounds(bounds, vis); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 3c2900c574341..03638e134a681 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -342,10 +342,10 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { visitor.visit_ty(self_ty); walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl); } - ItemKind::Struct(ref struct_definition, ref generics) - | ItemKind::Union(ref struct_definition, ref generics) => { + ItemKind::Struct(box Struct { ref vdata, ref generics }) + | ItemKind::Union(box Struct { ref vdata, ref generics }) => { visitor.visit_generics(generics); - visitor.visit_variant_data(struct_definition); + visitor.visit_variant_data(vdata); } ItemKind::Trait(box Trait { unsafety: _, @@ -358,7 +358,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { walk_list!(visitor, visit_param_bound, bounds, BoundKind::SuperTraits); walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait); } - ItemKind::TraitAlias(ref generics, ref bounds) => { + ItemKind::TraitAlias(box TraitAlias { ref generics, ref bounds }) => { visitor.visit_generics(generics); walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound); } diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 250cd41e05cbf..bf819f5afa953 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -335,16 +335,16 @@ impl<'hir> LoweringContext<'_, 'hir> { ); hir::ItemKind::Enum(hir::EnumDef { variants }, generics) } - ItemKind::Struct(ref struct_def, ref generics) => { - let (generics, struct_def) = self.lower_generics( + ItemKind::Struct(box Struct { ref vdata, ref generics }) => { + let (generics, vdata) = self.lower_generics( generics, id, ImplTraitContext::Disallowed(ImplTraitPosition::Generic), - |this| this.lower_variant_data(hir_id, struct_def), + |this| this.lower_variant_data(hir_id, vdata), ); - hir::ItemKind::Struct(struct_def, generics) + hir::ItemKind::Struct(vdata, generics) } - ItemKind::Union(ref vdata, ref generics) => { + ItemKind::Union(box Struct { ref vdata, ref generics }) => { let (generics, vdata) = self.lower_generics( generics, id, @@ -441,7 +441,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ); hir::ItemKind::Trait(is_auto, unsafety, generics, bounds, items) } - ItemKind::TraitAlias(ref generics, ref bounds) => { + ItemKind::TraitAlias(box TraitAlias { ref generics, ref bounds }) => { let (generics, bounds) = self.lower_generics( generics, id, diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 2c6376c61ffb3..8b82127b7efe7 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1290,7 +1290,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.check_mod_file_item_asciionly(item.ident); } } - ItemKind::Struct(ref vdata, ref generics) => match vdata { + ItemKind::Struct(box Struct { ref vdata, ref generics }) => match vdata { // Duplicating the `Visitor` logic allows catching all cases // of `Anonymous(Struct, Union)` outside of a field struct or union. // @@ -1309,7 +1309,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } _ => {} }, - ItemKind::Union(ref vdata, ref generics) => { + ItemKind::Union(box Struct { ref vdata, ref generics }) => { if vdata.fields().is_empty() { self.err_handler().span_err(item.span, "unions cannot have zero fields"); } diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index b592c3112e3ac..6684fdc6fa819 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -243,13 +243,13 @@ impl<'a> State<'a> { ast::ItemKind::Enum(box ast::Enum { ref variants, ref generics }) => { self.print_enum(variants, generics, item.ident, item.span, &item.vis); } - ast::ItemKind::Struct(ref struct_def, ref generics) => { + ast::ItemKind::Struct(box ast::Struct { ref vdata, ref generics }) => { self.head(visibility_qualified(&item.vis, "struct")); - self.print_struct(struct_def, generics, item.ident, item.span, true); + self.print_struct(vdata, generics, item.ident, item.span, true); } - ast::ItemKind::Union(ref struct_def, ref generics) => { + ast::ItemKind::Union(box ast::Struct { ref vdata, ref generics }) => { self.head(visibility_qualified(&item.vis, "union")); - self.print_struct(struct_def, generics, item.ident, item.span, true); + self.print_struct(vdata, generics, item.ident, item.span, true); } ast::ItemKind::Impl(box ast::Impl { unsafety, @@ -337,7 +337,7 @@ impl<'a> State<'a> { let empty = item.attrs.is_empty() && items.is_empty(); self.bclose(item.span, empty); } - ast::ItemKind::TraitAlias(ref generics, ref bounds) => { + ast::ItemKind::TraitAlias(box ast::TraitAlias { ref generics, ref bounds }) => { self.head(visibility_qualified(&item.vis, "trait")); self.print_ident(item.ident); self.print_generic_params(&generics.params); @@ -455,7 +455,7 @@ impl<'a> State<'a> { fn print_struct( &mut self, - struct_def: &ast::VariantData, + variant_data: &ast::VariantData, generics: &ast::Generics, ident: Ident, span: rustc_span::Span, @@ -463,11 +463,11 @@ impl<'a> State<'a> { ) { self.print_ident(ident); self.print_generic_params(&generics.params); - match struct_def { + match variant_data { ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => { - if let ast::VariantData::Tuple(..) = struct_def { + if let ast::VariantData::Tuple(..) = variant_data { self.popen(); - self.commasep(Inconsistent, struct_def.fields(), |s, field| { + self.commasep(Inconsistent, variant_data.fields(), |s, field| { s.maybe_print_comment(field.span.lo()); s.print_outer_attributes(&field.attrs); s.print_visibility(&field.vis); diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index 1e5ac49ccb3e6..05e4e94a90b56 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -2,7 +2,7 @@ use crate::deriving::generic::ty::*; use crate::deriving::generic::*; use crate::deriving::path_std; -use rustc_ast::{self as ast, Enum, Generics, ItemKind, MetaItem, VariantData}; +use rustc_ast::{self as ast, Enum, Generics, ItemKind, MetaItem, Struct, VariantData}; use rustc_data_structures::fx::FxHashSet; use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_span::symbol::{kw, sym, Ident}; @@ -33,8 +33,8 @@ pub fn expand_deriving_clone( let is_simple; match *item { Annotatable::Item(ref annitem) => match annitem.kind { - ItemKind::Struct(_, box Generics { ref params, .. }) - | ItemKind::Enum(box Enum { variants: _, generics: Generics { ref params, .. } }) => { + ItemKind::Struct(box Struct { generics: Generics { ref params, .. }, .. }) + | ItemKind::Enum(box Enum { generics: Generics { ref params, .. }, .. }) => { let container_id = cx.current_expansion.id.expn_data().parent.expect_local(); let has_derive_copy = cx.resolver.has_derive_copy(container_id); if has_derive_copy diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index d52b033297f2d..3bb38074bb1cf 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -443,9 +443,9 @@ impl<'a> TraitDef<'a> { false }); let has_no_type_params = match item.kind { - ast::ItemKind::Struct(_, box ref generics) - | ast::ItemKind::Enum(box ast::Enum { ref generics, .. }) - | ast::ItemKind::Union(_, box ref generics) => !generics + ast::ItemKind::Enum(box ast::Enum { ref generics, .. }) + | ast::ItemKind::Struct(box ast::Struct { ref generics, .. }) + | ast::ItemKind::Union(box ast::Struct { ref generics, .. }) => !generics .params .iter() .any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. })), @@ -455,15 +455,16 @@ impl<'a> TraitDef<'a> { let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id); let newitem = match item.kind { - ast::ItemKind::Struct(ref struct_def, ref generics) => self.expand_struct_def( - cx, - &struct_def, - item.ident, - generics, - from_scratch, - is_packed, - always_copy, - ), + ast::ItemKind::Struct(box ast::Struct { ref vdata, ref generics }) => self + .expand_struct_def( + cx, + &vdata, + item.ident, + generics, + from_scratch, + is_packed, + always_copy, + ), ast::ItemKind::Enum(box ast::Enum { ref variants, ref generics }) => { // We ignore `is_packed`/`always_copy` here, because // `repr(packed)` enums cause an error later on. @@ -473,11 +474,11 @@ impl<'a> TraitDef<'a> { // is fine. self.expand_enum_def(cx, variants, item.ident, generics, from_scratch) } - ast::ItemKind::Union(ref struct_def, ref generics) => { + ast::ItemKind::Union(box ast::Struct { ref vdata, ref generics }) => { if self.supports_unions { self.expand_struct_def( cx, - &struct_def, + &vdata, item.ident, generics, from_scratch, @@ -751,7 +752,7 @@ impl<'a> TraitDef<'a> { fn expand_struct_def( &self, cx: &mut ExtCtxt<'_>, - struct_def: &'a VariantData, + vdata: &'a VariantData, type_ident: Ident, generics: &Generics, from_scratch: bool, @@ -759,7 +760,7 @@ impl<'a> TraitDef<'a> { always_copy: bool, ) -> P { let field_tys: Vec> = - struct_def.fields().iter().map(|field| field.ty.clone()).collect(); + vdata.fields().iter().map(|field| field.ty.clone()).collect(); let methods = self .methods @@ -772,7 +773,7 @@ impl<'a> TraitDef<'a> { method_def.expand_static_struct_method_body( cx, self, - struct_def, + vdata, type_ident, &nonselflike_args, ) @@ -780,7 +781,7 @@ impl<'a> TraitDef<'a> { method_def.expand_struct_method_body( cx, self, - struct_def, + vdata, type_ident, &selflike_args, &nonselflike_args, @@ -1040,7 +1041,7 @@ impl<'a> MethodDef<'a> { &self, cx: &mut ExtCtxt<'_>, trait_: &TraitDef<'b>, - struct_def: &'b VariantData, + vdata: &'b VariantData, type_ident: Ident, selflike_args: &[P], nonselflike_args: &[P], @@ -1056,17 +1057,17 @@ impl<'a> MethodDef<'a> { trait_, type_ident, nonselflike_args, - &Struct(struct_def, selflike_fields), + &Struct(vdata, selflike_fields), ) }; if !is_packed { let selflike_fields = - trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, false); + trait_.create_struct_field_access_fields(cx, selflike_args, vdata, false); mk_body(cx, selflike_fields) } else if always_copy { let selflike_fields = - trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, true); + trait_.create_struct_field_access_fields(cx, selflike_args, vdata, true); mk_body(cx, selflike_fields) } else { // Neither packed nor copy. Need to use ref patterns. @@ -1074,13 +1075,13 @@ impl<'a> MethodDef<'a> { (0..selflike_args.len()).map(|i| format!("__self_{}", i)).collect(); let addr_of = always_copy; let selflike_fields = - trait_.create_struct_pattern_fields(cx, struct_def, &prefixes, addr_of); + trait_.create_struct_pattern_fields(cx, vdata, &prefixes, addr_of); let mut body = mk_body(cx, selflike_fields); let struct_path = cx.path(span, vec![Ident::new(kw::SelfUpper, type_ident.span)]); let use_ref_pat = is_packed && !always_copy; let patterns = - trait_.create_struct_patterns(cx, struct_path, struct_def, &prefixes, use_ref_pat); + trait_.create_struct_patterns(cx, struct_path, vdata, &prefixes, use_ref_pat); // Do the let-destructuring. let mut stmts: Vec<_> = iter::zip(selflike_args, patterns) @@ -1098,18 +1099,18 @@ impl<'a> MethodDef<'a> { &self, cx: &mut ExtCtxt<'_>, trait_: &TraitDef<'_>, - struct_def: &VariantData, + vdata: &VariantData, type_ident: Ident, nonselflike_args: &[P], ) -> BlockOrExpr { - let summary = trait_.summarise_struct(cx, struct_def); + let summary = trait_.summarise_struct(cx, vdata); self.call_substructure_method( cx, trait_, type_ident, nonselflike_args, - &StaticStruct(struct_def, summary), + &StaticStruct(vdata, summary), ) } @@ -1396,10 +1397,10 @@ impl<'a> MethodDef<'a> { // general helper methods. impl<'a> TraitDef<'a> { - fn summarise_struct(&self, cx: &mut ExtCtxt<'_>, struct_def: &VariantData) -> StaticFields { + fn summarise_struct(&self, cx: &mut ExtCtxt<'_>, vdata: &VariantData) -> StaticFields { let mut named_idents = Vec::new(); let mut just_spans = Vec::new(); - for field in struct_def.fields() { + for field in vdata.fields() { let sp = field.span.with_ctxt(self.span.ctxt()); match field.ident { Some(ident) => named_idents.push((ident, sp)), @@ -1407,7 +1408,7 @@ impl<'a> TraitDef<'a> { } } - let is_tuple = matches!(struct_def, ast::VariantData::Tuple(..)); + let is_tuple = matches!(vdata, ast::VariantData::Tuple(..)); match (just_spans.is_empty(), named_idents.is_empty()) { (false, false) => { cx.span_bug(self.span, "a struct with named and unnamed fields in generic `derive`") @@ -1425,32 +1426,31 @@ impl<'a> TraitDef<'a> { &self, cx: &mut ExtCtxt<'_>, struct_path: ast::Path, - struct_def: &'a VariantData, + vdata: &'a VariantData, prefixes: &[String], use_ref_pat: bool, ) -> Vec> { prefixes .iter() .map(|prefix| { - let pieces_iter = - struct_def.fields().iter().enumerate().map(|(i, struct_field)| { - let sp = struct_field.span.with_ctxt(self.span.ctxt()); - let binding_mode = if use_ref_pat { - ast::BindingMode::ByRef(ast::Mutability::Not) - } else { - ast::BindingMode::ByValue(ast::Mutability::Not) - }; - let ident = self.mk_pattern_ident(prefix, i); - let path = ident.with_span_pos(sp); - ( - sp, - struct_field.ident, - cx.pat(path.span, PatKind::Ident(binding_mode, path, None)), - ) - }); + let pieces_iter = vdata.fields().iter().enumerate().map(|(i, struct_field)| { + let sp = struct_field.span.with_ctxt(self.span.ctxt()); + let binding_mode = if use_ref_pat { + ast::BindingMode::ByRef(ast::Mutability::Not) + } else { + ast::BindingMode::ByValue(ast::Mutability::Not) + }; + let ident = self.mk_pattern_ident(prefix, i); + let path = ident.with_span_pos(sp); + ( + sp, + struct_field.ident, + cx.pat(path.span, PatKind::Ident(binding_mode, path, None)), + ) + }); let struct_path = struct_path.clone(); - match *struct_def { + match *vdata { VariantData::Struct(..) => { let field_pats = pieces_iter .map(|(sp, ident, pat)| { @@ -1483,11 +1483,11 @@ impl<'a> TraitDef<'a> { .collect() } - fn create_fields(&self, struct_def: &'a VariantData, mk_exprs: F) -> Vec + fn create_fields(&self, vdata: &'a VariantData, mk_exprs: F) -> Vec where F: Fn(usize, &ast::FieldDef, Span) -> Vec>, { - struct_def + vdata .fields() .iter() .enumerate() @@ -1515,11 +1515,11 @@ impl<'a> TraitDef<'a> { fn create_struct_pattern_fields( &self, cx: &mut ExtCtxt<'_>, - struct_def: &'a VariantData, + vdata: &'a VariantData, prefixes: &[String], addr_of: bool, ) -> Vec { - self.create_fields(struct_def, |i, _struct_field, sp| { + self.create_fields(vdata, |i, _struct_field, sp| { prefixes .iter() .map(|prefix| { @@ -1535,10 +1535,10 @@ impl<'a> TraitDef<'a> { &self, cx: &mut ExtCtxt<'_>, selflike_args: &[P], - struct_def: &'a VariantData, + vdata: &'a VariantData, copy: bool, ) -> Vec { - self.create_fields(struct_def, |i, struct_field, sp| { + self.create_fields(vdata, |i, struct_field, sp| { selflike_args .iter() .map(|selflike_arg| { @@ -1644,7 +1644,7 @@ pub fn is_type_without_fields(item: &Annotatable) -> bool { ast::ItemKind::Enum(box ast::Enum { ref variants, .. }) => { variants.iter().all(|v| v.data.fields().is_empty()) } - ast::ItemKind::Struct(ref variant_data, _) => variant_data.fields().is_empty(), + ast::ItemKind::Struct(box ast::Struct { ref vdata, .. }) => vdata.fields().is_empty(), _ => false, } } else { diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs index f31b69acca252..e6a5348e6dd5d 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::{Enum, GenericArg, Impl, ItemKind, MetaItem}; +use rustc_ast::{Enum, GenericArg, Impl, ItemKind, MetaItem, Struct}; use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier}; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::Span; @@ -121,9 +121,8 @@ fn inject_impl_of_structural_trait( }; let generics = match item.kind { - ItemKind::Struct(_, box ref generics) | ItemKind::Enum(box Enum { ref generics, .. }) => { - generics - } + ItemKind::Struct(box Struct { ref generics, .. }) + | ItemKind::Enum(box Enum { ref generics, .. }) => generics, // Do not inject `impl Structural for Union`. (`PartialEq` does not // support unions, so we will see error downstream.) ItemKind::Union(..) => return, diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index c0b0d40a591ba..54546e0d70195 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -782,7 +782,7 @@ impl<'a> Parser<'a> { self.sess.gated_spans.gate(sym::trait_alias, whole_span); - Ok((ident, ItemKind::TraitAlias(Box::new(generics), bounds))) + Ok((ident, ItemKind::TraitAlias(Box::new(TraitAlias { generics, bounds })))) } else { // It's a normal trait. generics.where_clause = self.parse_where_clause()?; @@ -1359,7 +1359,7 @@ impl<'a> Parser<'a> { return Err(err); }; - Ok((class_name, ItemKind::Struct(vdata, Box::new(generics)))) + Ok((class_name, ItemKind::Struct(Box::new(Struct { vdata, generics })))) } /// Parses `union Foo { ... }`. @@ -1385,7 +1385,7 @@ impl<'a> Parser<'a> { return Err(err); }; - Ok((class_name, ItemKind::Union(vdata, Box::new(generics)))) + Ok((class_name, ItemKind::Union(Box::new(Struct { vdata, generics })))) } fn parse_record_struct_body( diff --git a/compiler/rustc_resolve/src/access_levels.rs b/compiler/rustc_resolve/src/access_levels.rs index 5ebc48b9e785a..fb327a5730e5a 100644 --- a/compiler/rustc_resolve/src/access_levels.rs +++ b/compiler/rustc_resolve/src/access_levels.rs @@ -201,12 +201,13 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> { } } } - ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => { - if let Some(ctor_id) = def.ctor_id() { + ast::ItemKind::Struct(box ast::Struct { ref vdata, .. }) + | ast::ItemKind::Union(box ast::Struct { ref vdata, .. }) => { + if let Some(ctor_id) = vdata.ctor_id() { self.set_access_level(ctor_id, access_level); } - for field in def.fields() { + for field in vdata.fields() { if field.vis.kind.is_pub() { self.set_access_level(field.id, access_level); } diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 9563759e741d4..5b7ec3b39ac59 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -17,7 +17,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, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId}; +use rustc_ast::{Block, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId, Struct}; use rustc_attr as attr; use rustc_data_structures::sync::Lrc; use rustc_errors::{struct_span_err, Applicability}; @@ -753,7 +753,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { } // These items live in both the type and value namespaces. - ItemKind::Struct(ref vdata, _) => { + ItemKind::Struct(box Struct { ref vdata, .. }) => { // Define a name in the type namespace. let res = Res::Def(DefKind::Struct, def_id); self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); @@ -800,7 +800,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { } } - ItemKind::Union(ref vdata, _) => { + ItemKind::Union(box Struct { ref vdata, .. }) => { let res = Res::Def(DefKind::Union, def_id); self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 66641fb2cb248..534adefae3369 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -118,9 +118,10 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> { self.with_parent(def, |this| { this.with_impl_trait(ImplTraitContext::Existential, |this| { match i.kind { - ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => { + ItemKind::Struct(box Struct { ref vdata, .. }) + | ItemKind::Union(box Struct { ref vdata, .. }) => { // If this is a unit or tuple-like struct, register the constructor. - if let Some(ctor_hir_id) = struct_def.ctor_id() { + if let Some(ctor_hir_id) = vdata.ctor_id() { this.create_def(ctor_hir_id, DefPathData::Ctor, i.span); } } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 7760898a53d80..ba46a9ca3073c 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2165,8 +2165,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { } ItemKind::Enum(box Enum { ref generics, .. }) - | ItemKind::Struct(_, box ref generics) - | ItemKind::Union(_, box ref generics) => { + | ItemKind::Struct(box Struct { ref generics, .. }) + | ItemKind::Union(box Struct { ref generics, .. }) => { self.resolve_adt(item, generics); } @@ -2206,7 +2206,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ); } - ItemKind::TraitAlias(ref generics, ref bounds) => { + ItemKind::TraitAlias(box TraitAlias { ref generics, ref bounds }) => { // Create a new rib for the trait-wide type parameters. self.with_generic_param_rib( &generics.params, @@ -3944,11 +3944,11 @@ impl<'ast> Visitor<'ast> for LifetimeCountVisitor<'_, '_> { ItemKind::TyAlias(box TyAlias { ref generics, .. }) | ItemKind::Fn(box Fn { ref generics, .. }) | ItemKind::Enum(box Enum { ref generics, .. }) - | ItemKind::Struct(_, box ref generics) - | ItemKind::Union(_, box ref generics) + | ItemKind::Struct(box Struct { ref generics, .. }) + | ItemKind::Union(box Struct { ref generics, .. }) | ItemKind::Impl(box Impl { ref generics, .. }) | ItemKind::Trait(box Trait { ref generics, .. }) - | ItemKind::TraitAlias(box ref generics, _) => { + | ItemKind::TraitAlias(box TraitAlias { ref generics, .. }) => { let def_id = self.r.local_def_id(item.id); let count = generics .params diff --git a/src/tools/clippy/clippy_lints/src/empty_structs_with_brackets.rs b/src/tools/clippy/clippy_lints/src/empty_structs_with_brackets.rs index 08bf80a422900..624cbe563bf35 100644 --- a/src/tools/clippy/clippy_lints/src/empty_structs_with_brackets.rs +++ b/src/tools/clippy/clippy_lints/src/empty_structs_with_brackets.rs @@ -1,5 +1,5 @@ use clippy_utils::{diagnostics::span_lint_and_then, source::snippet_opt}; -use rustc_ast::ast::{Item, ItemKind, VariantData}; +use rustc_ast::ast::{Item, ItemKind, Struct, VariantData}; use rustc_errors::Applicability; use rustc_lexer::TokenKind; use rustc_lint::{EarlyContext, EarlyLintPass}; @@ -32,9 +32,9 @@ impl EarlyLintPass for EmptyStructsWithBrackets { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { let span_after_ident = item.span.with_lo(item.ident.span.hi()); - if let ItemKind::Struct(var_data, _) = &item.kind - && has_brackets(var_data) - && has_no_fields(cx, var_data, span_after_ident) { + if let ItemKind::Struct(box Struct { vdata, .. }) = &item.kind + && has_brackets(vdata) + && has_no_fields(cx, vdata, span_after_ident) { span_lint_and_then( cx, EMPTY_STRUCTS_WITH_BRACKETS, @@ -56,12 +56,12 @@ fn has_no_ident_token(braces_span_str: &str) -> bool { !rustc_lexer::tokenize(braces_span_str).any(|t| t.kind == TokenKind::Ident) } -fn has_brackets(var_data: &VariantData) -> bool { - !matches!(var_data, VariantData::Unit(_)) +fn has_brackets(vdata: &VariantData) -> bool { + !matches!(vdata, VariantData::Unit(_)) } -fn has_no_fields(cx: &EarlyContext<'_>, var_data: &VariantData, braces_span: Span) -> bool { - if !var_data.fields().is_empty() { +fn has_no_fields(cx: &EarlyContext<'_>, vdata: &VariantData, braces_span: Span) -> bool { + if !vdata.fields().is_empty() { return false; } diff --git a/src/tools/clippy/clippy_lints/src/excessive_bools.rs b/src/tools/clippy/clippy_lints/src/excessive_bools.rs index 453471c8cdda6..913dd5fc7bc19 100644 --- a/src/tools/clippy/clippy_lints/src/excessive_bools.rs +++ b/src/tools/clippy/clippy_lints/src/excessive_bools.rs @@ -1,5 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_help; -use rustc_ast::ast::{AssocItemKind, Extern, Fn, FnSig, Impl, Item, ItemKind, Trait, Ty, TyKind}; +use rustc_ast::ast::{ + AssocItemKind, Extern, Fn, FnSig, Impl, Item, ItemKind, Struct, Trait, Ty, TyKind +}; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::{sym, Span}; @@ -136,12 +138,12 @@ impl EarlyLintPass for ExcessiveBools { return; } match &item.kind { - ItemKind::Struct(variant_data, _) => { + ItemKind::Struct(box Struct { vdata, .. }) => { if item.attrs.iter().any(|attr| attr.has_name(sym::repr)) { return; } - let struct_bools = variant_data + let struct_bools = vdata .fields() .iter() .filter(|field| is_bool_ty(&field.ty)) diff --git a/src/tools/clippy/clippy_lints/src/manual_non_exhaustive.rs b/src/tools/clippy/clippy_lints/src/manual_non_exhaustive.rs index 2b04475c7a9de..7021d0c00cbdc 100644 --- a/src/tools/clippy/clippy_lints/src/manual_non_exhaustive.rs +++ b/src/tools/clippy/clippy_lints/src/manual_non_exhaustive.rs @@ -101,8 +101,8 @@ impl EarlyLintPass for ManualNonExhaustiveStruct { return; } - if let ast::ItemKind::Struct(variant_data, _) = &item.kind { - let (fields, delimiter) = match variant_data { + if let ast::ItemKind::Struct(box ast::Struct { vdata, .. }) = &item.kind { + let (fields, delimiter) = match vdata { ast::VariantData::Struct(fields, _) => (&**fields, '{'), ast::VariantData::Tuple(fields, _) => (&**fields, '('), ast::VariantData::Unit(_) => return, diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 622caa8098ccb..63aacbdc0062b 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -309,7 +309,13 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { Enum(box ast::Enum { variants: lv, generics: lg }), Enum(box ast::Enum { variants: rv, generics: rg }) ) => over(lv, rv, eq_variant) && eq_generics(lg, rg), - (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => { + ( + Struct(box ast::Struct { vdata: lv, generics: lg }), + Struct(box ast::Struct { vdata: rv, generics: rg }) + ) | ( + Union(box ast::Struct { vdata: lv, generics: lg }), + Union(box ast::Struct { vdata: rv, generics: rg }) + ) => { eq_variant_data(lv, rv) && eq_generics(lg, rg) }, ( @@ -334,7 +340,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { && over(lb, rb, eq_generic_bound) && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind)) }, - (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound), + ( + TraitAlias(box ast::TraitAlias { generics: lg, bounds: lb }), + TraitAlias(box ast::TraitAlias { generics: rg, bounds: rb }) + ) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound), ( Impl(box ast::Impl { unsafety: lu, diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 15c09e2d22a67..70739a1d90f85 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -398,7 +398,7 @@ impl<'a> FmtVisitor<'a> { } pub(crate) fn visit_struct(&mut self, struct_parts: &StructParts<'_>) { - let is_tuple = match struct_parts.def { + let is_tuple = match struct_parts.vdata { ast::VariantData::Tuple(..) => true, _ => false, }; @@ -940,7 +940,7 @@ pub(crate) struct StructParts<'a> { prefix: &'a str, ident: symbol::Ident, vis: &'a ast::Visibility, - def: &'a ast::VariantData, + vdata: &'a ast::VariantData, generics: Option<&'a ast::Generics>, span: Span, } @@ -955,23 +955,23 @@ impl<'a> StructParts<'a> { prefix: "", ident: variant.ident, vis: &DEFAULT_VISIBILITY, - def: &variant.data, + vdata: &variant.data, generics: None, span: variant.span, } } pub(crate) fn from_item(item: &'a ast::Item) -> Self { - let (prefix, def, generics) = match item.kind { - ast::ItemKind::Struct(ref def, ref generics) => ("struct ", def, generics), - ast::ItemKind::Union(ref def, ref generics) => ("union ", def, generics), + let (prefix, vdata, generics) = match &item.kind { + ast::ItemKind::Struct(struct_) => ("struct ", &struct_.vdata, &struct_.generics), + ast::ItemKind::Union(struct_) => ("union ", &struct_.vdata, &struct_.generics), _ => unreachable!(), }; StructParts { prefix, ident: item.ident, vis: &item.vis, - def, + vdata, generics: Some(generics), span: item.span, } @@ -984,7 +984,7 @@ fn format_struct( offset: Indent, one_line_width: Option, ) -> Option { - match *struct_parts.def { + match *struct_parts.vdata { ast::VariantData::Unit(..) => format_unit_struct(context, struct_parts, offset), ast::VariantData::Tuple(ref fields, _) => { format_tuple_struct(context, struct_parts, fields, offset) diff --git a/src/tools/rustfmt/src/visitor.rs b/src/tools/rustfmt/src/visitor.rs index bfda5b78a3543..d61080c7ea878 100644 --- a/src/tools/rustfmt/src/visitor.rs +++ b/src/tools/rustfmt/src/visitor.rs @@ -494,14 +494,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { let rw = self.with_context(|ctx| format_trait(ctx, item, block_indent)); self.push_rewrite(item.span, rw); } - ast::ItemKind::TraitAlias(ref generics, ref generic_bounds) => { + ast::ItemKind::TraitAlias(ref trait_alias) => { let shape = Shape::indented(self.block_indent, self.config); let rw = format_trait_alias( &self.get_context(), item.ident, &item.vis, - generics, - generic_bounds, + &trait_alias.generics, + &trait_alias.bounds, shape, ); self.push_rewrite(item.span, rw); From 300d74d243b326b123fc462f5ddc2b395e07607e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 17:00:13 +1000 Subject: [PATCH 12/14] XXX: Const/Constness --- compiler/rustc_ast/src/ast.rs | 35 +++++++----- compiler/rustc_ast/src/mut_visit.rs | 10 ++-- compiler/rustc_ast/src/visit.rs | 7 ++- compiler/rustc_ast_lowering/src/item.rs | 16 +++--- .../rustc_ast_passes/src/ast_validation.rs | 30 +++++----- compiler/rustc_ast_passes/src/feature_gate.rs | 10 ++-- compiler/rustc_ast_pretty/src/pprust/state.rs | 6 +- .../rustc_ast_pretty/src/pprust/state/item.rs | 28 +++++++--- .../src/deriving/generic/mod.rs | 2 +- .../rustc_builtin_macros/src/deriving/mod.rs | 2 +- compiler/rustc_builtin_macros/src/test.rs | 12 ++-- compiler/rustc_expand/src/build.rs | 9 ++- compiler/rustc_lint/src/unused.rs | 3 +- compiler/rustc_parse/src/parser/item.rs | 24 ++++---- compiler/rustc_parse/src/parser/mod.rs | 8 +-- compiler/rustc_parse/src/parser/ty.rs | 2 +- compiler/rustc_resolve/src/late.rs | 10 ++-- .../src/redundant_static_lifetimes.rs | 10 ++-- .../clippy/clippy_utils/src/ast_utils.rs | 14 +++-- src/tools/rustfmt/src/items.rs | 56 +++++++++---------- src/tools/rustfmt/src/utils.rs | 12 ++-- 21 files changed, 173 insertions(+), 133 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index a8a9db62430e9..c8a4b5b63bb25 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2346,7 +2346,7 @@ impl Async { #[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)] #[derive(HashStable_Generic)] -pub enum Const { +pub enum Constness { Yes(Span), No, } @@ -2713,7 +2713,7 @@ impl Extern { pub struct FnHeader { pub unsafety: Unsafe, pub asyncness: Async, - pub constness: Const, + pub constness: Constness, pub ext: Extern, } @@ -2723,7 +2723,7 @@ impl FnHeader { let Self { unsafety, asyncness, constness, ext } = self; matches!(unsafety, Unsafe::Yes(_)) || asyncness.is_async() - || matches!(constness, Const::Yes(_)) + || matches!(constness, Constness::Yes(_)) || !matches!(ext, Extern::None) } } @@ -2733,7 +2733,7 @@ impl Default for FnHeader { FnHeader { unsafety: Unsafe::No, asyncness: Async::No, - constness: Const::No, + constness: Constness::No, ext: Extern::None, } } @@ -2788,7 +2788,7 @@ pub struct Impl { pub defaultness: Defaultness, pub unsafety: Unsafe, pub generics: Generics, - pub constness: Const, + pub constness: Constness, pub polarity: ImplPolarity, /// The trait being implemented, if any. pub of_trait: Option, @@ -2822,6 +2822,13 @@ pub struct TraitAlias { pub bounds: GenericBounds, } +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct Const { + pub defaultness: Defaultness, + pub ty: P, + pub expr: Option>, +} + #[derive(Clone, Encodable, Decodable, Debug)] pub enum ItemKind { /// An `extern crate` item, with the optional *original* crate name if the crate was renamed. @@ -2839,7 +2846,7 @@ pub enum ItemKind { /// A constant item (`const`). /// /// E.g., `const FOO: i32 = 42;`. - Const(Defaultness, P, Option>), + Const(Box), /// A function declaration (`fn`). /// /// E.g., `fn foo(bar: usize) -> usize { .. }`. @@ -2955,7 +2962,7 @@ pub type AssocItem = Item; pub enum AssocItemKind { /// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`. /// If `def` is parsed, then the constant is provided, and otherwise required. - Const(Defaultness, P, Option>), + Const(Box), /// An associated function. Fn(Box), /// An associated type. @@ -2967,7 +2974,7 @@ pub enum AssocItemKind { impl AssocItemKind { pub fn defaultness(&self) -> Defaultness { match *self { - Self::Const(defaultness, ..) + Self::Const(box Const { defaultness, .. }) | Self::Fn(box Fn { defaultness, .. }) | Self::TyAlias(box TyAlias { defaultness, .. }) => defaultness, Self::MacCall(..) => Defaultness::Final, @@ -2978,7 +2985,7 @@ impl AssocItemKind { impl From for ItemKind { fn from(assoc_item_kind: AssocItemKind) -> ItemKind { match assoc_item_kind { - AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c), + AssocItemKind::Const(const_) => ItemKind::Const(const_), AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), AssocItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), AssocItemKind::MacCall(a) => ItemKind::MacCall(a), @@ -2991,7 +2998,7 @@ impl TryFrom for AssocItemKind { fn try_from(item_kind: ItemKind) -> Result { Ok(match item_kind { - ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c), + ItemKind::Const(const_) => AssocItemKind::Const(const_), ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind), ItemKind::TyAlias(ty_alias_kind) => AssocItemKind::TyAlias(ty_alias_kind), ItemKind::MacCall(a) => AssocItemKind::MacCall(a), @@ -3046,8 +3053,8 @@ mod size_asserts { use super::*; use rustc_data_structures::static_assert_size; // These are in alphabetical order, which is easy to maintain. - static_assert_size!(AssocItem, 120); - static_assert_size!(AssocItemKind, 32); + static_assert_size!(AssocItem, 104); + static_assert_size!(AssocItemKind, 16); static_assert_size!(Attribute, 152); static_assert_size!(Block, 48); static_assert_size!(Expr, 104); @@ -3057,8 +3064,8 @@ mod size_asserts { static_assert_size!(GenericBound, 88); static_assert_size!(Generics, 72); static_assert_size!(Impl, 200); - static_assert_size!(Item, 120); - static_assert_size!(ItemKind, 32); + static_assert_size!(Item, 112); + static_assert_size!(ItemKind, 24); static_assert_size!(Lit, 48); static_assert_size!(Pat, 120); static_assert_size!(Path, 40); diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 873bf7cacfe25..4bac07d4c2388 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -822,10 +822,10 @@ pub fn visit_polarity(polarity: &mut ImplPolarity, vis: &mut T) { } // No `noop_` prefix because there isn't a corresponding method in `MutVisitor`. -pub fn visit_constness(constness: &mut Const, vis: &mut T) { +pub fn visit_constness(constness: &mut Constness, vis: &mut T) { match constness { - Const::Yes(span) => vis.visit_span(span), - Const::No => {} + Constness::Yes(span) => vis.visit_span(span), + Constness::No => {} } } @@ -1014,7 +1014,7 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { vis.visit_ty(ty); visit_opt(expr, |expr| vis.visit_expr(expr)); } - ItemKind::Const(defaultness, ty, expr) => { + ItemKind::Const(box Const { defaultness, ty, expr }) => { visit_defaultness(defaultness, vis); vis.visit_ty(ty); visit_opt(expr, |expr| vis.visit_expr(expr)); @@ -1101,7 +1101,7 @@ pub fn noop_flat_map_assoc_item( visitor.visit_vis(vis); visit_attrs(attrs, visitor); match kind { - AssocItemKind::Const(defaultness, ty, expr) => { + AssocItemKind::Const(box Const { defaultness, ty, expr }) => { visit_defaultness(defaultness, visitor); visitor.visit_ty(ty); visit_opt(expr, |expr| visitor.visit_expr(expr)); diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 03638e134a681..b2688247f2394 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -299,8 +299,9 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { match item.kind { ItemKind::ExternCrate(_) => {} ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false), - ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => { - visitor.visit_ty(typ); + ItemKind::Static(ref ty, _, ref expr) + | ItemKind::Const(box Const { ref ty, ref expr, .. }) => { + visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => { @@ -677,7 +678,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem, visitor.visit_ident(ident); walk_list!(visitor, visit_attribute, attrs); match kind { - AssocItemKind::Const(_, ty, expr) => { + AssocItemKind::Const(box Const { ty, expr, .. }) => { visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index bf819f5afa953..ff7d50e0d695c 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -238,8 +238,8 @@ impl<'hir> LoweringContext<'_, 'hir> { let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); hir::ItemKind::Static(ty, m, body_id) } - ItemKind::Const(_, ref t, ref e) => { - let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); + ItemKind::Const(box Const { ref ty, ref expr, .. }) => { + let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref()); hir::ItemKind::Const(ty, body_id) } ItemKind::Fn(box Fn { @@ -753,9 +753,9 @@ impl<'hir> LoweringContext<'_, 'hir> { let trait_item_def_id = hir_id.expect_owner(); let (generics, kind, has_default) = match i.kind { - AssocItemKind::Const(_, ref ty, ref default) => { + AssocItemKind::Const(box Const { ref ty, ref expr, .. }) => { let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type)); - let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x))); + let body = expr.as_ref().map(|x| self.lower_const_body(i.span, Some(x))); (hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some()) } AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => { @@ -849,7 +849,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value); let (generics, kind) = match &i.kind { - AssocItemKind::Const(_, ty, expr) => { + AssocItemKind::Const(box Const { ty, expr, .. }) => { let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type)); ( hir::Generics::empty(), @@ -1271,10 +1271,10 @@ impl<'hir> LoweringContext<'_, 'hir> { } } - fn lower_constness(&mut self, c: Const) -> hir::Constness { + fn lower_constness(&mut self, c: Constness) -> hir::Constness { match c { - Const::Yes(_) => hir::Constness::Const, - Const::No => hir::Constness::NotConst, + Constness::Yes(_) => hir::Constness::Const, + Constness::No => hir::Constness::NotConst, } } diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 8b82127b7efe7..ec2d75f4e43ae 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -76,12 +76,14 @@ impl<'a> AstValidator<'a> { fn with_in_trait_impl( &mut self, is_in: bool, - constness: Option, + constness: Option, f: impl FnOnce(&mut Self), ) { let old = mem::replace(&mut self.in_trait_impl, is_in); - let old_const = - mem::replace(&mut self.in_const_trait_impl, matches!(constness, Some(Const::Yes(_)))); + let old_const = mem::replace( + &mut self.in_const_trait_impl, + matches!(constness, Some(Constness::Yes(_))), + ); f(self); self.in_trait_impl = old; self.in_const_trait_impl = old_const; @@ -324,8 +326,8 @@ impl<'a> AstValidator<'a> { } } - fn check_trait_fn_not_const(&self, constness: Const) { - if let Const::Yes(span) = constness { + fn check_trait_fn_not_const(&self, constness: Constness) { + if let Constness::Yes(span) = constness { struct_span_err!( self.session, span, @@ -1136,7 +1138,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { this.visit_vis(&item.vis); this.visit_ident(item.ident); - if let Const::Yes(_) = constness { + if let Constness::Yes(_) = constness { this.with_tilde_const_allowed(|this| this.visit_generics(generics)); } else { this.visit_generics(generics); @@ -1183,7 +1185,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { .note("only trait implementations may be annotated with `default`") .emit(); } - if let Const::Yes(span) = constness { + if let Constness::Yes(span) = constness { error(span, "`const`") .note("only trait implementations may be annotated with `const`") .emit(); @@ -1327,8 +1329,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { _ => {} } } - ItemKind::Const(def, .., None) => { - self.check_defaultness(item.span, def); + ItemKind::Const(box Const { defaultness, expr: None, .. }) => { + self.check_defaultness(item.span, defaultness); let msg = "free constant item without body"; self.error_item_without_body(item.span, "constant", msg, " = ;"); } @@ -1559,7 +1561,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { // Functions cannot both be `const async` if let Some(FnHeader { - constness: Const::Yes(cspan), + constness: Constness::Yes(cspan), asyncness: Async::Yes { span: aspan, .. }, .. }) = fk.header() @@ -1628,7 +1630,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { } let tilde_const_allowed = - matches!(fk.header(), Some(FnHeader { constness: Const::Yes(_), .. })) + matches!(fk.header(), Some(FnHeader { constness: Constness::Yes(_), .. })) || matches!(fk.ctxt(), Some(FnCtxt::Assoc(_))); self.with_tilde_const(tilde_const_allowed, |this| visit::walk_fn(this, fk, span)); @@ -1645,8 +1647,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if ctxt == AssocCtxt::Impl { match &item.kind { - AssocItemKind::Const(_, _, body) => { - self.check_impl_item_provided(item.span, body, "constant", " = ;"); + AssocItemKind::Const(box Const { expr, .. }) => { + self.check_impl_item_provided(item.span, expr, "constant", " = ;"); } AssocItemKind::Fn(box Fn { body, .. }) => { self.check_impl_item_provided(item.span, body, "function", " { }"); @@ -1701,7 +1703,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { 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(_)) => + || matches!(sig.header.constness, Constness::Yes(_)) => { self.visit_vis(&item.vis); self.visit_ident(item.ident); diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 8728e18a9ffb0..c43385889a508 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -58,10 +58,10 @@ struct PostExpansionVisitor<'a> { } impl<'a> PostExpansionVisitor<'a> { - fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) { + fn check_abi(&self, abi: ast::StrLit, constness: ast::Constness) { let ast::StrLit { symbol_unescaped, span, .. } = abi; - if let ast::Const::Yes(_) = constness { + if let ast::Constness::Yes(_) = constness { match symbol_unescaped { // Stable sym::Rust | sym::C => {} @@ -284,7 +284,7 @@ impl<'a> PostExpansionVisitor<'a> { } } - fn check_extern(&self, ext: ast::Extern, constness: ast::Const) { + fn check_extern(&self, ext: ast::Extern, constness: ast::Constness) { if let ast::Extern::Explicit(abi, _) = ext { self.check_abi(abi, constness); } @@ -433,7 +433,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { match i.kind { ast::ItemKind::ForeignMod(ref foreign_module) => { if let Some(abi) = foreign_module.abi { - self.check_abi(abi, ast::Const::No); + self.check_abi(abi, ast::Constness::No); } } @@ -557,7 +557,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { match ty.kind { ast::TyKind::BareFn(ref bare_fn_ty) => { // Function pointers cannot be `const` - self.check_extern(bare_fn_ty.ext, ast::Const::No); + self.check_extern(bare_fn_ty.ext, ast::Constness::No); } ast::TyKind::Never => { gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental"); diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 5eb7bf6347f7b..73a38a2e2f8bd 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1754,10 +1754,10 @@ impl<'a> State<'a> { } } - pub(crate) fn print_constness(&mut self, s: ast::Const) { + pub(crate) fn print_constness(&mut self, s: ast::Constness) { match s { - ast::Const::No => {} - ast::Const::Yes(_) => self.word_nbsp("const"), + ast::Constness::No => {} + ast::Constness::Yes(_) => self.word_nbsp("const"), } } diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 6684fdc6fa819..3c6ec45b66a2b 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -156,12 +156,26 @@ impl<'a> State<'a> { self.print_use_tree(tree); self.word(";"); } - ast::ItemKind::Static(ref ty, mutbl, ref body) => { - let def = ast::Defaultness::Final; - self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def); + ast::ItemKind::Static(ref ty, mutbl, ref expr) => { + let defaultness = ast::Defaultness::Final; + self.print_item_const( + item.ident, + Some(mutbl), + ty, + expr.as_deref(), + &item.vis, + defaultness, + ); } - ast::ItemKind::Const(def, ref ty, ref body) => { - self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def); + ast::ItemKind::Const(box ast::Const { defaultness, ref ty, ref expr }) => { + self.print_item_const( + item.ident, + None, + ty, + expr.as_deref(), + &item.vis, + defaultness, + ); } ast::ItemKind::Fn(box ast::Fn { defaultness, ref sig, ref generics, ref body }) => { let body = body.as_deref(); @@ -511,8 +525,8 @@ impl<'a> State<'a> { 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::Const(box ast::Const { defaultness, ty, expr }) => { + self.print_item_const(ident, None, ty, expr.as_deref(), vis, *defaultness); } ast::AssocItemKind::TyAlias(box ast::TyAlias { defaultness, diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 3bb38074bb1cf..a0d7dd7907f45 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -740,7 +740,7 @@ impl<'a> TraitDef<'a> { unsafety: ast::Unsafe::No, polarity: ast::ImplPolarity::Positive, defaultness: ast::Defaultness::Final, - constness: ast::Const::No, + constness: ast::Constness::No, generics: trait_generics, of_trait: opt_trait_ref, self_ty: self_type, diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs index e6a5348e6dd5d..b8fdd7cd040e7 100644 --- a/compiler/rustc_builtin_macros/src/deriving/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/mod.rs @@ -184,7 +184,7 @@ fn inject_impl_of_structural_trait( unsafety: ast::Unsafe::No, polarity: ast::ImplPolarity::Positive, defaultness: ast::Defaultness::Final, - constness: ast::Const::No, + constness: ast::Constness::No, generics, of_trait: Some(trait_ref), self_ty: self_type, diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index e20375689f3d1..6aa4c9581ba4f 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -229,11 +229,11 @@ pub fn expand_test_or_bench( cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)), ], // const $ident: test::TestDescAndFn = - ast::ItemKind::Const( - ast::Defaultness::Final, - cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))), + ast::ItemKind::Const(Box::new(ast::Const { + defaultness: ast::Defaultness::Final, + ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))), // test::TestDescAndFn { - Some( + expr: Some( cx.expr_struct( sp, test_path("TestDescAndFn"), @@ -325,9 +325,9 @@ pub fn expand_test_or_bench( field("testfn", test_fn), // } ], ), // } - ), + )}, ), - ); + )); test_const = test_const.map(|mut tc| { tc.vis.kind = ast::VisibilityKind::Public; tc diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index fa3e2a4a5b81c..62821ec64636f 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -602,8 +602,13 @@ impl<'a> ExtCtxt<'a> { ty: P, expr: P, ) -> P { - let def = ast::Defaultness::Final; - self.item(span, name, Vec::new(), ast::ItemKind::Const(def, ty, Some(expr))) + let defaultness = ast::Defaultness::Final; + self.item( + span, + name, + Vec::new(), + ast::ItemKind::Const(Box::new(ast::Const { defaultness, ty, expr: Some(expr) })), + ) } pub fn attribute(&self, mi: ast::MetaItem) -> ast::Attribute { diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index b6cf182916cb9..f01097a63259a 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -657,7 +657,8 @@ trait UnusedDelimLint { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { use ast::ItemKind::*; - if let Const(.., Some(expr)) | Static(.., Some(expr)) = &item.kind { + if let Const(box ast::Const { expr: Some(expr), .. }) | Static(.., Some(expr)) = &item.kind + { self.check_unused_delims_expr( cx, expr, diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 54546e0d70195..74aa368f44307 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -228,7 +228,7 @@ impl<'a> Parser<'a> { let m = self.parse_mutability(); let (ident, ty, expr) = self.parse_item_global(Some(m))?; (ident, ItemKind::Static(ty, m, expr)) - } else if let Const::Yes(const_span) = self.parse_constness() { + } else if let Constness::Yes(const_span) = self.parse_constness() { // CONST ITEM if self.token.is_keyword(kw::Impl) { // recover from `const impl`, suggest `impl const` @@ -236,7 +236,7 @@ impl<'a> Parser<'a> { } else { self.recover_const_mut(const_span); let (ident, ty, expr) = self.parse_item_global(None)?; - (ident, ItemKind::Const(def(), ty, expr)) + (ident, ItemKind::Const(Box::new(Const { defaultness: def(), ty, expr }))) } } else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() { // TRAIT ITEM @@ -547,7 +547,7 @@ impl<'a> Parser<'a> { }; let constness = self.parse_constness(); - if let Const::Yes(span) = constness { + if let Constness::Yes(span) = constness { self.sess.gated_spans.gate(sym::const_trait_impl, span); } @@ -822,10 +822,14 @@ impl<'a> Parser<'a> { let kind = match AssocItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Static(a, _, b) => { + ItemKind::Static(ty, _, expr) => { self.struct_span_err(span, "associated `static` items are not allowed") .emit(); - AssocItemKind::Const(Defaultness::Final, a, b) + AssocItemKind::Const(Box::new(Const { + defaultness: Defaultness::Final, + ty, + expr, + })) } _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"), }, @@ -1058,9 +1062,9 @@ impl<'a> Parser<'a> { let kind = match ForeignItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Const(_, a, b) => { + ItemKind::Const(box Const { ty, expr, .. }) => { self.error_on_foreign_const(span, ident); - ForeignItemKind::Static(a, Mutability::Not, b) + ForeignItemKind::Static(ty, Mutability::Not, expr) } _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"), }, @@ -1152,7 +1156,7 @@ impl<'a> Parser<'a> { match impl_info.1 { ItemKind::Impl(box Impl { of_trait: Some(ref trai), ref mut constness, .. }) => { - *constness = Const::Yes(const_span); + *constness = Constness::Yes(const_span); let before_trait = trai.path.span.shrink_to_lo(); let const_up_to_impl = const_span.with_hi(impl_span.lo()); @@ -2087,8 +2091,8 @@ impl<'a> Parser<'a> { // that the keyword is already present and the second instance should be removed. let wrong_kw = if self.check_keyword(kw::Const) { match constness { - Const::Yes(sp) => Some(WrongKw::Duplicated(sp)), - Const::No => Some(WrongKw::Misplaced(async_start_sp)), + Constness::Yes(sp) => Some(WrongKw::Duplicated(sp)), + Constness::No => Some(WrongKw::Misplaced(async_start_sp)), } } else if self.check_keyword(kw::Async) { match asyncness { diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 0c523ad22c205..51b9d93a19a46 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -25,7 +25,7 @@ use rustc_ast::tokenstream::{self, DelimSpan, Spacing}; use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::AttrId; use rustc_ast::DUMMY_NODE_ID; -use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Const, Extern}; +use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Constness, Extern}; use rustc_ast::{Async, Expr, ExprKind, MacArgs, MacArgsEq, MacDelimiter, Mutability, StrLit}; use rustc_ast::{HasAttrs, HasTokens, Unsafe, Visibility, VisibilityKind}; use rustc_ast_pretty::pprust; @@ -1094,14 +1094,14 @@ impl<'a> Parser<'a> { } /// Parses constness: `const` or nothing. - fn parse_constness(&mut self) -> Const { + fn parse_constness(&mut self) -> Constness { // Avoid const blocks to be parsed as const items if self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace)) && self.eat_keyword(kw::Const) { - Const::Yes(self.prev_token.uninterpolated_span()) + Constness::Yes(self.prev_token.uninterpolated_span()) } else { - Const::No + Constness::No } } diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 76b710095d798..a3b89309d40f1 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -528,7 +528,7 @@ impl<'a> Parser<'a> { self.parse_fn_front_matter(&inherited_vis)?; let decl = self.parse_fn_decl(|_| false, AllowPlus::No, recover_return_sign)?; let whole_span = lo.to(self.prev_token.span); - if let ast::Const::Yes(span) = constness { + if let ast::Constness::Yes(span) = constness { // If we ever start to allow `const fn()`, then update // feature gating for `#![feature(const_extern_fn)]` to // cover it. diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index ba46a9ca3073c..32a28b807b593 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2235,7 +2235,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }); } - ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(_, ref ty, ref expr) => { + ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(box Const { ref ty, ref expr, .. }) => { self.with_item_rib(|this| { this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| { this.visit_ty(ty); @@ -2502,11 +2502,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { for item in trait_items { match &item.kind { - AssocItemKind::Const(_, ty, default) => { + AssocItemKind::Const(box Const { ty, expr, .. }) => { self.visit_ty(ty); // Only impose the restrictions of `ConstRibKind` for an // actual constant expression in a provided default. - if let Some(expr) = default { + if let Some(expr) = expr { // We allow arbitrary const expressions inside of associated consts, // even if they are potentially not const evaluatable. // @@ -2666,7 +2666,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { fn resolve_impl_item(&mut self, item: &'ast AssocItem) { use crate::ResolutionError::*; match &item.kind { - AssocItemKind::Const(_, ty, default) => { + AssocItemKind::Const(box Const { expr, ty, .. }) => { debug!("resolve_implementation AssocItemKind::Const"); // If this is a trait impl, ensure the const // exists in trait @@ -2680,7 +2680,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ); self.visit_ty(ty); - if let Some(expr) = default { + if let Some(expr) = expr { // We allow arbitrary const expressions inside of associated consts, // even if they are potentially not const evaluatable. // diff --git a/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs b/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs index f8801f769e83d..e0dcd18f4f6b1 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::snippet; use clippy_utils::{meets_msrv, msrvs}; -use rustc_ast::ast::{Item, ItemKind, Ty, TyKind}; +use rustc_ast::ast::{Const, Item, ItemKind, Ty, TyKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_semver::RustcVersion; @@ -101,14 +101,14 @@ impl EarlyLintPass for RedundantStaticLifetimes { } if !item.span.from_expansion() { - if let ItemKind::Const(_, ref var_type, _) = item.kind { - self.visit_type(var_type, cx, "constants have by default a `'static` lifetime"); + if let ItemKind::Const(box Const { ref ty, .. }) = item.kind { + self.visit_type(ty, cx, "constants have by default a `'static` lifetime"); // Don't check associated consts because `'static` cannot be elided on those (issue // #2438) } - if let ItemKind::Static(ref var_type, _, _) = item.kind { - self.visit_type(var_type, cx, "statics have by default a `'static` lifetime"); + if let ItemKind::Static(ref ty, _, _) = item.kind { + self.visit_type(ty, cx, "statics have by default a `'static` lifetime"); } } } diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 63aacbdc0062b..8834f2479fc26 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -254,7 +254,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { (ExternCrate(l), ExternCrate(r)) => l == r, (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), + ( + Const(box ast::Const { defaultness: ld, ty: lt, expr: le }), + Const(box ast::Const { defaultness: rd, ty: rt, expr: re }) + ) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), ( Fn(box ast::Fn { defaultness: ld, @@ -369,7 +372,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No) && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive) && eq_defaultness(*ld, *rd) - && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No) + && matches!(lc, ast::Constness::No) == matches!(rc, ast::Constness::No) && eq_generics(lg, rg) && both(lot, rot, |l, r| eq_path(&l.path, &r.path)) && eq_ty(lst, rst) @@ -430,7 +433,10 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool { 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), + ( + Const(box ast::Const { defaultness: ld, ty: lt, expr: le }), + Const(box ast::Const { defaultness: rd, ty: rt, expr: re }) + ) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), ( Fn(box ast::Fn { defaultness: ld, @@ -506,7 +512,7 @@ pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool { pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool { matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No) && l.asyncness.is_async() == r.asyncness.is_async() - && matches!(l.constness, Const::No) == matches!(r.constness, Const::No) + && matches!(l.constness, Constness::No) == matches!(r.constness, Constness::No) && eq_ext(&l.ext, &r.ext) } diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 70739a1d90f85..5187ddee848ef 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -178,7 +178,7 @@ pub(crate) struct FnSig<'a> { generics: &'a ast::Generics, ext: ast::Extern, is_async: Cow<'a, ast::Async>, - constness: ast::Const, + constness: ast::Constness, defaultness: ast::Defaultness, unsafety: ast::Unsafe, visibility: &'a ast::Visibility, @@ -1803,8 +1803,8 @@ impl<'a> StaticParts<'a> { ast::ItemKind::Static(ref ty, mutability, ref expr) => { (None, "static", ty, mutability, expr) } - ast::ItemKind::Const(defaultness, ref ty, ref expr) => { - (Some(defaultness), "const", ty, ast::Mutability::Not, expr) + ast::ItemKind::Const(ref const_) => { + (Some(const_.defaultness), "const", &const_.ty, ast::Mutability::Not, &const_.expr) } _ => unreachable!(), }; @@ -1821,38 +1821,38 @@ impl<'a> StaticParts<'a> { } pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self { - let (defaultness, ty, expr_opt) = match ti.kind { - ast::AssocItemKind::Const(defaultness, ref ty, ref expr_opt) => { - (defaultness, ty, expr_opt) + match &ti.kind { + ast::AssocItemKind::Const(const_) => { + StaticParts { + prefix: "const", + vis: &ti.vis, + ident: ti.ident, + ty: &const_.ty, + mutability: ast::Mutability::Not, + expr_opt: const_.expr.as_ref(), + defaultness: Some(const_.defaultness), + span: ti.span, + } } _ => unreachable!(), - }; - StaticParts { - prefix: "const", - vis: &ti.vis, - ident: ti.ident, - ty, - mutability: ast::Mutability::Not, - expr_opt: expr_opt.as_ref(), - defaultness: Some(defaultness), - span: ti.span, } } pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self { - let (defaultness, ty, expr) = match ii.kind { - ast::AssocItemKind::Const(defaultness, ref ty, ref expr) => (defaultness, ty, expr), + match &ii.kind { + ast::AssocItemKind::Const(const_) => { + StaticParts { + prefix: "const", + vis: &ii.vis, + ident: ii.ident, + ty: &const_.ty, + mutability: ast::Mutability::Not, + expr_opt: const_.expr.as_ref(), + defaultness: Some(const_.defaultness), + span: ii.span, + } + } _ => unreachable!(), - }; - StaticParts { - prefix: "const", - vis: &ii.vis, - ident: ii.ident, - ty, - mutability: ast::Mutability::Not, - expr_opt: expr.as_ref(), - defaultness: Some(defaultness), - span: ii.span, } } } diff --git a/src/tools/rustfmt/src/utils.rs b/src/tools/rustfmt/src/utils.rs index cd852855602e8..d74fdd82679f7 100644 --- a/src/tools/rustfmt/src/utils.rs +++ b/src/tools/rustfmt/src/utils.rs @@ -83,18 +83,18 @@ pub(crate) fn format_async(is_async: &ast::Async) -> &'static str { } #[inline] -pub(crate) fn format_constness(constness: ast::Const) -> &'static str { +pub(crate) fn format_constness(constness: ast::Constness) -> &'static str { match constness { - ast::Const::Yes(..) => "const ", - ast::Const::No => "", + ast::Constness::Yes(..) => "const ", + ast::Constness::No => "", } } #[inline] -pub(crate) fn format_constness_right(constness: ast::Const) -> &'static str { +pub(crate) fn format_constness_right(constness: ast::Constness) -> &'static str { match constness { - ast::Const::Yes(..) => " const", - ast::Const::No => "", + ast::Constness::Yes(..) => " const", + ast::Constness::No => "", } } From c0a3752926e7b1ec3b043955ff27fa4d1201c0da Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 17:17:27 +1000 Subject: [PATCH 13/14] XXX: Static --- compiler/rustc_ast/src/ast.rs | 19 +++++++++++++------ compiler/rustc_ast/src/mut_visit.rs | 4 ++-- compiler/rustc_ast/src/visit.rs | 4 ++-- compiler/rustc_ast_lowering/src/item.rs | 12 ++++++------ .../rustc_ast_passes/src/ast_validation.rs | 6 +++--- .../rustc_ast_pretty/src/pprust/state/item.rs | 8 ++++---- .../src/global_allocator.rs | 10 +++++++--- compiler/rustc_builtin_macros/src/test.rs | 6 +++--- compiler/rustc_expand/src/build.rs | 7 ++++++- compiler/rustc_lint/src/unused.rs | 3 ++- compiler/rustc_parse/src/parser/item.rs | 14 +++++++++----- .../rustc_resolve/src/build_reduced_graph.rs | 8 ++++---- compiler/rustc_resolve/src/late.rs | 3 ++- .../src/redundant_static_lifetimes.rs | 4 ++-- .../clippy/clippy_utils/src/ast_utils.rs | 10 ++++++++-- src/tools/rustfmt/src/items.rs | 10 +++++----- 16 files changed, 78 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index c8a4b5b63bb25..862ac541d5c3d 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2822,6 +2822,13 @@ pub struct TraitAlias { pub bounds: GenericBounds, } +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct Static { + pub ty: P, + pub mutbl: Mutability, + pub expr: Option>, +} + #[derive(Clone, Encodable, Decodable, Debug)] pub struct Const { pub defaultness: Defaultness, @@ -2842,7 +2849,7 @@ pub enum ItemKind { /// A static item (`static`). /// /// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`. - Static(P, Mutability, Option>), + Static(Box), /// A constant item (`const`). /// /// E.g., `const FOO: i32 = 42;`. @@ -3011,7 +3018,7 @@ impl TryFrom for AssocItemKind { #[derive(Clone, Encodable, Decodable, Debug)] pub enum ForeignItemKind { /// A foreign static item (`static FOO: u8`). - Static(P, Mutability, Option>), + Static(Box), /// An foreign function. Fn(Box), /// An foreign type. @@ -3023,7 +3030,7 @@ pub enum ForeignItemKind { impl From for ItemKind { fn from(foreign_item_kind: ForeignItemKind) -> ItemKind { match foreign_item_kind { - ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c), + ForeignItemKind::Static(static_) => ItemKind::Static(static_), ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), ForeignItemKind::MacCall(a) => ItemKind::MacCall(a), @@ -3036,7 +3043,7 @@ impl TryFrom for ForeignItemKind { fn try_from(item_kind: ItemKind) -> Result { Ok(match item_kind { - ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c), + ItemKind::Static(static_) => ForeignItemKind::Static(static_), ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind), ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind), ItemKind::MacCall(a) => ForeignItemKind::MacCall(a), @@ -3059,8 +3066,8 @@ mod size_asserts { static_assert_size!(Block, 48); static_assert_size!(Expr, 104); static_assert_size!(Fn, 192); - static_assert_size!(ForeignItem, 112); - static_assert_size!(ForeignItemKind, 24); + static_assert_size!(ForeignItem, 104); + static_assert_size!(ForeignItemKind, 16); static_assert_size!(GenericBound, 88); static_assert_size!(Generics, 72); static_assert_size!(Impl, 200); diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 4bac07d4c2388..7b3d201bb0ee9 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1010,7 +1010,7 @@ 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::Static(box Static { ty, mutbl: _, expr }) => { vis.visit_ty(ty); visit_opt(expr, |expr| vis.visit_expr(expr)); } @@ -1178,7 +1178,7 @@ pub fn noop_flat_map_foreign_item( visitor.visit_vis(vis); visit_attrs(attrs, visitor); match kind { - ForeignItemKind::Static(ty, _, expr) => { + ForeignItemKind::Static(box Static { ty, mutbl: _, expr }) => { visitor.visit_ty(ty); visit_opt(expr, |expr| visitor.visit_expr(expr)); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index b2688247f2394..2827050c88a6d 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -299,7 +299,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { match item.kind { ItemKind::ExternCrate(_) => {} ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false), - ItemKind::Static(ref ty, _, ref expr) + ItemKind::Static(box Static { ref ty, mutbl: _, ref expr }) | ItemKind::Const(box Const { ref ty, ref expr, .. }) => { visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); @@ -560,7 +560,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI visitor.visit_ident(ident); walk_list!(visitor, visit_attribute, attrs); match kind { - ForeignItemKind::Static(ty, _, expr) => { + ForeignItemKind::Static(box Static { ty, mutbl: _, expr }) => { visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index ff7d50e0d695c..a1309ccbd040d 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -234,9 +234,9 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs) } - ItemKind::Static(ref t, m, ref e) => { - let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); - hir::ItemKind::Static(ty, m, body_id) + ItemKind::Static(box Static { ref ty, mutbl, ref expr }) => { + let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref()); + hir::ItemKind::Static(ty, mutbl, body_id) } ItemKind::Const(box Const { ref ty, ref expr, .. }) => { let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref()); @@ -657,10 +657,10 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ForeignItemKind::Fn(fn_dec, fn_args, generics) } - ForeignItemKind::Static(ref t, m, _) => { + ForeignItemKind::Static(box Static { ref ty, mutbl, .. }) => { let ty = - self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Type)); - hir::ForeignItemKind::Static(ty, m) + self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type)); + hir::ForeignItemKind::Static(ty, mutbl) } ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type, ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"), diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index ec2d75f4e43ae..cef303100a8e2 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1334,7 +1334,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { let msg = "free constant item without body"; self.error_item_without_body(item.span, "constant", msg, " = ;"); } - ItemKind::Static(.., None) => { + ItemKind::Static(box Static { expr: None, .. }) => { let msg = "free static item without body"; self.error_item_without_body(item.span, "static", msg, " = ;"); } @@ -1390,8 +1390,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.check_foreign_ty_genericless(generics, where_clauses.0.1); self.check_foreign_item_ascii_only(fi.ident); } - ForeignItemKind::Static(_, _, body) => { - self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span)); + ForeignItemKind::Static(box Static { expr, .. }) => { + self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|e| e.span)); self.check_foreign_item_ascii_only(fi.ident); } ForeignItemKind::MacCall(..) => {} diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 3c6ec45b66a2b..1b097b52b799b 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -29,9 +29,9 @@ impl<'a> State<'a> { 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::Static(box ast::Static { ty, mutbl, expr }) => { + let defaultness = ast::Defaultness::Final; + self.print_item_const(ident, Some(*mutbl), ty, expr.as_deref(), vis, defaultness); } ast::ForeignItemKind::TyAlias(box ast::TyAlias { defaultness, @@ -156,7 +156,7 @@ impl<'a> State<'a> { self.print_use_tree(tree); self.word(";"); } - ast::ItemKind::Static(ref ty, mutbl, ref expr) => { + ast::ItemKind::Static(box ast::Static { ref ty, mutbl, ref expr }) => { let defaultness = ast::Defaultness::Final; self.print_item_const( item.ident, diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs index 36cfbba45dacc..6fa725ea3e0bd 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::{Fn, ItemKind, Mutability, Stmt, Ty, TyKind, Unsafe}; +use rustc_ast::{Fn, ItemKind, Mutability, Static, Stmt, Ty, TyKind, Unsafe}; use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::Span; @@ -28,12 +28,16 @@ pub fn expand( // FIXME - if we get deref patterns, use them to reduce duplication here let (item, is_stmt, ty_span) = match &item { Annotatable::Item(item) => match item.kind { - ItemKind::Static(ref ty, ..) => (item, false, ecx.with_def_site_ctxt(ty.span)), + ItemKind::Static(box Static { ref ty, .. }) => { + (item, false, ecx.with_def_site_ctxt(ty.span)) + } _ => return not_static(), }, Annotatable::Stmt(stmt) => match &stmt.kind { StmtKind::Item(item_) => match item_.kind { - ItemKind::Static(ref ty, ..) => (item_, true, ecx.with_def_site_ctxt(ty.span)), + ItemKind::Static(box Static { ref ty, .. }) => { + (item_, true, ecx.with_def_site_ctxt(ty.span)) + } _ => return not_static(), }, _ => return not_static(), diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 6aa4c9581ba4f..d72af771b35dd 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -325,9 +325,9 @@ pub fn expand_test_or_bench( field("testfn", test_fn), // } ], ), // } - )}, - ), - )); + ), + })), + ); test_const = test_const.map(|mut tc| { tc.vis.kind = ast::VisibilityKind::Public; tc diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index 62821ec64636f..59b2bca7f917a 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -592,7 +592,12 @@ impl<'a> ExtCtxt<'a> { mutbl: ast::Mutability, expr: P, ) -> P { - self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, Some(expr))) + self.item( + span, + name, + Vec::new(), + ast::ItemKind::Static(Box::new(ast::Static { ty, mutbl, expr: Some(expr) })), + ) } pub fn item_const( diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index f01097a63259a..3ee5746803b38 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -657,7 +657,8 @@ trait UnusedDelimLint { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { use ast::ItemKind::*; - if let Const(box ast::Const { expr: Some(expr), .. }) | Static(.., Some(expr)) = &item.kind + if let Const(box ast::Const { expr: Some(expr), .. }) + | Static(box ast::Static { expr: Some(expr), .. }) = &item.kind { self.check_unused_delims_expr( cx, diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 74aa368f44307..c003a7d262341 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -225,9 +225,9 @@ impl<'a> Parser<'a> { } else if self.is_static_global() { // STATIC ITEM self.bump(); // `static` - let m = self.parse_mutability(); - let (ident, ty, expr) = self.parse_item_global(Some(m))?; - (ident, ItemKind::Static(ty, m, expr)) + let mutbl = self.parse_mutability(); + let (ident, ty, expr) = self.parse_item_global(Some(mutbl))?; + (ident, ItemKind::Static(Box::new(Static { ty, mutbl, expr }))) } else if let Constness::Yes(const_span) = self.parse_constness() { // CONST ITEM if self.token.is_keyword(kw::Impl) { @@ -822,7 +822,7 @@ impl<'a> Parser<'a> { let kind = match AssocItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Static(ty, _, expr) => { + ItemKind::Static(box Static { ty, expr, .. }) => { self.struct_span_err(span, "associated `static` items are not allowed") .emit(); AssocItemKind::Const(Box::new(Const { @@ -1064,7 +1064,11 @@ impl<'a> Parser<'a> { Err(kind) => match kind { ItemKind::Const(box Const { ty, expr, .. }) => { self.error_on_foreign_const(span, ident); - ForeignItemKind::Static(ty, Mutability::Not, expr) + ForeignItemKind::Static(Box::new(Static { + ty, + mutbl: Mutability::Not, + expr, + })) } _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"), }, diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 5b7ec3b39ac59..d1398b838fe75 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -16,7 +16,7 @@ use crate::{ 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::{self as ast, AssocItem, AssocItemKind, MetaItemKind, Static, StmtKind}; use rustc_ast::{Block, Fn, ForeignItem, ForeignItemKind, Impl, Item, ItemKind, NodeId, Struct}; use rustc_attr as attr; use rustc_data_structures::sync::Lrc; @@ -712,8 +712,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { } // These items live in the value namespace. - ItemKind::Static(_, mt, _) => { - let res = Res::Def(DefKind::Static(mt), def_id); + ItemKind::Static(box Static { mutbl, .. }) => { + let res = Res::Def(DefKind::Static(mutbl), def_id); self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion)); } ItemKind::Const(..) => { @@ -918,7 +918,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let def_id = local_def_id.to_def_id(); let (def_kind, ns) = match item.kind { ForeignItemKind::Fn(..) => (DefKind::Fn, ValueNS), - ForeignItemKind::Static(_, mt, _) => (DefKind::Static(mt), ValueNS), + ForeignItemKind::Static(box Static { mutbl, .. }) => (DefKind::Static(mutbl), ValueNS), ForeignItemKind::TyAlias(..) => (DefKind::ForeignTy, TypeNS), ForeignItemKind::MacCall(_) => unreachable!(), }; diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 32a28b807b593..31a0e1abef9d3 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2235,7 +2235,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }); } - ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(box Const { ref ty, ref expr, .. }) => { + ItemKind::Static(box Static { ref ty, ref expr, .. }) + | ItemKind::Const(box Const { ref ty, ref expr, .. }) => { self.with_item_rib(|this| { this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| { this.visit_ty(ty); diff --git a/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs b/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs index e0dcd18f4f6b1..7dfeadfd97334 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs @@ -1,7 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::snippet; use clippy_utils::{meets_msrv, msrvs}; -use rustc_ast::ast::{Const, Item, ItemKind, Ty, TyKind}; +use rustc_ast::ast::{Const, Item, ItemKind, Static, Ty, TyKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_semver::RustcVersion; @@ -107,7 +107,7 @@ impl EarlyLintPass for RedundantStaticLifetimes { // #2438) } - if let ItemKind::Static(ref ty, _, _) = item.kind { + if let ItemKind::Static(box Static { ref ty, .. }) = item.kind { self.visit_type(ty, cx, "statics have by default a `'static` lifetime"); } } diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 8834f2479fc26..87192c60fa79f 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -253,7 +253,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { match (l, r) { (ExternCrate(l), ExternCrate(r)) => l == r, (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), + ( + Static(box ast::Static { ty: lt, mutbl: lm, expr: le }), + Static(box ast::Static { ty: rt, mutbl: rm, expr: re }) + ) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), ( Const(box ast::Const { defaultness: ld, ty: lt, expr: le }), Const(box ast::Const { defaultness: rd, ty: rt, expr: re }) @@ -387,7 +390,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool { 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), + ( + Static(box ast::Static { ty: lt, mutbl: lm, expr: le }), + Static(box ast::Static { ty: rt, mutbl: rm, expr: re }) + ) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), ( Fn(box ast::Fn { defaultness: ld, diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 5187ddee848ef..c77065e855c3a 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -1800,8 +1800,8 @@ pub(crate) struct StaticParts<'a> { impl<'a> StaticParts<'a> { pub(crate) fn from_item(item: &'a ast::Item) -> Self { let (defaultness, prefix, ty, mutability, expr) = match item.kind { - ast::ItemKind::Static(ref ty, mutability, ref expr) => { - (None, "static", ty, mutability, expr) + ast::ItemKind::Static(ref static_) => { + (None, "static", &static_.ty, static_.mutbl, &static_.expr) } ast::ItemKind::Const(ref const_) => { (Some(const_.defaultness), "const", &const_.ty, ast::Mutability::Not, &const_.expr) @@ -3204,11 +3204,11 @@ impl Rewrite for ast::ForeignItem { .map(|(s, _, _)| format!("{};", s)) } } - ast::ForeignItemKind::Static(ref ty, mutability, _) => { + ast::ForeignItemKind::Static(ref static_) => { // FIXME(#21): we're dropping potential comments in between the // function kw here. let vis = format_visibility(context, &self.vis); - let mut_str = format_mutability(mutability); + let mut_str = format_mutability(static_.mutbl); let prefix = format!( "{}static {}{}:", vis, @@ -3219,7 +3219,7 @@ impl Rewrite for ast::ForeignItem { rewrite_assign_rhs( context, prefix, - &**ty, + &*static_.ty, &RhsAssignKind::Ty, shape.sub_width(1)?, ) From 0e9ea62ecdc21587b2545ba6ad87c35c2dbe85e8 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 10 Aug 2022 20:58:38 +1000 Subject: [PATCH 14/14] XXX: various fixes for CI failures --- compiler/rustc_ast/src/ast.rs | 2 +- src/tools/rustfmt/src/items.rs | 54 +++++++++++++++++----------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 862ac541d5c3d..9c7ff078df129 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -12,7 +12,7 @@ //! - [`Stmt`] and [`StmtKind`]: An executable action that does not return a value. //! - [`FnDecl`], [`FnHeader`] and [`Param`]: Metadata associated with a function declaration. //! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters. -//! - [`EnumDef`] and [`Variant`]: Enum declaration. +//! - [`Enum`] and [`Variant`]: Enum declaration. //! - [`Lit`] and [`LitKind`]: Literal expressions. //! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`], [`MacDelimiter`]: Macro definition and invocation. //! - [`Attribute`]: Metadata associated with item. diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index c77065e855c3a..9624b0fbd7ed8 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -1803,9 +1803,13 @@ impl<'a> StaticParts<'a> { ast::ItemKind::Static(ref static_) => { (None, "static", &static_.ty, static_.mutbl, &static_.expr) } - ast::ItemKind::Const(ref const_) => { - (Some(const_.defaultness), "const", &const_.ty, ast::Mutability::Not, &const_.expr) - } + ast::ItemKind::Const(ref const_) => ( + Some(const_.defaultness), + "const", + &const_.ty, + ast::Mutability::Not, + &const_.expr, + ), _ => unreachable!(), }; StaticParts { @@ -1822,36 +1826,32 @@ impl<'a> StaticParts<'a> { pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self { match &ti.kind { - ast::AssocItemKind::Const(const_) => { - StaticParts { - prefix: "const", - vis: &ti.vis, - ident: ti.ident, - ty: &const_.ty, - mutability: ast::Mutability::Not, - expr_opt: const_.expr.as_ref(), - defaultness: Some(const_.defaultness), - span: ti.span, - } - } + ast::AssocItemKind::Const(const_) => StaticParts { + prefix: "const", + vis: &ti.vis, + ident: ti.ident, + ty: &const_.ty, + mutability: ast::Mutability::Not, + expr_opt: const_.expr.as_ref(), + defaultness: Some(const_.defaultness), + span: ti.span, + }, _ => unreachable!(), } } pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self { match &ii.kind { - ast::AssocItemKind::Const(const_) => { - StaticParts { - prefix: "const", - vis: &ii.vis, - ident: ii.ident, - ty: &const_.ty, - mutability: ast::Mutability::Not, - expr_opt: const_.expr.as_ref(), - defaultness: Some(const_.defaultness), - span: ii.span, - } - } + ast::AssocItemKind::Const(const_) => StaticParts { + prefix: "const", + vis: &ii.vis, + ident: ii.ident, + ty: &const_.ty, + mutability: ast::Mutability::Not, + expr_opt: const_.expr.as_ref(), + defaultness: Some(const_.defaultness), + span: ii.span, + }, _ => unreachable!(), } }