Skip to content
Open
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
51 changes: 28 additions & 23 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,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 @@ -366,8 +366,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 @@ -637,7 +636,7 @@ 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>> {
pub fn to_ty(&self) -> Option<Ty> {
let kind = match &self.kind {
PatKind::Missing => unreachable!(),
// In a type expression `_` is an inference variable.
Expand All @@ -649,13 +648,13 @@ impl Pat {
PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
PatKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
// `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
PatKind::Ref(pat, mutbl) => {
pat.to_ty().map(|ty| TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }))?
}
PatKind::Ref(pat, mutbl) => pat
.to_ty()
.map(|ty| TyKind::Ref(None, MutTy { ty: Box::new(ty), mutbl: *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)?
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.
Expand All @@ -670,7 +669,7 @@ impl Pat {
_ => 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 @@ -870,11 +869,11 @@ pub enum PatKind {
Struct(Option<Box<QSelf>>, Path, ThinVec<PatField>, PatFieldsRest),

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

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

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

/// A tuple pattern (`(a, b)`).
Tuple(ThinVec<Box<Pat>>),
Tuple(ThinVec<Pat>),

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

/// A slice pattern `[a, b, c]`.
Slice(ThinVec<Box<Pat>>),
Slice(ThinVec<Pat>),

/// A rest pattern `..`.
///
Expand Down Expand Up @@ -1468,24 +1467,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 @@ -1510,7 +1509,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 @@ -2309,6 +2308,12 @@ pub enum Term {
Const(AnonConst),
}

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

impl From<Box<Ty>> for Term {
fn from(v: Box<Ty>) -> Self {
Term::Ty(v)
Expand Down Expand Up @@ -2423,7 +2428,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 @@ -2532,7 +2537,7 @@ pub enum TyPatKind {
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
Range(Option<Box<AnonConst>>, Option<Box<AnonConst>>, Spanned<RangeEnd>),

Or(ThinVec<Box<TyPat>>),
Or(ThinVec<TyPat>),

/// Placeholder for a pattern that wasn't syntactically well formed in some way.
Err(ErrorGuaranteed),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ macro_rules! common_visitor_and_walkers {
ThinVec<(NodeId, Path)>,
ThinVec<PathSegment>,
ThinVec<PreciseCapturingArg>,
ThinVec<Box<Pat>>,
ThinVec<Box<Ty>>,
ThinVec<Box<TyPat>>,
ThinVec<Pat>,
ThinVec<Ty>,
ThinVec<TyPat>,
);

// This macro generates `impl Visitable` and `impl MutVisitable` that forward to `Walkable`
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

fn lower_pat_tuple(
&mut self,
pats: &[Box<Pat>],
pats: &[Pat],
ctx: &str,
) -> (&'hir [hir::Pat<'hir>], hir::DotDotPos) {
let mut elems = Vec::with_capacity(pats.len());
Expand Down Expand Up @@ -209,7 +209,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
/// When encountering `($binding_mode $ident @)? ..` (`slice`),
/// this is interpreted as a sub-slice pattern semantically.
/// Patterns that follow, which are not like `slice` -- or an error occurs, are in `after`.
fn lower_pat_slice(&mut self, pats: &[Box<Pat>]) -> hir::PatKind<'hir> {
fn lower_pat_slice(&mut self, pats: &[Pat]) -> hir::PatKind<'hir> {
let mut before = Vec::new();
let mut after = Vec::new();
let mut slice = None;
Expand Down
21 changes: 8 additions & 13 deletions compiler/rustc_builtin_macros/src/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ mod llvm_enzyme {
d_inputs.push(arg.clone());
match activity {
DiffActivity::Active => {
act_ret.push(arg.ty.clone());
act_ret.push((&*arg.ty).clone());
// if width =/= 1, then push [arg.ty; width] to act_ret
}
DiffActivity::ActiveOnly => {
Expand Down Expand Up @@ -786,17 +786,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 @@ -814,7 +809,7 @@ mod llvm_enzyme {
id: ast::DUMMY_NODE_ID,
value: ecx.expr_usize(span, 1 + x.width as usize),
};
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 @@ -828,7 +823,7 @@ mod llvm_enzyme {
id: ast::DUMMY_NODE_ID,
value: ecx.expr_usize(span, x.width as usize),
};
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 @@ -849,14 +844,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
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ impl<'a> TraitDef<'a> {
struct_def: &'a VariantData,
prefixes: &[String],
by_ref: ByRef,
) -> ThinVec<Box<ast::Pat>> {
) -> ThinVec<ast::Pat> {
prefixes
.iter()
.map(|prefix| {
Expand Down Expand Up @@ -1543,7 +1543,7 @@ impl<'a> TraitDef<'a> {
attrs: ast::AttrVec::new(),
id: ast::DUMMY_NODE_ID,
span: pat.span.with_ctxt(self.span.ctxt()),
pat,
pat: Box::new(pat),
is_placeholder: false,
}
})
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_builtin_macros/src/pattern_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn parse_pat_ty<'a>(

let pat = pat_to_ty_pat(
cx,
*parser.parse_pat_no_top_guard(
parser.parse_pat_no_top_guard(
None,
RecoverComma::No,
RecoverColon::No,
Expand All @@ -44,22 +44,22 @@ fn parse_pat_ty<'a>(
parser.unexpected()?;
}

Ok((ty, pat))
Ok((ty, Box::new(pat)))
}

fn ty_pat(kind: TyPatKind, span: Span) -> Box<TyPat> {
Box::new(TyPat { id: DUMMY_NODE_ID, kind, span, tokens: None })
fn ty_pat(kind: TyPatKind, span: Span) -> TyPat {
TyPat { id: DUMMY_NODE_ID, kind, span, tokens: None }
}

fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> Box<TyPat> {
fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> TyPat {
let kind = match pat.kind {
ast::PatKind::Range(start, end, include_end) => TyPatKind::Range(
start.map(|value| Box::new(AnonConst { id: DUMMY_NODE_ID, value })),
end.map(|value| Box::new(AnonConst { id: DUMMY_NODE_ID, value })),
include_end,
),
ast::PatKind::Or(variants) => {
TyPatKind::Or(variants.into_iter().map(|pat| pat_to_ty_pat(cx, *pat)).collect())
TyPatKind::Or(variants.into_iter().map(|pat| pat_to_ty_pat(cx, pat)).collect())
}
ast::PatKind::Err(guar) => TyPatKind::Err(guar),
_ => TyPatKind::Err(cx.dcx().span_err(pat.span, "pattern not supported in pattern types")),
Expand Down
Loading
Loading