Skip to content

Commit

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

r? `@cjgillot`
  • Loading branch information
bors committed Sep 3, 2022
2 parents 0209485 + 43a0268 commit 0421444
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 181 deletions.
62 changes: 31 additions & 31 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pub enum StmtKind<'tcx> {
/// `let <PAT> = ...`
///
/// If a type annotation is included, it is added as an ascription pattern.
pattern: Pat<'tcx>,
pattern: Box<Pat<'tcx>>,

/// `let pat: ty = <INIT>`
initializer: Option<ExprId>,
Expand Down Expand Up @@ -301,7 +301,7 @@ pub enum ExprKind<'tcx> {
},
Let {
expr: ExprId,
pat: Pat<'tcx>,
pat: Box<Pat<'tcx>>,
},
/// A `match` expression.
Match {
Expand Down Expand Up @@ -467,7 +467,7 @@ pub struct FruInfo<'tcx> {
/// A `match` arm.
#[derive(Clone, Debug, HashStable)]
pub struct Arm<'tcx> {
pub pattern: Pat<'tcx>,
pub pattern: Box<Pat<'tcx>>,
pub guard: Option<Guard<'tcx>>,
pub body: ExprId,
pub lint_level: LintLevel,
Expand All @@ -479,7 +479,7 @@ pub struct Arm<'tcx> {
#[derive(Clone, Debug, HashStable)]
pub enum Guard<'tcx> {
If(ExprId),
IfLet(Pat<'tcx>, ExprId),
IfLet(Box<Pat<'tcx>>, ExprId),
}

#[derive(Copy, Clone, Debug, HashStable)]
Expand Down Expand Up @@ -534,19 +534,19 @@ pub enum BindingMode {
#[derive(Clone, Debug, HashStable)]
pub struct FieldPat<'tcx> {
pub field: Field,
pub pattern: Pat<'tcx>,
pub pattern: Box<Pat<'tcx>>,
}

#[derive(Clone, Debug, HashStable)]
pub struct Pat<'tcx> {
pub ty: Ty<'tcx>,
pub span: Span,
pub kind: Box<PatKind<'tcx>>,
pub kind: PatKind<'tcx>,
}

impl<'tcx> Pat<'tcx> {
pub fn wildcard_from_ty(ty: Ty<'tcx>) -> Self {
Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) }
Pat { ty, span: DUMMY_SP, kind: PatKind::Wild }
}
}

Expand Down Expand Up @@ -581,7 +581,7 @@ pub enum PatKind<'tcx> {

AscribeUserType {
ascription: Ascription<'tcx>,
subpattern: Pat<'tcx>,
subpattern: Box<Pat<'tcx>>,
},

/// `x`, `ref x`, `x @ P`, etc.
Expand All @@ -591,7 +591,7 @@ pub enum PatKind<'tcx> {
mode: BindingMode,
var: LocalVarId,
ty: Ty<'tcx>,
subpattern: Option<Pat<'tcx>>,
subpattern: Option<Box<Pat<'tcx>>>,
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
/// `HirId` of this pattern?
is_primary: bool,
Expand All @@ -614,7 +614,7 @@ pub enum PatKind<'tcx> {

/// `box P`, `&P`, `&mut P`, etc.
Deref {
subpattern: Pat<'tcx>,
subpattern: Box<Pat<'tcx>>,
},

/// One of the following:
Expand All @@ -628,32 +628,32 @@ pub enum PatKind<'tcx> {
value: mir::ConstantKind<'tcx>,
},

Range(PatRange<'tcx>),
Range(Box<PatRange<'tcx>>),

/// Matches against a slice, checking the length and extracting elements.
/// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty.
/// e.g., `&[ref xs @ ..]`.
Slice {
prefix: Vec<Pat<'tcx>>,
slice: Option<Pat<'tcx>>,
suffix: Vec<Pat<'tcx>>,
prefix: Box<[Box<Pat<'tcx>>]>,
slice: Option<Box<Pat<'tcx>>>,
suffix: Box<[Box<Pat<'tcx>>]>,
},

/// Fixed match against an array; irrefutable.
Array {
prefix: Vec<Pat<'tcx>>,
slice: Option<Pat<'tcx>>,
suffix: Vec<Pat<'tcx>>,
prefix: Box<[Box<Pat<'tcx>>]>,
slice: Option<Box<Pat<'tcx>>>,
suffix: Box<[Box<Pat<'tcx>>]>,
},

/// An or-pattern, e.g. `p | q`.
/// Invariant: `pats.len() >= 2`.
Or {
pats: Vec<Pat<'tcx>>,
pats: Box<[Box<Pat<'tcx>>]>,
},
}

#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
#[derive(Clone, Debug, PartialEq, HashStable)]
pub struct PatRange<'tcx> {
pub lo: mir::ConstantKind<'tcx>,
pub hi: mir::ConstantKind<'tcx>,
Expand All @@ -674,7 +674,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
};
let mut start_or_comma = || start_or_continue(", ");

match *self.kind {
match self.kind {
PatKind::Wild => write!(f, "_"),
PatKind::AscribeUserType { ref subpattern, .. } => write!(f, "{}: _", subpattern),
PatKind::Binding { mutability, name, mode, ref subpattern, .. } => {
Expand All @@ -695,7 +695,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
Ok(())
}
PatKind::Variant { ref subpatterns, .. } | PatKind::Leaf { ref subpatterns } => {
let variant = match *self.kind {
let variant = match self.kind {
PatKind::Variant { adt_def, variant_index, .. } => {
Some(adt_def.variant(variant_index))
}
Expand All @@ -714,7 +714,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {

let mut printed = 0;
for p in subpatterns {
if let PatKind::Wild = *p.pattern.kind {
if let PatKind::Wild = p.pattern.kind {
continue;
}
let name = variant.fields[p.field.index()].name;
Expand Down Expand Up @@ -767,32 +767,32 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
write!(f, "{}", subpattern)
}
PatKind::Constant { value } => write!(f, "{}", value),
PatKind::Range(PatRange { lo, hi, end }) => {
PatKind::Range(box PatRange { lo, hi, end }) => {
write!(f, "{}", lo)?;
write!(f, "{}", end)?;
write!(f, "{}", hi)
}
PatKind::Slice { ref prefix, ref slice, ref suffix }
| PatKind::Array { ref prefix, ref slice, ref suffix } => {
write!(f, "[")?;
for p in prefix {
for p in prefix.iter() {
write!(f, "{}{}", start_or_comma(), p)?;
}
if let Some(ref slice) = *slice {
write!(f, "{}", start_or_comma())?;
match *slice.kind {
match slice.kind {
PatKind::Wild => {}
_ => write!(f, "{}", slice)?,
}
write!(f, "..")?;
}
for p in suffix {
for p in suffix.iter() {
write!(f, "{}{}", start_or_comma(), p)?;
}
write!(f, "]")
}
PatKind::Or { ref pats } => {
for pat in pats {
for pat in pats.iter() {
write!(f, "{}{}", start_or_continue(" | "), pat)?;
}
Ok(())
Expand All @@ -809,8 +809,8 @@ mod size_asserts {
static_assert_size!(Block, 56);
static_assert_size!(Expr<'_>, 64);
static_assert_size!(ExprKind<'_>, 40);
static_assert_size!(Pat<'_>, 24);
static_assert_size!(PatKind<'_>, 112);
static_assert_size!(Stmt<'_>, 72);
static_assert_size!(StmtKind<'_>, 64);
static_assert_size!(Pat<'_>, 72);
static_assert_size!(PatKind<'_>, 56);
static_assert_size!(Stmt<'_>, 56);
static_assert_size!(StmtKind<'_>, 48);
}
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/thir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub fn walk_arm<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, arm: &Arm<'

pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'tcx>) {
use PatKind::*;
match pat.kind.as_ref() {
match &pat.kind {
AscribeUserType { subpattern, ascription: _ }
| Deref { subpattern }
| Binding {
Expand All @@ -232,18 +232,18 @@ pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<'
Constant { value: _ } => {}
Range(_) => {}
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
for subpattern in prefix {
for subpattern in prefix.iter() {
visitor.visit_pat(&subpattern);
}
if let Some(pat) = slice {
visitor.visit_pat(pat);
visitor.visit_pat(&pat);
}
for subpattern in suffix {
for subpattern in suffix.iter() {
visitor.visit_pat(&subpattern);
}
}
Or { pats } => {
for pat in pats {
for pat in pats.iter() {
visitor.visit_pat(&pat);
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ pub type CanonicalUserTypeAnnotations<'tcx> =

#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable, Lift)]
pub struct CanonicalUserTypeAnnotation<'tcx> {
pub user_ty: CanonicalUserType<'tcx>,
pub user_ty: Box<CanonicalUserType<'tcx>>,
pub span: Span,
pub inferred_ty: Ty<'tcx>,
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
lint_level,
else_block,
} => {
let ignores_expr_result = matches!(*pattern.kind, PatKind::Wild);
let ignores_expr_result = matches!(pattern.kind, PatKind::Wild);
this.block_context.push(BlockFrame::Statement { ignores_expr_result });

// Enter the remainder scope, i.e., the bindings' destruction scope.
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ArmHasGuard(false),
Some((None, initializer_span)),
);
this.expr_into_pattern(block, pattern.clone(), init) // irrefutable pattern
this.expr_into_pattern(block, pattern, init) // irrefutable pattern
}
})
},
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_mir_build/src/build/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { span, user_ty: None, literal }
}
ExprKind::NonHirLiteral { lit, ref user_ty } => {
let user_ty = user_ty.as_ref().map(|box user_ty| {
let user_ty = user_ty.as_ref().map(|user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span,
user_ty: *user_ty,
user_ty: user_ty.clone(),
inferred_ty: ty,
})
});
Expand All @@ -54,10 +54,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { span, user_ty: user_ty, literal }
}
ExprKind::ZstLiteral { ref user_ty } => {
let user_ty = user_ty.as_ref().map(|box user_ty| {
let user_ty = user_ty.as_ref().map(|user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span,
user_ty: *user_ty,
user_ty: user_ty.clone(),
inferred_ty: ty,
})
});
Expand All @@ -66,10 +66,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { span, user_ty: user_ty, literal }
}
ExprKind::NamedConst { def_id, substs, ref user_ty } => {
let user_ty = user_ty.as_ref().map(|box user_ty| {
let user_ty = user_ty.as_ref().map(|user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span,
user_ty: *user_ty,
user_ty: user_ty.clone(),
inferred_ty: ty,
})
});
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fake_borrow_temps,
)
);
if let Some(box user_ty) = user_ty {
if let Some(user_ty) = user_ty {
let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
user_ty: *user_ty,
user_ty: user_ty.clone(),
inferred_ty: expr.ty,
});

Expand All @@ -551,11 +551,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let source = &this.thir[source];
let temp =
unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
if let Some(box user_ty) = user_ty {
if let Some(user_ty) = user_ty {
let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
user_ty: *user_ty,
user_ty: user_ty.clone(),
inferred_ty: expr.ty,
});
this.cfg.push(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
};

let inferred_ty = expr.ty;
let user_ty = user_ty.as_ref().map(|box user_ty| {
let user_ty = user_ty.as_ref().map(|user_ty| {
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
user_ty: *user_ty,
user_ty: user_ty.clone(),
inferred_ty,
})
});
Expand Down
Loading

0 comments on commit 0421444

Please sign in to comment.