Skip to content

Commit 6689603

Browse files
Auto merge of #146221 - camsteffen:ast-boxes, r=<try>
Remove boxes from ast list elements
2 parents 227ac7c + 0ea65e0 commit 6689603

File tree

16 files changed

+149
-125
lines changed

16 files changed

+149
-125
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
@@ -1152,12 +1152,12 @@ pub fn parse_ast_fragment<'a>(
11521152
}
11531153
}
11541154
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
1155-
AstFragmentKind::Pat => AstFragment::Pat(this.parse_pat_allow_top_guard(
1155+
AstFragmentKind::Pat => AstFragment::Pat(Box::new(this.parse_pat_allow_top_guard(
11561156
None,
11571157
RecoverComma::No,
11581158
RecoverColon::Yes,
11591159
CommaRecoveryMode::LikelyTuple,
1160-
)?),
1160+
)?)),
11611161
AstFragmentKind::Crate => AstFragment::Crate(this.parse_crate_mod()?),
11621162
AstFragmentKind::Arms
11631163
| AstFragmentKind::ExprFields

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 32 additions & 14 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,17 +1843,19 @@ 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>> {
1838-
if !self.may_recover() {
1839-
return Ok(base);
1846+
base: T,
1847+
) -> PResult<'a, T> {
1848+
// Do not add `::` to expected tokens.
1849+
if self.may_recover() && self.token == token::PathSep {
1850+
return self.recover_from_bad_qpath(base);
18401851
}
1852+
Ok(base)
1853+
}
18411854

1842-
// Do not add `::` to expected tokens.
1843-
if self.token == token::PathSep {
1844-
if let Some(ty) = base.to_ty() {
1845-
return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
1846-
}
1855+
#[cold]
1856+
fn recover_from_bad_qpath<T: RecoverQPath>(&mut self, base: T) -> PResult<'a, T> {
1857+
if let Some(ty) = base.to_ty() {
1858+
return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
18471859
}
18481860
Ok(base)
18491861
}
@@ -1854,7 +1866,7 @@ impl<'a> Parser<'a> {
18541866
&mut self,
18551867
ty_span: Span,
18561868
ty: Box<Ty>,
1857-
) -> PResult<'a, Box<T>> {
1869+
) -> PResult<'a, T> {
18581870
self.expect(exp!(PathSep))?;
18591871

18601872
let mut path = ast::Path { segments: ThinVec::new(), span: DUMMY_SP, tokens: None };
@@ -1867,7 +1879,7 @@ impl<'a> Parser<'a> {
18671879
});
18681880

18691881
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)))
1882+
Ok(T::recovered(Some(Box::new(QSelf { ty, path_span, position: 0 })), path))
18711883
}
18721884

18731885
/// This function gets called in places where a semicolon is NOT expected and if there's a
@@ -2360,6 +2372,7 @@ impl<'a> Parser<'a> {
23602372
None
23612373
}
23622374

2375+
#[cold]
23632376
pub(super) fn recover_arg_parse(&mut self) -> PResult<'a, (Box<ast::Pat>, Box<ast::Ty>)> {
23642377
let pat = self.parse_pat_no_top_alt(Some(Expected::ArgumentName), None)?;
23652378
self.expect(exp!(Colon))?;
@@ -2740,11 +2753,12 @@ impl<'a> Parser<'a> {
27402753

27412754
/// Some special error handling for the "top-level" patterns in a match arm,
27422755
/// `for` loop, `let`, &c. (in contrast to subpatterns within such).
2743-
pub(crate) fn maybe_recover_colon_colon_in_pat_typo(
2756+
#[cold]
2757+
pub(crate) fn recover_colon_colon_in_pat_typo(
27442758
&mut self,
2745-
mut first_pat: Box<Pat>,
2759+
mut first_pat: Pat,
27462760
expected: Option<Expected>,
2747-
) -> Box<Pat> {
2761+
) -> Pat {
27482762
if token::Colon != self.token.kind {
27492763
return first_pat;
27502764
}
@@ -2925,7 +2939,11 @@ impl<'a> Parser<'a> {
29252939
if self.token != token::Comma {
29262940
return Ok(());
29272941
}
2942+
self.recover_unexpected_comma(lo, rt)
2943+
}
29282944

2945+
#[cold]
2946+
fn recover_unexpected_comma(&mut self, lo: Span, rt: CommaRecoveryMode) -> PResult<'a, ()> {
29292947
// An unexpected comma after a top-level pattern is a clue that the
29302948
// user (perhaps more accustomed to some other language) forgot the
29312949
// parentheses in what should have been a tuple pattern; return a

0 commit comments

Comments
 (0)