Skip to content

Commit 1fb03d7

Browse files
committed
Auto merge of #152798 - camsteffen:ast-ty-boxes, r=<try>
Remove some boxes from `ast::Ty`
2 parents 8387095 + db3c461 commit 1fb03d7

32 files changed

Lines changed: 197 additions & 202 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pub struct ParenthesizedArgs {
344344
pub span: Span,
345345

346346
/// `(A, B)`
347-
pub inputs: ThinVec<Box<Ty>>,
347+
pub inputs: ThinVec<Ty>,
348348

349349
/// ```text
350350
/// Foo(A, B) -> C
@@ -361,8 +361,7 @@ impl ParenthesizedArgs {
361361
let args = self
362362
.inputs
363363
.iter()
364-
.cloned()
365-
.map(|input| AngleBracketedArg::Arg(GenericArg::Type(input)))
364+
.map(|input| AngleBracketedArg::Arg(GenericArg::Type(Box::new(input.clone()))))
366365
.collect();
367366
AngleBracketedArgs { span: self.inputs_span, args }
368367
}
@@ -632,37 +631,37 @@ pub struct Pat {
632631
impl Pat {
633632
/// Attempt reparsing the pattern as a type.
634633
/// This is intended for use by diagnostics.
635-
pub fn to_ty(&self) -> Option<Box<Ty>> {
636-
let kind = match &self.kind {
634+
pub fn to_ty(&self) -> Option<Ty> {
635+
let kind = match self.kind {
637636
PatKind::Missing => unreachable!(),
638637
// In a type expression `_` is an inference variable.
639638
PatKind::Wild => TyKind::Infer,
640639
// An IDENT pattern with no binding mode would be valid as path to a type. E.g. `u32`.
641640
PatKind::Ident(BindingMode::NONE, ident, None) => {
642-
TyKind::Path(None, Path::from_ident(*ident))
641+
TyKind::Path(None, Path::from_ident(ident))
643642
}
644-
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
645-
PatKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
643+
PatKind::Path(ref qself, ref path) => TyKind::Path(qself.clone(), path.clone()),
644+
PatKind::MacCall(ref mac) => TyKind::MacCall(mac.clone()),
646645
// `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
647-
PatKind::Ref(pat, pinned, mutbl) => pat.to_ty().map(|ty| match pinned {
648-
Pinnedness::Not => TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }),
649-
Pinnedness::Pinned => TyKind::PinnedRef(None, MutTy { ty, mutbl: *mutbl }),
646+
PatKind::Ref(ref pat, pinned, mutbl) => pat.to_ty().map(|ty| match pinned {
647+
Pinnedness::Not => TyKind::Ref(None, MutTy { ty: Box::new(ty), mutbl }),
648+
Pinnedness::Pinned => TyKind::PinnedRef(None, MutTy { ty: Box::new(ty), mutbl }),
650649
})?,
651650
// A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
652651
// when `P` can be reparsed as a type `T`.
653-
PatKind::Slice(pats) if let [pat] = pats.as_slice() => {
654-
pat.to_ty().map(TyKind::Slice)?
652+
PatKind::Slice(ref pats) if let [pat] = pats.as_slice() => {
653+
TyKind::Slice(Box::new(pat.to_ty()?))
655654
}
656655
// A tuple pattern `(P0, .., Pn)` can be reparsed as `(T0, .., Tn)`
657656
// assuming `T0` to `Tn` are all syntactically valid as types.
658-
PatKind::Tuple(pats) => {
657+
PatKind::Tuple(ref pats) => {
659658
let tys = pats.iter().map(|pat| pat.to_ty()).collect::<Option<ThinVec<_>>>()?;
660659
TyKind::Tup(tys)
661660
}
662661
_ => return None,
663662
};
664663

665-
Some(Box::new(Ty { kind, id: self.id, span: self.span, tokens: None }))
664+
Some(Ty { kind, id: self.id, span: self.span, tokens: None })
666665
}
667666

668667
/// Walk top-down and call `it` in each place where a pattern occurs
@@ -1501,24 +1500,24 @@ impl Expr {
15011500
}
15021501

15031502
/// Attempts to reparse as `Ty` (for diagnostic purposes).
1504-
pub fn to_ty(&self) -> Option<Box<Ty>> {
1503+
pub fn to_ty(&self) -> Option<Ty> {
15051504
let kind = match &self.kind {
15061505
// Trivial conversions.
15071506
ExprKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
15081507
ExprKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
15091508

1510-
ExprKind::Paren(expr) => expr.to_ty().map(TyKind::Paren)?,
1509+
ExprKind::Paren(expr) => TyKind::Paren(Box::new(expr.to_ty()?)),
15111510

1512-
ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => {
1513-
expr.to_ty().map(|ty| TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }))?
1514-
}
1511+
ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => expr
1512+
.to_ty()
1513+
.map(|ty| TyKind::Ref(None, MutTy { ty: Box::new(ty), mutbl: *mutbl }))?,
15151514

15161515
ExprKind::Repeat(expr, expr_len) => {
1517-
expr.to_ty().map(|ty| TyKind::Array(ty, expr_len.clone()))?
1516+
expr.to_ty().map(|ty| TyKind::Array(Box::new(ty), expr_len.clone()))?
15181517
}
15191518

15201519
ExprKind::Array(exprs) if let [expr] = exprs.as_slice() => {
1521-
expr.to_ty().map(TyKind::Slice)?
1520+
TyKind::Slice(Box::new(expr.to_ty()?))
15221521
}
15231522

15241523
ExprKind::Tup(exprs) => {
@@ -1542,7 +1541,7 @@ impl Expr {
15421541
_ => return None,
15431542
};
15441543

1545-
Some(Box::new(Ty { kind, id: self.id, span: self.span, tokens: None }))
1544+
Some(Ty { kind, id: self.id, span: self.span, tokens: None })
15461545
}
15471546

15481547
pub fn precedence(&self) -> ExprPrecedence {
@@ -1978,7 +1977,7 @@ pub enum UnsafeBinderCastKind {
19781977
/// ```
19791978
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
19801979
pub struct QSelf {
1981-
pub ty: Box<Ty>,
1980+
pub ty: Ty,
19821981

19831982
/// The span of `a::b::Trait` in a path like `<Vec<T> as
19841983
/// a::b::Trait>::AssociatedItem`; in the case where `position ==
@@ -2409,18 +2408,6 @@ pub enum Term {
24092408
Const(AnonConst),
24102409
}
24112410

2412-
impl From<Box<Ty>> for Term {
2413-
fn from(v: Box<Ty>) -> Self {
2414-
Term::Ty(v)
2415-
}
2416-
}
2417-
2418-
impl From<AnonConst> for Term {
2419-
fn from(v: AnonConst) -> Self {
2420-
Term::Const(v)
2421-
}
2422-
}
2423-
24242411
/// The kind of [associated item constraint][AssocItemConstraint].
24252412
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
24262413
pub enum AssocItemConstraintKind {
@@ -2496,7 +2483,7 @@ pub struct FnPtrTy {
24962483
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
24972484
pub struct UnsafeBinderTy {
24982485
pub generic_params: ThinVec<GenericParam>,
2499-
pub inner_ty: Box<Ty>,
2486+
pub inner_ty: Ty,
25002487
}
25012488

25022489
/// The various kinds of type recognized by the compiler.
@@ -2523,7 +2510,7 @@ pub enum TyKind {
25232510
/// The never type (`!`).
25242511
Never,
25252512
/// A tuple (`(A, B, C, D,...)`).
2526-
Tup(ThinVec<Box<Ty>>),
2513+
Tup(ThinVec<Ty>),
25272514
/// A path (`module::module::...::Type`), optionally
25282515
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
25292516
///
@@ -2907,7 +2894,7 @@ pub struct InlineAsm {
29072894
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
29082895
pub struct Param {
29092896
pub attrs: AttrVec,
2910-
pub ty: Box<Ty>,
2897+
pub ty: Ty,
29112898
pub pat: Box<Pat>,
29122899
pub id: NodeId,
29132900
pub span: Span,
@@ -2926,7 +2913,7 @@ pub enum SelfKind {
29262913
/// `&'lt pin const self`, `&'lt pin mut self`
29272914
Pinned(Option<Lifetime>, Mutability),
29282915
/// `self: TYPE`, `mut self: TYPE`
2929-
Explicit(Box<Ty>, Mutability),
2916+
Explicit(Ty, Mutability),
29302917
}
29312918

29322919
impl SelfKind {
@@ -2982,32 +2969,32 @@ impl Param {
29822969
/// Builds a `Param` object from `ExplicitSelf`.
29832970
pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param {
29842971
let span = eself.span.to(eself_ident.span);
2985-
let infer_ty = Box::new(Ty {
2972+
let infer_ty = Ty {
29862973
id: DUMMY_NODE_ID,
29872974
kind: TyKind::ImplicitSelf,
29882975
span: eself_ident.span,
29892976
tokens: None,
2990-
});
2977+
};
29912978
let (mutbl, ty) = match eself.node {
29922979
SelfKind::Explicit(ty, mutbl) => (mutbl, ty),
29932980
SelfKind::Value(mutbl) => (mutbl, infer_ty),
29942981
SelfKind::Region(lt, mutbl) => (
29952982
Mutability::Not,
2996-
Box::new(Ty {
2983+
Ty {
29972984
id: DUMMY_NODE_ID,
2998-
kind: TyKind::Ref(lt, MutTy { ty: infer_ty, mutbl }),
2985+
kind: TyKind::Ref(lt, MutTy { ty: Box::new(infer_ty), mutbl }),
29992986
span,
30002987
tokens: None,
3001-
}),
2988+
},
30022989
),
30032990
SelfKind::Pinned(lt, mutbl) => (
30042991
mutbl,
3005-
Box::new(Ty {
2992+
Ty {
30062993
id: DUMMY_NODE_ID,
3007-
kind: TyKind::PinnedRef(lt, MutTy { ty: infer_ty, mutbl }),
2994+
kind: TyKind::PinnedRef(lt, MutTy { ty: Box::new(infer_ty), mutbl }),
30082995
span,
30092996
tokens: None,
3010-
}),
2997+
},
30112998
),
30122999
};
30133000
Param {
@@ -3554,7 +3541,7 @@ pub struct FieldDef {
35543541
pub safety: Safety,
35553542
pub ident: Option<Ident>,
35563543

3557-
pub ty: Box<Ty>,
3544+
pub ty: Ty,
35583545
pub default: Option<AnonConst>,
35593546
pub is_placeholder: bool,
35603547
}
@@ -3864,7 +3851,7 @@ pub struct DelegationMac {
38643851
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
38653852
pub struct StaticItem {
38663853
pub ident: Ident,
3867-
pub ty: Box<Ty>,
3854+
pub ty: Ty,
38683855
pub safety: Safety,
38693856
pub mutability: Mutability,
38703857
pub expr: Option<Box<Expr>>,
@@ -3876,7 +3863,7 @@ pub struct ConstItem {
38763863
pub defaultness: Defaultness,
38773864
pub ident: Ident,
38783865
pub generics: Generics,
3879-
pub ty: Box<Ty>,
3866+
pub ty: Ty,
38803867
pub rhs_kind: ConstItemRhsKind,
38813868
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
38823869
}
@@ -4262,7 +4249,7 @@ mod size_asserts {
42624249
static_assert_size!(LitKind, 24);
42634250
static_assert_size!(Local, 96);
42644251
static_assert_size!(MetaItemLit, 40);
4265-
static_assert_size!(Param, 40);
4252+
static_assert_size!(Param, 96);
42664253
static_assert_size!(Pat, 80);
42674254
static_assert_size!(PatKind, 56);
42684255
static_assert_size!(Path, 24);

compiler/rustc_ast/src/expand/autodiff_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub fn valid_ret_activity(mode: DiffMode, activity: DiffActivity) -> bool {
164164
/// since Duplicated expects a mutable ref/ptr and we would thus end up with a shadow value
165165
/// who is an indirect type, which doesn't match the primal scalar type. We can't prevent
166166
/// users here from marking scalars as Duplicated, due to type aliases.
167-
pub fn valid_ty_for_activity(ty: &Box<Ty>, activity: DiffActivity) -> bool {
167+
pub fn valid_ty_for_activity(ty: &Ty, activity: DiffActivity) -> bool {
168168
use DiffActivity::*;
169169
// It's always allowed to mark something as Const, since we won't compute derivatives wrt. it.
170170
// Dual variants also support all types.

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ macro_rules! common_visitor_and_walkers {
392392
ThinVec<PathSegment>,
393393
ThinVec<PreciseCapturingArg>,
394394
ThinVec<Pat>,
395-
ThinVec<Box<Ty>>,
395+
ThinVec<Ty>,
396396
ThinVec<TyPat>,
397397
ThinVec<EiiImpl>,
398398
);

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ fn deny_equality_constraints(
19111911
ident: *ident,
19121912
gen_args,
19131913
kind: AssocItemConstraintKind::Equality {
1914-
term: predicate.rhs_ty.clone().into(),
1914+
term: Term::Ty(predicate.rhs_ty.clone()),
19151915
},
19161916
span: ident.span,
19171917
});

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
7575

7676
let call = cx.expr_call_ident(sig_span, handler, thin_vec![layout]);
7777

78-
let never = ast::FnRetTy::Ty(cx.ty(span, TyKind::Never));
78+
let never = ast::FnRetTy::Ty(Box::new(cx.ty(span, TyKind::Never)));
7979
let params = thin_vec![cx.param(span, size, ty_usize.clone()), cx.param(span, align, ty_usize)];
8080
let decl = cx.fn_decl(params, never);
8181
let header = FnHeader { safety: Safety::Unsafe(span), ..FnHeader::default() };

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ mod llvm_enzyme {
551551
.filter_map(|p| match &p.kind {
552552
GenericParamKind::Type { .. } => {
553553
let path = ast::Path::from_ident(p.ident);
554-
let ty = ecx.ty_path(path);
554+
let ty = Box::new(ecx.ty_path(path));
555555
Some(AngleBracketedArg::Arg(GenericArg::Type(ty)))
556556
}
557557
GenericParamKind::Const { .. } => {
@@ -682,7 +682,7 @@ mod llvm_enzyme {
682682
for i in 0..x.width {
683683
let mut shadow_arg = arg.clone();
684684
// We += into the shadow in reverse mode.
685-
shadow_arg.ty = Box::new(assure_mut_ref(&arg.ty));
685+
shadow_arg.ty = assure_mut_ref(&arg.ty);
686686
let old_name = if let PatKind::Ident(_, ident, _) = arg.pat.kind {
687687
ident.name
688688
} else {
@@ -758,7 +758,7 @@ mod llvm_enzyme {
758758
match x.ret_activity {
759759
DiffActivity::Active | DiffActivity::ActiveOnly => {
760760
let ty = match d_decl.output {
761-
FnRetTy::Ty(ref ty) => ty.clone(),
761+
FnRetTy::Ty(ref ty) => (&**ty).clone(),
762762
FnRetTy::Default(span) => {
763763
panic!("Did not expect Default ret ty: {:?}", span);
764764
}
@@ -788,17 +788,12 @@ mod llvm_enzyme {
788788

789789
if x.mode.is_fwd() {
790790
let ty = match d_decl.output {
791-
FnRetTy::Ty(ref ty) => ty.clone(),
791+
FnRetTy::Ty(ref ty) => (&**ty).clone(),
792792
FnRetTy::Default(span) => {
793793
// We want to return std::hint::black_box(()).
794794
let kind = TyKind::Tup(ThinVec::new());
795-
let ty = Box::new(rustc_ast::Ty {
796-
kind,
797-
id: ast::DUMMY_NODE_ID,
798-
span,
799-
tokens: None,
800-
});
801-
d_decl.output = FnRetTy::Ty(ty.clone());
795+
let ty = rustc_ast::Ty { kind, id: ast::DUMMY_NODE_ID, span, tokens: None };
796+
d_decl.output = FnRetTy::Ty(Box::new(ty.clone()));
802797
assert!(matches!(x.ret_activity, DiffActivity::None));
803798
// this won't be used below, so any type would be fine.
804799
ty
@@ -817,7 +812,7 @@ mod llvm_enzyme {
817812
value: ecx.expr_usize(span, 1 + x.width as usize),
818813
mgca_disambiguation: MgcaDisambiguation::Direct,
819814
};
820-
TyKind::Array(ty.clone(), anon_const)
815+
TyKind::Array(Box::new(ty.clone()), anon_const)
821816
};
822817
let ty = Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None });
823818
d_decl.output = FnRetTy::Ty(ty);
@@ -832,7 +827,7 @@ mod llvm_enzyme {
832827
value: ecx.expr_usize(span, x.width as usize),
833828
mgca_disambiguation: MgcaDisambiguation::Direct,
834829
};
835-
let kind = TyKind::Array(ty.clone(), anon_const);
830+
let kind = TyKind::Array(Box::new(ty.clone()), anon_const);
836831
let ty =
837832
Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None });
838833
d_decl.output = FnRetTy::Ty(ty);
@@ -853,14 +848,14 @@ mod llvm_enzyme {
853848
let ret_ty = match d_decl.output {
854849
FnRetTy::Ty(ref ty) => {
855850
if !active_only_ret {
856-
act_ret.insert(0, ty.clone());
851+
act_ret.insert(0, (&**ty).clone());
857852
}
858853
let kind = TyKind::Tup(act_ret);
859854
Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None })
860855
}
861856
FnRetTy::Default(span) => {
862857
if act_ret.len() == 1 {
863-
act_ret[0].clone()
858+
Box::new(act_ret[0].clone())
864859
} else {
865860
let kind = TyKind::Tup(act_ret.iter().map(|arg| arg.clone()).collect());
866861
Box::new(rustc_ast::Ty { kind, id: ast::DUMMY_NODE_ID, span, tokens: None })

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn cs_clone_simple(
143143
super::assert_ty_bounds(
144144
cx,
145145
&mut stmts,
146-
field.ty.clone(),
146+
Box::new(field.ty.clone()),
147147
field.span,
148148
&[sym::clone, sym::AssertParamIsClone],
149149
);
@@ -154,7 +154,8 @@ fn cs_clone_simple(
154154
if is_union {
155155
// Just a single assertion for unions, that the union impls `Copy`.
156156
// let _: AssertParamIsCopy<Self>;
157-
let self_ty = cx.ty_path(cx.path_ident(trait_span, Ident::with_dummy_span(kw::SelfUpper)));
157+
let self_ty =
158+
Box::new(cx.ty_path(cx.path_ident(trait_span, Ident::with_dummy_span(kw::SelfUpper))));
158159
super::assert_ty_bounds(
159160
cx,
160161
&mut stmts,

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn cs_total_eq_assert(
7171
super::assert_ty_bounds(
7272
cx,
7373
&mut stmts,
74-
field.ty.clone(),
74+
Box::new(field.ty.clone()),
7575
field.span,
7676
&[sym::cmp, sym::AssertParamIsEq],
7777
);

0 commit comments

Comments
 (0)