Skip to content

Commit d1522b3

Browse files
committed
ast: Reduce size of ExprKind by boxing fields of ExprKind::Struct
1 parent b25d3ba commit d1522b3

File tree

12 files changed

+48
-28
lines changed

12 files changed

+48
-28
lines changed

compiler/rustc_ast/src/ast.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ pub struct Expr {
10741074

10751075
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
10761076
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
1077-
rustc_data_structures::static_assert_size!(Expr, 120);
1077+
rustc_data_structures::static_assert_size!(Expr, 104);
10781078

10791079
impl Expr {
10801080
/// Returns `true` if this expression would be valid somewhere that expects a value;
@@ -1244,6 +1244,13 @@ pub enum StructRest {
12441244
None,
12451245
}
12461246

1247+
#[derive(Clone, Encodable, Decodable, Debug)]
1248+
pub struct StructExpr {
1249+
pub path: Path,
1250+
pub fields: Vec<ExprField>,
1251+
pub rest: StructRest,
1252+
}
1253+
12471254
#[derive(Clone, Encodable, Decodable, Debug)]
12481255
pub enum ExprKind {
12491256
/// A `box x` expression.
@@ -1369,7 +1376,7 @@ pub enum ExprKind {
13691376
/// A struct literal expression.
13701377
///
13711378
/// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. rest}`.
1372-
Struct(Path, Vec<ExprField>, StructRest),
1379+
Struct(P<StructExpr>),
13731380

13741381
/// An array literal constructed from one repeated element.
13751382
///

compiler/rustc_ast/src/mut_visit.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1286,10 +1286,11 @@ pub fn noop_visit_expr<T: MutVisitor>(
12861286
visit_vec(inputs, |(_c, expr)| vis.visit_expr(expr));
12871287
}
12881288
ExprKind::MacCall(mac) => vis.visit_mac_call(mac),
1289-
ExprKind::Struct(path, fields, expr) => {
1289+
ExprKind::Struct(se) => {
1290+
let StructExpr { path, fields, rest } = se.deref_mut();
12901291
vis.visit_path(path);
12911292
fields.flat_map_in_place(|field| vis.flat_map_expr_field(field));
1292-
match expr {
1293+
match rest {
12931294
StructRest::Base(expr) => vis.visit_expr(expr),
12941295
StructRest::Rest(_span) => {}
12951296
StructRest::None => {}

compiler/rustc_ast/src/visit.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,10 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
721721
visitor.visit_expr(element);
722722
visitor.visit_anon_const(count)
723723
}
724-
ExprKind::Struct(ref path, ref fields, ref optional_base) => {
725-
visitor.visit_path(path, expression.id);
726-
walk_list!(visitor, visit_expr_field, fields);
727-
match optional_base {
724+
ExprKind::Struct(ref se) => {
725+
visitor.visit_path(&se.path, expression.id);
726+
walk_list!(visitor, visit_expr_field, &se.fields);
727+
match &se.rest {
728728
StructRest::Base(expr) => visitor.visit_expr(expr),
729729
StructRest::Rest(_span) => {}
730730
StructRest::None => {}

compiler/rustc_ast_lowering/src/expr.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
224224
}
225225
ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(e.span, asm),
226226
ExprKind::LlvmInlineAsm(ref asm) => self.lower_expr_llvm_asm(asm),
227-
ExprKind::Struct(ref path, ref fields, ref rest) => {
228-
let rest = match rest {
227+
ExprKind::Struct(ref se) => {
228+
let rest = match &se.rest {
229229
StructRest::Base(e) => Some(self.lower_expr(e)),
230230
StructRest::Rest(sp) => {
231231
self.sess
@@ -240,11 +240,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
240240
self.arena.alloc(self.lower_qpath(
241241
e.id,
242242
&None,
243-
path,
243+
&se.path,
244244
ParamMode::Optional,
245245
ImplTraitContext::disallowed(),
246246
)),
247-
self.arena.alloc_from_iter(fields.iter().map(|x| self.lower_expr_field(x))),
247+
self.arena
248+
.alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))),
248249
rest,
249250
)
250251
}
@@ -1110,8 +1111,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
11101111
}
11111112
}
11121113
// Structs.
1113-
ExprKind::Struct(path, fields, rest) => {
1114-
let field_pats = self.arena.alloc_from_iter(fields.iter().map(|f| {
1114+
ExprKind::Struct(se) => {
1115+
let field_pats = self.arena.alloc_from_iter(se.fields.iter().map(|f| {
11151116
let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
11161117
hir::PatField {
11171118
hir_id: self.next_id(),
@@ -1124,11 +1125,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
11241125
let qpath = self.lower_qpath(
11251126
lhs.id,
11261127
&None,
1127-
path,
1128+
&se.path,
11281129
ParamMode::Optional,
11291130
ImplTraitContext::disallowed(),
11301131
);
1131-
let fields_omitted = match rest {
1132+
let fields_omitted = match &se.rest {
11321133
StructRest::Base(e) => {
11331134
self.sess
11341135
.struct_span_err(

compiler/rustc_ast_pretty/src/pprust/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1873,8 +1873,8 @@ impl<'a> State<'a> {
18731873
ast::ExprKind::Repeat(ref element, ref count) => {
18741874
self.print_expr_repeat(element, count, attrs);
18751875
}
1876-
ast::ExprKind::Struct(ref path, ref fields, ref rest) => {
1877-
self.print_expr_struct(path, &fields[..], rest, attrs);
1876+
ast::ExprKind::Struct(ref se) => {
1877+
self.print_expr_struct(&se.path, &se.fields, &se.rest, attrs);
18781878
}
18791879
ast::ExprKind::Tup(ref exprs) => {
18801880
self.print_expr_tup(&exprs[..], attrs);

compiler/rustc_expand/src/build.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ impl<'a> ExtCtxt<'a> {
284284
path: ast::Path,
285285
fields: Vec<ast::ExprField>,
286286
) -> P<ast::Expr> {
287-
self.expr(span, ast::ExprKind::Struct(path, fields, ast::StructRest::None))
287+
self.expr(
288+
span,
289+
ast::ExprKind::Struct(P(ast::StructExpr { path, fields, rest: ast::StructRest::None })),
290+
)
288291
}
289292
pub fn expr_struct_ident(
290293
&self,

compiler/rustc_parse/src/parser/expr.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,11 @@ impl<'a> Parser<'a> {
23732373

23742374
let span = pth.span.to(self.token.span);
23752375
self.expect(&token::CloseDelim(token::Brace))?;
2376-
let expr = if recover_async { ExprKind::Err } else { ExprKind::Struct(pth, fields, base) };
2376+
let expr = if recover_async {
2377+
ExprKind::Err
2378+
} else {
2379+
ExprKind::Struct(P(ast::StructExpr { path: pth, fields, rest: base }))
2380+
};
23772381
Ok(self.mk_expr(span, expr, attrs))
23782382
}
23792383

compiler/rustc_resolve/src/late.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2251,8 +2251,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22512251
visit::walk_expr(self, expr);
22522252
}
22532253

2254-
ExprKind::Struct(ref path, ..) => {
2255-
self.smart_resolve_path(expr.id, None, path, PathSource::Struct);
2254+
ExprKind::Struct(ref se) => {
2255+
self.smart_resolve_path(expr.id, None, &se.path, PathSource::Struct);
22562256
visit::walk_expr(self, expr);
22572257
}
22582258

src/test/ui-fulldeps/pprust-expr-roundtrip.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ fn iter_exprs(depth: usize, f: &mut dyn FnMut(P<Expr>)) {
155155
},
156156
17 => {
157157
let path = Path::from_ident(Ident::from_str("S"));
158-
g(ExprKind::Struct(path, vec![], StructRest::Base(make_x())));
158+
g(ExprKind::Struct(P(StructExpr {
159+
path, fields: vec![], rest: StructRest::Base(make_x())
160+
})));
159161
},
160162
18 => {
161163
iter_exprs(depth - 1, &mut |e| g(ExprKind::Try(e)));

src/tools/clippy/clippy_lints/src/redundant_field_names.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ impl EarlyLintPass for RedundantFieldNames {
5858
if in_external_macro(cx.sess, expr.span) {
5959
return;
6060
}
61-
if let ExprKind::Struct(_, ref fields, _) = expr.kind {
62-
for field in fields {
61+
if let ExprKind::Struct(ref se) = expr.kind {
62+
for field in &se.fields {
6363
if field.is_shorthand {
6464
continue;
6565
}

src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ fn ident_difference_expr_with_base_location(
564564
| (Try(_), Try(_))
565565
| (Paren(_), Paren(_))
566566
| (Repeat(_, _), Repeat(_, _))
567-
| (Struct(_, _, _), Struct(_, _, _))
567+
| (Struct(_), Struct(_))
568568
| (MacCall(_), MacCall(_))
569569
| (LlvmInlineAsm(_), LlvmInlineAsm(_))
570570
| (InlineAsm(_), InlineAsm(_))

src/tools/clippy/clippy_utils/src/ast_utils.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
168168
(AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
169169
(Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
170170
(MacCall(l), MacCall(r)) => eq_mac_call(l, r),
171-
(Struct(lp, lfs, lb), Struct(rp, rfs, rb)) => {
172-
eq_path(lp, rp) && eq_struct_rest(lb, rb) && unordered_over(lfs, rfs, |l, r| eq_field(l, r))
171+
(Struct(lse), Struct(rse)) => {
172+
eq_path(&lse.path, &rse.path) &&
173+
eq_struct_rest(&lse.rest, &rse.rest) &&
174+
unordered_over(&lse.fields, &rse.fields, |l, r| eq_field(l, r))
173175
},
174176
_ => false,
175177
}

0 commit comments

Comments
 (0)