Skip to content

Commit e7c25c3

Browse files
committed
Box user_ty fields in thir::ExprKind.
This shrinks several large variants of `ExprKind`.
1 parent c9429b1 commit e7c25c3

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

compiler/rustc_middle/src/thir.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ 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

@@ -106,6 +104,8 @@ pub struct Block {
106104
pub safety_mode: BlockSafety,
107105
}
108106

107+
type UserTy<'tcx> = Option<Box<CanonicalUserType<'tcx>>>;
108+
109109
#[derive(Clone, Debug, HashStable)]
110110
pub struct Adt<'tcx> {
111111
/// The ADT we're constructing.
@@ -116,7 +116,7 @@ pub struct Adt<'tcx> {
116116

117117
/// Optional user-given substs: for something like `let x =
118118
/// Bar::<T> { ... }`.
119-
pub user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
119+
pub user_ty: UserTy<'tcx>,
120120

121121
pub fields: Box<[FieldExpr]>,
122122
/// The base, e.g. `Foo {x: 1, .. base}`.
@@ -377,13 +377,13 @@ pub enum ExprKind<'tcx> {
377377
PlaceTypeAscription {
378378
source: ExprId,
379379
/// Type that the user gave to this expression
380-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
380+
user_ty: UserTy<'tcx>,
381381
},
382382
/// A type ascription on a value, e.g. `42: i32`.
383383
ValueTypeAscription {
384384
source: ExprId,
385385
/// Type that the user gave to this expression
386-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
386+
user_ty: UserTy<'tcx>,
387387
},
388388
/// A closure definition.
389389
Closure {
@@ -401,17 +401,17 @@ pub enum ExprKind<'tcx> {
401401
/// For literals that don't correspond to anything in the HIR
402402
NonHirLiteral {
403403
lit: ty::ScalarInt,
404-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
404+
user_ty: UserTy<'tcx>,
405405
},
406406
/// A literal of a ZST type.
407407
ZstLiteral {
408-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
408+
user_ty: UserTy<'tcx>,
409409
},
410410
/// Associated constants and named constants
411411
NamedConst {
412412
def_id: DefId,
413413
substs: SubstsRef<'tcx>,
414-
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
414+
user_ty: UserTy<'tcx>,
415415
},
416416
ConstParam {
417417
param: ty::ParamConst,
@@ -800,7 +800,7 @@ mod size_asserts {
800800
use super::*;
801801
// These are in alphabetical order, which is easy to maintain.
802802
static_assert_size!(Block, 56);
803-
static_assert_size!(Expr<'_>, 104);
803+
static_assert_size!(Expr<'_>, 88);
804804
static_assert_size!(Pat<'_>, 24);
805805
static_assert_size!(Stmt<'_>, 120);
806806
}

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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 6 additions & 6 deletions
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(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
318318
adt_def,
319319
variant_index,
320320
substs,
321-
user_ty,
321+
ref user_ty,
322322
ref fields,
323323
ref base,
324324
}) => {
@@ -378,10 +378,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
378378
};
379379

380380
let inferred_ty = expr.ty;
381-
let user_ty = user_ty.map(|ty| {
381+
let user_ty = user_ty.as_ref().map(|box user_ty| {
382382
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
383383
span: source_info.span,
384-
user_ty: ty,
384+
user_ty: *user_ty,
385385
inferred_ty,
386386
})
387387
});

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<'tcx> Cx<'tcx> {
329329
if let UserType::TypeOf(ref mut did, _) = &mut u_ty.value {
330330
*did = adt_def.did();
331331
}
332-
u_ty
332+
Box::new(u_ty)
333333
});
334334
debug!("make_mirror_unadjusted: (call) user_ty={:?}", user_ty);
335335

@@ -464,7 +464,7 @@ impl<'tcx> Cx<'tcx> {
464464
ty::Adt(adt, substs) => match adt.adt_kind() {
465465
AdtKind::Struct | AdtKind::Union => {
466466
let user_provided_types = self.typeck_results().user_provided_types();
467-
let user_ty = user_provided_types.get(expr.hir_id).copied();
467+
let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
468468
debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty);
469469
ExprKind::Adt(Box::new(Adt {
470470
adt_def: *adt,
@@ -490,7 +490,8 @@ impl<'tcx> Cx<'tcx> {
490490
let index = adt.variant_index_with_id(variant_id);
491491
let user_provided_types =
492492
self.typeck_results().user_provided_types();
493-
let user_ty = user_provided_types.get(expr.hir_id).copied();
493+
let user_ty =
494+
user_provided_types.get(expr.hir_id).copied().map(Box::new);
494495
debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty);
495496
ExprKind::Adt(Box::new(Adt {
496497
adt_def: *adt,
@@ -712,14 +713,17 @@ impl<'tcx> Cx<'tcx> {
712713
});
713714
debug!("make_mirror_unadjusted: (cast) user_ty={:?}", user_ty);
714715

715-
ExprKind::ValueTypeAscription { source: cast_expr, user_ty: Some(*user_ty) }
716+
ExprKind::ValueTypeAscription {
717+
source: cast_expr,
718+
user_ty: Some(Box::new(*user_ty)),
719+
}
716720
} else {
717721
cast
718722
}
719723
}
720724
hir::ExprKind::Type(ref source, ref ty) => {
721725
let user_provided_types = self.typeck_results.user_provided_types();
722-
let user_ty = user_provided_types.get(ty.hir_id).copied();
726+
let user_ty = user_provided_types.get(ty.hir_id).copied().map(Box::new);
723727
debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty);
724728
let mirrored = self.mirror_expr(source);
725729
if source.is_syntactic_place_expr() {
@@ -748,7 +752,7 @@ impl<'tcx> Cx<'tcx> {
748752
&mut self,
749753
hir_id: hir::HirId,
750754
res: Res,
751-
) -> Option<ty::CanonicalUserType<'tcx>> {
755+
) -> Option<Box<ty::CanonicalUserType<'tcx>>> {
752756
debug!("user_substs_applied_to_res: res={:?}", res);
753757
let user_provided_type = match res {
754758
// A reference to something callable -- e.g., a fn, method, or
@@ -759,19 +763,19 @@ impl<'tcx> Cx<'tcx> {
759763
| Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
760764
| Res::Def(DefKind::Const, _)
761765
| Res::Def(DefKind::AssocConst, _) => {
762-
self.typeck_results().user_provided_types().get(hir_id).copied()
766+
self.typeck_results().user_provided_types().get(hir_id).copied().map(Box::new)
763767
}
764768

765769
// A unit struct/variant which is used as a value (e.g.,
766770
// `None`). This has the type of the enum/struct that defines
767771
// this variant -- but with the substitutions given by the
768772
// user.
769773
Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => {
770-
self.user_substs_applied_to_ty_of_hir_id(hir_id)
774+
self.user_substs_applied_to_ty_of_hir_id(hir_id).map(Box::new)
771775
}
772776

773777
// `Self` is used in expression as a tuple struct constructor or a unit struct constructor
774-
Res::SelfCtor(_) => self.user_substs_applied_to_ty_of_hir_id(hir_id),
778+
Res::SelfCtor(_) => self.user_substs_applied_to_ty_of_hir_id(hir_id).map(Box::new),
775779

776780
_ => bug!("user_substs_applied_to_res: unexpected res {:?} at {:?}", res, hir_id),
777781
};
@@ -846,13 +850,13 @@ impl<'tcx> Cx<'tcx> {
846850

847851
Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
848852
let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
849-
ExprKind::NamedConst { def_id, substs, user_ty: user_ty }
853+
ExprKind::NamedConst { def_id, substs, user_ty }
850854
}
851855

852856
Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => {
853857
let user_provided_types = self.typeck_results.user_provided_types();
854-
let user_provided_type = user_provided_types.get(expr.hir_id).copied();
855-
debug!("convert_path_expr: user_provided_type={:?}", user_provided_type);
858+
let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
859+
debug!("convert_path_expr: user_ty={:?}", user_ty);
856860
let ty = self.typeck_results().node_type(expr.hir_id);
857861
match ty.kind() {
858862
// A unit struct/variant which is used as a value.
@@ -861,7 +865,7 @@ impl<'tcx> Cx<'tcx> {
861865
adt_def: *adt_def,
862866
variant_index: adt_def.variant_index_with_ctor_id(def_id),
863867
substs,
864-
user_ty: user_provided_type,
868+
user_ty,
865869
fields: Box::new([]),
866870
base: None,
867871
})),

0 commit comments

Comments
 (0)