Skip to content

Commit 4c3b5d3

Browse files
Auto merge of #146221 - camsteffen:ast-boxes, r=<try>
Remove boxes from ast list elements
2 parents 1ed3cd7 + 9b8d217 commit 4c3b5d3

File tree

21 files changed

+191
-162
lines changed

21 files changed

+191
-162
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ pub struct ParenthesizedArgs {
349349
pub span: Span,
350350

351351
/// `(A, B)`
352-
pub inputs: ThinVec<Box<Ty>>,
352+
pub inputs: ThinVec<Ty>,
353353

354354
/// ```text
355355
/// Foo(A, B) -> C
@@ -366,8 +366,7 @@ impl ParenthesizedArgs {
366366
let args = self
367367
.inputs
368368
.iter()
369-
.cloned()
370-
.map(|input| AngleBracketedArg::Arg(GenericArg::Type(input)))
369+
.map(|input| AngleBracketedArg::Arg(GenericArg::Type(Box::new(input.clone()))))
371370
.collect();
372371
AngleBracketedArgs { span: self.inputs_span, args }
373372
}
@@ -637,7 +636,7 @@ pub struct Pat {
637636
impl Pat {
638637
/// Attempt reparsing the pattern as a type.
639638
/// This is intended for use by diagnostics.
640-
pub fn to_ty(&self) -> Option<Box<Ty>> {
639+
pub fn to_ty(&self) -> Option<Ty> {
641640
let kind = match &self.kind {
642641
PatKind::Missing => unreachable!(),
643642
// In a type expression `_` is an inference variable.
@@ -649,13 +648,13 @@ impl Pat {
649648
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
650649
PatKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
651650
// `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
652-
PatKind::Ref(pat, mutbl) => {
653-
pat.to_ty().map(|ty| TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }))?
654-
}
651+
PatKind::Ref(pat, mutbl) => pat
652+
.to_ty()
653+
.map(|ty| TyKind::Ref(None, MutTy { ty: Box::new(ty), mutbl: *mutbl }))?,
655654
// A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
656655
// when `P` can be reparsed as a type `T`.
657656
PatKind::Slice(pats) if let [pat] = pats.as_slice() => {
658-
pat.to_ty().map(TyKind::Slice)?
657+
TyKind::Slice(Box::new(pat.to_ty()?))
659658
}
660659
// A tuple pattern `(P0, .., Pn)` can be reparsed as `(T0, .., Tn)`
661660
// assuming `T0` to `Tn` are all syntactically valid as types.
@@ -670,7 +669,7 @@ impl Pat {
670669
_ => return None,
671670
};
672671

673-
Some(Box::new(Ty { kind, id: self.id, span: self.span, tokens: None }))
672+
Some(Ty { kind, id: self.id, span: self.span, tokens: None })
674673
}
675674

676675
/// Walk top-down and call `it` in each place where a pattern occurs
@@ -870,11 +869,11 @@ pub enum PatKind {
870869
Struct(Option<Box<QSelf>>, Path, ThinVec<PatField>, PatFieldsRest),
871870

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

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

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

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

888887
/// A `box` pattern.
889888
Box(Box<Pat>),
@@ -901,7 +900,7 @@ pub enum PatKind {
901900
Range(Option<Box<Expr>>, Option<Box<Expr>>, Spanned<RangeEnd>),
902901

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

906905
/// A rest pattern `..`.
907906
///
@@ -1468,24 +1467,24 @@ impl Expr {
14681467
}
14691468

14701469
/// Attempts to reparse as `Ty` (for diagnostic purposes).
1471-
pub fn to_ty(&self) -> Option<Box<Ty>> {
1470+
pub fn to_ty(&self) -> Option<Ty> {
14721471
let kind = match &self.kind {
14731472
// Trivial conversions.
14741473
ExprKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
14751474
ExprKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
14761475

1477-
ExprKind::Paren(expr) => expr.to_ty().map(TyKind::Paren)?,
1476+
ExprKind::Paren(expr) => TyKind::Paren(Box::new(expr.to_ty()?)),
14781477

1479-
ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => {
1480-
expr.to_ty().map(|ty| TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }))?
1481-
}
1478+
ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => expr
1479+
.to_ty()
1480+
.map(|ty| TyKind::Ref(None, MutTy { ty: Box::new(ty), mutbl: *mutbl }))?,
14821481

14831482
ExprKind::Repeat(expr, expr_len) => {
1484-
expr.to_ty().map(|ty| TyKind::Array(ty, expr_len.clone()))?
1483+
expr.to_ty().map(|ty| TyKind::Array(Box::new(ty), expr_len.clone()))?
14851484
}
14861485

14871486
ExprKind::Array(exprs) if let [expr] = exprs.as_slice() => {
1488-
expr.to_ty().map(TyKind::Slice)?
1487+
TyKind::Slice(Box::new(expr.to_ty()?))
14891488
}
14901489

14911490
ExprKind::Tup(exprs) => {
@@ -1510,7 +1509,7 @@ impl Expr {
15101509
_ => return None,
15111510
};
15121511

1513-
Some(Box::new(Ty { kind, id: self.id, span: self.span, tokens: None }))
1512+
Some(Ty { kind, id: self.id, span: self.span, tokens: None })
15141513
}
15151514

15161515
pub fn precedence(&self) -> ExprPrecedence {
@@ -2309,6 +2308,12 @@ pub enum Term {
23092308
Const(AnonConst),
23102309
}
23112310

2311+
impl From<Ty> for Term {
2312+
fn from(v: Ty) -> Self {
2313+
Term::Ty(Box::new(v))
2314+
}
2315+
}
2316+
23122317
impl From<Box<Ty>> for Term {
23132318
fn from(v: Box<Ty>) -> Self {
23142319
Term::Ty(v)
@@ -2423,7 +2428,7 @@ pub enum TyKind {
24232428
/// The never type (`!`).
24242429
Never,
24252430
/// A tuple (`(A, B, C, D,...)`).
2426-
Tup(ThinVec<Box<Ty>>),
2431+
Tup(ThinVec<Ty>),
24272432
/// A path (`module::module::...::Type`), optionally
24282433
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
24292434
///
@@ -2532,7 +2537,7 @@ pub enum TyPatKind {
25322537
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
25332538
Range(Option<Box<AnonConst>>, Option<Box<AnonConst>>, Spanned<RangeEnd>),
25342539

2535-
Or(ThinVec<Box<TyPat>>),
2540+
Or(ThinVec<TyPat>),
25362541

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

compiler/rustc_ast/src/visit.rs

Lines changed: 3 additions & 3 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>>,
393-
ThinVec<Box<Ty>>,
394-
ThinVec<Box<TyPat>>,
392+
ThinVec<Pat>,
393+
ThinVec<Ty>,
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/autodiff.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ mod llvm_enzyme {
669669
d_inputs.push(arg.clone());
670670
match activity {
671671
DiffActivity::Active => {
672-
act_ret.push(arg.ty.clone());
672+
act_ret.push((&*arg.ty).clone());
673673
// if width =/= 1, then push [arg.ty; width] to act_ret
674674
}
675675
DiffActivity::ActiveOnly => {
@@ -786,17 +786,12 @@ mod llvm_enzyme {
786786

787787
if x.mode.is_fwd() {
788788
let ty = match d_decl.output {
789-
FnRetTy::Ty(ref ty) => ty.clone(),
789+
FnRetTy::Ty(ref ty) => (&**ty).clone(),
790790
FnRetTy::Default(span) => {
791791
// We want to return std::hint::black_box(()).
792792
let kind = TyKind::Tup(ThinVec::new());
793-
let ty = Box::new(rustc_ast::Ty {
794-
kind,
795-
id: ast::DUMMY_NODE_ID,
796-
span,
797-
tokens: None,
798-
});
799-
d_decl.output = FnRetTy::Ty(ty.clone());
793+
let ty = rustc_ast::Ty { kind, id: ast::DUMMY_NODE_ID, span, tokens: None };
794+
d_decl.output = FnRetTy::Ty(Box::new(ty.clone()));
800795
assert!(matches!(x.ret_activity, DiffActivity::None));
801796
// this won't be used below, so any type would be fine.
802797
ty
@@ -814,7 +809,7 @@ mod llvm_enzyme {
814809
id: ast::DUMMY_NODE_ID,
815810
value: ecx.expr_usize(span, 1 + x.width as usize),
816811
};
817-
TyKind::Array(ty.clone(), anon_const)
812+
TyKind::Array(Box::new(ty.clone()), anon_const)
818813
};
819814
let ty = Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None });
820815
d_decl.output = FnRetTy::Ty(ty);
@@ -828,7 +823,7 @@ mod llvm_enzyme {
828823
id: ast::DUMMY_NODE_ID,
829824
value: ecx.expr_usize(span, x.width as usize),
830825
};
831-
let kind = TyKind::Array(ty.clone(), anon_const);
826+
let kind = TyKind::Array(Box::new(ty.clone()), anon_const);
832827
let ty =
833828
Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None });
834829
d_decl.output = FnRetTy::Ty(ty);
@@ -849,14 +844,14 @@ mod llvm_enzyme {
849844
let ret_ty = match d_decl.output {
850845
FnRetTy::Ty(ref ty) => {
851846
if !active_only_ret {
852-
act_ret.insert(0, ty.clone());
847+
act_ret.insert(0, (&**ty).clone());
853848
}
854849
let kind = TyKind::Tup(act_ret);
855850
Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None })
856851
}
857852
FnRetTy::Default(span) => {
858853
if act_ret.len() == 1 {
859-
act_ret[0].clone()
854+
Box::new(act_ret[0].clone())
860855
} else {
861856
let kind = TyKind::Tup(act_ret.iter().map(|arg| arg.clone()).collect());
862857
Box::new(rustc_ast::Ty { kind, id: ast::DUMMY_NODE_ID, span, tokens: 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")),

0 commit comments

Comments
 (0)