From b08a557f80865433d39d47934904a81b8ab3d720 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 29 Mar 2023 08:37:47 +0000 Subject: [PATCH 1/5] rust-analyzer guided enum variant structification --- compiler/rustc_ast/src/ast.rs | 11 +++++++---- compiler/rustc_ast/src/mut_visit.rs | 4 ++-- compiler/rustc_ast/src/visit.rs | 4 ++-- compiler/rustc_ast_lowering/src/item.rs | 2 +- compiler/rustc_ast_passes/src/ast_validation.rs | 4 ++-- compiler/rustc_ast_pretty/src/pprust/state/item.rs | 3 ++- compiler/rustc_builtin_macros/src/global_allocator.rs | 4 ++-- compiler/rustc_expand/src/build.rs | 7 ++++++- compiler/rustc_lint/src/builtin.rs | 3 ++- compiler/rustc_lint/src/unused.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 5 +++-- compiler/rustc_resolve/src/build_reduced_graph.rs | 2 +- compiler/rustc_resolve/src/late.rs | 2 +- .../clippy_lints/src/redundant_static_lifetimes.rs | 4 ++-- src/tools/clippy/clippy_utils/src/ast_utils.rs | 2 +- src/tools/rustfmt/src/items.rs | 2 +- 16 files changed, 36 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 9b6bfaadef05a..6c18e15ceb868 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2890,6 +2890,9 @@ pub struct Fn { pub body: Option>, } +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct Static(pub P, pub Mutability, pub Option>); + #[derive(Clone, Encodable, Decodable, Debug)] pub enum ItemKind { /// An `extern crate` item, with the optional *original* crate name if the crate was renamed. @@ -2903,7 +2906,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(Static), /// A constant item (`const`). /// /// E.g., `const FOO: i32 = 42;`. @@ -2975,7 +2978,7 @@ impl ItemKind { match self { ItemKind::ExternCrate(..) => "extern crate", ItemKind::Use(..) => "`use` import", - ItemKind::Static(..) => "static item", + ItemKind::Static(Static(..)) => "static item", ItemKind::Const(..) => "constant item", ItemKind::Fn(..) => "function", ItemKind::Mod(..) => "module", @@ -3084,7 +3087,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(a, b, c) => ItemKind::Static(Static(a, b, c)), ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), ForeignItemKind::MacCall(a) => ItemKind::MacCall(a), @@ -3097,7 +3100,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(a, b, c)) => ForeignItemKind::Static(a, b, c), ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind), ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind), ItemKind::MacCall(a) => ForeignItemKind::MacCall(a), diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 514978f5569a7..d5b5faf5cd77f 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -7,10 +7,10 @@ //! a `MutVisitor` renaming item names in a module will miss all of those //! that are created by the expansion of a macro. -use crate::ast::*; use crate::ptr::P; use crate::token::{self, Token}; use crate::tokenstream::*; +use crate::{ast::*, Static}; use rustc_data_structures::flat_map_in_place::FlatMapInPlace; use rustc_data_structures::sync::Lrc; @@ -1030,7 +1030,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(Static(ty, _, expr)) => { vis.visit_ty(ty); visit_opt(expr, |expr| vis.visit_expr(expr)); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index e5a0ad1f1e481..bfd047c67c466 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -13,7 +13,7 @@ //! instance, a walker looking for item names in a module will miss all of //! those that are created by the expansion of a macro. -use crate::ast::*; +use crate::{ast::*, Static}; use rustc_span::symbol::Ident; use rustc_span::Span; @@ -305,7 +305,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { match &item.kind { ItemKind::ExternCrate(_) => {} ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false), - ItemKind::Static(typ, _, expr) | ItemKind::Const(_, typ, expr) => { + ItemKind::Static(Static(typ, _, expr)) | ItemKind::Const(_, typ, expr) => { visitor.visit_ty(typ); 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 2efffbb6dc5e3..cfcb55275c108 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -229,7 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs) } - ItemKind::Static(t, m, e) => { + ItemKind::Static(ast::Static(t, m, e)) => { let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); hir::ItemKind::Static(ty, *m, body_id) } diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 44b6c77fc4195..777a1763e261f 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -9,8 +9,8 @@ use itertools::{Either, Itertools}; use rustc_ast::ptr::P; use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor}; -use rustc_ast::walk_list; use rustc_ast::*; +use rustc_ast::{walk_list, Static}; use rustc_ast_pretty::pprust::{self, State}; use rustc_data_structures::fx::FxIndexMap; use rustc_macros::Subdiagnostic; @@ -990,7 +990,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { replace_span: self.ending_semi_or_hi(item.span), }); } - ItemKind::Static(.., None) => { + ItemKind::Static(Static(.., None)) => { self.session.emit_err(errors::StaticWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index bf2c73a66a2cb..b62e006fbb745 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -2,6 +2,7 @@ use crate::pp::Breaks::Inconsistent; use crate::pprust::state::delimited::IterDelimited; use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT}; +use ast::Static; use rustc_ast as ast; use rustc_ast::GenericBound; use rustc_ast::ModKind; @@ -156,7 +157,7 @@ impl<'a> State<'a> { self.print_use_tree(tree); self.word(";"); } - ast::ItemKind::Static(ty, mutbl, body) => { + ast::ItemKind::Static(Static(ty, mutbl, body)) => { let def = 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 41b51bae73645..f817b20daf403 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -25,12 +25,12 @@ pub fn expand( // FIXME - if we get deref patterns, use them to reduce duplication here let (item, is_stmt, ty_span) = if let Annotatable::Item(item) = &item - && let ItemKind::Static(ty, ..) = &item.kind + && let ItemKind::Static(ast::Static(ty, ..)) = &item.kind { (item, false, ecx.with_def_site_ctxt(ty.span)) } else if let Annotatable::Stmt(stmt) = &item && let StmtKind::Item(item) = &stmt.kind - && let ItemKind::Static(ty, ..) = &item.kind + && let ItemKind::Static(ast::Static(ty, ..)) = &item.kind { (item, true, ecx.with_def_site_ctxt(ty.span)) } else { diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index 8a16143311b8f..82d1ff46ba9a0 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -623,7 +623,12 @@ impl<'a> ExtCtxt<'a> { mutbl: ast::Mutability, expr: P, ) -> P { - self.item(span, name, AttrVec::new(), ast::ItemKind::Static(ty, mutbl, Some(expr))) + self.item( + span, + name, + AttrVec::new(), + ast::ItemKind::Static(ast::Static(ty, mutbl, Some(expr))), + ) } pub fn item_const( diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index c1b247e3d615a..fbcc40f6657b5 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -43,6 +43,7 @@ use crate::{ types::{transparent_newtype_field, CItemKind}, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext, }; +use ast::Static; use hir::IsAsync; use rustc_ast::attr; use rustc_ast::tokenstream::{TokenStream, TokenTree}; @@ -370,7 +371,7 @@ impl EarlyLintPass for UnsafeCode { } } - ast::ItemKind::Static(..) => { + ast::ItemKind::Static(Static(..)) => { if let Some(attr) = attr::find_by_name(&it.attrs, sym::no_mangle) { self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleStatic); } diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 42e59f92840e0..b620e4592f5df 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -805,7 +805,7 @@ 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(.., Some(expr)) | Static(ast::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 ae8fe90e9d611..f2d37d810cfca 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -3,6 +3,7 @@ use crate::errors; use super::diagnostics::{dummy_arg, ConsumeClosingDelim}; use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign}; use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, TrailingToken}; +use ast::Static; use rustc_ast::ast::*; use rustc_ast::ptr::P; use rustc_ast::token::{self, Delimiter, TokenKind}; @@ -227,7 +228,7 @@ impl<'a> Parser<'a> { self.bump(); // `static` let m = self.parse_mutability(); let (ident, ty, expr) = self.parse_item_global(Some(m))?; - (ident, ItemKind::Static(ty, m, expr)) + (ident, ItemKind::Static(Static(ty, m, expr))) } else if let Const::Yes(const_span) = self.parse_constness(Case::Sensitive) { // CONST ITEM if self.token.is_keyword(kw::Impl) { @@ -862,7 +863,7 @@ impl<'a> Parser<'a> { let kind = match AssocItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Static(a, _, b) => { + ItemKind::Static(Static(a, _, b)) => { self.sess.emit_err(errors::AssociatedStaticItemNotAllowed { span }); AssocItemKind::Const(Defaultness::Final, a, b) } diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 19ccb3a6484af..b66df4752704d 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -688,7 +688,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { } // These items live in the value namespace. - ItemKind::Static(_, mt, _) => { + ItemKind::Static(ast::Static(_, mt, _)) => { let res = Res::Def(DefKind::Static(mt), def_id); self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion)); } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index b82b07bcf0cc8..7e3c43e380760 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2346,7 +2346,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { }); } - ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(_, ref ty, ref expr) => { + ItemKind::Static(ast::Static(ref ty, _, ref expr)) | ItemKind::Const(_, ref ty, ref expr) => { self.with_static_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 44bf824aa0e2d..d2f03f2a97fb0 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::msrvs::{self, Msrv}; use clippy_utils::source::snippet; -use rustc_ast::ast::{Item, ItemKind, Ty, TyKind}; +use rustc_ast::ast::{Item, ItemKind, Ty, TyKind, Static}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -106,7 +106,7 @@ impl EarlyLintPass for RedundantStaticLifetimes { // #2438) } - if let ItemKind::Static(ref var_type, _, _) = item.kind { + if let ItemKind::Static(Static(ref var_type, _, _)) = item.kind { Self::visit_type(var_type, 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 d2dedc2043957..30be129ad75a1 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -286,7 +286,7 @@ 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(ast::Static(lt, lm, le)), Static(ast::Static(rt, rm, re))) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), ( Fn(box ast::Fn { diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 25e8a024857ce..520bb5278001f 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -1805,7 +1805,7 @@ 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) => { + ast::ItemKind::Static(ast::Static(ref ty, mutability, ref expr)) => { (None, "static", ty, mutability, expr) } ast::ItemKind::Const(defaultness, ref ty, ref expr) => { From e3828777a6b669c33fc59f2bddde44e888d304af Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 29 Mar 2023 08:50:04 +0000 Subject: [PATCH 2/5] rust-analyzer guided tuple field to named field --- compiler/rustc_ast/src/ast.rs | 16 ++++++++++++---- compiler/rustc_ast/src/mut_visit.rs | 2 +- compiler/rustc_ast/src/visit.rs | 3 ++- compiler/rustc_ast_lowering/src/item.rs | 2 +- compiler/rustc_ast_passes/src/ast_validation.rs | 2 +- .../rustc_ast_pretty/src/pprust/state/item.rs | 2 +- .../rustc_builtin_macros/src/global_allocator.rs | 4 ++-- compiler/rustc_expand/src/build.rs | 4 ++-- compiler/rustc_lint/src/builtin.rs | 3 +-- compiler/rustc_lint/src/unused.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 4 ++-- .../rustc_resolve/src/build_reduced_graph.rs | 4 ++-- compiler/rustc_resolve/src/late.rs | 3 ++- .../src/redundant_static_lifetimes.rs | 2 +- src/tools/clippy/clippy_utils/src/ast_utils.rs | 2 +- src/tools/rustfmt/src/items.rs | 2 +- 16 files changed, 33 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 6c18e15ceb868..898e2fc8b3721 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2891,7 +2891,11 @@ pub struct Fn { } #[derive(Clone, Encodable, Decodable, Debug)] -pub struct Static(pub P, pub Mutability, pub Option>); +pub struct Static { + pub ty: P, + pub mutability: Mutability, + pub expr: Option>, +} #[derive(Clone, Encodable, Decodable, Debug)] pub enum ItemKind { @@ -2978,7 +2982,7 @@ impl ItemKind { match self { ItemKind::ExternCrate(..) => "extern crate", ItemKind::Use(..) => "`use` import", - ItemKind::Static(Static(..)) => "static item", + ItemKind::Static(..) => "static item", ItemKind::Const(..) => "constant item", ItemKind::Fn(..) => "function", ItemKind::Mod(..) => "module", @@ -3087,7 +3091,9 @@ 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(Static(a, b, c)), + ForeignItemKind::Static(a, b, c) => { + ItemKind::Static(Static { ty: a, mutability: b, expr: c }) + } ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), ForeignItemKind::MacCall(a) => ItemKind::MacCall(a), @@ -3100,7 +3106,9 @@ impl TryFrom for ForeignItemKind { fn try_from(item_kind: ItemKind) -> Result { Ok(match item_kind { - ItemKind::Static(Static(a, b, c)) => ForeignItemKind::Static(a, b, c), + ItemKind::Static(Static { ty: a, mutability: b, expr: c }) => { + ForeignItemKind::Static(a, b, c) + } ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind), ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind), ItemKind::MacCall(a) => ForeignItemKind::MacCall(a), diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index d5b5faf5cd77f..b7372af1e0d30 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1030,7 +1030,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(Static(ty, _, expr)) => { + ItemKind::Static(Static { ty, mutability: _, expr }) => { vis.visit_ty(ty); visit_opt(expr, |expr| vis.visit_expr(expr)); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index bfd047c67c466..e1d3c9d2cb74c 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -305,7 +305,8 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { match &item.kind { ItemKind::ExternCrate(_) => {} ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false), - ItemKind::Static(Static(typ, _, expr)) | ItemKind::Const(_, typ, expr) => { + ItemKind::Static(Static { ty: typ, mutability: _, expr }) + | ItemKind::Const(_, typ, expr) => { visitor.visit_ty(typ); 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 cfcb55275c108..769b20a579815 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -229,7 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs) } - ItemKind::Static(ast::Static(t, m, e)) => { + ItemKind::Static(ast::Static { ty: t, mutability: m, expr: e }) => { let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); hir::ItemKind::Static(ty, *m, body_id) } diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 777a1763e261f..8f77a372638e8 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -990,7 +990,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { replace_span: self.ending_semi_or_hi(item.span), }); } - ItemKind::Static(Static(.., None)) => { + ItemKind::Static(Static { expr: None, .. }) => { self.session.emit_err(errors::StaticWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index b62e006fbb745..d545fc87674aa 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -157,7 +157,7 @@ impl<'a> State<'a> { self.print_use_tree(tree); self.word(";"); } - ast::ItemKind::Static(Static(ty, mutbl, body)) => { + ast::ItemKind::Static(Static { ty, mutability: mutbl, expr: body }) => { let def = 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 f817b20daf403..f26e2572088be 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -25,12 +25,12 @@ pub fn expand( // FIXME - if we get deref patterns, use them to reduce duplication here let (item, is_stmt, ty_span) = if let Annotatable::Item(item) = &item - && let ItemKind::Static(ast::Static(ty, ..)) = &item.kind + && let ItemKind::Static(ast::Static { ty, ..}) = &item.kind { (item, false, ecx.with_def_site_ctxt(ty.span)) } else if let Annotatable::Stmt(stmt) = &item && let StmtKind::Item(item) = &stmt.kind - && let ItemKind::Static(ast::Static(ty, ..)) = &item.kind + && let ItemKind::Static(ast::Static { ty, ..}) = &item.kind { (item, true, ecx.with_def_site_ctxt(ty.span)) } else { diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index 82d1ff46ba9a0..e184f73209082 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -620,14 +620,14 @@ impl<'a> ExtCtxt<'a> { span: Span, name: Ident, ty: P, - mutbl: ast::Mutability, + mutability: ast::Mutability, expr: P, ) -> P { self.item( span, name, AttrVec::new(), - ast::ItemKind::Static(ast::Static(ty, mutbl, Some(expr))), + ast::ItemKind::Static(ast::Static { ty, mutability, expr: Some(expr) }), ) } diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index fbcc40f6657b5..c1b247e3d615a 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -43,7 +43,6 @@ use crate::{ types::{transparent_newtype_field, CItemKind}, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext, }; -use ast::Static; use hir::IsAsync; use rustc_ast::attr; use rustc_ast::tokenstream::{TokenStream, TokenTree}; @@ -371,7 +370,7 @@ impl EarlyLintPass for UnsafeCode { } } - ast::ItemKind::Static(Static(..)) => { + ast::ItemKind::Static(..) => { if let Some(attr) = attr::find_by_name(&it.attrs, sym::no_mangle) { self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleStatic); } diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index b620e4592f5df..8f652511459c5 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -805,7 +805,7 @@ trait UnusedDelimLint { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { use ast::ItemKind::*; - if let Const(.., Some(expr)) | Static(ast::Static(.., Some(expr))) = &item.kind { + if let Const(.., Some(expr)) | Static(ast::Static { expr: 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 f2d37d810cfca..60ecaf5cb22a5 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> { self.bump(); // `static` let m = self.parse_mutability(); let (ident, ty, expr) = self.parse_item_global(Some(m))?; - (ident, ItemKind::Static(Static(ty, m, expr))) + (ident, ItemKind::Static(Static { ty, mutability: m, expr })) } else if let Const::Yes(const_span) = self.parse_constness(Case::Sensitive) { // CONST ITEM if self.token.is_keyword(kw::Impl) { @@ -863,7 +863,7 @@ impl<'a> Parser<'a> { let kind = match AssocItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Static(Static(a, _, b)) => { + ItemKind::Static(Static { ty: a, mutability: _, expr: b }) => { self.sess.emit_err(errors::AssociatedStaticItemNotAllowed { span }); AssocItemKind::Const(Defaultness::Final, a, b) } diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index b66df4752704d..3a79ddca2eb49 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -688,8 +688,8 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { } // These items live in the value namespace. - ItemKind::Static(ast::Static(_, mt, _)) => { - let res = Res::Def(DefKind::Static(mt), def_id); + ItemKind::Static(ast::Static { mutability, .. }) => { + let res = Res::Def(DefKind::Static(mutability), def_id); self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion)); } ItemKind::Const(..) => { diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 7e3c43e380760..35b4d9dad83bf 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2346,7 +2346,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { }); } - ItemKind::Static(ast::Static(ref ty, _, ref expr)) | ItemKind::Const(_, ref ty, ref expr) => { + ItemKind::Static(ast::Static { ref ty, ref expr, .. }) + | ItemKind::Const(_, ref ty, ref expr) => { self.with_static_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 d2f03f2a97fb0..434f001133193 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs @@ -106,7 +106,7 @@ impl EarlyLintPass for RedundantStaticLifetimes { // #2438) } - if let ItemKind::Static(Static(ref var_type, _, _)) = item.kind { + if let ItemKind::Static(Static{ ty: ref var_type,.. }) = item.kind { Self::visit_type(var_type, 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 30be129ad75a1..258207639a170 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -286,7 +286,7 @@ 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(ast::Static(lt, lm, le)), Static(ast::Static(rt, rm, re))) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), + (Static(ast::Static{ ty: lt, mutability: lm, expr: le}), Static(ast::Static { ty: rt, mutability: rm, expr: re})) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), ( Fn(box ast::Fn { diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 520bb5278001f..6cbe68ae39848 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -1805,7 +1805,7 @@ 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(ast::Static(ref ty, mutability, ref expr)) => { + ast::ItemKind::Static(ast::Static { ref ty, mutability, ref expr}) => { (None, "static", ty, mutability, expr) } ast::ItemKind::Const(defaultness, ref ty, ref expr) => { From ec74653652e59fc209506c084357ccee922a2d73 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 29 Mar 2023 09:20:45 +0000 Subject: [PATCH 3/5] Split out ast::ItemKind::Const into its own struct --- compiler/rustc_ast/src/ast.rs | 17 ++++++++++----- compiler/rustc_ast/src/mut_visit.rs | 21 ++++++++++++------- compiler/rustc_ast/src/visit.rs | 8 +++---- compiler/rustc_ast_lowering/src/item.rs | 10 ++++----- .../rustc_ast_passes/src/ast_validation.rs | 16 +++++++------- .../rustc_ast_pretty/src/pprust/state/item.rs | 15 +++++++++---- compiler/rustc_builtin_macros/src/test.rs | 10 ++++----- compiler/rustc_expand/src/build.rs | 9 ++++++-- compiler/rustc_lint/src/unused.rs | 4 +++- compiler/rustc_parse/src/parser/item.rs | 14 ++++++++----- compiler/rustc_resolve/src/late.rs | 10 ++++----- .../src/redundant_static_lifetimes.rs | 4 ++-- .../clippy/clippy_utils/src/ast_utils.rs | 4 ++-- src/tools/rustfmt/src/items.rs | 8 +++---- 14 files changed, 89 insertions(+), 61 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 898e2fc8b3721..985b66e6bea6b 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2897,6 +2897,13 @@ pub struct Static { pub expr: Option>, } +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct ConstItem { + 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. @@ -2914,7 +2921,7 @@ pub enum ItemKind { /// A constant item (`const`). /// /// E.g., `const FOO: i32 = 42;`. - Const(Defaultness, P, Option>), + Const(ConstItem), /// A function declaration (`fn`). /// /// E.g., `fn foo(bar: usize) -> usize { .. }`. @@ -3030,7 +3037,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(ConstItem), /// An associated function. Fn(Box), /// An associated type. @@ -3042,7 +3049,7 @@ pub enum AssocItemKind { impl AssocItemKind { pub fn defaultness(&self) -> Defaultness { match *self { - Self::Const(defaultness, ..) + Self::Const(ConstItem { defaultness, .. }) | Self::Fn(box Fn { defaultness, .. }) | Self::Type(box TyAlias { defaultness, .. }) => defaultness, Self::MacCall(..) => Defaultness::Final, @@ -3053,7 +3060,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(item) => ItemKind::Const(item), AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), AssocItemKind::MacCall(a) => ItemKind::MacCall(a), @@ -3066,7 +3073,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(item) => AssocItemKind::Const(item), ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind), ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind), ItemKind::MacCall(a) => AssocItemKind::MacCall(a), diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index b7372af1e0d30..e99a2fee67c88 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1034,10 +1034,8 @@ 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) => { - visit_defaultness(defaultness, vis); - vis.visit_ty(ty); - visit_opt(expr, |expr| vis.visit_expr(expr)); + ItemKind::Const(item) => { + visit_const_item(item, vis); } ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => { visit_defaultness(defaultness, vis); @@ -1120,10 +1118,8 @@ pub fn noop_flat_map_assoc_item( visitor.visit_vis(vis); visit_attrs(attrs, visitor); match kind { - AssocItemKind::Const(defaultness, ty, expr) => { - visit_defaultness(defaultness, visitor); - visitor.visit_ty(ty); - visit_opt(expr, |expr| visitor.visit_expr(expr)); + AssocItemKind::Const(item) => { + visit_const_item(item, visitor); } AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => { visit_defaultness(defaultness, visitor); @@ -1153,6 +1149,15 @@ pub fn noop_flat_map_assoc_item( smallvec![item] } +fn visit_const_item( + ConstItem { defaultness, ty, expr }: &mut ConstItem, + visitor: &mut T, +) { + visit_defaultness(defaultness, visitor); + visitor.visit_ty(ty); + visit_opt(expr, |expr| visitor.visit_expr(expr)); +} + pub fn noop_visit_fn_header(header: &mut FnHeader, vis: &mut T) { let FnHeader { unsafety, asyncness, constness, ext: _ } = header; visit_constness(constness, vis); diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index e1d3c9d2cb74c..55a17efe41602 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -305,9 +305,9 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { match &item.kind { ItemKind::ExternCrate(_) => {} ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false), - ItemKind::Static(Static { ty: typ, mutability: _, expr }) - | ItemKind::Const(_, typ, expr) => { - visitor.visit_ty(typ); + ItemKind::Static(Static { ty, mutability: _, expr }) + | ItemKind::Const(ConstItem { ty, expr, .. }) => { + visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => { @@ -675,7 +675,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(ConstItem { 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 769b20a579815..b41bb5017e51d 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -233,8 +233,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(_, t, e) => { - let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); + ItemKind::Const(ast::ConstItem { ty, expr, .. }) => { + let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref()); hir::ItemKind::Const(ty, body_id) } ItemKind::Fn(box Fn { @@ -708,10 +708,10 @@ impl<'hir> LoweringContext<'_, 'hir> { let trait_item_def_id = hir_id.expect_owner(); let (generics, kind, has_default) = match &i.kind { - AssocItemKind::Const(_, ty, default) => { + AssocItemKind::Const(ConstItem { ty, expr, .. }) => { let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy)); - 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 { sig, generics, body: None, .. }) => { @@ -809,7 +809,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_attrs(hir_id, &i.attrs); let (generics, kind) = match &i.kind { - AssocItemKind::Const(_, ty, expr) => { + AssocItemKind::Const(ConstItem { ty, expr, .. }) => { let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy)); ( diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 8f77a372638e8..43d086ec52d85 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -983,8 +983,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.err_handler().emit_err(errors::FieldlessUnion { span: item.span }); } } - ItemKind::Const(def, .., None) => { - self.check_defaultness(item.span, *def); + ItemKind::Const(ConstItem { defaultness, expr: None, .. }) => { + self.check_defaultness(item.span, *defaultness); self.session.emit_err(errors::ConstWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), @@ -1259,13 +1259,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if ctxt == AssocCtxt::Impl { match &item.kind { - AssocItemKind::Const(_, _, body) => { - if body.is_none() { - self.session.emit_err(errors::AssocConstWithoutBody { - span: item.span, - replace_span: self.ending_semi_or_hi(item.span), - }); - } + AssocItemKind::Const(ConstItem { expr: None, .. }) => { + self.session.emit_err(errors::AssocConstWithoutBody { + span: item.span, + replace_span: self.ending_semi_or_hi(item.span), + }); } AssocItemKind::Fn(box Fn { body, .. }) => { if body.is_none() { diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index d545fc87674aa..92b9c8fd766ab 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -168,8 +168,15 @@ impl<'a> State<'a> { def, ); } - ast::ItemKind::Const(def, ty, body) => { - self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, *def); + ast::ItemKind::Const(ast::ConstItem { defaultness, ty, expr }) => { + self.print_item_const( + item.ident, + None, + ty, + expr.as_deref(), + &item.vis, + *defaultness, + ); } ast::ItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => { self.print_fn_full( @@ -508,8 +515,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(ast::ConstItem { defaultness, ty, expr }) => { + self.print_item_const(ident, None, ty, expr.as_deref(), vis, *defaultness); } ast::AssocItemKind::Type(box ast::TyAlias { defaultness, diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 44b9c4718a75f..ced341e54130e 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -264,11 +264,11 @@ pub fn expand_test_or_bench( cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp), ], // const $ident: test::TestDescAndFn = - ast::ItemKind::Const( - ast::Defaultness::Final, - cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))), + ast::ItemKind::Const(ast::ConstItem { + 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"), @@ -361,7 +361,7 @@ pub fn expand_test_or_bench( ], ), // } ), - ), + }), ); test_const = test_const.map(|mut tc| { tc.vis.kind = ast::VisibilityKind::Public; diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index e184f73209082..39fed9fdd0d40 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -638,8 +638,13 @@ impl<'a> ExtCtxt<'a> { ty: P, expr: P, ) -> P { - let def = ast::Defaultness::Final; - self.item(span, name, AttrVec::new(), ast::ItemKind::Const(def, ty, Some(expr))) + let defaultness = ast::Defaultness::Final; + self.item( + span, + name, + AttrVec::new(), + ast::ItemKind::Const(ast::ConstItem { defaultness, ty, expr: Some(expr) }), + ) } // Builds `#[name]`. diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 8f652511459c5..d920e237508c7 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -805,7 +805,9 @@ trait UnusedDelimLint { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { use ast::ItemKind::*; - if let Const(.., Some(expr)) | Static(ast::Static { expr: Some(expr), .. }) = &item.kind { + if let Const(ast::ConstItem { expr: Some(expr), .. }) + | Static(ast::Static { expr: 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 60ecaf5cb22a5..f6422d30ba506 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -237,7 +237,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(ConstItem { defaultness: def_(), ty, expr })) } } else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() { // TRAIT ITEM @@ -863,9 +863,13 @@ impl<'a> Parser<'a> { let kind = match AssocItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Static(Static { ty: a, mutability: _, expr: b }) => { + ItemKind::Static(Static { ty, mutability: _, expr }) => { self.sess.emit_err(errors::AssociatedStaticItemNotAllowed { span }); - AssocItemKind::Const(Defaultness::Final, a, b) + AssocItemKind::Const(ConstItem { + defaultness: Defaultness::Final, + ty, + expr, + }) } _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"), }, @@ -1115,12 +1119,12 @@ impl<'a> Parser<'a> { let kind = match ForeignItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Const(_, a, b) => { + ItemKind::Const(ConstItem { ty, expr, .. }) => { self.sess.emit_err(errors::ExternItemCannotBeConst { ident_span: ident.span, const_span: span.with_hi(ident.span.lo()), }); - ForeignItemKind::Static(a, Mutability::Not, b) + ForeignItemKind::Static(ty, Mutability::Not, expr) } _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"), }, diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 35b4d9dad83bf..735644ec8d51c 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2347,7 +2347,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { } ItemKind::Static(ast::Static { ref ty, ref expr, .. }) - | ItemKind::Const(_, ref ty, ref expr) => { + | ItemKind::Const(ast::ConstItem { ref ty, ref expr, .. }) => { self.with_static_rib(|this| { this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| { this.visit_ty(ty); @@ -2625,11 +2625,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { for item in trait_items { self.resolve_doc_links(&item.attrs, MaybeExported::Ok(item.id)); match &item.kind { - AssocItemKind::Const(_, ty, default) => { + AssocItemKind::Const(ast::ConstItem { 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. // @@ -2800,7 +2800,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { use crate::ResolutionError::*; self.resolve_doc_links(&item.attrs, MaybeExported::ImplItem(trait_id.ok_or(&item.vis))); match &item.kind { - AssocItemKind::Const(_, ty, default) => { + AssocItemKind::Const(ast::ConstItem { ty, expr, .. }) => { debug!("resolve_implementation AssocItemKind::Const"); // If this is a trait impl, ensure the const // exists in trait @@ -2815,7 +2815,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { ); 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 434f001133193..d41f79f81315e 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::msrvs::{self, Msrv}; use clippy_utils::source::snippet; -use rustc_ast::ast::{Item, ItemKind, Ty, TyKind, Static}; +use rustc_ast::ast::{Item, ItemKind, Ty, TyKind, Static, ConstItem}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -100,7 +100,7 @@ impl EarlyLintPass for RedundantStaticLifetimes { } if !item.span.from_expansion() { - if let ItemKind::Const(_, ref var_type, _) = item.kind { + if let ItemKind::Const(ConstItem { ty: ref var_type, .. }) = item.kind { Self::visit_type(var_type, cx, "constants have by default a `'static` lifetime"); // Don't check associated consts because `'static` cannot be elided on those (issue // #2438) diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 258207639a170..be841098dba30 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -287,7 +287,7 @@ 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(ast::Static{ ty: lt, mutability: lm, expr: le}), Static(ast::Static { ty: rt, mutability: rm, expr: 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(ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(ast::ConstItem { 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, @@ -451,7 +451,7 @@ 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(ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(ast::ConstItem { 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, diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 6cbe68ae39848..0dc8a4e937f2a 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -1808,7 +1808,7 @@ impl<'a> StaticParts<'a> { ast::ItemKind::Static(ast::Static { ref ty, mutability, ref expr}) => { (None, "static", ty, mutability, expr) } - ast::ItemKind::Const(defaultness, ref ty, ref expr) => { + ast::ItemKind::Const(ast::ConstItem { defaultness, ref ty, ref expr}) => { (Some(defaultness), "const", ty, ast::Mutability::Not, expr) } _ => unreachable!(), @@ -1827,8 +1827,8 @@ 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) + ast::AssocItemKind::Const(ast::ConstItem {defaultness, ref ty, ref expr}) => { + (defaultness, ty, expr) } _ => unreachable!(), }; @@ -1846,7 +1846,7 @@ impl<'a> StaticParts<'a> { 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), + ast::AssocItemKind::Const(ast::ConstItem { defaultness, ref ty, ref expr}) => (defaultness, ty, expr), _ => unreachable!(), }; StaticParts { From 4bebdd7104c41fb1a32e2d9588f64874dd5a484f Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 29 Mar 2023 12:34:05 +0000 Subject: [PATCH 4/5] box a bunch of large types --- compiler/rustc_ast/src/ast.rs | 16 +++---- compiler/rustc_ast/src/mut_visit.rs | 2 +- compiler/rustc_ast/src/visit.rs | 6 +-- compiler/rustc_ast_lowering/src/item.rs | 8 ++-- .../rustc_ast_passes/src/ast_validation.rs | 6 +-- .../rustc_ast_pretty/src/pprust/state/item.rs | 6 +-- .../src/global_allocator.rs | 4 +- compiler/rustc_builtin_macros/src/test.rs | 48 ++++++++++--------- compiler/rustc_expand/src/build.rs | 4 +- compiler/rustc_lint/src/unused.rs | 4 +- compiler/rustc_parse/src/parser/item.rs | 12 ++--- .../rustc_resolve/src/build_reduced_graph.rs | 2 +- compiler/rustc_resolve/src/late.rs | 8 ++-- .../src/redundant_static_lifetimes.rs | 4 +- .../clippy/clippy_utils/src/ast_utils.rs | 6 +-- src/tools/rustfmt/src/items.rs | 26 +++++----- 16 files changed, 83 insertions(+), 79 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 985b66e6bea6b..14c190daf3512 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2917,11 +2917,11 @@ pub enum ItemKind { /// A static item (`static`). /// /// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`. - Static(Static), + Static(Box), /// A constant item (`const`). /// /// E.g., `const FOO: i32 = 42;`. - Const(ConstItem), + Const(Box), /// A function declaration (`fn`). /// /// E.g., `fn foo(bar: usize) -> usize { .. }`. @@ -3037,7 +3037,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(ConstItem), + Const(Box), /// An associated function. Fn(Box), /// An associated type. @@ -3049,7 +3049,7 @@ pub enum AssocItemKind { impl AssocItemKind { pub fn defaultness(&self) -> Defaultness { match *self { - Self::Const(ConstItem { defaultness, .. }) + Self::Const(box ConstItem { defaultness, .. }) | Self::Fn(box Fn { defaultness, .. }) | Self::Type(box TyAlias { defaultness, .. }) => defaultness, Self::MacCall(..) => Defaultness::Final, @@ -3099,7 +3099,7 @@ impl From for ItemKind { fn from(foreign_item_kind: ForeignItemKind) -> ItemKind { match foreign_item_kind { ForeignItemKind::Static(a, b, c) => { - ItemKind::Static(Static { ty: a, mutability: b, expr: c }) + ItemKind::Static(Static { ty: a, mutability: b, expr: c }.into()) } ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), @@ -3113,7 +3113,7 @@ impl TryFrom for ForeignItemKind { fn try_from(item_kind: ItemKind) -> Result { Ok(match item_kind { - ItemKind::Static(Static { ty: a, mutability: b, expr: c }) => { + ItemKind::Static(box Static { ty: a, mutability: b, expr: c }) => { ForeignItemKind::Static(a, b, c) } ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind), @@ -3132,8 +3132,8 @@ mod size_asserts { use super::*; use rustc_data_structures::static_assert_size; // tidy-alphabetical-start - static_assert_size!(AssocItem, 104); - static_assert_size!(AssocItemKind, 32); + static_assert_size!(AssocItem, 88); + static_assert_size!(AssocItemKind, 16); static_assert_size!(Attribute, 32); static_assert_size!(Block, 32); static_assert_size!(Expr, 72); diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index e99a2fee67c88..4d569e0fb94c9 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1030,7 +1030,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(Static { ty, mutability: _, expr }) => { + ItemKind::Static(box Static { ty, mutability: _, expr }) => { vis.visit_ty(ty); visit_opt(expr, |expr| vis.visit_expr(expr)); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 55a17efe41602..970bda58f350c 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -305,8 +305,8 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { match &item.kind { ItemKind::ExternCrate(_) => {} ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false), - ItemKind::Static(Static { ty, mutability: _, expr }) - | ItemKind::Const(ConstItem { ty, expr, .. }) => { + ItemKind::Static(box Static { ty, mutability: _, expr }) + | ItemKind::Const(box ConstItem { ty, expr, .. }) => { visitor.visit_ty(ty); walk_list!(visitor, visit_expr, expr); } @@ -675,7 +675,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(ConstItem { ty, expr, .. }) => { + AssocItemKind::Const(box ConstItem { 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 b41bb5017e51d..803dbbffbd863 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -229,11 +229,11 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs) } - ItemKind::Static(ast::Static { ty: t, mutability: m, expr: e }) => { + ItemKind::Static(box ast::Static { ty: t, mutability: m, expr: e }) => { let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); hir::ItemKind::Static(ty, *m, body_id) } - ItemKind::Const(ast::ConstItem { ty, expr, .. }) => { + ItemKind::Const(box ast::ConstItem { ty, expr, .. }) => { let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref()); hir::ItemKind::Const(ty, body_id) } @@ -708,7 +708,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let trait_item_def_id = hir_id.expect_owner(); let (generics, kind, has_default) = match &i.kind { - AssocItemKind::Const(ConstItem { ty, expr, .. }) => { + AssocItemKind::Const(box ConstItem { ty, expr, .. }) => { let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy)); let body = expr.as_ref().map(|x| self.lower_const_body(i.span, Some(x))); @@ -809,7 +809,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_attrs(hir_id, &i.attrs); let (generics, kind) = match &i.kind { - AssocItemKind::Const(ConstItem { ty, expr, .. }) => { + AssocItemKind::Const(box ConstItem { ty, expr, .. }) => { let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy)); ( diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 43d086ec52d85..431a569c1e0d8 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -983,14 +983,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.err_handler().emit_err(errors::FieldlessUnion { span: item.span }); } } - ItemKind::Const(ConstItem { defaultness, expr: None, .. }) => { + ItemKind::Const(box ConstItem { defaultness, expr: None, .. }) => { self.check_defaultness(item.span, *defaultness); self.session.emit_err(errors::ConstWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), }); } - ItemKind::Static(Static { expr: None, .. }) => { + ItemKind::Static(box Static { expr: None, .. }) => { self.session.emit_err(errors::StaticWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), @@ -1259,7 +1259,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if ctxt == AssocCtxt::Impl { match &item.kind { - AssocItemKind::Const(ConstItem { expr: None, .. }) => { + AssocItemKind::Const(box ConstItem { expr: None, .. }) => { self.session.emit_err(errors::AssocConstWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 92b9c8fd766ab..e2d133eb233a2 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -157,7 +157,7 @@ impl<'a> State<'a> { self.print_use_tree(tree); self.word(";"); } - ast::ItemKind::Static(Static { ty, mutability: mutbl, expr: body }) => { + ast::ItemKind::Static(box Static { ty, mutability: mutbl, expr: body }) => { let def = ast::Defaultness::Final; self.print_item_const( item.ident, @@ -168,7 +168,7 @@ impl<'a> State<'a> { def, ); } - ast::ItemKind::Const(ast::ConstItem { defaultness, ty, expr }) => { + ast::ItemKind::Const(box ast::ConstItem { defaultness, ty, expr }) => { self.print_item_const( item.ident, None, @@ -515,7 +515,7 @@ 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(ast::ConstItem { defaultness, ty, expr }) => { + ast::AssocItemKind::Const(box ast::ConstItem { defaultness, ty, expr }) => { self.print_item_const(ident, None, ty, expr.as_deref(), vis, *defaultness); } ast::AssocItemKind::Type(box ast::TyAlias { diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs index f26e2572088be..34361a6d868e5 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -25,12 +25,12 @@ pub fn expand( // FIXME - if we get deref patterns, use them to reduce duplication here let (item, is_stmt, ty_span) = if let Annotatable::Item(item) = &item - && let ItemKind::Static(ast::Static { ty, ..}) = &item.kind + && let ItemKind::Static(box ast::Static { ty, ..}) = &item.kind { (item, false, ecx.with_def_site_ctxt(ty.span)) } else if let Annotatable::Stmt(stmt) = &item && let StmtKind::Item(item) = &stmt.kind - && let ItemKind::Static(ast::Static { ty, ..}) = &item.kind + && let ItemKind::Static(box ast::Static { ty, ..}) = &item.kind { (item, true, ecx.with_def_site_ctxt(ty.span)) } else { diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index ced341e54130e..a76ed4ee6cee3 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -254,25 +254,27 @@ pub fn expand_test_or_bench( let location_info = get_location_info(cx, &item); - let mut test_const = cx.item( - sp, - Ident::new(item.ident.name, sp), - thin_vec![ - // #[cfg(test)] - cx.attr_nested_word(sym::cfg, sym::test, attr_sp), - // #[rustc_test_marker = "test_case_sort_key"] - cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp), - ], - // const $ident: test::TestDescAndFn = - ast::ItemKind::Const(ast::ConstItem { - defaultness: ast::Defaultness::Final, - ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))), - // test::TestDescAndFn { - expr: Some( - cx.expr_struct( - sp, - test_path("TestDescAndFn"), - thin_vec![ + let mut test_const = + cx.item( + sp, + Ident::new(item.ident.name, sp), + thin_vec![ + // #[cfg(test)] + cx.attr_nested_word(sym::cfg, sym::test, attr_sp), + // #[rustc_test_marker = "test_case_sort_key"] + cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp), + ], + // const $ident: test::TestDescAndFn = + ast::ItemKind::Const( + ast::ConstItem { + defaultness: ast::Defaultness::Final, + ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))), + // test::TestDescAndFn { + expr: Some( + cx.expr_struct( + sp, + test_path("TestDescAndFn"), + thin_vec![ // desc: test::TestDesc { field( "desc", @@ -359,10 +361,12 @@ pub fn expand_test_or_bench( // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...) field("testfn", test_fn), // } ], - ), // } + ), // } + ), + } + .into(), ), - }), - ); + ); 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 39fed9fdd0d40..59c588b39554e 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -627,7 +627,7 @@ impl<'a> ExtCtxt<'a> { span, name, AttrVec::new(), - ast::ItemKind::Static(ast::Static { ty, mutability, expr: Some(expr) }), + ast::ItemKind::Static(ast::Static { ty, mutability, expr: Some(expr) }.into()), ) } @@ -643,7 +643,7 @@ impl<'a> ExtCtxt<'a> { span, name, AttrVec::new(), - ast::ItemKind::Const(ast::ConstItem { defaultness, ty, expr: Some(expr) }), + ast::ItemKind::Const(ast::ConstItem { defaultness, ty, expr: Some(expr) }.into()), ) } diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index d920e237508c7..c856e973e5c82 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -805,8 +805,8 @@ trait UnusedDelimLint { fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { use ast::ItemKind::*; - if let Const(ast::ConstItem { expr: Some(expr), .. }) - | Static(ast::Static { expr: Some(expr), .. }) = &item.kind + if let Const(box ast::ConstItem { 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 f6422d30ba506..66fa9fb38d424 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> { self.bump(); // `static` let m = self.parse_mutability(); let (ident, ty, expr) = self.parse_item_global(Some(m))?; - (ident, ItemKind::Static(Static { ty, mutability: m, expr })) + (ident, ItemKind::Static(Box::new(Static { ty, mutability: m, expr }))) } else if let Const::Yes(const_span) = self.parse_constness(Case::Sensitive) { // CONST ITEM if self.token.is_keyword(kw::Impl) { @@ -237,7 +237,7 @@ impl<'a> Parser<'a> { } else { self.recover_const_mut(const_span); let (ident, ty, expr) = self.parse_item_global(None)?; - (ident, ItemKind::Const(ConstItem { defaultness: def_(), ty, expr })) + (ident, ItemKind::Const(Box::new(ConstItem { defaultness: def_(), ty, expr }))) } } else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() { // TRAIT ITEM @@ -863,13 +863,13 @@ impl<'a> Parser<'a> { let kind = match AssocItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Static(Static { ty, mutability: _, expr }) => { + ItemKind::Static(box Static { ty, mutability: _, expr }) => { self.sess.emit_err(errors::AssociatedStaticItemNotAllowed { span }); - AssocItemKind::Const(ConstItem { + AssocItemKind::Const(Box::new(ConstItem { defaultness: Defaultness::Final, ty, expr, - }) + })) } _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"), }, @@ -1119,7 +1119,7 @@ impl<'a> Parser<'a> { let kind = match ForeignItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Const(ConstItem { ty, expr, .. }) => { + ItemKind::Const(box ConstItem { ty, expr, .. }) => { self.sess.emit_err(errors::ExternItemCannotBeConst { ident_span: ident.span, const_span: span.with_hi(ident.span.lo()), diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 3a79ddca2eb49..10d282a21a7ce 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -688,7 +688,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { } // These items live in the value namespace. - ItemKind::Static(ast::Static { mutability, .. }) => { + ItemKind::Static(box ast::Static { mutability, .. }) => { let res = Res::Def(DefKind::Static(mutability), def_id); self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion)); } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 735644ec8d51c..d38bfc62abd9b 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2346,8 +2346,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { }); } - ItemKind::Static(ast::Static { ref ty, ref expr, .. }) - | ItemKind::Const(ast::ConstItem { ref ty, ref expr, .. }) => { + ItemKind::Static(box ast::Static { ref ty, ref expr, .. }) + | ItemKind::Const(box ast::ConstItem { ref ty, ref expr, .. }) => { self.with_static_rib(|this| { this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| { this.visit_ty(ty); @@ -2625,7 +2625,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { for item in trait_items { self.resolve_doc_links(&item.attrs, MaybeExported::Ok(item.id)); match &item.kind { - AssocItemKind::Const(ast::ConstItem { ty, expr, .. }) => { + AssocItemKind::Const(box ast::ConstItem { ty, expr, .. }) => { self.visit_ty(ty); // Only impose the restrictions of `ConstRibKind` for an // actual constant expression in a provided default. @@ -2800,7 +2800,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { use crate::ResolutionError::*; self.resolve_doc_links(&item.attrs, MaybeExported::ImplItem(trait_id.ok_or(&item.vis))); match &item.kind { - AssocItemKind::Const(ast::ConstItem { ty, expr, .. }) => { + AssocItemKind::Const(box ast::ConstItem { ty, expr, .. }) => { debug!("resolve_implementation AssocItemKind::Const"); // If this is a trait impl, ensure the const // exists in trait 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 d41f79f81315e..aa47f3aed54d4 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs @@ -100,13 +100,13 @@ impl EarlyLintPass for RedundantStaticLifetimes { } if !item.span.from_expansion() { - if let ItemKind::Const(ConstItem { ty: ref var_type, .. }) = item.kind { + if let ItemKind::Const(box ConstItem { ty: ref var_type, .. }) = item.kind { Self::visit_type(var_type, 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(Static{ ty: ref var_type,.. }) = item.kind { + if let ItemKind::Static(box Static { ty: ref var_type,.. }) = item.kind { Self::visit_type(var_type, 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 be841098dba30..0d2a7440c4659 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -286,8 +286,8 @@ 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(ast::Static{ ty: lt, mutability: lm, expr: le}), Static(ast::Static { ty: rt, mutability: rm, expr: re})) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), - (Const(ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(ast::ConstItem { defaultness: rd, ty: rt, expr: re} )) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), + (Static(box ast::Static{ ty: lt, mutability: lm, expr: le}), Static(box ast::Static { ty: rt, mutability: rm, expr: re})) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), + (Const(box ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(box ast::ConstItem { 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, @@ -451,7 +451,7 @@ 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(ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(ast::ConstItem { defaultness: rd, ty: rt, expr: re})) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), + (Const(box ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(box ast::ConstItem { 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, diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs index 0dc8a4e937f2a..43779cfaecd3a 100644 --- a/src/tools/rustfmt/src/items.rs +++ b/src/tools/rustfmt/src/items.rs @@ -1804,13 +1804,15 @@ 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(ast::Static { ref ty, mutability, ref expr}) => { - (None, "static", ty, mutability, expr) - } - ast::ItemKind::Const(ast::ConstItem { defaultness, ref ty, ref expr}) => { - (Some(defaultness), "const", ty, ast::Mutability::Not, expr) - } + let (defaultness, prefix, ty, mutability, expr) = match &item.kind { + ast::ItemKind::Static(s) => (None, "static", &s.ty, s.mutability, &s.expr), + ast::ItemKind::Const(c) => ( + Some(c.defaultness), + "const", + &c.ty, + ast::Mutability::Not, + &c.expr, + ), _ => unreachable!(), }; StaticParts { @@ -1826,10 +1828,8 @@ 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(ast::ConstItem {defaultness, ref ty, ref expr}) => { - (defaultness, ty, expr) - } + let (defaultness, ty, expr_opt) = match &ti.kind { + ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr), _ => unreachable!(), }; StaticParts { @@ -1845,8 +1845,8 @@ impl<'a> StaticParts<'a> { } pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self { - let (defaultness, ty, expr) = match ii.kind { - ast::AssocItemKind::Const(ast::ConstItem { defaultness, ref ty, ref expr}) => (defaultness, ty, expr), + let (defaultness, ty, expr) = match &ii.kind { + ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr), _ => unreachable!(), }; StaticParts { From 373807a95cc427bb2672c96cb00ce71d8a5f4040 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 29 Mar 2023 14:14:11 +0000 Subject: [PATCH 5/5] Rename `ast::Static` to `ast::StaticItem` to match `ast::ConstItem` --- compiler/rustc_ast/src/ast.rs | 8 +- compiler/rustc_ast/src/mut_visit.rs | 4 +- compiler/rustc_ast/src/visit.rs | 4 +- compiler/rustc_ast_lowering/src/item.rs | 2 +- .../rustc_ast_passes/src/ast_validation.rs | 4 +- .../rustc_ast_pretty/src/pprust/state/item.rs | 4 +- .../src/global_allocator.rs | 4 +- compiler/rustc_expand/src/build.rs | 2 +- compiler/rustc_lint/src/unused.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 6 +- .../rustc_resolve/src/build_reduced_graph.rs | 2 +- compiler/rustc_resolve/src/late.rs | 2 +- .../src/redundant_static_lifetimes.rs | 4 +- .../clippy/clippy_utils/src/ast_utils.rs | 2 +- tests/ui/stats/hir-stats.stderr | 86 +++++++++---------- 15 files changed, 68 insertions(+), 68 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 14c190daf3512..fb9d71b52a8a6 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2891,7 +2891,7 @@ pub struct Fn { } #[derive(Clone, Encodable, Decodable, Debug)] -pub struct Static { +pub struct StaticItem { pub ty: P, pub mutability: Mutability, pub expr: Option>, @@ -2917,7 +2917,7 @@ pub enum ItemKind { /// A static item (`static`). /// /// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`. - Static(Box), + Static(Box), /// A constant item (`const`). /// /// E.g., `const FOO: i32 = 42;`. @@ -3099,7 +3099,7 @@ impl From for ItemKind { fn from(foreign_item_kind: ForeignItemKind) -> ItemKind { match foreign_item_kind { ForeignItemKind::Static(a, b, c) => { - ItemKind::Static(Static { ty: a, mutability: b, expr: c }.into()) + ItemKind::Static(StaticItem { ty: a, mutability: b, expr: c }.into()) } ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind), ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind), @@ -3113,7 +3113,7 @@ impl TryFrom for ForeignItemKind { fn try_from(item_kind: ItemKind) -> Result { Ok(match item_kind { - ItemKind::Static(box Static { ty: a, mutability: b, expr: c }) => { + ItemKind::Static(box StaticItem { ty: a, mutability: b, expr: c }) => { ForeignItemKind::Static(a, b, c) } ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind), diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 4d569e0fb94c9..2424073ae53ad 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -10,7 +10,7 @@ use crate::ptr::P; use crate::token::{self, Token}; use crate::tokenstream::*; -use crate::{ast::*, Static}; +use crate::{ast::*, StaticItem}; use rustc_data_structures::flat_map_in_place::FlatMapInPlace; use rustc_data_structures::sync::Lrc; @@ -1030,7 +1030,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(box Static { ty, mutability: _, expr }) => { + ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => { vis.visit_ty(ty); visit_opt(expr, |expr| vis.visit_expr(expr)); } diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 970bda58f350c..3b08467fde2bf 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -13,7 +13,7 @@ //! instance, a walker looking for item names in a module will miss all of //! those that are created by the expansion of a macro. -use crate::{ast::*, Static}; +use crate::{ast::*, StaticItem}; use rustc_span::symbol::Ident; use rustc_span::Span; @@ -305,7 +305,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { match &item.kind { ItemKind::ExternCrate(_) => {} ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false), - ItemKind::Static(box Static { ty, mutability: _, expr }) + ItemKind::Static(box StaticItem { ty, mutability: _, expr }) | ItemKind::Const(box ConstItem { 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 803dbbffbd863..f89e254a2f54d 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -229,7 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs) } - ItemKind::Static(box ast::Static { ty: t, mutability: m, expr: e }) => { + ItemKind::Static(box ast::StaticItem { ty: t, mutability: m, expr: e }) => { let (ty, body_id) = self.lower_const_item(t, span, e.as_deref()); hir::ItemKind::Static(ty, *m, body_id) } diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 431a569c1e0d8..236ebd04ae88d 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -10,7 +10,7 @@ use itertools::{Either, Itertools}; use rustc_ast::ptr::P; use rustc_ast::visit::{self, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor}; use rustc_ast::*; -use rustc_ast::{walk_list, Static}; +use rustc_ast::{walk_list, StaticItem}; use rustc_ast_pretty::pprust::{self, State}; use rustc_data_structures::fx::FxIndexMap; use rustc_macros::Subdiagnostic; @@ -990,7 +990,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { replace_span: self.ending_semi_or_hi(item.span), }); } - ItemKind::Static(box Static { expr: None, .. }) => { + ItemKind::Static(box StaticItem { expr: None, .. }) => { self.session.emit_err(errors::StaticWithoutBody { span: item.span, replace_span: self.ending_semi_or_hi(item.span), diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index e2d133eb233a2..c465f8c948a80 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -2,7 +2,7 @@ use crate::pp::Breaks::Inconsistent; use crate::pprust::state::delimited::IterDelimited; use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT}; -use ast::Static; +use ast::StaticItem; use rustc_ast as ast; use rustc_ast::GenericBound; use rustc_ast::ModKind; @@ -157,7 +157,7 @@ impl<'a> State<'a> { self.print_use_tree(tree); self.word(";"); } - ast::ItemKind::Static(box Static { ty, mutability: mutbl, expr: body }) => { + ast::ItemKind::Static(box StaticItem { ty, mutability: mutbl, expr: body }) => { let def = 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 34361a6d868e5..866cc5adbf3b1 100644 --- a/compiler/rustc_builtin_macros/src/global_allocator.rs +++ b/compiler/rustc_builtin_macros/src/global_allocator.rs @@ -25,12 +25,12 @@ pub fn expand( // FIXME - if we get deref patterns, use them to reduce duplication here let (item, is_stmt, ty_span) = if let Annotatable::Item(item) = &item - && let ItemKind::Static(box ast::Static { ty, ..}) = &item.kind + && let ItemKind::Static(box ast::StaticItem { ty, ..}) = &item.kind { (item, false, ecx.with_def_site_ctxt(ty.span)) } else if let Annotatable::Stmt(stmt) = &item && let StmtKind::Item(item) = &stmt.kind - && let ItemKind::Static(box ast::Static { ty, ..}) = &item.kind + && let ItemKind::Static(box ast::StaticItem { ty, ..}) = &item.kind { (item, true, ecx.with_def_site_ctxt(ty.span)) } else { diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index 59c588b39554e..264f30fb10a12 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -627,7 +627,7 @@ impl<'a> ExtCtxt<'a> { span, name, AttrVec::new(), - ast::ItemKind::Static(ast::Static { ty, mutability, expr: Some(expr) }.into()), + ast::ItemKind::Static(ast::StaticItem { ty, mutability, expr: Some(expr) }.into()), ) } diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index c856e973e5c82..ad328006051f1 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -806,7 +806,7 @@ trait UnusedDelimLint { use ast::ItemKind::*; if let Const(box ast::ConstItem { expr: Some(expr), .. }) - | Static(box ast::Static { expr: Some(expr), .. }) = &item.kind + | Static(box ast::StaticItem { 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 66fa9fb38d424..6422b8ac1ba45 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -3,7 +3,7 @@ use crate::errors; use super::diagnostics::{dummy_arg, ConsumeClosingDelim}; use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign}; use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, TrailingToken}; -use ast::Static; +use ast::StaticItem; use rustc_ast::ast::*; use rustc_ast::ptr::P; use rustc_ast::token::{self, Delimiter, TokenKind}; @@ -228,7 +228,7 @@ impl<'a> Parser<'a> { self.bump(); // `static` let m = self.parse_mutability(); let (ident, ty, expr) = self.parse_item_global(Some(m))?; - (ident, ItemKind::Static(Box::new(Static { ty, mutability: m, expr }))) + (ident, ItemKind::Static(Box::new(StaticItem { ty, mutability: m, expr }))) } else if let Const::Yes(const_span) = self.parse_constness(Case::Sensitive) { // CONST ITEM if self.token.is_keyword(kw::Impl) { @@ -863,7 +863,7 @@ impl<'a> Parser<'a> { let kind = match AssocItemKind::try_from(kind) { Ok(kind) => kind, Err(kind) => match kind { - ItemKind::Static(box Static { ty, mutability: _, expr }) => { + ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => { self.sess.emit_err(errors::AssociatedStaticItemNotAllowed { span }); AssocItemKind::Const(Box::new(ConstItem { defaultness: Defaultness::Final, diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 10d282a21a7ce..49c41470a1514 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -688,7 +688,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { } // These items live in the value namespace. - ItemKind::Static(box ast::Static { mutability, .. }) => { + ItemKind::Static(box ast::StaticItem { mutability, .. }) => { let res = Res::Def(DefKind::Static(mutability), def_id); self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion)); } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index d38bfc62abd9b..19f46d45af678 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2346,7 +2346,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { }); } - ItemKind::Static(box ast::Static { ref ty, ref expr, .. }) + ItemKind::Static(box ast::StaticItem { ref ty, ref expr, .. }) | ItemKind::Const(box ast::ConstItem { ref ty, ref expr, .. }) => { self.with_static_rib(|this| { this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| { 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 aa47f3aed54d4..11b908e7e53d1 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::msrvs::{self, Msrv}; use clippy_utils::source::snippet; -use rustc_ast::ast::{Item, ItemKind, Ty, TyKind, Static, ConstItem}; +use rustc_ast::ast::{Item, ItemKind, Ty, TyKind, StaticItem, ConstItem}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -106,7 +106,7 @@ impl EarlyLintPass for RedundantStaticLifetimes { // #2438) } - if let ItemKind::Static(box Static { ty: ref var_type,.. }) = item.kind { + if let ItemKind::Static(box StaticItem { ty: ref var_type,.. }) = item.kind { Self::visit_type(var_type, 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 0d2a7440c4659..c5b58b0c060c0 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -286,7 +286,7 @@ 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(box ast::Static{ ty: lt, mutability: lm, expr: le}), Static(box ast::Static { ty: rt, mutability: rm, expr: re})) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), + (Static(box ast::StaticItem { ty: lt, mutability: lm, expr: le}), Static(box ast::StaticItem { ty: rt, mutability: rm, expr: re})) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re), (Const(box ast::ConstItem { defaultness: ld, ty: lt, expr: le}), Const(box ast::ConstItem { defaultness: rd, ty: rt, expr: re} )) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re), ( Fn(box ast::Fn { diff --git a/tests/ui/stats/hir-stats.stderr b/tests/ui/stats/hir-stats.stderr index ee62d8f2d25ce..d723ff538a886 100644 --- a/tests/ui/stats/hir-stats.stderr +++ b/tests/ui/stats/hir-stats.stderr @@ -15,45 +15,45 @@ ast-stats-1 Arm 96 ( 1.5%) 2 48 ast-stats-1 ForeignItem 96 ( 1.5%) 1 96 ast-stats-1 - Fn 96 ( 1.5%) 1 ast-stats-1 FnDecl 120 ( 1.8%) 5 24 -ast-stats-1 FieldDef 160 ( 2.4%) 2 80 -ast-stats-1 Stmt 160 ( 2.4%) 5 32 +ast-stats-1 FieldDef 160 ( 2.5%) 2 80 +ast-stats-1 Stmt 160 ( 2.5%) 5 32 ast-stats-1 - Local 32 ( 0.5%) 1 ast-stats-1 - MacCall 32 ( 0.5%) 1 ast-stats-1 - Expr 96 ( 1.5%) 3 -ast-stats-1 Param 160 ( 2.4%) 4 40 -ast-stats-1 Block 192 ( 2.9%) 6 32 +ast-stats-1 Param 160 ( 2.5%) 4 40 +ast-stats-1 Block 192 ( 3.0%) 6 32 ast-stats-1 Variant 208 ( 3.2%) 2 104 -ast-stats-1 GenericBound 224 ( 3.4%) 4 56 -ast-stats-1 - Trait 224 ( 3.4%) 4 -ast-stats-1 AssocItem 416 ( 6.3%) 4 104 -ast-stats-1 - Type 208 ( 3.2%) 2 -ast-stats-1 - Fn 208 ( 3.2%) 2 -ast-stats-1 GenericParam 480 ( 7.3%) 5 96 -ast-stats-1 Pat 504 ( 7.7%) 7 72 +ast-stats-1 GenericBound 224 ( 3.5%) 4 56 +ast-stats-1 - Trait 224 ( 3.5%) 4 +ast-stats-1 AssocItem 352 ( 5.4%) 4 88 +ast-stats-1 - Type 176 ( 2.7%) 2 +ast-stats-1 - Fn 176 ( 2.7%) 2 +ast-stats-1 GenericParam 480 ( 7.4%) 5 96 +ast-stats-1 Pat 504 ( 7.8%) 7 72 ast-stats-1 - Struct 72 ( 1.1%) 1 ast-stats-1 - Wild 72 ( 1.1%) 1 ast-stats-1 - Ident 360 ( 5.5%) 5 -ast-stats-1 Expr 576 ( 8.8%) 8 72 +ast-stats-1 Expr 576 ( 8.9%) 8 72 ast-stats-1 - Path 72 ( 1.1%) 1 ast-stats-1 - Match 72 ( 1.1%) 1 ast-stats-1 - Struct 72 ( 1.1%) 1 ast-stats-1 - Lit 144 ( 2.2%) 2 ast-stats-1 - Block 216 ( 3.3%) 3 -ast-stats-1 PathSegment 720 (11.0%) 30 24 -ast-stats-1 Ty 896 (13.7%) 14 64 +ast-stats-1 PathSegment 720 (11.1%) 30 24 +ast-stats-1 Ty 896 (13.8%) 14 64 ast-stats-1 - Ptr 64 ( 1.0%) 1 ast-stats-1 - Ref 64 ( 1.0%) 1 ast-stats-1 - ImplicitSelf 128 ( 2.0%) 2 -ast-stats-1 - Path 640 ( 9.8%) 10 -ast-stats-1 Item 1_224 (18.7%) 9 136 +ast-stats-1 - Path 640 ( 9.9%) 10 +ast-stats-1 Item 1_224 (18.9%) 9 136 ast-stats-1 - Trait 136 ( 2.1%) 1 ast-stats-1 - Enum 136 ( 2.1%) 1 ast-stats-1 - ForeignMod 136 ( 2.1%) 1 ast-stats-1 - Impl 136 ( 2.1%) 1 ast-stats-1 - Fn 272 ( 4.2%) 2 -ast-stats-1 - Use 408 ( 6.2%) 3 +ast-stats-1 - Use 408 ( 6.3%) 3 ast-stats-1 ---------------------------------------------------------------- -ast-stats-1 Total 6_552 +ast-stats-1 Total 6_488 ast-stats-1 ast-stats-2 POST EXPANSION AST STATS ast-stats-2 Name Accumulated Size Count Item Size @@ -65,32 +65,32 @@ ast-stats-2 ExprField 48 ( 0.7%) 1 48 ast-stats-2 WherePredicate 56 ( 0.8%) 1 56 ast-stats-2 - BoundPredicate 56 ( 0.8%) 1 ast-stats-2 Local 72 ( 1.0%) 1 72 -ast-stats-2 Arm 96 ( 1.3%) 2 48 -ast-stats-2 ForeignItem 96 ( 1.3%) 1 96 -ast-stats-2 - Fn 96 ( 1.3%) 1 +ast-stats-2 Arm 96 ( 1.4%) 2 48 +ast-stats-2 ForeignItem 96 ( 1.4%) 1 96 +ast-stats-2 - Fn 96 ( 1.4%) 1 ast-stats-2 InlineAsm 120 ( 1.7%) 1 120 ast-stats-2 FnDecl 120 ( 1.7%) 5 24 ast-stats-2 Attribute 128 ( 1.8%) 4 32 -ast-stats-2 - DocComment 32 ( 0.4%) 1 -ast-stats-2 - Normal 96 ( 1.3%) 3 -ast-stats-2 FieldDef 160 ( 2.2%) 2 80 -ast-stats-2 Stmt 160 ( 2.2%) 5 32 -ast-stats-2 - Local 32 ( 0.4%) 1 -ast-stats-2 - Semi 32 ( 0.4%) 1 -ast-stats-2 - Expr 96 ( 1.3%) 3 -ast-stats-2 Param 160 ( 2.2%) 4 40 +ast-stats-2 - DocComment 32 ( 0.5%) 1 +ast-stats-2 - Normal 96 ( 1.4%) 3 +ast-stats-2 FieldDef 160 ( 2.3%) 2 80 +ast-stats-2 Stmt 160 ( 2.3%) 5 32 +ast-stats-2 - Local 32 ( 0.5%) 1 +ast-stats-2 - Semi 32 ( 0.5%) 1 +ast-stats-2 - Expr 96 ( 1.4%) 3 +ast-stats-2 Param 160 ( 2.3%) 4 40 ast-stats-2 Block 192 ( 2.7%) 6 32 ast-stats-2 Variant 208 ( 2.9%) 2 104 -ast-stats-2 GenericBound 224 ( 3.1%) 4 56 -ast-stats-2 - Trait 224 ( 3.1%) 4 -ast-stats-2 AssocItem 416 ( 5.8%) 4 104 -ast-stats-2 - Type 208 ( 2.9%) 2 -ast-stats-2 - Fn 208 ( 2.9%) 2 -ast-stats-2 GenericParam 480 ( 6.7%) 5 96 -ast-stats-2 Pat 504 ( 7.0%) 7 72 +ast-stats-2 GenericBound 224 ( 3.2%) 4 56 +ast-stats-2 - Trait 224 ( 3.2%) 4 +ast-stats-2 AssocItem 352 ( 5.0%) 4 88 +ast-stats-2 - Type 176 ( 2.5%) 2 +ast-stats-2 - Fn 176 ( 2.5%) 2 +ast-stats-2 GenericParam 480 ( 6.8%) 5 96 +ast-stats-2 Pat 504 ( 7.1%) 7 72 ast-stats-2 - Struct 72 ( 1.0%) 1 ast-stats-2 - Wild 72 ( 1.0%) 1 -ast-stats-2 - Ident 360 ( 5.0%) 5 +ast-stats-2 - Ident 360 ( 5.1%) 5 ast-stats-2 Expr 648 ( 9.1%) 9 72 ast-stats-2 - Path 72 ( 1.0%) 1 ast-stats-2 - Match 72 ( 1.0%) 1 @@ -98,22 +98,22 @@ ast-stats-2 - Struct 72 ( 1.0%) 1 ast-stats-2 - InlineAsm 72 ( 1.0%) 1 ast-stats-2 - Lit 144 ( 2.0%) 2 ast-stats-2 - Block 216 ( 3.0%) 3 -ast-stats-2 PathSegment 792 (11.1%) 33 24 -ast-stats-2 Ty 896 (12.5%) 14 64 +ast-stats-2 PathSegment 792 (11.2%) 33 24 +ast-stats-2 Ty 896 (12.6%) 14 64 ast-stats-2 - Ptr 64 ( 0.9%) 1 ast-stats-2 - Ref 64 ( 0.9%) 1 ast-stats-2 - ImplicitSelf 128 ( 1.8%) 2 -ast-stats-2 - Path 640 ( 8.9%) 10 -ast-stats-2 Item 1_496 (20.9%) 11 136 +ast-stats-2 - Path 640 ( 9.0%) 10 +ast-stats-2 Item 1_496 (21.1%) 11 136 ast-stats-2 - Trait 136 ( 1.9%) 1 ast-stats-2 - Enum 136 ( 1.9%) 1 ast-stats-2 - ExternCrate 136 ( 1.9%) 1 ast-stats-2 - ForeignMod 136 ( 1.9%) 1 ast-stats-2 - Impl 136 ( 1.9%) 1 ast-stats-2 - Fn 272 ( 3.8%) 2 -ast-stats-2 - Use 544 ( 7.6%) 4 +ast-stats-2 - Use 544 ( 7.7%) 4 ast-stats-2 ---------------------------------------------------------------- -ast-stats-2 Total 7_152 +ast-stats-2 Total 7_088 ast-stats-2 hir-stats HIR STATS hir-stats Name Accumulated Size Count Item Size