From b25d3ba78118033b3f25b6de7a32e210d113872c Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 16 Mar 2021 00:36:07 +0300 Subject: ast/hir: Rename field-related structures StructField -> FieldDef ("field definition") Field -> ExprField ("expression field", not "field expression") FieldPat -> PatField ("pattern field", not "field pattern") Also rename visiting and other methods working on them. --- compiler/rustc_parse/src/parser/expr.rs | 12 ++++++------ compiler/rustc_parse/src/parser/item.rs | 20 +++++++++----------- compiler/rustc_parse/src/parser/pat.rs | 8 ++++---- 3 files changed, 19 insertions(+), 21 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 608b0248274..083702d9469 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -10,7 +10,7 @@ use rustc_ast::tokenstream::Spacing; use rustc_ast::util::classify; use rustc_ast::util::literal::LitError; use rustc_ast::util::parser::{prec_let_scrutinee_needs_par, AssocOp, Fixity}; -use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, Field, Lit, UnOp, DUMMY_NODE_ID}; +use rustc_ast::{self as ast, AttrStyle, AttrVec, CaptureBy, ExprField, Lit, UnOp, DUMMY_NODE_ID}; use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty, TyKind}; use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits}; use rustc_ast_pretty::pprust; @@ -2316,7 +2316,7 @@ impl<'a> Parser<'a> { } let recovery_field = self.find_struct_error_after_field_looking_code(); - let parsed_field = match self.parse_field() { + let parsed_field = match self.parse_expr_field() { Ok(f) => Some(f), Err(mut e) => { if pth == kw::Async { @@ -2378,13 +2378,13 @@ impl<'a> Parser<'a> { } /// Use in case of error after field-looking code: `S { foo: () with a }`. - fn find_struct_error_after_field_looking_code(&self) -> Option { + fn find_struct_error_after_field_looking_code(&self) -> Option { match self.token.ident() { Some((ident, is_raw)) if (is_raw || !ident.is_reserved()) && self.look_ahead(1, |t| *t == token::Colon) => { - Some(ast::Field { + Some(ast::ExprField { ident, span: self.token.span, expr: self.mk_expr_err(self.token.span), @@ -2418,7 +2418,7 @@ impl<'a> Parser<'a> { } /// Parses `ident (COLON expr)?`. - fn parse_field(&mut self) -> PResult<'a, Field> { + fn parse_expr_field(&mut self) -> PResult<'a, ExprField> { let attrs = self.parse_outer_attributes()?; self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { let lo = this.token.span; @@ -2438,7 +2438,7 @@ impl<'a> Parser<'a> { }; Ok(( - ast::Field { + ast::ExprField { ident, span: lo.to(expr.span), expr, diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index a28595e6fae..025415036b6 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, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData}; +use rustc_ast::{EnumDef, 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; @@ -1231,14 +1231,12 @@ impl<'a> Parser<'a> { Ok((class_name, ItemKind::Union(vdata, generics))) } - fn parse_record_struct_body( - &mut self, - ) -> PResult<'a, (Vec, /* recovered */ bool)> { + fn parse_record_struct_body(&mut self) -> PResult<'a, (Vec, /* recovered */ bool)> { let mut fields = Vec::new(); let mut recovered = false; if self.eat(&token::OpenDelim(token::Brace)) { while self.token != token::CloseDelim(token::Brace) { - let field = self.parse_struct_decl_field().map_err(|e| { + let field = self.parse_field_def().map_err(|e| { self.consume_block(token::Brace, ConsumeClosingDelim::No); recovered = true; e @@ -1263,7 +1261,7 @@ impl<'a> Parser<'a> { Ok((fields, recovered)) } - fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec> { + fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec> { // This is the case where we find `struct Foo(T) where T: Copy;` // Unit like structs are handled in parse_item_struct function self.parse_paren_comma_seq(|p| { @@ -1274,7 +1272,7 @@ impl<'a> Parser<'a> { let ty = p.parse_ty()?; Ok(( - StructField { + FieldDef { span: lo.to(ty.span), vis, ident: None, @@ -1291,7 +1289,7 @@ impl<'a> Parser<'a> { } /// Parses an element of a struct declaration. - fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> { + fn parse_field_def(&mut self) -> PResult<'a, FieldDef> { let attrs = self.parse_outer_attributes()?; self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { let lo = this.token.span; @@ -1306,7 +1304,7 @@ impl<'a> Parser<'a> { lo: Span, vis: Visibility, attrs: Vec, - ) -> PResult<'a, StructField> { + ) -> PResult<'a, FieldDef> { let mut seen_comma: bool = false; let a_var = self.parse_name_and_ty(lo, vis, attrs)?; if self.token == token::Comma { @@ -1398,11 +1396,11 @@ impl<'a> Parser<'a> { lo: Span, vis: Visibility, attrs: Vec, - ) -> PResult<'a, StructField> { + ) -> PResult<'a, FieldDef> { let name = self.parse_ident_common(false)?; self.expect(&token::Colon)?; let ty = self.parse_ty()?; - Ok(StructField { + Ok(FieldDef { span: lo.to(self.prev_token.span), ident: Some(name), vis, diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 5a68afdfa59..51c01f5a775 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -3,7 +3,7 @@ use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole}; use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor}; use rustc_ast::ptr::P; use rustc_ast::token; -use rustc_ast::{self as ast, AttrVec, Attribute, FieldPat, MacCall, Pat, PatKind, RangeEnd}; +use rustc_ast::{self as ast, AttrVec, Attribute, MacCall, Pat, PatField, PatKind, RangeEnd}; use rustc_ast::{BindingMode, Expr, ExprKind, Mutability, Path, QSelf, RangeSyntax}; use rustc_ast_pretty::pprust; use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult}; @@ -928,7 +928,7 @@ impl<'a> Parser<'a> { } /// Parses the fields of a struct-like pattern. - fn parse_pat_fields(&mut self) -> PResult<'a, (Vec, bool)> { + fn parse_pat_fields(&mut self) -> PResult<'a, (Vec, bool)> { let mut fields = Vec::new(); let mut etc = false; let mut ate_comma = true; @@ -1072,7 +1072,7 @@ impl<'a> Parser<'a> { .emit(); } - fn parse_pat_field(&mut self, lo: Span, attrs: Vec) -> PResult<'a, FieldPat> { + fn parse_pat_field(&mut self, lo: Span, attrs: Vec) -> PResult<'a, PatField> { // Check if a colon exists one ahead. This means we're parsing a fieldname. let hi; let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) { @@ -1104,7 +1104,7 @@ impl<'a> Parser<'a> { (subpat, fieldname, true) }; - Ok(FieldPat { + Ok(PatField { ident: fieldname, pat: subpat, is_shorthand, -- cgit 1.4.1-3-g733a5 From d1522b39dded68a3c442bed0b0a231020c6e2291 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 16 Mar 2021 03:15:53 +0300 Subject: ast: Reduce size of `ExprKind` by boxing fields of `ExprKind::Struct` --- compiler/rustc_ast/src/ast.rs | 11 +++++++++-- compiler/rustc_ast/src/mut_visit.rs | 5 +++-- compiler/rustc_ast/src/visit.rs | 8 ++++---- compiler/rustc_ast_lowering/src/expr.rs | 17 +++++++++-------- compiler/rustc_ast_pretty/src/pprust/state.rs | 4 ++-- compiler/rustc_expand/src/build.rs | 5 ++++- compiler/rustc_parse/src/parser/expr.rs | 6 +++++- compiler/rustc_resolve/src/late.rs | 4 ++-- src/test/ui-fulldeps/pprust-expr-roundtrip.rs | 4 +++- .../clippy/clippy_lints/src/redundant_field_names.rs | 4 ++-- .../clippy_lints/src/suspicious_operation_groupings.rs | 2 +- src/tools/clippy/clippy_utils/src/ast_utils.rs | 6 ++++-- 12 files changed, 48 insertions(+), 28 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index e3de0cc0a7e..d48b70e902e 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -1074,7 +1074,7 @@ pub struct Expr { // `Expr` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(Expr, 120); +rustc_data_structures::static_assert_size!(Expr, 104); impl Expr { /// Returns `true` if this expression would be valid somewhere that expects a value; @@ -1244,6 +1244,13 @@ pub enum StructRest { None, } +#[derive(Clone, Encodable, Decodable, Debug)] +pub struct StructExpr { + pub path: Path, + pub fields: Vec, + pub rest: StructRest, +} + #[derive(Clone, Encodable, Decodable, Debug)] pub enum ExprKind { /// A `box x` expression. @@ -1369,7 +1376,7 @@ pub enum ExprKind { /// A struct literal expression. /// /// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. rest}`. - Struct(Path, Vec, StructRest), + Struct(P), /// An array literal constructed from one repeated element. /// diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 9bdd8935914..f426f2c7fec 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1286,10 +1286,11 @@ pub fn noop_visit_expr( visit_vec(inputs, |(_c, expr)| vis.visit_expr(expr)); } ExprKind::MacCall(mac) => vis.visit_mac_call(mac), - ExprKind::Struct(path, fields, expr) => { + ExprKind::Struct(se) => { + let StructExpr { path, fields, rest } = se.deref_mut(); vis.visit_path(path); fields.flat_map_in_place(|field| vis.flat_map_expr_field(field)); - match expr { + match rest { StructRest::Base(expr) => vis.visit_expr(expr), StructRest::Rest(_span) => {} StructRest::None => {} diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 722666af7ac..b1ad29e4ad8 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -721,10 +721,10 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) { visitor.visit_expr(element); visitor.visit_anon_const(count) } - ExprKind::Struct(ref path, ref fields, ref optional_base) => { - visitor.visit_path(path, expression.id); - walk_list!(visitor, visit_expr_field, fields); - match optional_base { + ExprKind::Struct(ref se) => { + visitor.visit_path(&se.path, expression.id); + walk_list!(visitor, visit_expr_field, &se.fields); + match &se.rest { StructRest::Base(expr) => visitor.visit_expr(expr), StructRest::Rest(_span) => {} StructRest::None => {} diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 16514f7cc13..0400a421f7f 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -224,8 +224,8 @@ impl<'hir> LoweringContext<'_, 'hir> { } ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(e.span, asm), ExprKind::LlvmInlineAsm(ref asm) => self.lower_expr_llvm_asm(asm), - ExprKind::Struct(ref path, ref fields, ref rest) => { - let rest = match rest { + ExprKind::Struct(ref se) => { + let rest = match &se.rest { StructRest::Base(e) => Some(self.lower_expr(e)), StructRest::Rest(sp) => { self.sess @@ -240,11 +240,12 @@ impl<'hir> LoweringContext<'_, 'hir> { self.arena.alloc(self.lower_qpath( e.id, &None, - path, + &se.path, ParamMode::Optional, ImplTraitContext::disallowed(), )), - self.arena.alloc_from_iter(fields.iter().map(|x| self.lower_expr_field(x))), + self.arena + .alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))), rest, ) } @@ -1110,8 +1111,8 @@ impl<'hir> LoweringContext<'_, 'hir> { } } // Structs. - ExprKind::Struct(path, fields, rest) => { - let field_pats = self.arena.alloc_from_iter(fields.iter().map(|f| { + ExprKind::Struct(se) => { + let field_pats = self.arena.alloc_from_iter(se.fields.iter().map(|f| { let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments); hir::PatField { hir_id: self.next_id(), @@ -1124,11 +1125,11 @@ impl<'hir> LoweringContext<'_, 'hir> { let qpath = self.lower_qpath( lhs.id, &None, - path, + &se.path, ParamMode::Optional, ImplTraitContext::disallowed(), ); - let fields_omitted = match rest { + let fields_omitted = match &se.rest { StructRest::Base(e) => { self.sess .struct_span_err( diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 9b893de7da3..cb6f567c551 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1873,8 +1873,8 @@ impl<'a> State<'a> { ast::ExprKind::Repeat(ref element, ref count) => { self.print_expr_repeat(element, count, attrs); } - ast::ExprKind::Struct(ref path, ref fields, ref rest) => { - self.print_expr_struct(path, &fields[..], rest, attrs); + ast::ExprKind::Struct(ref se) => { + self.print_expr_struct(&se.path, &se.fields, &se.rest, attrs); } ast::ExprKind::Tup(ref exprs) => { self.print_expr_tup(&exprs[..], attrs); diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index efaac1f11e9..3664ff3ae8a 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -284,7 +284,10 @@ impl<'a> ExtCtxt<'a> { path: ast::Path, fields: Vec, ) -> P { - self.expr(span, ast::ExprKind::Struct(path, fields, ast::StructRest::None)) + self.expr( + span, + ast::ExprKind::Struct(P(ast::StructExpr { path, fields, rest: ast::StructRest::None })), + ) } pub fn expr_struct_ident( &self, diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 083702d9469..a3f2a8b3c57 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2373,7 +2373,11 @@ impl<'a> Parser<'a> { let span = pth.span.to(self.token.span); self.expect(&token::CloseDelim(token::Brace))?; - let expr = if recover_async { ExprKind::Err } else { ExprKind::Struct(pth, fields, base) }; + let expr = if recover_async { + ExprKind::Err + } else { + ExprKind::Struct(P(ast::StructExpr { path: pth, fields, rest: base })) + }; Ok(self.mk_expr(span, expr, attrs)) } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 6f24d7e9413..af241ef8afc 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -2251,8 +2251,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { visit::walk_expr(self, expr); } - ExprKind::Struct(ref path, ..) => { - self.smart_resolve_path(expr.id, None, path, PathSource::Struct); + ExprKind::Struct(ref se) => { + self.smart_resolve_path(expr.id, None, &se.path, PathSource::Struct); visit::walk_expr(self, expr); } diff --git a/src/test/ui-fulldeps/pprust-expr-roundtrip.rs b/src/test/ui-fulldeps/pprust-expr-roundtrip.rs index bff92d8607e..ac2d29c9caf 100644 --- a/src/test/ui-fulldeps/pprust-expr-roundtrip.rs +++ b/src/test/ui-fulldeps/pprust-expr-roundtrip.rs @@ -155,7 +155,9 @@ fn iter_exprs(depth: usize, f: &mut dyn FnMut(P)) { }, 17 => { let path = Path::from_ident(Ident::from_str("S")); - g(ExprKind::Struct(path, vec![], StructRest::Base(make_x()))); + g(ExprKind::Struct(P(StructExpr { + path, fields: vec![], rest: StructRest::Base(make_x()) + }))); }, 18 => { iter_exprs(depth - 1, &mut |e| g(ExprKind::Try(e))); diff --git a/src/tools/clippy/clippy_lints/src/redundant_field_names.rs b/src/tools/clippy/clippy_lints/src/redundant_field_names.rs index 38dcf7a192c..9688ef39331 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_field_names.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_field_names.rs @@ -58,8 +58,8 @@ impl EarlyLintPass for RedundantFieldNames { if in_external_macro(cx.sess, expr.span) { return; } - if let ExprKind::Struct(_, ref fields, _) = expr.kind { - for field in fields { + if let ExprKind::Struct(ref se) = expr.kind { + for field in &se.fields { if field.is_shorthand { continue; } diff --git a/src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs b/src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs index 44521885d20..9acc47deb06 100644 --- a/src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs +++ b/src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs @@ -564,7 +564,7 @@ fn ident_difference_expr_with_base_location( | (Try(_), Try(_)) | (Paren(_), Paren(_)) | (Repeat(_, _), Repeat(_, _)) - | (Struct(_, _, _), Struct(_, _, _)) + | (Struct(_), Struct(_)) | (MacCall(_), MacCall(_)) | (LlvmInlineAsm(_), LlvmInlineAsm(_)) | (InlineAsm(_), InlineAsm(_)) diff --git a/src/tools/clippy/clippy_utils/src/ast_utils.rs b/src/tools/clippy/clippy_utils/src/ast_utils.rs index 05afa534296..ea9a910d1b9 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils.rs @@ -168,8 +168,10 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool { (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re), (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp), (MacCall(l), MacCall(r)) => eq_mac_call(l, r), - (Struct(lp, lfs, lb), Struct(rp, rfs, rb)) => { - eq_path(lp, rp) && eq_struct_rest(lb, rb) && unordered_over(lfs, rfs, |l, r| eq_field(l, r)) + (Struct(lse), Struct(rse)) => { + eq_path(&lse.path, &rse.path) && + eq_struct_rest(&lse.rest, &rse.rest) && + unordered_over(&lse.fields, &rse.fields, |l, r| eq_field(l, r)) }, _ => false, } -- cgit 1.4.1-3-g733a5