Skip to content

Commit 7af71c1

Browse files
committed
Auto merge of #158720 - nnethercote:Expr64, r=folkertdev
Shrink `ast::Expr64` From 72 bytes to 64 bytes, by boxing `ForLoop`, which is the biggest variant. There are now 11 `ExprKind` variants that are 31 bytes, so future improvements here will be very difficult.
2 parents da80ed0 + 36487c7 commit 7af71c1

11 files changed

Lines changed: 57 additions & 54 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,15 @@ impl From<Box<Expr>> for Expr {
16551655
}
16561656
}
16571657

1658+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
1659+
pub struct ForLoop {
1660+
pub pat: Box<Pat>,
1661+
pub iter: Box<Expr>,
1662+
pub body: Box<Block>,
1663+
pub label: Option<Label>,
1664+
pub kind: ForLoopKind,
1665+
}
1666+
16581667
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
16591668
pub struct Closure {
16601669
pub binder: ClosureBinder,
@@ -1780,13 +1789,7 @@ pub enum ExprKind {
17801789
/// `'label: for await? pat in iter { block }`
17811790
///
17821791
/// This is desugared to a combination of `loop` and `match` expressions.
1783-
ForLoop {
1784-
pat: Box<Pat>,
1785-
iter: Box<Expr>,
1786-
body: Box<Block>,
1787-
label: Option<Label>,
1788-
kind: ForLoopKind,
1789-
},
1792+
ForLoop(Box<ForLoop>),
17901793
/// Conditionless loop (can be exited with `break`, `continue`, or `return`).
17911794
///
17921795
/// `'label: loop { block }`
@@ -4385,8 +4388,8 @@ mod size_asserts {
43854388
static_assert_size!(AttrKind, 16);
43864389
static_assert_size!(Attribute, 32);
43874390
static_assert_size!(Block, 24);
4388-
static_assert_size!(Expr, 72);
4389-
static_assert_size!(ExprKind, 40);
4391+
static_assert_size!(Expr, 64);
4392+
static_assert_size!(ExprKind, 32);
43904393
static_assert_size!(Fn, 192);
43914394
static_assert_size!(FnDecl, 24);
43924395
static_assert_size!(FnHeader, 76);

compiler/rustc_ast/src/util/classify.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
101101
pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
102102
loop {
103103
match &expr.kind {
104-
Block(_, label) | ForLoop { label, .. } | Loop(_, label, _) | While(_, _, label) => {
104+
Block(_, label)
105+
| ForLoop(ast::ForLoop { label, .. })
106+
| Loop(_, label, _)
107+
| While(_, _, label) => {
105108
return label.is_some();
106109
}
107110

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ macro_rules! common_visitor_and_walkers {
10001000
visit_visitable!($($mut)? vis, head_expression, if_block, optional_else),
10011001
ExprKind::While(subexpression, block, opt_label) =>
10021002
visit_visitable!($($mut)? vis, subexpression, block, opt_label),
1003-
ExprKind::ForLoop { pat, iter, body, label, kind } =>
1003+
ExprKind::ForLoop(ForLoop { pat, iter, body, label, kind }) =>
10041004
visit_visitable!($($mut)? vis, pat, iter, body, label, kind),
10051005
ExprKind::Loop(block, opt_label, span) =>
10061006
visit_visitable!($($mut)? vis, block, opt_label, span),

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
191191
//
192192
// This also needs special handling because the HirId of the returned `hir::Expr` will not
193193
// correspond to the `e.id`, so `lower_expr_for` handles attribute lowering itself.
194-
ExprKind::ForLoop { pat, iter, body, label, kind } => {
194+
ExprKind::ForLoop(ForLoop { pat, iter, body, label, kind }) => {
195195
return self.lower_expr_for(e, pat, iter, body, *label, *kind);
196196
}
197197
ExprKind::Closure(closure) => return self.lower_expr_closure_expr(e, closure),

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl<'a> State<'a> {
526526
self.space();
527527
self.print_block_with_attrs(blk, attrs, cb, ib);
528528
}
529-
ast::ExprKind::ForLoop { pat, iter, body, label, kind } => {
529+
ast::ExprKind::ForLoop(ast::ForLoop { pat, iter, body, label, kind }) => {
530530
if let Some(label) = label {
531531
self.print_ident(label.ident);
532532
self.word_space(":");

compiler/rustc_lint/src/unused.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::util::{classify, parser};
2-
use rustc_ast::{self as ast, ExprKind, FnRetTy, HasAttrs as _, StmtKind};
2+
use rustc_ast::{self as ast, ExprKind, FnRetTy, ForLoop, HasAttrs as _, StmtKind};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_errors::MultiSpan;
55
use rustc_hir::{self as hir};
@@ -381,7 +381,7 @@ trait UnusedDelimLint {
381381
(cond, UnusedDelimsCtx::WhileCond, true, Some(left), Some(right), true)
382382
}
383383

384-
ForLoop { ref iter, ref body, .. } => {
384+
ForLoop(ast::ForLoop { ref iter, ref body, .. }) => {
385385
(iter, UnusedDelimsCtx::ForIterExpr, true, None, Some(body.span.lo()), true)
386386
}
387387

@@ -728,7 +728,7 @@ impl EarlyLintPass for UnusedParens {
728728
}
729729

730730
match e.kind {
731-
ExprKind::Let(ref pat, _, _, _) | ExprKind::ForLoop { ref pat, .. } => {
731+
ExprKind::Let(ref pat, _, _, _) | ExprKind::ForLoop(ForLoop { ref pat, .. }) => {
732732
self.check_unused_parens_pat(cx, pat, false, false, (true, true));
733733
}
734734
// We ignore parens in cases like `if (((let Some(0) = Some(1))))` because we already

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use rustc_ast::visit::{Visitor, walk_expr};
1515
use rustc_ast::{
1616
self as ast, AnonConst, Arm, AssignOp, AssignOpKind, AttrStyle, AttrVec, BinOp, BinOpKind,
1717
BlockCheckMode, CaptureBy, ClosureBinder, DUMMY_NODE_ID, Expr, ExprField, ExprKind, FnDecl,
18-
FnRetTy, Guard, Label, MacCall, MetaItemLit, Movability, Param, RangeLimits, StmtKind, Ty,
19-
TyKind, UnOp, UnsafeBinderCastKind, YieldKind,
18+
FnRetTy, ForLoop, Guard, Label, MacCall, MetaItemLit, Movability, Param, RangeLimits, StmtKind,
19+
Ty, TyKind, UnOp, UnsafeBinderCastKind, YieldKind,
2020
};
2121
use rustc_ast_pretty::pprust;
2222
use rustc_data_structures::stack::ensure_sufficient_stack;
@@ -1955,7 +1955,7 @@ impl<'a> Parser<'a> {
19551955
if label.is_some()
19561956
&& match &expr.kind {
19571957
ExprKind::While(_, _, None)
1958-
| ExprKind::ForLoop { label: None, .. }
1958+
| ExprKind::ForLoop(ForLoop { label: None, .. })
19591959
| ExprKind::Loop(_, None, _) => true,
19601960
ExprKind::Block(block, None) => {
19611961
matches!(block.rules, BlockCheckMode::Default)
@@ -3057,7 +3057,13 @@ impl<'a> Parser<'a> {
30573057
let block = self.mk_block(thin_vec![], BlockCheckMode::Default, self.prev_token.span);
30583058
return Ok(self.mk_expr(
30593059
lo.to(self.prev_token.span),
3060-
ExprKind::ForLoop { pat, iter: err_expr, body: block, label: opt_label, kind },
3060+
ExprKind::ForLoop(Box::new(ForLoop {
3061+
pat,
3062+
iter: err_expr,
3063+
body: block,
3064+
label: opt_label,
3065+
kind,
3066+
})),
30613067
));
30623068
}
30633069

@@ -3067,7 +3073,13 @@ impl<'a> Parser<'a> {
30673073
opt_label.is_none().then_some(lo),
30683074
)?;
30693075

3070-
let kind = ExprKind::ForLoop { pat, iter: expr, body: loop_block, label: opt_label, kind };
3076+
let kind = ExprKind::ForLoop(Box::new(ForLoop {
3077+
pat,
3078+
iter: expr,
3079+
body: loop_block,
3080+
label: opt_label,
3081+
kind,
3082+
}));
30713083

30723084
self.recover_loop_else("for", lo)?;
30733085

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5290,7 +5290,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
52905290
});
52915291
}
52925292

5293-
ExprKind::ForLoop { ref pat, ref iter, ref body, label, kind: _ } => {
5293+
ExprKind::ForLoop(ForLoop { ref pat, ref iter, ref body, label, kind: _ }) => {
52945294
self.visit_expr(iter);
52955295
self.with_rib(ValueNS, RibKind::Normal, |this| {
52965296
this.resolve_pattern_top(pat, PatternSource::For);

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,13 @@ fn eq_expr(l: &Expr, r: &Expr) -> bool {
183183
(While(lc, lt, ll), While(rc, rt, rl)) => {
184184
eq_label(ll.as_ref(), rl.as_ref()) && eq_expr(lc, rc) && eq_block(lt, rt)
185185
},
186-
(
187-
ForLoop {
188-
pat: lp,
189-
iter: li,
190-
body: lt,
191-
label: ll,
192-
kind: lk,
193-
},
194-
ForLoop {
195-
pat: rp,
196-
iter: ri,
197-
body: rt,
198-
label: rl,
199-
kind: rk,
200-
},
201-
) => eq_label(ll.as_ref(), rl.as_ref()) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt) && lk == rk,
186+
(ForLoop(lf), ForLoop(rf)) => {
187+
eq_label(lf.label.as_ref(), rf.label.as_ref())
188+
&& eq_pat(&lf.pat, &rf.pat)
189+
&& eq_expr(&lf.iter, &rf.iter)
190+
&& eq_block(&lf.body, &rf.body)
191+
&& lf.kind == rf.kind
192+
}
202193
(Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lt, rt),
203194
(Block(lb, ll), Block(rb, rl)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lb, rb),
204195
(TryBlock(lb, lt), TryBlock(rb, rt)) => eq_block(lb, rb) && both(lt.as_deref(), rt.as_deref(), eq_ty),

src/tools/rustfmt/src/expr.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -779,14 +779,8 @@ fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow<
779779
expr.span,
780780
))
781781
}
782-
ast::ExprKind::ForLoop {
783-
ref pat,
784-
ref iter,
785-
ref body,
786-
label,
787-
kind,
788-
} => Some(ControlFlow::new_for(
789-
pat, iter, body, label, expr.span, kind,
782+
ast::ExprKind::ForLoop(ref f) => Some(ControlFlow::new_for(
783+
&f.pat, &f.iter, &f.body, f.label, expr.span, f.kind,
790784
)),
791785
ast::ExprKind::Loop(ref block, label, _) => {
792786
Some(ControlFlow::new_loop(block, label, expr.span))

0 commit comments

Comments
 (0)