1
Fork 0

Box user_ty fields in thir::ExprKind.

This shrinks several large variants of `ExprKind`.
This commit is contained in:
Nicholas Nethercote 2022-08-24 10:37:41 +10:00
parent c9429b1cec
commit e7c25c3a97
5 changed files with 47 additions and 43 deletions

View file

@ -15,19 +15,17 @@ use rustc_hir::def_id::DefId;
use rustc_hir::RangeEnd; use rustc_hir::RangeEnd;
use rustc_index::newtype_index; use rustc_index::newtype_index;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_middle::infer::canonical::Canonical;
use rustc_middle::middle::region; use rustc_middle::middle::region;
use rustc_middle::mir::interpret::AllocId; use rustc_middle::mir::interpret::AllocId;
use rustc_middle::mir::{self, BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp}; use rustc_middle::mir::{self, BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp};
use rustc_middle::ty::adjustment::PointerCast; use rustc_middle::ty::adjustment::PointerCast;
use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::CanonicalUserTypeAnnotation; use rustc_middle::ty::{self, AdtDef, Ty, UpvarSubsts};
use rustc_middle::ty::{self, AdtDef, Ty, UpvarSubsts, UserType}; use rustc_middle::ty::{CanonicalUserType, CanonicalUserTypeAnnotation};
use rustc_span::def_id::LocalDefId;
use rustc_span::{Span, Symbol, DUMMY_SP}; use rustc_span::{Span, Symbol, DUMMY_SP};
use rustc_target::abi::VariantIdx; use rustc_target::abi::VariantIdx;
use rustc_target::asm::InlineAsmRegOrRegClass; use rustc_target::asm::InlineAsmRegOrRegClass;
use rustc_span::def_id::LocalDefId;
use std::fmt; use std::fmt;
use std::ops::Index; use std::ops::Index;
@ -106,6 +104,8 @@ pub struct Block {
pub safety_mode: BlockSafety, pub safety_mode: BlockSafety,
} }
type UserTy<'tcx> = Option<Box<CanonicalUserType<'tcx>>>;
#[derive(Clone, Debug, HashStable)] #[derive(Clone, Debug, HashStable)]
pub struct Adt<'tcx> { pub struct Adt<'tcx> {
/// The ADT we're constructing. /// The ADT we're constructing.
@ -116,7 +116,7 @@ pub struct Adt<'tcx> {
/// Optional user-given substs: for something like `let x = /// Optional user-given substs: for something like `let x =
/// Bar::<T> { ... }`. /// Bar::<T> { ... }`.
pub user_ty: Option<Canonical<'tcx, UserType<'tcx>>>, pub user_ty: UserTy<'tcx>,
pub fields: Box<[FieldExpr]>, pub fields: Box<[FieldExpr]>,
/// The base, e.g. `Foo {x: 1, .. base}`. /// The base, e.g. `Foo {x: 1, .. base}`.
@ -377,13 +377,13 @@ pub enum ExprKind<'tcx> {
PlaceTypeAscription { PlaceTypeAscription {
source: ExprId, source: ExprId,
/// Type that the user gave to this expression /// Type that the user gave to this expression
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>, user_ty: UserTy<'tcx>,
}, },
/// A type ascription on a value, e.g. `42: i32`. /// A type ascription on a value, e.g. `42: i32`.
ValueTypeAscription { ValueTypeAscription {
source: ExprId, source: ExprId,
/// Type that the user gave to this expression /// Type that the user gave to this expression
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>, user_ty: UserTy<'tcx>,
}, },
/// A closure definition. /// A closure definition.
Closure { Closure {
@ -401,17 +401,17 @@ pub enum ExprKind<'tcx> {
/// For literals that don't correspond to anything in the HIR /// For literals that don't correspond to anything in the HIR
NonHirLiteral { NonHirLiteral {
lit: ty::ScalarInt, lit: ty::ScalarInt,
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>, user_ty: UserTy<'tcx>,
}, },
/// A literal of a ZST type. /// A literal of a ZST type.
ZstLiteral { ZstLiteral {
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>, user_ty: UserTy<'tcx>,
}, },
/// Associated constants and named constants /// Associated constants and named constants
NamedConst { NamedConst {
def_id: DefId, def_id: DefId,
substs: SubstsRef<'tcx>, substs: SubstsRef<'tcx>,
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>, user_ty: UserTy<'tcx>,
}, },
ConstParam { ConstParam {
param: ty::ParamConst, param: ty::ParamConst,
@ -800,7 +800,7 @@ mod size_asserts {
use super::*; use super::*;
// These are in alphabetical order, which is easy to maintain. // These are in alphabetical order, which is easy to maintain.
static_assert_size!(Block, 56); static_assert_size!(Block, 56);
static_assert_size!(Expr<'_>, 104); static_assert_size!(Expr<'_>, 88);
static_assert_size!(Pat<'_>, 24); static_assert_size!(Pat<'_>, 24);
static_assert_size!(Stmt<'_>, 120); static_assert_size!(Stmt<'_>, 120);
} }

View file

@ -41,11 +41,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { span, user_ty: None, literal } Constant { span, user_ty: None, literal }
} }
ExprKind::NonHirLiteral { lit, user_ty } => { ExprKind::NonHirLiteral { lit, ref user_ty } => {
let user_ty = user_ty.map(|user_ty| { let user_ty = user_ty.as_ref().map(|box user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span, span,
user_ty, user_ty: *user_ty,
inferred_ty: ty, inferred_ty: ty,
}) })
}); });
@ -53,11 +53,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { span, user_ty: user_ty, literal } Constant { span, user_ty: user_ty, literal }
} }
ExprKind::ZstLiteral { user_ty } => { ExprKind::ZstLiteral { ref user_ty } => {
let user_ty = user_ty.map(|user_ty| { let user_ty = user_ty.as_ref().map(|box user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span, span,
user_ty, user_ty: *user_ty,
inferred_ty: ty, inferred_ty: ty,
}) })
}); });
@ -65,11 +65,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { span, user_ty: user_ty, literal } Constant { span, user_ty: user_ty, literal }
} }
ExprKind::NamedConst { def_id, substs, user_ty } => { ExprKind::NamedConst { def_id, substs, ref user_ty } => {
let user_ty = user_ty.map(|user_ty| { let user_ty = user_ty.as_ref().map(|box user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span, span,
user_ty, user_ty: *user_ty,
inferred_ty: ty, inferred_ty: ty,
}) })
}); });

View file

@ -513,7 +513,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(place_builder) block.and(place_builder)
} }
ExprKind::PlaceTypeAscription { source, user_ty } => { ExprKind::PlaceTypeAscription { source, ref user_ty } => {
let place_builder = unpack!( let place_builder = unpack!(
block = this.expr_as_place( block = this.expr_as_place(
block, block,
@ -522,11 +522,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fake_borrow_temps, fake_borrow_temps,
) )
); );
if let Some(user_ty) = user_ty { if let Some(box user_ty) = user_ty {
let annotation_index = let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span, span: source_info.span,
user_ty, user_ty: *user_ty,
inferred_ty: expr.ty, inferred_ty: expr.ty,
}); });
@ -547,15 +547,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
} }
block.and(place_builder) block.and(place_builder)
} }
ExprKind::ValueTypeAscription { source, user_ty } => { ExprKind::ValueTypeAscription { source, ref user_ty } => {
let source = &this.thir[source]; let source = &this.thir[source];
let temp = let temp =
unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability)); unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
if let Some(user_ty) = user_ty { if let Some(box user_ty) = user_ty {
let annotation_index = let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span, span: source_info.span,
user_ty, user_ty: *user_ty,
inferred_ty: expr.ty, inferred_ty: expr.ty,
}); });
this.cfg.push( this.cfg.push(

View file

@ -318,7 +318,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
adt_def, adt_def,
variant_index, variant_index,
substs, substs,
user_ty, ref user_ty,
ref fields, ref fields,
ref base, ref base,
}) => { }) => {
@ -378,10 +378,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}; };
let inferred_ty = expr.ty; let inferred_ty = expr.ty;
let user_ty = user_ty.map(|ty| { let user_ty = user_ty.as_ref().map(|box user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation { this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span, span: source_info.span,
user_ty: ty, user_ty: *user_ty,
inferred_ty, inferred_ty,
}) })
}); });

View file

@ -329,7 +329,7 @@ impl<'tcx> Cx<'tcx> {
if let UserType::TypeOf(ref mut did, _) = &mut u_ty.value { if let UserType::TypeOf(ref mut did, _) = &mut u_ty.value {
*did = adt_def.did(); *did = adt_def.did();
} }
u_ty Box::new(u_ty)
}); });
debug!("make_mirror_unadjusted: (call) user_ty={:?}", user_ty); debug!("make_mirror_unadjusted: (call) user_ty={:?}", user_ty);
@ -464,7 +464,7 @@ impl<'tcx> Cx<'tcx> {
ty::Adt(adt, substs) => match adt.adt_kind() { ty::Adt(adt, substs) => match adt.adt_kind() {
AdtKind::Struct | AdtKind::Union => { AdtKind::Struct | AdtKind::Union => {
let user_provided_types = self.typeck_results().user_provided_types(); let user_provided_types = self.typeck_results().user_provided_types();
let user_ty = user_provided_types.get(expr.hir_id).copied(); let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty); debug!("make_mirror_unadjusted: (struct/union) user_ty={:?}", user_ty);
ExprKind::Adt(Box::new(Adt { ExprKind::Adt(Box::new(Adt {
adt_def: *adt, adt_def: *adt,
@ -490,7 +490,8 @@ impl<'tcx> Cx<'tcx> {
let index = adt.variant_index_with_id(variant_id); let index = adt.variant_index_with_id(variant_id);
let user_provided_types = let user_provided_types =
self.typeck_results().user_provided_types(); self.typeck_results().user_provided_types();
let user_ty = user_provided_types.get(expr.hir_id).copied(); let user_ty =
user_provided_types.get(expr.hir_id).copied().map(Box::new);
debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty); debug!("make_mirror_unadjusted: (variant) user_ty={:?}", user_ty);
ExprKind::Adt(Box::new(Adt { ExprKind::Adt(Box::new(Adt {
adt_def: *adt, adt_def: *adt,
@ -712,14 +713,17 @@ impl<'tcx> Cx<'tcx> {
}); });
debug!("make_mirror_unadjusted: (cast) user_ty={:?}", user_ty); debug!("make_mirror_unadjusted: (cast) user_ty={:?}", user_ty);
ExprKind::ValueTypeAscription { source: cast_expr, user_ty: Some(*user_ty) } ExprKind::ValueTypeAscription {
source: cast_expr,
user_ty: Some(Box::new(*user_ty)),
}
} else { } else {
cast cast
} }
} }
hir::ExprKind::Type(ref source, ref ty) => { hir::ExprKind::Type(ref source, ref ty) => {
let user_provided_types = self.typeck_results.user_provided_types(); let user_provided_types = self.typeck_results.user_provided_types();
let user_ty = user_provided_types.get(ty.hir_id).copied(); let user_ty = user_provided_types.get(ty.hir_id).copied().map(Box::new);
debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty); debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty);
let mirrored = self.mirror_expr(source); let mirrored = self.mirror_expr(source);
if source.is_syntactic_place_expr() { if source.is_syntactic_place_expr() {
@ -748,7 +752,7 @@ impl<'tcx> Cx<'tcx> {
&mut self, &mut self,
hir_id: hir::HirId, hir_id: hir::HirId,
res: Res, res: Res,
) -> Option<ty::CanonicalUserType<'tcx>> { ) -> Option<Box<ty::CanonicalUserType<'tcx>>> {
debug!("user_substs_applied_to_res: res={:?}", res); debug!("user_substs_applied_to_res: res={:?}", res);
let user_provided_type = match res { let user_provided_type = match res {
// A reference to something callable -- e.g., a fn, method, or // A reference to something callable -- e.g., a fn, method, or
@ -759,7 +763,7 @@ impl<'tcx> Cx<'tcx> {
| Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _)
| Res::Def(DefKind::Const, _) | Res::Def(DefKind::Const, _)
| Res::Def(DefKind::AssocConst, _) => { | Res::Def(DefKind::AssocConst, _) => {
self.typeck_results().user_provided_types().get(hir_id).copied() self.typeck_results().user_provided_types().get(hir_id).copied().map(Box::new)
} }
// A unit struct/variant which is used as a value (e.g., // A unit struct/variant which is used as a value (e.g.,
@ -767,11 +771,11 @@ impl<'tcx> Cx<'tcx> {
// this variant -- but with the substitutions given by the // this variant -- but with the substitutions given by the
// user. // user.
Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => { Res::Def(DefKind::Ctor(_, CtorKind::Const), _) => {
self.user_substs_applied_to_ty_of_hir_id(hir_id) self.user_substs_applied_to_ty_of_hir_id(hir_id).map(Box::new)
} }
// `Self` is used in expression as a tuple struct constructor or a unit struct constructor // `Self` is used in expression as a tuple struct constructor or a unit struct constructor
Res::SelfCtor(_) => self.user_substs_applied_to_ty_of_hir_id(hir_id), Res::SelfCtor(_) => self.user_substs_applied_to_ty_of_hir_id(hir_id).map(Box::new),
_ => bug!("user_substs_applied_to_res: unexpected res {:?} at {:?}", res, hir_id), _ => bug!("user_substs_applied_to_res: unexpected res {:?} at {:?}", res, hir_id),
}; };
@ -846,13 +850,13 @@ impl<'tcx> Cx<'tcx> {
Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => { Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
let user_ty = self.user_substs_applied_to_res(expr.hir_id, res); let user_ty = self.user_substs_applied_to_res(expr.hir_id, res);
ExprKind::NamedConst { def_id, substs, user_ty: user_ty } ExprKind::NamedConst { def_id, substs, user_ty }
} }
Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => { Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => {
let user_provided_types = self.typeck_results.user_provided_types(); let user_provided_types = self.typeck_results.user_provided_types();
let user_provided_type = user_provided_types.get(expr.hir_id).copied(); let user_ty = user_provided_types.get(expr.hir_id).copied().map(Box::new);
debug!("convert_path_expr: user_provided_type={:?}", user_provided_type); debug!("convert_path_expr: user_ty={:?}", user_ty);
let ty = self.typeck_results().node_type(expr.hir_id); let ty = self.typeck_results().node_type(expr.hir_id);
match ty.kind() { match ty.kind() {
// A unit struct/variant which is used as a value. // A unit struct/variant which is used as a value.
@ -861,7 +865,7 @@ impl<'tcx> Cx<'tcx> {
adt_def: *adt_def, adt_def: *adt_def,
variant_index: adt_def.variant_index_with_ctor_id(def_id), variant_index: adt_def.variant_index_with_ctor_id(def_id),
substs, substs,
user_ty: user_provided_type, user_ty,
fields: Box::new([]), fields: Box::new([]),
base: None, base: None,
})), })),