Skip to content

Commit dc26336

Browse files
committed
Remove boxes from ast Ty lists
1 parent b1ca781 commit dc26336

File tree

11 files changed

+66
-54
lines changed

11 files changed

+66
-54
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 23 additions & 18 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
@@ -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
///

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ macro_rules! common_visitor_and_walkers {
390390
ThinVec<PathSegment>,
391391
ThinVec<PreciseCapturingArg>,
392392
ThinVec<Pat>,
393-
ThinVec<Box<Ty>>,
393+
ThinVec<Ty>,
394394
ThinVec<Box<TyPat>>,
395395
);
396396

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_parse/src/parser/diagnostics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl RecoverQPath for Ty {
101101
impl RecoverQPath for Pat {
102102
const PATH_STYLE: PathStyle = PathStyle::Pat;
103103
fn to_ty(&self) -> Option<Box<Ty>> {
104-
self.to_ty()
104+
self.to_ty().map(Box::new)
105105
}
106106
fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
107107
Self {
@@ -115,7 +115,7 @@ impl RecoverQPath for Pat {
115115

116116
impl RecoverQPath for Expr {
117117
fn to_ty(&self) -> Option<Box<Ty>> {
118-
self.to_ty()
118+
self.to_ty().map(Box::new)
119119
}
120120
fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
121121
Self {
@@ -1605,7 +1605,7 @@ impl<'a> Parser<'a> {
16051605
}
16061606

16071607
/// Swift lets users write `Ty?` to mean `Option<Ty>`. Parse the construct and recover from it.
1608-
pub(super) fn maybe_recover_from_question_mark(&mut self, ty: Box<Ty>) -> Box<Ty> {
1608+
pub(super) fn maybe_recover_from_question_mark(&mut self, ty: Ty) -> Ty {
16091609
if self.token == token::Question {
16101610
self.bump();
16111611
let guar = self.dcx().emit_err(QuestionMarkInType {
@@ -1615,7 +1615,7 @@ impl<'a> Parser<'a> {
16151615
right: self.prev_token.span,
16161616
},
16171617
});
1618-
self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err(guar))
1618+
self.mk_ty_mut(ty.span.to(self.prev_token.span), TyKind::Err(guar))
16191619
} else {
16201620
ty
16211621
}

compiler/rustc_parse/src/parser/item.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,6 @@ impl<'a> Parser<'a> {
637637
self.dcx().emit_err(errors::MissingForInTraitImpl { span: missing_for_span });
638638
}
639639

640-
let ty_first = *ty_first;
641640
let path = match ty_first.kind {
642641
// This notably includes paths passed through `ty` macro fragments (#46438).
643642
TyKind::Path(None, path) => path,

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
157157
let ty = $self
158158
.eat_metavar_seq(mv_kind, |this| this.parse_ty_no_question_mark_recover())
159159
.expect("metavar seq ty");
160+
let ty = Box::new(ty);
160161

161162
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
162163
}

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ impl<'a> Parser<'a> {
155155
self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
156156
))
157157
}
158-
NonterminalKind::Ty => Ok(ParseNtResult::Ty(
158+
NonterminalKind::Ty => Ok(ParseNtResult::Ty(Box::new(
159159
self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
160-
)),
160+
))),
161161
// This could be handled like a token, since it is one.
162162
NonterminalKind::Ident => {
163163
if let Some((ident, is_raw)) = get_macro_ident(&self.token) {

compiler/rustc_parse/src/parser/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl<'a> Parser<'a> {
422422
span: param.attrs[0].span,
423423
});
424424
}
425-
param.ty
425+
*param.ty
426426
})
427427
});
428428

0 commit comments

Comments
 (0)