From 5e802e5e97c1c5b995bb68edc2d40557b8af83fc Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Sun, 13 Jun 2021 15:39:57 +0200 Subject: [PATCH] Box `ExprKind::Adt` --- compiler/rustc_middle/src/thir.rs | 29 ++++++++++--------- .../rustc_mir_build/src/build/expr/into.rs | 9 +++++- .../rustc_mir_build/src/check_unsafety.rs | 4 +-- compiler/rustc_mir_build/src/thir/cx/expr.rs | 16 +++++----- compiler/rustc_mir_build/src/thir/visit.rs | 11 +++++-- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index a5069113702c2..436cd305a7fc2 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -97,6 +97,20 @@ pub struct Block { pub safety_mode: BlockSafety, } +#[derive(Debug, HashStable)] +pub struct Adt<'tcx> { + pub adt_def: &'tcx AdtDef, + pub variant_index: VariantIdx, + pub substs: SubstsRef<'tcx>, + + /// Optional user-given substs: for something like `let x = + /// Bar:: { ... }`. + pub user_ty: Option>>, + + pub fields: Box<[FieldExpr]>, + pub base: Option>, +} + #[derive(Copy, Clone, Debug, HashStable)] pub enum BlockSafety { Safe, @@ -145,7 +159,7 @@ pub enum StmtKind<'tcx> { // `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<'_>, 144); +rustc_data_structures::static_assert_size!(Expr<'_>, 104); /// The Thir trait implementor lowers their expressions (`&'tcx H::Expr`) /// into instances of this `Expr` enum. This lowering can be done @@ -304,18 +318,7 @@ pub enum ExprKind<'tcx> { Tuple { fields: Box<[ExprId]>, }, - Adt { - adt_def: &'tcx AdtDef, - variant_index: VariantIdx, - substs: SubstsRef<'tcx>, - - /// Optional user-given substs: for something like `let x = - /// Bar:: { ... }`. - user_ty: Option>>, - - fields: Box<[FieldExpr]>, - base: Option>, - }, + Adt(Box>), PlaceTypeAscription { source: ExprId, /// Type that the user gave to this expression diff --git a/compiler/rustc_mir_build/src/build/expr/into.rs b/compiler/rustc_mir_build/src/build/expr/into.rs index f2b00f0f6edaa..d7b3a85c15d8f 100644 --- a/compiler/rustc_mir_build/src/build/expr/into.rs +++ b/compiler/rustc_mir_build/src/build/expr/into.rs @@ -264,7 +264,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { this.cfg.push_assign(block, source_info, destination, address_of); block.unit() } - ExprKind::Adt { adt_def, variant_index, substs, user_ty, ref fields, ref base } => { + ExprKind::Adt(box Adt { + adt_def, + variant_index, + substs, + user_ty, + ref fields, + ref base, + }) => { // See the notes for `ExprKind::Array` in `as_rvalue` and for // `ExprKind::Borrow` above. let is_union = adt_def.is_union(); diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 2d52577829c71..e4ed5dece8622 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -195,14 +195,14 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> { ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => { self.requires_unsafe(expr.span, UseOfInlineAssembly); } - ExprKind::Adt { + ExprKind::Adt(box Adt { adt_def, variant_index: _, substs: _, user_ty: _, fields: _, base: _, - } => match self.tcx.layout_scalar_valid_range(adt_def.did) { + }) => match self.tcx.layout_scalar_valid_range(adt_def.did) { (Bound::Unbounded, Bound::Unbounded) => {} _ => self.requires_unsafe(expr.span, InitializingTypeWith), }, diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index aa4acfab5c810..da8cd66acb175 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -228,14 +228,14 @@ impl<'tcx> Cx<'tcx> { expr: self.mirror_expr(e), }) .collect(); - ExprKind::Adt { + ExprKind::Adt(Box::new(Adt { adt_def, substs, variant_index: index, fields: field_refs, user_ty, base: None, - } + })) } else { ExprKind::Call { ty: self.typeck_results().node_type(fun.hir_id), @@ -362,7 +362,7 @@ impl<'tcx> Cx<'tcx> { let user_provided_types = self.typeck_results().user_provided_types(); let user_ty = user_provided_types.get(expr.hir_id).copied(); debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty); - ExprKind::Adt { + ExprKind::Adt(Box::new(Adt { adt_def: adt, variant_index: VariantIdx::new(0), substs, @@ -375,7 +375,7 @@ impl<'tcx> Cx<'tcx> { .copied() .collect(), }), - } + })) } AdtKind::Enum => { let res = self.typeck_results().qpath_res(qpath, expr.hir_id); @@ -388,14 +388,14 @@ impl<'tcx> Cx<'tcx> { self.typeck_results().user_provided_types(); let user_ty = user_provided_types.get(expr.hir_id).copied(); debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty); - ExprKind::Adt { + ExprKind::Adt(Box::new(Adt { adt_def: adt, variant_index: index, substs, user_ty, fields: self.field_refs(fields), base: None, - } + })) } _ => { span_bug!(expr.span, "unexpected res: {:?}", res); @@ -906,14 +906,14 @@ impl<'tcx> Cx<'tcx> { match ty.kind() { // A unit struct/variant which is used as a value. // We return a completely different ExprKind here to account for this special case. - ty::Adt(adt_def, substs) => ExprKind::Adt { + ty::Adt(adt_def, substs) => ExprKind::Adt(Box::new(Adt { adt_def, variant_index: adt_def.variant_index_with_ctor_id(def_id), substs, user_ty: user_provided_type, fields: box [], base: None, - }, + })), _ => bug!("unexpected ty: {:?}", ty), } } diff --git a/compiler/rustc_mir_build/src/thir/visit.rs b/compiler/rustc_mir_build/src/thir/visit.rs index 1a60b1de7fd98..6e0229f38a574 100644 --- a/compiler/rustc_mir_build/src/thir/visit.rs +++ b/compiler/rustc_mir_build/src/thir/visit.rs @@ -1,4 +1,4 @@ -use rustc_middle::thir::*; +use rustc_middle::thir::{self, *}; use rustc_middle::ty::Const; pub trait Visitor<'a, 'tcx: 'a>: Sized { @@ -94,7 +94,14 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp visitor.visit_expr(&visitor.thir()[field]); } } - Adt { ref fields, ref base, adt_def: _, variant_index: _, substs: _, user_ty: _ } => { + Adt(box thir::Adt { + ref fields, + ref base, + adt_def: _, + variant_index: _, + substs: _, + user_ty: _, + }) => { for field in &**fields { visitor.visit_expr(&visitor.thir()[field.expr]); }