Skip to content

Commit

Permalink
Auto merge of #100944 - nnethercote:shrink-thir-Expr, r=cjgillot
Browse files Browse the repository at this point in the history
Shrink `thir::Expr`

r? `@cjgillot`
  • Loading branch information
bors committed Aug 26, 2022
2 parents 983f4da + 5a41eb8 commit 8a13871
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 150 deletions.
111 changes: 53 additions & 58 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,33 @@ use rustc_hir::def_id::DefId;
use rustc_hir::RangeEnd;
use rustc_index::newtype_index;
use rustc_index::vec::IndexVec;
use rustc_middle::infer::canonical::Canonical;
use rustc_middle::middle::region;
use rustc_middle::mir::interpret::AllocId;
use rustc_middle::mir::{self, BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp};
use rustc_middle::ty::adjustment::PointerCast;
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::CanonicalUserTypeAnnotation;
use rustc_middle::ty::{self, AdtDef, Ty, UpvarSubsts, UserType};
use rustc_middle::ty::{self, AdtDef, Ty, UpvarSubsts};
use rustc_middle::ty::{CanonicalUserType, CanonicalUserTypeAnnotation};
use rustc_span::def_id::LocalDefId;
use rustc_span::{Span, Symbol, DUMMY_SP};
use rustc_target::abi::VariantIdx;
use rustc_target::asm::InlineAsmRegOrRegClass;

use rustc_span::def_id::LocalDefId;
use std::fmt;
use std::ops::Index;

pub mod visit;

newtype_index! {
/// An index to an [`Arm`] stored in [`Thir::arms`]
#[derive(HashStable)]
pub struct ArmId {
DEBUG_FORMAT = "a{}"
}
}

newtype_index! {
/// An index to an [`Expr`] stored in [`Thir::exprs`]
#[derive(HashStable)]
pub struct ExprId {
DEBUG_FORMAT = "e{}"
}
}

newtype_index! {
#[derive(HashStable)]
/// An index to a [`Stmt`] stored in [`Thir::stmts`]
pub struct StmtId {
DEBUG_FORMAT = "s{}"
}
}

macro_rules! thir_with_elements {
($($name:ident: $id:ty => $value:ty,)*) => {
($($name:ident: $id:ty => $value:ty => $format:literal,)*) => {
$(
newtype_index! {
#[derive(HashStable)]
pub struct $id {
DEBUG_FORMAT = $format
}
}
)*

/// A container for a THIR body.
///
/// This can be indexed directly by any THIR index (e.g. [`ExprId`]).
Expand Down Expand Up @@ -91,9 +74,10 @@ macro_rules! thir_with_elements {
}

thir_with_elements! {
arms: ArmId => Arm<'tcx>,
exprs: ExprId => Expr<'tcx>,
stmts: StmtId => Stmt<'tcx>,
arms: ArmId => Arm<'tcx> => "a{}",
blocks: BlockId => Block => "b{}",
exprs: ExprId => Expr<'tcx> => "e{}",
stmts: StmtId => Stmt<'tcx> => "s{}",
}

#[derive(Copy, Clone, Debug, HashStable)]
Expand Down Expand Up @@ -121,8 +105,10 @@ pub struct Block {
pub safety_mode: BlockSafety,
}

type UserTy<'tcx> = Option<Box<CanonicalUserType<'tcx>>>;

#[derive(Clone, Debug, HashStable)]
pub struct Adt<'tcx> {
pub struct AdtExpr<'tcx> {
/// The ADT we're constructing.
pub adt_def: AdtDef<'tcx>,
/// The variant of the ADT.
Expand All @@ -131,13 +117,30 @@ pub struct Adt<'tcx> {

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

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

#[derive(Clone, Debug, HashStable)]
pub struct ClosureExpr<'tcx> {
pub closure_id: LocalDefId,
pub substs: UpvarSubsts<'tcx>,
pub upvars: Box<[ExprId]>,
pub movability: Option<hir::Movability>,
pub fake_reads: Vec<(ExprId, FakeReadCause, hir::HirId)>,
}

#[derive(Clone, Debug, HashStable)]
pub struct InlineAsmExpr<'tcx> {
pub template: &'tcx [InlineAsmTemplatePiece],
pub operands: Box<[InlineAsmOperand<'tcx>]>,
pub options: InlineAsmOptions,
pub line_spans: &'tcx [Span],
}

#[derive(Copy, Clone, Debug, HashStable)]
pub enum BlockSafety {
Safe,
Expand Down Expand Up @@ -183,7 +186,7 @@ pub enum StmtKind<'tcx> {
initializer: Option<ExprId>,

/// `let pat: ty = <INIT> else { <ELSE> }
else_block: Option<Block>,
else_block: Option<BlockId>,

/// The lint level for this `let` statement.
lint_level: LintLevel,
Expand Down Expand Up @@ -307,7 +310,7 @@ pub enum ExprKind<'tcx> {
},
/// A block.
Block {
body: Block,
block: BlockId,
},
/// An assignment: `lhs = rhs`.
Assign {
Expand Down Expand Up @@ -387,27 +390,21 @@ pub enum ExprKind<'tcx> {
fields: Box<[ExprId]>,
},
/// An ADT constructor, e.g. `Foo {x: 1, y: 2}`.
Adt(Box<Adt<'tcx>>),
Adt(Box<AdtExpr<'tcx>>),
/// A type ascription on a place.
PlaceTypeAscription {
source: ExprId,
/// 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`.
ValueTypeAscription {
source: ExprId,
/// Type that the user gave to this expression
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
user_ty: UserTy<'tcx>,
},
/// A closure definition.
Closure {
closure_id: LocalDefId,
substs: UpvarSubsts<'tcx>,
upvars: Box<[ExprId]>,
movability: Option<hir::Movability>,
fake_reads: Vec<(ExprId, FakeReadCause, hir::HirId)>,
},
Closure(Box<ClosureExpr<'tcx>>),
/// A literal.
Literal {
lit: &'tcx hir::Lit,
Expand All @@ -416,17 +413,17 @@ pub enum ExprKind<'tcx> {
/// For literals that don't correspond to anything in the HIR
NonHirLiteral {
lit: ty::ScalarInt,
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
user_ty: UserTy<'tcx>,
},
/// A literal of a ZST type.
ZstLiteral {
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
user_ty: UserTy<'tcx>,
},
/// Associated constants and named constants
NamedConst {
def_id: DefId,
substs: SubstsRef<'tcx>,
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
user_ty: UserTy<'tcx>,
},
ConstParam {
param: ty::ParamConst,
Expand All @@ -443,12 +440,7 @@ pub enum ExprKind<'tcx> {
def_id: DefId,
},
/// Inline assembly, i.e. `asm!()`.
InlineAsm {
template: &'tcx [InlineAsmTemplatePiece],
operands: Box<[InlineAsmOperand<'tcx>]>,
options: InlineAsmOptions,
line_spans: &'tcx [Span],
},
InlineAsm(Box<InlineAsmExpr<'tcx>>),
/// An expression taking a reference to a thread local.
ThreadLocalRef(DefId),
/// A `yield` expression.
Expand Down Expand Up @@ -815,7 +807,10 @@ mod size_asserts {
use super::*;
// These are in alphabetical order, which is easy to maintain.
static_assert_size!(Block, 56);
static_assert_size!(Expr<'_>, 104);
static_assert_size!(Expr<'_>, 64);
static_assert_size!(ExprKind<'_>, 40);
static_assert_size!(Pat<'_>, 24);
static_assert_size!(Stmt<'_>, 120);
static_assert_size!(PatKind<'_>, 112);
static_assert_size!(Stmt<'_>, 72);
static_assert_size!(StmtKind<'_>, 64);
}
19 changes: 13 additions & 6 deletions compiler/rustc_middle/src/thir/visit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir,
AdtExpr, Arm, Block, ClosureExpr, Expr, ExprKind, Guard, InlineAsmExpr, InlineAsmOperand, Pat,
PatKind, Stmt, StmtKind, Thir,
};

pub trait Visitor<'a, 'tcx: 'a>: Sized {
Expand Down Expand Up @@ -75,7 +76,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
visitor.visit_arm(&visitor.thir()[arm]);
}
}
Block { ref body } => visitor.visit_block(body),
Block { block } => visitor.visit_block(&visitor.thir()[block]),
Assign { lhs, rhs } | AssignOp { lhs, rhs, op: _ } => {
visitor.visit_expr(&visitor.thir()[lhs]);
visitor.visit_expr(&visitor.thir()[rhs]);
Expand Down Expand Up @@ -108,7 +109,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
visitor.visit_expr(&visitor.thir()[field]);
}
}
Adt(box crate::thir::Adt {
Adt(box AdtExpr {
ref fields,
ref base,
adt_def: _,
Expand All @@ -126,14 +127,20 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
PlaceTypeAscription { source, user_ty: _ } | ValueTypeAscription { source, user_ty: _ } => {
visitor.visit_expr(&visitor.thir()[source])
}
Closure { closure_id: _, substs: _, upvars: _, movability: _, fake_reads: _ } => {}
Closure(box ClosureExpr {
closure_id: _,
substs: _,
upvars: _,
movability: _,
fake_reads: _,
}) => {}
Literal { lit: _, neg: _ } => {}
NonHirLiteral { lit: _, user_ty: _ } => {}
ZstLiteral { user_ty: _ } => {}
NamedConst { def_id: _, substs: _, user_ty: _ } => {}
ConstParam { param: _, def_id: _ } => {}
StaticRef { alloc_id: _, ty: _, def_id: _ } => {}
InlineAsm { ref operands, template: _, options: _, line_spans: _ } => {
InlineAsm(box InlineAsmExpr { ref operands, template: _, options: _, line_spans: _ }) => {
for op in &**operands {
use InlineAsmOperand::*;
match op {
Expand Down Expand Up @@ -174,7 +181,7 @@ pub fn walk_stmt<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, stmt: &Stm
}
visitor.visit_pat(pattern);
if let Some(block) = else_block {
visitor.visit_block(block)
visitor.visit_block(&visitor.thir()[*block])
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_build/src/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut self,
destination: Place<'tcx>,
block: BasicBlock,
ast_block: &Block,
ast_block: BlockId,
source_info: SourceInfo,
) -> BlockAnd<()> {
let Block {
Expand All @@ -22,7 +22,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr,
targeted_by_break,
safety_mode,
} = *ast_block;
} = self.thir[ast_block];
let expr = expr.map(|expr| &self.thir[expr]);
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block,
init,
initializer_span,
else_block,
*else_block,
visibility_scope,
last_remainder_scope,
remainder_span,
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_mir_build/src/build/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,35 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

Constant { span, user_ty: None, literal }
}
ExprKind::NonHirLiteral { lit, user_ty } => {
let user_ty = user_ty.map(|user_ty| {
ExprKind::NonHirLiteral { lit, ref user_ty } => {
let user_ty = user_ty.as_ref().map(|box user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span,
user_ty,
user_ty: *user_ty,
inferred_ty: ty,
})
});
let literal = ConstantKind::Val(ConstValue::Scalar(Scalar::Int(lit)), ty);

Constant { span, user_ty: user_ty, literal }
}
ExprKind::ZstLiteral { user_ty } => {
let user_ty = user_ty.map(|user_ty| {
ExprKind::ZstLiteral { ref user_ty } => {
let user_ty = user_ty.as_ref().map(|box user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span,
user_ty,
user_ty: *user_ty,
inferred_ty: ty,
})
});
let literal = ConstantKind::Val(ConstValue::ZeroSized, ty);

Constant { span, user_ty: user_ty, literal }
}
ExprKind::NamedConst { def_id, substs, user_ty } => {
let user_ty = user_ty.map(|user_ty| {
ExprKind::NamedConst { def_id, substs, ref user_ty } => {
let user_ty = user_ty.as_ref().map(|box user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span,
user_ty,
user_ty: *user_ty,
inferred_ty: ty,
})
});
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
block.and(place_builder)
}

ExprKind::PlaceTypeAscription { source, user_ty } => {
ExprKind::PlaceTypeAscription { source, ref user_ty } => {
let place_builder = unpack!(
block = this.expr_as_place(
block,
Expand All @@ -522,11 +522,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fake_borrow_temps,
)
);
if let Some(user_ty) = user_ty {
if let Some(box user_ty) = user_ty {
let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
user_ty,
user_ty: *user_ty,
inferred_ty: expr.ty,
});

Expand All @@ -547,15 +547,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
block.and(place_builder)
}
ExprKind::ValueTypeAscription { source, user_ty } => {
ExprKind::ValueTypeAscription { source, ref user_ty } => {
let source = &this.thir[source];
let temp =
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 =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
user_ty,
user_ty: *user_ty,
inferred_ty: expr.ty,
});
this.cfg.push(
Expand Down
Loading

0 comments on commit 8a13871

Please sign in to comment.