Skip to content

Commit 8a13871

Browse files
committed
Auto merge of #100944 - nnethercote:shrink-thir-Expr, r=cjgillot
Shrink `thir::Expr` r? `@cjgillot`
2 parents 983f4da + 5a41eb8 commit 8a13871

File tree

15 files changed

+191
-150
lines changed

15 files changed

+191
-150
lines changed

compiler/rustc_middle/src/thir.rs

+53-58
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,33 @@ use rustc_hir::def_id::DefId;
1515
use rustc_hir::RangeEnd;
1616
use rustc_index::newtype_index;
1717
use rustc_index::vec::IndexVec;
18-
use rustc_middle::infer::canonical::Canonical;
1918
use rustc_middle::middle::region;
2019
use rustc_middle::mir::interpret::AllocId;
2120
use rustc_middle::mir::{self, BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp};
2221
use rustc_middle::ty::adjustment::PointerCast;
2322
use rustc_middle::ty::subst::SubstsRef;
24-
use rustc_middle::ty::CanonicalUserTypeAnnotation;
25-
use rustc_middle::ty::{self, AdtDef, Ty, UpvarSubsts, UserType};
23+
use rustc_middle::ty::{self, AdtDef, Ty, UpvarSubsts};
24+
use rustc_middle::ty::{CanonicalUserType, CanonicalUserTypeAnnotation};
25+
use rustc_span::def_id::LocalDefId;
2626
use rustc_span::{Span, Symbol, DUMMY_SP};
2727
use rustc_target::abi::VariantIdx;
2828
use rustc_target::asm::InlineAsmRegOrRegClass;
29-
30-
use rustc_span::def_id::LocalDefId;
3129
use std::fmt;
3230
use std::ops::Index;
3331

3432
pub mod visit;
3533

36-
newtype_index! {
37-
/// An index to an [`Arm`] stored in [`Thir::arms`]
38-
#[derive(HashStable)]
39-
pub struct ArmId {
40-
DEBUG_FORMAT = "a{}"
41-
}
42-
}
43-
44-
newtype_index! {
45-
/// An index to an [`Expr`] stored in [`Thir::exprs`]
46-
#[derive(HashStable)]
47-
pub struct ExprId {
48-
DEBUG_FORMAT = "e{}"
49-
}
50-
}
51-
52-
newtype_index! {
53-
#[derive(HashStable)]
54-
/// An index to a [`Stmt`] stored in [`Thir::stmts`]
55-
pub struct StmtId {
56-
DEBUG_FORMAT = "s{}"
57-
}
58-
}
59-
6034
macro_rules! thir_with_elements {
61-
($($name:ident: $id:ty => $value:ty,)*) => {
35+
($($name:ident: $id:ty => $value:ty => $format:literal,)*) => {
36+
$(
37+
newtype_index! {
38+
#[derive(HashStable)]
39+
pub struct $id {
40+
DEBUG_FORMAT = $format
41+
}
42+
}
43+
)*
44+
6245
/// A container for a THIR body.
6346
///
6447
/// This can be indexed directly by any THIR index (e.g. [`ExprId`]).
@@ -91,9 +74,10 @@ macro_rules! thir_with_elements {
9174
}
9275

9376
thir_with_elements! {
94-
arms: ArmId => Arm<'tcx>,
95-
exprs: ExprId => Expr<'tcx>,
96-
stmts: StmtId => Stmt<'tcx>,
77+
arms: ArmId => Arm<'tcx> => "a{}",
78+
blocks: BlockId => Block => "b{}",
79+
exprs: ExprId => Expr<'tcx> => "e{}",
80+
stmts: StmtId => Stmt<'tcx> => "s{}",
9781
}
9882

9983
#[derive(Copy, Clone, Debug, HashStable)]
@@ -121,8 +105,10 @@ pub struct Block {
121105
pub safety_mode: BlockSafety,
122106
}
123107

108+
type UserTy<'tcx> = Option<Box<CanonicalUserType<'tcx>>>;
109+
124110
#[derive(Clone, Debug, HashStable)]
125-
pub struct Adt<'tcx> {
111+
pub struct AdtExpr<'tcx> {
126112
/// The ADT we're constructing.
127113
pub adt_def: AdtDef<'tcx>,
128114
/// The variant of the ADT.
@@ -131,13 +117,30 @@ pub struct Adt<'tcx> {
131117

132118
/// Optional user-given substs: for something like `let x =
133119
/// Bar::<T> { ... }`.
134-
pub user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
120+
pub user_ty: UserTy<'tcx>,
135121

136122
pub fields: Box<[FieldExpr]>,
137123
/// The base, e.g. `Foo {x: 1, .. base}`.
138124
pub base: Option<FruInfo<'tcx>>,
139125
}
140126

127+
#[derive(Clone, Debug, HashStable)]
128+
pub struct ClosureExpr<'tcx> {
129+
pub closure_id: LocalDefId,
130+
pub substs: UpvarSubsts<'tcx>,
131+
pub upvars: Box<[ExprId]>,
132+
pub movability: Option<hir::Movability>,
133+
pub fake_reads: Vec<(ExprId, FakeReadCause, hir::HirId)>,
134+
}
135+
136+
#[derive(Clone, Debug, HashStable)]
137+
pub struct InlineAsmExpr<'tcx> {
138+
pub template: &'tcx [InlineAsmTemplatePiece],
139+
pub operands: Box<[InlineAsmOperand<'tcx>]>,
140+
pub options: InlineAsmOptions,
141+
pub line_spans: &'tcx [Span],
142+
}
143+
141144
#[derive(Copy, Clone, Debug, HashStable)]
142145
pub enum BlockSafety {
143146
Safe,
@@ -183,7 +186,7 @@ pub enum StmtKind<'tcx> {
183186
initializer: Option<ExprId>,
184187

185188
/// `let pat: ty = <INIT> else { <ELSE> }
186-
else_block: Option<Block>,
189+
else_block: Option<BlockId>,
187190

188191
/// The lint level for this `let` statement.
189192
lint_level: LintLevel,
@@ -307,7 +310,7 @@ pub enum ExprKind<'tcx> {
307310
},
308311
/// A block.
309312
Block {
310-
body: Block,
313+
block: BlockId,
311314
},
312315
/// An assignment: `lhs = rhs`.
313316
Assign {
@@ -387,27 +390,21 @@ pub enum ExprKind<'tcx> {
387390
fields: Box<[ExprId]>,
388391
},
389392
/// An ADT constructor, e.g. `Foo {x: 1, y: 2}`.
390-
Adt(Box<Adt<'tcx>>),
393+
Adt(Box<AdtExpr<'tcx>>),
391394
/// A type ascription on a place.
392395
PlaceTypeAscription {
393396
source: ExprId,
394397
/// Type that the user gave to this expression
395-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
398+
user_ty: UserTy<'tcx>,
396399
},
397400
/// A type ascription on a value, e.g. `42: i32`.
398401
ValueTypeAscription {
399402
source: ExprId,
400403
/// Type that the user gave to this expression
401-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
404+
user_ty: UserTy<'tcx>,
402405
},
403406
/// A closure definition.
404-
Closure {
405-
closure_id: LocalDefId,
406-
substs: UpvarSubsts<'tcx>,
407-
upvars: Box<[ExprId]>,
408-
movability: Option<hir::Movability>,
409-
fake_reads: Vec<(ExprId, FakeReadCause, hir::HirId)>,
410-
},
407+
Closure(Box<ClosureExpr<'tcx>>),
411408
/// A literal.
412409
Literal {
413410
lit: &'tcx hir::Lit,
@@ -416,17 +413,17 @@ pub enum ExprKind<'tcx> {
416413
/// For literals that don't correspond to anything in the HIR
417414
NonHirLiteral {
418415
lit: ty::ScalarInt,
419-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
416+
user_ty: UserTy<'tcx>,
420417
},
421418
/// A literal of a ZST type.
422419
ZstLiteral {
423-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
420+
user_ty: UserTy<'tcx>,
424421
},
425422
/// Associated constants and named constants
426423
NamedConst {
427424
def_id: DefId,
428425
substs: SubstsRef<'tcx>,
429-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
426+
user_ty: UserTy<'tcx>,
430427
},
431428
ConstParam {
432429
param: ty::ParamConst,
@@ -443,12 +440,7 @@ pub enum ExprKind<'tcx> {
443440
def_id: DefId,
444441
},
445442
/// Inline assembly, i.e. `asm!()`.
446-
InlineAsm {
447-
template: &'tcx [InlineAsmTemplatePiece],
448-
operands: Box<[InlineAsmOperand<'tcx>]>,
449-
options: InlineAsmOptions,
450-
line_spans: &'tcx [Span],
451-
},
443+
InlineAsm(Box<InlineAsmExpr<'tcx>>),
452444
/// An expression taking a reference to a thread local.
453445
ThreadLocalRef(DefId),
454446
/// A `yield` expression.
@@ -815,7 +807,10 @@ mod size_asserts {
815807
use super::*;
816808
// These are in alphabetical order, which is easy to maintain.
817809
static_assert_size!(Block, 56);
818-
static_assert_size!(Expr<'_>, 104);
810+
static_assert_size!(Expr<'_>, 64);
811+
static_assert_size!(ExprKind<'_>, 40);
819812
static_assert_size!(Pat<'_>, 24);
820-
static_assert_size!(Stmt<'_>, 120);
813+
static_assert_size!(PatKind<'_>, 112);
814+
static_assert_size!(Stmt<'_>, 72);
815+
static_assert_size!(StmtKind<'_>, 64);
821816
}

compiler/rustc_middle/src/thir/visit.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{
2-
Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir,
2+
AdtExpr, Arm, Block, ClosureExpr, Expr, ExprKind, Guard, InlineAsmExpr, InlineAsmOperand, Pat,
3+
PatKind, Stmt, StmtKind, Thir,
34
};
45

56
pub trait Visitor<'a, 'tcx: 'a>: Sized {
@@ -75,7 +76,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
7576
visitor.visit_arm(&visitor.thir()[arm]);
7677
}
7778
}
78-
Block { ref body } => visitor.visit_block(body),
79+
Block { block } => visitor.visit_block(&visitor.thir()[block]),
7980
Assign { lhs, rhs } | AssignOp { lhs, rhs, op: _ } => {
8081
visitor.visit_expr(&visitor.thir()[lhs]);
8182
visitor.visit_expr(&visitor.thir()[rhs]);
@@ -108,7 +109,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
108109
visitor.visit_expr(&visitor.thir()[field]);
109110
}
110111
}
111-
Adt(box crate::thir::Adt {
112+
Adt(box AdtExpr {
112113
ref fields,
113114
ref base,
114115
adt_def: _,
@@ -126,14 +127,20 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
126127
PlaceTypeAscription { source, user_ty: _ } | ValueTypeAscription { source, user_ty: _ } => {
127128
visitor.visit_expr(&visitor.thir()[source])
128129
}
129-
Closure { closure_id: _, substs: _, upvars: _, movability: _, fake_reads: _ } => {}
130+
Closure(box ClosureExpr {
131+
closure_id: _,
132+
substs: _,
133+
upvars: _,
134+
movability: _,
135+
fake_reads: _,
136+
}) => {}
130137
Literal { lit: _, neg: _ } => {}
131138
NonHirLiteral { lit: _, user_ty: _ } => {}
132139
ZstLiteral { user_ty: _ } => {}
133140
NamedConst { def_id: _, substs: _, user_ty: _ } => {}
134141
ConstParam { param: _, def_id: _ } => {}
135142
StaticRef { alloc_id: _, ty: _, def_id: _ } => {}
136-
InlineAsm { ref operands, template: _, options: _, line_spans: _ } => {
143+
InlineAsm(box InlineAsmExpr { ref operands, template: _, options: _, line_spans: _ }) => {
137144
for op in &**operands {
138145
use InlineAsmOperand::*;
139146
match op {
@@ -174,7 +181,7 @@ pub fn walk_stmt<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, stmt: &Stm
174181
}
175182
visitor.visit_pat(pattern);
176183
if let Some(block) = else_block {
177-
visitor.visit_block(block)
184+
visitor.visit_block(&visitor.thir()[*block])
178185
}
179186
}
180187
}

compiler/rustc_mir_build/src/build/block.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1111
&mut self,
1212
destination: Place<'tcx>,
1313
block: BasicBlock,
14-
ast_block: &Block,
14+
ast_block: BlockId,
1515
source_info: SourceInfo,
1616
) -> BlockAnd<()> {
1717
let Block {
@@ -22,7 +22,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2222
expr,
2323
targeted_by_break,
2424
safety_mode,
25-
} = *ast_block;
25+
} = self.thir[ast_block];
2626
let expr = expr.map(|expr| &self.thir[expr]);
2727
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
2828
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
@@ -146,7 +146,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
146146
block,
147147
init,
148148
initializer_span,
149-
else_block,
149+
*else_block,
150150
visibility_scope,
151151
last_remainder_scope,
152152
remainder_span,

compiler/rustc_mir_build/src/build/expr/as_constant.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,35 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4141

4242
Constant { span, user_ty: None, literal }
4343
}
44-
ExprKind::NonHirLiteral { lit, user_ty } => {
45-
let user_ty = user_ty.map(|user_ty| {
44+
ExprKind::NonHirLiteral { lit, ref user_ty } => {
45+
let user_ty = user_ty.as_ref().map(|box user_ty| {
4646
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
4747
span,
48-
user_ty,
48+
user_ty: *user_ty,
4949
inferred_ty: ty,
5050
})
5151
});
5252
let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty);
5353

5454
Constant { span, user_ty: user_ty, literal }
5555
}
56-
ExprKind::ZstLiteral { user_ty } => {
57-
let user_ty = user_ty.map(|user_ty| {
56+
ExprKind::ZstLiteral { ref user_ty } => {
57+
let user_ty = user_ty.as_ref().map(|box user_ty| {
5858
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
5959
span,
60-
user_ty,
60+
user_ty: *user_ty,
6161
inferred_ty: ty,
6262
})
6363
});
6464
let literal = ConstantKind::Val(ConstValue::ZeroSized, ty);
6565

6666
Constant { span, user_ty: user_ty, literal }
6767
}
68-
ExprKind::NamedConst { def_id, substs, user_ty } => {
69-
let user_ty = user_ty.map(|user_ty| {
68+
ExprKind::NamedConst { def_id, substs, ref user_ty } => {
69+
let user_ty = user_ty.as_ref().map(|box user_ty| {
7070
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
7171
span,
72-
user_ty,
72+
user_ty: *user_ty,
7373
inferred_ty: ty,
7474
})
7575
});

compiler/rustc_mir_build/src/build/expr/as_place.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
513513
block.and(place_builder)
514514
}
515515

516-
ExprKind::PlaceTypeAscription { source, user_ty } => {
516+
ExprKind::PlaceTypeAscription { source, ref user_ty } => {
517517
let place_builder = unpack!(
518518
block = this.expr_as_place(
519519
block,
@@ -522,11 +522,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
522522
fake_borrow_temps,
523523
)
524524
);
525-
if let Some(user_ty) = user_ty {
525+
if let Some(box user_ty) = user_ty {
526526
let annotation_index =
527527
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
528528
span: source_info.span,
529-
user_ty,
529+
user_ty: *user_ty,
530530
inferred_ty: expr.ty,
531531
});
532532

@@ -547,15 +547,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
547547
}
548548
block.and(place_builder)
549549
}
550-
ExprKind::ValueTypeAscription { source, user_ty } => {
550+
ExprKind::ValueTypeAscription { source, ref user_ty } => {
551551
let source = &this.thir[source];
552552
let temp =
553553
unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
554-
if let Some(user_ty) = user_ty {
554+
if let Some(box user_ty) = user_ty {
555555
let annotation_index =
556556
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
557557
span: source_info.span,
558-
user_ty,
558+
user_ty: *user_ty,
559559
inferred_ty: expr.ty,
560560
});
561561
this.cfg.push(

0 commit comments

Comments
 (0)