Skip to content

Commit a956f29

Browse files
Auto merge of #146221 - camsteffen:ast-boxes, r=<try>
Remove boxes from ast list elements
2 parents 3369e82 + 38db83e commit a956f29

File tree

16 files changed

+124
-107
lines changed

16 files changed

+124
-107
lines changed

compiler/rustc_ast/src/ast.rs

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

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

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

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

884884
/// A tuple pattern (`(a, b)`).
885-
Tuple(ThinVec<Box<Pat>>),
885+
Tuple(ThinVec<Pat>),
886886

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

902902
/// A slice pattern `[a, b, c]`.
903-
Slice(ThinVec<Box<Pat>>),
903+
Slice(ThinVec<Pat>),
904904

905905
/// A rest pattern `..`.
906906
///
@@ -2579,7 +2579,7 @@ pub enum TyPatKind {
25792579
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
25802580
Range(Option<Box<AnonConst>>, Option<Box<AnonConst>>, Spanned<RangeEnd>),
25812581

2582-
Or(ThinVec<Box<TyPat>>),
2582+
Or(ThinVec<TyPat>),
25832583

25842584
/// Placeholder for a pattern that wasn't syntactically well formed in some way.
25852585
Err(ErrorGuaranteed),

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ 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>>,
394-
ThinVec<Box<TyPat>>,
394+
ThinVec<TyPat>,
395395
);
396396

397397
// This macro generates `impl Visitable` and `impl MutVisitable` that forward to `Walkable`

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: 6 additions & 6 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,
@@ -44,22 +44,22 @@ fn parse_pat_ty<'a>(
4444
parser.unexpected()?;
4545
}
4646

47-
Ok((ty, pat))
47+
Ok((ty, Box::new(pat)))
4848
}
4949

50-
fn ty_pat(kind: TyPatKind, span: Span) -> Box<TyPat> {
51-
Box::new(TyPat { id: DUMMY_NODE_ID, kind, span, tokens: None })
50+
fn ty_pat(kind: TyPatKind, span: Span) -> TyPat {
51+
TyPat { id: DUMMY_NODE_ID, kind, span, tokens: None }
5252
}
5353

54-
fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> Box<TyPat> {
54+
fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> TyPat {
5555
let kind = match pat.kind {
5656
ast::PatKind::Range(start, end, include_end) => TyPatKind::Range(
5757
start.map(|value| Box::new(AnonConst { id: DUMMY_NODE_ID, value })),
5858
end.map(|value| Box::new(AnonConst { id: DUMMY_NODE_ID, value })),
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
@@ -2598,7 +2598,7 @@ impl<'a> Parser<'a> {
25982598
Param {
25992599
attrs,
26002600
ty,
2601-
pat,
2601+
pat: Box::new(pat),
26022602
span: lo.to(this.prev_token.span),
26032603
id: DUMMY_NODE_ID,
26042604
is_placeholder: false,
@@ -2781,7 +2781,7 @@ impl<'a> Parser<'a> {
27812781
let (expr, _) =
27822782
self.parse_expr_assoc_with(Bound::Excluded(prec_let_scrutinee_needs_par()), attrs)?;
27832783
let span = lo.to(expr.span);
2784-
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span, recovered)))
2784+
Ok(self.mk_expr(span, ExprKind::Let(Box::new(pat), expr, span, recovered)))
27852785
}
27862786

27872787
/// Parses an `else { ... }` expression (`else` token already eaten).
@@ -2897,7 +2897,7 @@ impl<'a> Parser<'a> {
28972897
}
28982898

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

29762976
let (pat, expr) = self.parse_for_head()?;
2977+
let pat = Box::new(pat);
29772978
// Recover from missing expression in `for` loop
29782979
if matches!(expr.kind, ExprKind::Block(..))
29792980
&& self.token.kind != token::OpenBrace
@@ -3142,7 +3143,7 @@ impl<'a> Parser<'a> {
31423143
// Always push at least one arm to make the match non-empty
31433144
arms.push(Arm {
31443145
attrs: Default::default(),
3145-
pat: self.mk_pat(span, ast::PatKind::Err(guar)),
3146+
pat: Box::new(self.mk_pat(span, ast::PatKind::Err(guar))),
31463147
guard: None,
31473148
body: Some(self.mk_expr_err(span, guar)),
31483149
span,
@@ -3424,7 +3425,7 @@ impl<'a> Parser<'a> {
34243425
Ok((
34253426
ast::Arm {
34263427
attrs,
3427-
pat,
3428+
pat: Box::new(pat),
34283429
guard,
34293430
body: arm_body,
34303431
span: arm_span,
@@ -3468,7 +3469,7 @@ impl<'a> Parser<'a> {
34683469
Ok(Some(cond))
34693470
}
34703471

3471-
fn parse_match_arm_pat_and_guard(&mut self) -> PResult<'a, (Box<Pat>, Option<Box<Expr>>)> {
3472+
fn parse_match_arm_pat_and_guard(&mut self) -> PResult<'a, (Pat, Option<Box<Expr>>)> {
34723473
if self.token == token::OpenParen {
34733474
let left = self.token.span;
34743475
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)