Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 39 additions & 52 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub struct ParenthesizedArgs {
pub span: Span,

/// `(A, B)`
pub inputs: ThinVec<Box<Ty>>,
pub inputs: ThinVec<Ty>,

/// ```text
/// Foo(A, B) -> C
Expand All @@ -361,8 +361,7 @@ impl ParenthesizedArgs {
let args = self
.inputs
.iter()
.cloned()
.map(|input| AngleBracketedArg::Arg(GenericArg::Type(input)))
.map(|input| AngleBracketedArg::Arg(GenericArg::Type(Box::new(input.clone()))))
.collect();
AngleBracketedArgs { span: self.inputs_span, args }
}
Expand Down Expand Up @@ -632,37 +631,37 @@ pub struct Pat {
impl Pat {
/// Attempt reparsing the pattern as a type.
/// This is intended for use by diagnostics.
pub fn to_ty(&self) -> Option<Box<Ty>> {
let kind = match &self.kind {
pub fn to_ty(&self) -> Option<Ty> {
let kind = match self.kind {
PatKind::Missing => unreachable!(),
// In a type expression `_` is an inference variable.
PatKind::Wild => TyKind::Infer,
// An IDENT pattern with no binding mode would be valid as path to a type. E.g. `u32`.
PatKind::Ident(BindingMode::NONE, ident, None) => {
TyKind::Path(None, Path::from_ident(*ident))
TyKind::Path(None, Path::from_ident(ident))
}
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
PatKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
PatKind::Path(ref qself, ref path) => TyKind::Path(qself.clone(), path.clone()),
PatKind::MacCall(ref mac) => TyKind::MacCall(mac.clone()),
// `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
PatKind::Ref(pat, pinned, mutbl) => pat.to_ty().map(|ty| match pinned {
Pinnedness::Not => TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }),
Pinnedness::Pinned => TyKind::PinnedRef(None, MutTy { ty, mutbl: *mutbl }),
PatKind::Ref(ref pat, pinned, mutbl) => pat.to_ty().map(|ty| match pinned {
Pinnedness::Not => TyKind::Ref(None, MutTy { ty: Box::new(ty), mutbl }),
Pinnedness::Pinned => TyKind::PinnedRef(None, MutTy { ty: Box::new(ty), mutbl }),
})?,
// A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
// when `P` can be reparsed as a type `T`.
PatKind::Slice(pats) if let [pat] = pats.as_slice() => {
pat.to_ty().map(TyKind::Slice)?
PatKind::Slice(ref pats) if let [pat] = pats.as_slice() => {
TyKind::Slice(Box::new(pat.to_ty()?))
}
// A tuple pattern `(P0, .., Pn)` can be reparsed as `(T0, .., Tn)`
// assuming `T0` to `Tn` are all syntactically valid as types.
PatKind::Tuple(pats) => {
PatKind::Tuple(ref pats) => {
let tys = pats.iter().map(|pat| pat.to_ty()).collect::<Option<ThinVec<_>>>()?;
TyKind::Tup(tys)
}
_ => return None,
};

Some(Box::new(Ty { kind, id: self.id, span: self.span, tokens: None }))
Some(Ty { kind, id: self.id, span: self.span, tokens: None })
}

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

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

ExprKind::Paren(expr) => expr.to_ty().map(TyKind::Paren)?,
ExprKind::Paren(expr) => TyKind::Paren(Box::new(expr.to_ty()?)),

ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => {
expr.to_ty().map(|ty| TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }))?
}
ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => expr
.to_ty()
.map(|ty| TyKind::Ref(None, MutTy { ty: Box::new(ty), mutbl: *mutbl }))?,

ExprKind::Repeat(expr, expr_len) => {
expr.to_ty().map(|ty| TyKind::Array(ty, expr_len.clone()))?
expr.to_ty().map(|ty| TyKind::Array(Box::new(ty), expr_len.clone()))?
}

ExprKind::Array(exprs) if let [expr] = exprs.as_slice() => {
expr.to_ty().map(TyKind::Slice)?
TyKind::Slice(Box::new(expr.to_ty()?))
}

ExprKind::Tup(exprs) => {
Expand All @@ -1542,7 +1541,7 @@ impl Expr {
_ => return None,
};

Some(Box::new(Ty { kind, id: self.id, span: self.span, tokens: None }))
Some(Ty { kind, id: self.id, span: self.span, tokens: None })
}

pub fn precedence(&self) -> ExprPrecedence {
Expand Down Expand Up @@ -1978,7 +1977,7 @@ pub enum UnsafeBinderCastKind {
/// ```
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct QSelf {
pub ty: Box<Ty>,
pub ty: Ty,

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

impl From<Box<Ty>> for Term {
fn from(v: Box<Ty>) -> Self {
Term::Ty(v)
}
}

impl From<AnonConst> for Term {
fn from(v: AnonConst) -> Self {
Term::Const(v)
}
}

/// The kind of [associated item constraint][AssocItemConstraint].
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub enum AssocItemConstraintKind {
Expand Down Expand Up @@ -2496,7 +2483,7 @@ pub struct FnPtrTy {
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct UnsafeBinderTy {
pub generic_params: ThinVec<GenericParam>,
pub inner_ty: Box<Ty>,
pub inner_ty: Ty,
}

/// The various kinds of type recognized by the compiler.
Expand All @@ -2523,7 +2510,7 @@ pub enum TyKind {
/// The never type (`!`).
Never,
/// A tuple (`(A, B, C, D,...)`).
Tup(ThinVec<Box<Ty>>),
Tup(ThinVec<Ty>),
/// A path (`module::module::...::Type`), optionally
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
///
Expand Down Expand Up @@ -2907,7 +2894,7 @@ pub struct InlineAsm {
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct Param {
pub attrs: AttrVec,
pub ty: Box<Ty>,
pub ty: Ty,
pub pat: Box<Pat>,
pub id: NodeId,
pub span: Span,
Expand All @@ -2926,7 +2913,7 @@ pub enum SelfKind {
/// `&'lt pin const self`, `&'lt pin mut self`
Pinned(Option<Lifetime>, Mutability),
/// `self: TYPE`, `mut self: TYPE`
Explicit(Box<Ty>, Mutability),
Explicit(Ty, Mutability),
}

impl SelfKind {
Expand Down Expand Up @@ -2982,32 +2969,32 @@ impl Param {
/// Builds a `Param` object from `ExplicitSelf`.
pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param {
let span = eself.span.to(eself_ident.span);
let infer_ty = Box::new(Ty {
let infer_ty = Ty {
id: DUMMY_NODE_ID,
kind: TyKind::ImplicitSelf,
span: eself_ident.span,
tokens: None,
});
};
let (mutbl, ty) = match eself.node {
SelfKind::Explicit(ty, mutbl) => (mutbl, ty),
SelfKind::Value(mutbl) => (mutbl, infer_ty),
SelfKind::Region(lt, mutbl) => (
Mutability::Not,
Box::new(Ty {
Ty {
id: DUMMY_NODE_ID,
kind: TyKind::Ref(lt, MutTy { ty: infer_ty, mutbl }),
kind: TyKind::Ref(lt, MutTy { ty: Box::new(infer_ty), mutbl }),
span,
tokens: None,
}),
},
),
SelfKind::Pinned(lt, mutbl) => (
mutbl,
Box::new(Ty {
Ty {
id: DUMMY_NODE_ID,
kind: TyKind::PinnedRef(lt, MutTy { ty: infer_ty, mutbl }),
kind: TyKind::PinnedRef(lt, MutTy { ty: Box::new(infer_ty), mutbl }),
span,
tokens: None,
}),
},
),
};
Param {
Expand Down Expand Up @@ -3554,7 +3541,7 @@ pub struct FieldDef {
pub safety: Safety,
pub ident: Option<Ident>,

pub ty: Box<Ty>,
pub ty: Ty,
pub default: Option<AnonConst>,
pub is_placeholder: bool,
}
Expand Down Expand Up @@ -3864,7 +3851,7 @@ pub struct DelegationMac {
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct StaticItem {
pub ident: Ident,
pub ty: Box<Ty>,
pub ty: Ty,
pub safety: Safety,
pub mutability: Mutability,
pub expr: Option<Box<Expr>>,
Expand All @@ -3876,7 +3863,7 @@ pub struct ConstItem {
pub defaultness: Defaultness,
pub ident: Ident,
pub generics: Generics,
pub ty: Box<Ty>,
pub ty: Ty,
pub rhs_kind: ConstItemRhsKind,
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
}
Expand Down Expand Up @@ -4262,7 +4249,7 @@ mod size_asserts {
static_assert_size!(LitKind, 24);
static_assert_size!(Local, 96);
static_assert_size!(MetaItemLit, 40);
static_assert_size!(Param, 40);
static_assert_size!(Param, 96);
static_assert_size!(Pat, 80);
static_assert_size!(PatKind, 56);
static_assert_size!(Path, 24);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/expand/autodiff_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn valid_ret_activity(mode: DiffMode, activity: DiffActivity) -> bool {
/// since Duplicated expects a mutable ref/ptr and we would thus end up with a shadow value
/// who is an indirect type, which doesn't match the primal scalar type. We can't prevent
/// users here from marking scalars as Duplicated, due to type aliases.
pub fn valid_ty_for_activity(ty: &Box<Ty>, activity: DiffActivity) -> bool {
pub fn valid_ty_for_activity(ty: &Ty, activity: DiffActivity) -> bool {
use DiffActivity::*;
// It's always allowed to mark something as Const, since we won't compute derivatives wrt. it.
// Dual variants also support all types.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ macro_rules! common_visitor_and_walkers {
ThinVec<PathSegment>,
ThinVec<PreciseCapturingArg>,
ThinVec<Pat>,
ThinVec<Box<Ty>>,
ThinVec<Ty>,
ThinVec<TyPat>,
ThinVec<EiiImpl>,
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1911,7 +1911,7 @@ fn deny_equality_constraints(
ident: *ident,
gen_args,
kind: AssocItemConstraintKind::Equality {
term: predicate.rhs_ty.clone().into(),
term: Term::Ty(predicate.rhs_ty.clone()),
},
span: ident.span,
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/alloc_error_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span

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

let never = ast::FnRetTy::Ty(cx.ty(span, TyKind::Never));
let never = ast::FnRetTy::Ty(Box::new(cx.ty(span, TyKind::Never)));
let params = thin_vec![cx.param(span, size, ty_usize.clone()), cx.param(span, align, ty_usize)];
let decl = cx.fn_decl(params, never);
let header = FnHeader { safety: Safety::Unsafe(span), ..FnHeader::default() };
Expand Down
25 changes: 10 additions & 15 deletions compiler/rustc_builtin_macros/src/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ mod llvm_enzyme {
.filter_map(|p| match &p.kind {
GenericParamKind::Type { .. } => {
let path = ast::Path::from_ident(p.ident);
let ty = ecx.ty_path(path);
let ty = Box::new(ecx.ty_path(path));
Some(AngleBracketedArg::Arg(GenericArg::Type(ty)))
}
GenericParamKind::Const { .. } => {
Expand Down Expand Up @@ -682,7 +682,7 @@ mod llvm_enzyme {
for i in 0..x.width {
let mut shadow_arg = arg.clone();
// We += into the shadow in reverse mode.
shadow_arg.ty = Box::new(assure_mut_ref(&arg.ty));
shadow_arg.ty = assure_mut_ref(&arg.ty);
let old_name = if let PatKind::Ident(_, ident, _) = arg.pat.kind {
ident.name
} else {
Expand Down Expand Up @@ -758,7 +758,7 @@ mod llvm_enzyme {
match x.ret_activity {
DiffActivity::Active | DiffActivity::ActiveOnly => {
let ty = match d_decl.output {
FnRetTy::Ty(ref ty) => ty.clone(),
FnRetTy::Ty(ref ty) => (&**ty).clone(),
FnRetTy::Default(span) => {
panic!("Did not expect Default ret ty: {:?}", span);
}
Expand Down Expand Up @@ -788,17 +788,12 @@ mod llvm_enzyme {

if x.mode.is_fwd() {
let ty = match d_decl.output {
FnRetTy::Ty(ref ty) => ty.clone(),
FnRetTy::Ty(ref ty) => (&**ty).clone(),
FnRetTy::Default(span) => {
// We want to return std::hint::black_box(()).
let kind = TyKind::Tup(ThinVec::new());
let ty = Box::new(rustc_ast::Ty {
kind,
id: ast::DUMMY_NODE_ID,
span,
tokens: None,
});
d_decl.output = FnRetTy::Ty(ty.clone());
let ty = rustc_ast::Ty { kind, id: ast::DUMMY_NODE_ID, span, tokens: None };
d_decl.output = FnRetTy::Ty(Box::new(ty.clone()));
assert!(matches!(x.ret_activity, DiffActivity::None));
// this won't be used below, so any type would be fine.
ty
Expand All @@ -817,7 +812,7 @@ mod llvm_enzyme {
value: ecx.expr_usize(span, 1 + x.width as usize),
mgca_disambiguation: MgcaDisambiguation::Direct,
};
TyKind::Array(ty.clone(), anon_const)
TyKind::Array(Box::new(ty.clone()), anon_const)
};
let ty = Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None });
d_decl.output = FnRetTy::Ty(ty);
Expand All @@ -832,7 +827,7 @@ mod llvm_enzyme {
value: ecx.expr_usize(span, x.width as usize),
mgca_disambiguation: MgcaDisambiguation::Direct,
};
let kind = TyKind::Array(ty.clone(), anon_const);
let kind = TyKind::Array(Box::new(ty.clone()), anon_const);
let ty =
Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None });
d_decl.output = FnRetTy::Ty(ty);
Expand All @@ -853,14 +848,14 @@ mod llvm_enzyme {
let ret_ty = match d_decl.output {
FnRetTy::Ty(ref ty) => {
if !active_only_ret {
act_ret.insert(0, ty.clone());
act_ret.insert(0, (&**ty).clone());
}
let kind = TyKind::Tup(act_ret);
Box::new(rustc_ast::Ty { kind, id: ty.id, span: ty.span, tokens: None })
}
FnRetTy::Default(span) => {
if act_ret.len() == 1 {
act_ret[0].clone()
Box::new(act_ret[0].clone())
} else {
let kind = TyKind::Tup(act_ret.iter().map(|arg| arg.clone()).collect());
Box::new(rustc_ast::Ty { kind, id: ast::DUMMY_NODE_ID, span, tokens: None })
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn cs_clone_simple(
super::assert_ty_bounds(
cx,
&mut stmts,
field.ty.clone(),
Box::new(field.ty.clone()),
field.span,
&[sym::clone, sym::AssertParamIsClone],
);
Expand All @@ -154,7 +154,8 @@ fn cs_clone_simple(
if is_union {
// Just a single assertion for unions, that the union impls `Copy`.
// let _: AssertParamIsCopy<Self>;
let self_ty = cx.ty_path(cx.path_ident(trait_span, Ident::with_dummy_span(kw::SelfUpper)));
let self_ty =
Box::new(cx.ty_path(cx.path_ident(trait_span, Ident::with_dummy_span(kw::SelfUpper))));
super::assert_ty_bounds(
cx,
&mut stmts,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn cs_total_eq_assert(
super::assert_ty_bounds(
cx,
&mut stmts,
field.ty.clone(),
Box::new(field.ty.clone()),
field.span,
&[sym::cmp, sym::AssertParamIsEq],
);
Expand Down
Loading
Loading