Skip to content

Commit b1ca781

Browse files
committed
Remove boxes from ast Pat lists
1 parent 0d6a806 commit b1ca781

File tree

16 files changed

+118
-101
lines changed

16 files changed

+118
-101
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,11 @@ pub enum PatKind {
870870
Struct(Option<Box<QSelf>>, Path, ThinVec<PatField>, PatFieldsRest),
871871

872872
/// A tuple struct/variant pattern (`Variant(x, y, .., z)`).
873-
TupleStruct(Option<Box<QSelf>>, Path, ThinVec<Box<Pat>>),
873+
TupleStruct(Option<Box<QSelf>>, Path, ThinVec<Pat>),
874874

875875
/// An or-pattern `A | B | C`.
876876
/// Invariant: `pats.len() >= 2`.
877-
Or(ThinVec<Box<Pat>>),
877+
Or(ThinVec<Pat>),
878878

879879
/// A possibly qualified path pattern.
880880
/// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
@@ -883,7 +883,7 @@ pub enum PatKind {
883883
Path(Option<Box<QSelf>>, Path),
884884

885885
/// A tuple pattern (`(a, b)`).
886-
Tuple(ThinVec<Box<Pat>>),
886+
Tuple(ThinVec<Pat>),
887887

888888
/// A `box` pattern.
889889
Box(Box<Pat>),
@@ -901,7 +901,7 @@ pub enum PatKind {
901901
Range(Option<Box<Expr>>, Option<Box<Expr>>, Spanned<RangeEnd>),
902902

903903
/// A slice pattern `[a, b, c]`.
904-
Slice(ThinVec<Box<Pat>>),
904+
Slice(ThinVec<Pat>),
905905

906906
/// A rest pattern `..`.
907907
///

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ macro_rules! common_visitor_and_walkers {
389389
ThinVec<(NodeId, Path)>,
390390
ThinVec<PathSegment>,
391391
ThinVec<PreciseCapturingArg>,
392-
ThinVec<Box<Pat>>,
392+
ThinVec<Pat>,
393393
ThinVec<Box<Ty>>,
394394
ThinVec<Box<TyPat>>,
395395
);

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
154154

155155
fn lower_pat_tuple(
156156
&mut self,
157-
pats: &[Box<Pat>],
157+
pats: &[Pat],
158158
ctx: &str,
159159
) -> (&'hir [hir::Pat<'hir>], hir::DotDotPos) {
160160
let mut elems = Vec::with_capacity(pats.len());
@@ -209,7 +209,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
209209
/// When encountering `($binding_mode $ident @)? ..` (`slice`),
210210
/// this is interpreted as a sub-slice pattern semantically.
211211
/// Patterns that follow, which are not like `slice` -- or an error occurs, are in `after`.
212-
fn lower_pat_slice(&mut self, pats: &[Box<Pat>]) -> hir::PatKind<'hir> {
212+
fn lower_pat_slice(&mut self, pats: &[Pat]) -> hir::PatKind<'hir> {
213213
let mut before = Vec::new();
214214
let mut after = Vec::new();
215215
let mut slice = None;

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ impl<'a> TraitDef<'a> {
15071507
struct_def: &'a VariantData,
15081508
prefixes: &[String],
15091509
by_ref: ByRef,
1510-
) -> ThinVec<Box<ast::Pat>> {
1510+
) -> ThinVec<ast::Pat> {
15111511
prefixes
15121512
.iter()
15131513
.map(|prefix| {
@@ -1543,7 +1543,7 @@ impl<'a> TraitDef<'a> {
15431543
attrs: ast::AttrVec::new(),
15441544
id: ast::DUMMY_NODE_ID,
15451545
span: pat.span.with_ctxt(self.span.ctxt()),
1546-
pat,
1546+
pat: Box::new(pat),
15471547
is_placeholder: false,
15481548
}
15491549
})

compiler/rustc_builtin_macros/src/pattern_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn parse_pat_ty<'a>(
3232

3333
let pat = pat_to_ty_pat(
3434
cx,
35-
*parser.parse_pat_no_top_guard(
35+
parser.parse_pat_no_top_guard(
3636
None,
3737
RecoverComma::No,
3838
RecoverColon::No,
@@ -59,7 +59,7 @@ fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> Box<TyPat> {
5959
include_end,
6060
),
6161
ast::PatKind::Or(variants) => {
62-
TyPatKind::Or(variants.into_iter().map(|pat| pat_to_ty_pat(cx, *pat)).collect())
62+
TyPatKind::Or(variants.into_iter().map(|pat| pat_to_ty_pat(cx, pat)).collect())
6363
}
6464
ast::PatKind::Err(guar) => TyPatKind::Err(guar),
6565
_ => TyPatKind::Err(cx.dcx().span_err(pat.span, "pattern not supported in pattern types")),

compiler/rustc_expand/src/build.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<'a> ExtCtxt<'a> {
233233
};
234234
let local = Box::new(ast::Local {
235235
super_: None,
236-
pat,
236+
pat: Box::new(pat),
237237
ty,
238238
id: ast::DUMMY_NODE_ID,
239239
kind: LocalKind::Init(ex),
@@ -249,7 +249,7 @@ impl<'a> ExtCtxt<'a> {
249249
pub fn stmt_let_type_only(&self, span: Span, ty: Box<ast::Ty>) -> ast::Stmt {
250250
let local = Box::new(ast::Local {
251251
super_: None,
252-
pat: self.pat_wild(span),
252+
pat: Box::new(self.pat_wild(span)),
253253
ty: Some(ty),
254254
id: ast::DUMMY_NODE_ID,
255255
kind: LocalKind::Decl,
@@ -528,16 +528,16 @@ impl<'a> ExtCtxt<'a> {
528528
self.expr_match(sp, head, thin_vec![ok_arm, err_arm])
529529
}
530530

531-
pub fn pat(&self, span: Span, kind: PatKind) -> Box<ast::Pat> {
532-
Box::new(ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None })
531+
pub fn pat(&self, span: Span, kind: PatKind) -> ast::Pat {
532+
ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None }
533533
}
534-
pub fn pat_wild(&self, span: Span) -> Box<ast::Pat> {
534+
pub fn pat_wild(&self, span: Span) -> ast::Pat {
535535
self.pat(span, PatKind::Wild)
536536
}
537-
pub fn pat_lit(&self, span: Span, expr: Box<ast::Expr>) -> Box<ast::Pat> {
537+
pub fn pat_lit(&self, span: Span, expr: Box<ast::Expr>) -> ast::Pat {
538538
self.pat(span, PatKind::Expr(expr))
539539
}
540-
pub fn pat_ident(&self, span: Span, ident: Ident) -> Box<ast::Pat> {
540+
pub fn pat_ident(&self, span: Span, ident: Ident) -> ast::Pat {
541541
self.pat_ident_binding_mode(span, ident, ast::BindingMode::NONE)
542542
}
543543

@@ -546,43 +546,43 @@ impl<'a> ExtCtxt<'a> {
546546
span: Span,
547547
ident: Ident,
548548
ann: ast::BindingMode,
549-
) -> Box<ast::Pat> {
549+
) -> ast::Pat {
550550
let pat = PatKind::Ident(ann, ident.with_span_pos(span), None);
551551
self.pat(span, pat)
552552
}
553-
pub fn pat_path(&self, span: Span, path: ast::Path) -> Box<ast::Pat> {
553+
pub fn pat_path(&self, span: Span, path: ast::Path) -> ast::Pat {
554554
self.pat(span, PatKind::Path(None, path))
555555
}
556556
pub fn pat_tuple_struct(
557557
&self,
558558
span: Span,
559559
path: ast::Path,
560-
subpats: ThinVec<Box<ast::Pat>>,
561-
) -> Box<ast::Pat> {
560+
subpats: ThinVec<ast::Pat>,
561+
) -> ast::Pat {
562562
self.pat(span, PatKind::TupleStruct(None, path, subpats))
563563
}
564564
pub fn pat_struct(
565565
&self,
566566
span: Span,
567567
path: ast::Path,
568568
field_pats: ThinVec<ast::PatField>,
569-
) -> Box<ast::Pat> {
569+
) -> ast::Pat {
570570
self.pat(span, PatKind::Struct(None, path, field_pats, ast::PatFieldsRest::None))
571571
}
572-
pub fn pat_tuple(&self, span: Span, pats: ThinVec<Box<ast::Pat>>) -> Box<ast::Pat> {
572+
pub fn pat_tuple(&self, span: Span, pats: ThinVec<ast::Pat>) -> ast::Pat {
573573
self.pat(span, PatKind::Tuple(pats))
574574
}
575575

576-
pub fn pat_some(&self, span: Span, pat: Box<ast::Pat>) -> Box<ast::Pat> {
576+
pub fn pat_some(&self, span: Span, pat: ast::Pat) -> ast::Pat {
577577
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
578578
let path = self.path_global(span, some);
579579
self.pat_tuple_struct(span, path, thin_vec![pat])
580580
}
581581

582-
pub fn arm(&self, span: Span, pat: Box<ast::Pat>, expr: Box<ast::Expr>) -> ast::Arm {
582+
pub fn arm(&self, span: Span, pat: ast::Pat, expr: Box<ast::Expr>) -> ast::Arm {
583583
ast::Arm {
584584
attrs: AttrVec::new(),
585-
pat,
585+
pat: Box::new(pat),
586586
guard: None,
587587
body: Some(expr),
588588
span,
@@ -661,11 +661,11 @@ impl<'a> ExtCtxt<'a> {
661661
}
662662

663663
pub fn param(&self, span: Span, ident: Ident, ty: Box<ast::Ty>) -> ast::Param {
664-
let arg_pat = self.pat_ident(span, ident);
664+
let pat = Box::new(self.pat_ident(span, ident));
665665
ast::Param {
666666
attrs: AttrVec::default(),
667667
id: ast::DUMMY_NODE_ID,
668-
pat: arg_pat,
668+
pat,
669669
span,
670670
ty,
671671
is_placeholder: false,

compiler/rustc_expand/src/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,12 +1145,12 @@ pub fn parse_ast_fragment<'a>(
11451145
}
11461146
}
11471147
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
1148-
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_guard(
1148+
AstFragmentKind::Pat => AstFragment::Pat(Box::new(this.parse_pat_allow_top_guard(
11491149
None,
11501150
RecoverComma::No,
11511151
RecoverColon::Yes,
11521152
CommaRecoveryMode::LikelyTuple,
1153-
)?),
1153+
)?)),
11541154
AstFragmentKind::Crate => AstFragment::Crate(this.parse_crate_mod()?),
11551155
AstFragmentKind::Arms
11561156
| AstFragmentKind::ExprFields

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ pub(super) trait RecoverQPath: Sized + 'static {
7373
fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self;
7474
}
7575

76+
impl<T: RecoverQPath> RecoverQPath for Box<T> {
77+
const PATH_STYLE: PathStyle = T::PATH_STYLE;
78+
fn to_ty(&self) -> Option<Box<Ty>> {
79+
T::to_ty(self)
80+
}
81+
fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
82+
Box::new(T::recovered(qself, path))
83+
}
84+
}
85+
7686
impl RecoverQPath for Ty {
7787
const PATH_STYLE: PathStyle = PathStyle::Type;
7888
fn to_ty(&self) -> Option<Box<Ty>> {
@@ -1833,8 +1843,8 @@ impl<'a> Parser<'a> {
18331843
/// tail, and combines them into a `<Ty>::AssocItem` expression/pattern/type.
18341844
pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
18351845
&mut self,
1836-
base: Box<T>,
1837-
) -> PResult<'a, Box<T>> {
1846+
base: T,
1847+
) -> PResult<'a, T> {
18381848
if !self.may_recover() {
18391849
return Ok(base);
18401850
}
@@ -1854,7 +1864,7 @@ impl<'a> Parser<'a> {
18541864
&mut self,
18551865
ty_span: Span,
18561866
ty: Box<Ty>,
1857-
) -> PResult<'a, Box<T>> {
1867+
) -> PResult<'a, T> {
18581868
self.expect(exp!(PathSep))?;
18591869

18601870
let mut path = ast::Path { segments: ThinVec::new(), span: DUMMY_SP, tokens: None };
@@ -1867,7 +1877,7 @@ impl<'a> Parser<'a> {
18671877
});
18681878

18691879
let path_span = ty_span.shrink_to_hi(); // Use an empty path since `position == 0`.
1870-
Ok(Box::new(T::recovered(Some(Box::new(QSelf { ty, path_span, position: 0 })), path)))
1880+
Ok(T::recovered(Some(Box::new(QSelf { ty, path_span, position: 0 })), path))
18711881
}
18721882

18731883
/// This function gets called in places where a semicolon is NOT expected and if there's a
@@ -2742,9 +2752,9 @@ impl<'a> Parser<'a> {
27422752
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
27432753
pub(crate) fn maybe_recover_colon_colon_in_pat_typo(
27442754
&mut self,
2745-
mut first_pat: Box<Pat>,
2755+
mut first_pat: Pat,
27462756
expected: Option<Expected>,
2747-
) -> Box<Pat> {
2757+
) -> Pat {
27482758
if token::Colon != self.token.kind {
27492759
return first_pat;
27502760
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ impl<'a> Parser<'a> {
26122612
Param {
26132613
attrs,
26142614
ty,
2615-
pat,
2615+
pat: Box::new(pat),
26162616
span: lo.to(this.prev_token.span),
26172617
id: DUMMY_NODE_ID,
26182618
is_placeholder: false,
@@ -2795,7 +2795,7 @@ impl<'a> Parser<'a> {
27952795
let (expr, _) =
27962796
self.parse_expr_assoc_with(Bound::Excluded(prec_let_scrutinee_needs_par()), attrs)?;
27972797
let span = lo.to(expr.span);
2798-
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span, recovered)))
2798+
Ok(self.mk_expr(span, ExprKind::Let(Box::new(pat), expr, span, recovered)))
27992799
}
28002800

28012801
/// Parses an `else { ... }` expression (`else` token already eaten).
@@ -2911,7 +2911,7 @@ impl<'a> Parser<'a> {
29112911
}
29122912

29132913
// Public to use it for custom `for` expressions in rustfmt forks like https://github.com/tucant/rustfmt
2914-
pub fn parse_for_head(&mut self) -> PResult<'a, (Box<Pat>, Box<Expr>)> {
2914+
pub fn parse_for_head(&mut self) -> PResult<'a, (Pat, Box<Expr>)> {
29152915
let begin_paren = if self.token == token::OpenParen {
29162916
// Record whether we are about to parse `for (`.
29172917
// This is used below for recovery in case of `for ( $stuff ) $block`
@@ -2988,6 +2988,7 @@ impl<'a> Parser<'a> {
29882988
let kind = if is_await { ForLoopKind::ForAwait } else { ForLoopKind::For };
29892989

29902990
let (pat, expr) = self.parse_for_head()?;
2991+
let pat = Box::new(pat);
29912992
// Recover from missing expression in `for` loop
29922993
if matches!(expr.kind, ExprKind::Block(..))
29932994
&& self.token.kind != token::OpenBrace
@@ -3156,7 +3157,7 @@ impl<'a> Parser<'a> {
31563157
// Always push at least one arm to make the match non-empty
31573158
arms.push(Arm {
31583159
attrs: Default::default(),
3159-
pat: self.mk_pat(span, ast::PatKind::Err(guar)),
3160+
pat: Box::new(self.mk_pat(span, ast::PatKind::Err(guar))),
31603161
guard: None,
31613162
body: Some(self.mk_expr_err(span, guar)),
31623163
span,
@@ -3438,7 +3439,7 @@ impl<'a> Parser<'a> {
34383439
Ok((
34393440
ast::Arm {
34403441
attrs,
3441-
pat,
3442+
pat: Box::new(pat),
34423443
guard,
34433444
body: arm_body,
34443445
span: arm_span,
@@ -3482,7 +3483,7 @@ impl<'a> Parser<'a> {
34823483
Ok(Some(cond))
34833484
}
34843485

3485-
fn parse_match_arm_pat_and_guard(&mut self) -> PResult<'a, (Box<Pat>, Option<Box<Expr>>)> {
3486+
fn parse_match_arm_pat_and_guard(&mut self) -> PResult<'a, (Pat, Option<Box<Expr>>)> {
34863487
if self.token == token::OpenParen {
34873488
let left = self.token.span;
34883489
let pat = self.parse_pat_no_top_guard(

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3111,7 +3111,7 @@ impl<'a> Parser<'a> {
31113111
match ty {
31123112
Ok(ty) => {
31133113
let pat = this.mk_pat(ty.span, PatKind::Missing);
3114-
(pat, ty)
3114+
(Box::new(pat), ty)
31153115
}
31163116
// If this is a C-variadic argument and we hit an error, return the error.
31173117
Err(err) if this.token == token::DotDotDot => return Err(err),

0 commit comments

Comments
 (0)