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
100 changes: 59 additions & 41 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub struct Path {
}

// Succeeds if the path has a single segment that is arg-free and matches the given symbol.
impl PartialEq<Symbol> for Path {
impl PartialEq<Symbol> for Box<Path> {
#[inline]
fn eq(&self, name: &Symbol) -> bool {
if let [segment] = self.segments.as_ref()
Expand All @@ -115,7 +115,7 @@ impl PartialEq<Symbol> for Path {
}

// Succeeds if the path has segments that are arg-free and match the given symbols.
impl PartialEq<&[Symbol]> for Path {
impl PartialEq<&[Symbol]> for Box<Path> {
#[inline]
fn eq(&self, names: &&[Symbol]) -> bool {
self.segments.iter().eq(*names)
Expand All @@ -134,8 +134,12 @@ impl StableHash for Path {
impl Path {
/// Convert a span and an identifier to the corresponding
/// one-segment path.
pub fn from_ident(ident: Ident) -> Path {
Path { segments: thin_vec![PathSegment::from_ident(ident)], span: ident.span, tokens: None }
pub fn from_ident(ident: Ident) -> Box<Path> {
Box::new(Path {
segments: thin_vec![PathSegment::from_ident(ident)],
span: ident.span,
tokens: None,
})
}

pub fn is_global(&self) -> bool {
Expand Down Expand Up @@ -407,7 +411,7 @@ impl GenericBound {
}
}

pub type GenericBounds = Vec<GenericBound>;
pub type GenericBounds = ThinVec<GenericBound>;

/// Specifies the enforced ordering for generic parameters. In the future,
/// if we wanted to relax this order, we could override `PartialEq` and
Expand Down Expand Up @@ -567,7 +571,7 @@ pub struct Crate {
#[derive(Clone, Encodable, Decodable, Debug, StableHash)]
pub struct MetaItem {
pub unsafety: Safety,
pub path: Path,
pub path: Box<Path>,
pub kind: MetaItemKind,
pub span: Span,
}
Expand Down Expand Up @@ -884,10 +888,10 @@ pub enum PatKind {
Ident(BindingMode, Ident, Option<Box<Pat>>),

/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
Struct(Option<Box<QSelf>>, Path, ThinVec<PatField>, PatFieldsRest),
Struct(Option<Box<QSelf>>, Box<Path>, ThinVec<PatField>, PatFieldsRest),

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

/// An or-pattern `A | B | C`.
/// Invariant: `pats.len() >= 2`.
Expand All @@ -897,7 +901,7 @@ pub enum PatKind {
/// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
/// or associated constants. Qualified path patterns `<A>::B::C`/`<A as Trait>::B::C` can
/// only legally refer to associated constants.
Path(Option<Box<QSelf>>, Path),
Path(Option<Box<QSelf>>, Box<Path>),

/// A tuple pattern (`(a, b)`).
Tuple(ThinVec<Pat>),
Expand Down Expand Up @@ -1534,7 +1538,7 @@ impl Expr {
let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) else {
return None;
};
TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
TyKind::TraitObject(thin_vec![lhs, rhs], TraitObjectSyntax::None)
}

ExprKind::Underscore => TyKind::Infer,
Expand Down Expand Up @@ -1667,6 +1671,15 @@ impl From<Box<Expr>> for Expr {
}
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct ForLoop {
pub pat: Box<Pat>,
pub iter: Box<Expr>,
pub body: Box<Block>,
pub label: Option<Label>,
pub kind: ForLoopKind,
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct Closure {
pub binder: ClosureBinder,
Expand Down Expand Up @@ -1733,7 +1746,7 @@ pub enum StructRest {
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct StructExpr {
pub qself: Option<Box<QSelf>>,
pub path: Path,
pub path: Box<Path>,
pub fields: ThinVec<ExprField>,
pub rest: StructRest,
}
Expand Down Expand Up @@ -1792,13 +1805,7 @@ pub enum ExprKind {
/// `'label: for await? pat in iter { block }`
///
/// This is desugared to a combination of `loop` and `match` expressions.
ForLoop {
pat: Box<Pat>,
iter: Box<Expr>,
body: Box<Block>,
label: Option<Label>,
kind: ForLoopKind,
},
ForLoop(Box<ForLoop>),
/// Conditionless loop (can be exited with `break`, `continue`, or `return`).
///
/// `'label: loop { block }`
Expand Down Expand Up @@ -1850,7 +1857,7 @@ pub enum ExprKind {
/// parameters (e.g., `foo::bar::<baz>`).
///
/// Optionally "qualified" (e.g., `<Vec<T> as SomeTrait>::SomeType`).
Path(Option<Box<QSelf>>, Path),
Path(Option<Box<QSelf>>, Box<Path>),

/// A referencing operation (`&a`, `&mut a`, `&raw const a` or `&raw mut a`).
AddrOf(BorrowKind, Mutability, Box<Expr>),
Expand All @@ -1868,9 +1875,10 @@ pub enum ExprKind {
///
/// Usually not written directly in user code but
/// indirectly via the macro `core::mem::offset_of!(...)`.
OffsetOf(Box<Ty>, Vec<Ident>),
OffsetOf(Box<Ty>, ThinVec<Ident>),

/// A macro invocation; pre-expansion.
// njn: debox all Box<MacCall>?
MacCall(Box<MacCall>),

/// A struct literal expression.
Expand Down Expand Up @@ -2043,7 +2051,7 @@ pub enum ClosureBinder {
/// is being invoked, and the `args` are arguments passed to it.
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct MacCall {
pub path: Path,
pub path: Box<Path>,
pub args: Box<DelimArgs>,
}

Expand Down Expand Up @@ -2122,7 +2130,7 @@ pub struct MacroDef {
#[derive(Clone, Encodable, Decodable, Debug, StableHash, Walkable)]
pub struct EiiDecl {
/// path to the extern item we're targeting
pub foreign_item: Path,
pub foreign_item: Box<Path>,
pub impl_unsafe: bool,
}

Expand Down Expand Up @@ -2534,7 +2542,7 @@ pub enum TyKind {
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
///
/// Type parameters are stored in the `Path` itself.
Path(Option<Box<QSelf>>, Path),
Path(Option<Box<QSelf>>, Box<Path>),
/// A trait object type `Bound1 + Bound2 + Bound3`
/// where `Bound` is a trait or a lifetime.
TraitObject(#[visitable(extra = BoundKind::TraitObject)] GenericBounds, TraitObjectSyntax),
Expand Down Expand Up @@ -2683,7 +2691,7 @@ pub enum PreciseCapturingArg {
/// Lifetime parameter.
Lifetime(#[visitable(extra = LifetimeCtxt::GenericArg)] Lifetime),
/// Type or const parameter.
Arg(Path, NodeId),
Arg(Box<Path>, NodeId),
}

/// Inline assembly operand explicit register or register class.
Expand Down Expand Up @@ -2810,7 +2818,7 @@ impl InlineAsmTemplatePiece {
pub struct InlineAsmSym {
pub id: NodeId,
pub qself: Option<Box<QSelf>>,
pub path: Path,
pub path: Box<Path>,
}

/// Inline assembly operand.
Expand Down Expand Up @@ -3366,7 +3374,7 @@ pub enum UseTreeKind {
/// Used in `use` items both at top-level and inside of braces in import groups.
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct UseTree {
pub prefix: Path,
pub prefix: Box<Path>,
pub kind: UseTreeKind,
}

Expand Down Expand Up @@ -3464,7 +3472,7 @@ impl NormalAttr {
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct AttrItem {
pub unsafety: Safety,
pub path: Path,
pub path: Box<Path>,
pub args: AttrItemKind,
// Tokens for the meta item, e.g. just the `foo` within `#[foo]` or `#![foo]`.
pub tokens: Option<LazyAttrTokenStream>,
Expand Down Expand Up @@ -3532,7 +3540,7 @@ impl AttrItem {
/// same as the impl's `NodeId`).
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct TraitRef {
pub path: Path,
pub path: Box<Path>,
pub ref_id: NodeId,
}

Expand Down Expand Up @@ -3564,7 +3572,7 @@ pub struct PolyTraitRef {
impl PolyTraitRef {
pub fn new(
generic_params: ThinVec<GenericParam>,
path: Path,
path: Box<Path>,
modifiers: TraitBoundModifiers,
span: Span,
parens: Parens,
Expand Down Expand Up @@ -3886,7 +3894,7 @@ pub struct Fn {
pub generics: Generics,
pub sig: FnSig,
pub contract: Option<Box<FnContract>>,
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
pub define_opaque: Option<ThinVec<(NodeId, Box<Path>)>>,
pub body: Option<Box<Block>>,

/// This function is an implementation of an externally implementable item (EII).
Expand All @@ -3911,7 +3919,7 @@ impl Fn {
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct EiiImpl {
pub node_id: NodeId,
pub eii_macro_path: Path,
pub eii_macro_path: Box<Path>,
/// This field is an implementation detail that prevents a lot of bugs.
/// See <https://github.com/rust-lang/rust/issues/149981> for an example.
///
Expand Down Expand Up @@ -3943,7 +3951,7 @@ pub struct Delegation {
/// Path resolution id.
pub id: NodeId,
pub qself: Option<Box<QSelf>>,
pub path: Path,
pub path: Box<Path>,
pub ident: Ident,
pub rename: Option<Ident>,
pub body: Option<Box<Block>>,
Expand All @@ -3967,7 +3975,7 @@ pub enum DelegationSuffixes {
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct DelegationMac {
pub qself: Option<Box<QSelf>>,
pub prefix: Path,
pub prefix: Box<Path>,
pub suffixes: DelegationSuffixes,
pub body: Option<Box<Block>>,
}
Expand All @@ -3979,7 +3987,7 @@ pub struct StaticItem {
pub safety: Safety,
pub mutability: Mutability,
pub expr: Option<Box<Expr>>,
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
pub define_opaque: Option<ThinVec<(NodeId, Box<Path>)>>,

/// This static is an implementation of an externally implementable item (EII).
/// This means, there was an EII declared somewhere and this static is the
Expand All @@ -3996,7 +4004,7 @@ pub struct ConstItem {
pub generics: Generics,
pub ty: Box<Ty>,
pub rhs_kind: ConstItemRhsKind,
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
pub define_opaque: Option<ThinVec<(NodeId, Box<Path>)>>,
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
Expand Down Expand Up @@ -4391,30 +4399,40 @@ mod size_asserts {
// tidy-alphabetical-start
static_assert_size!(AssocItem, 80);
static_assert_size!(AssocItemKind, 16);
static_assert_size!(AttrKind, 16);
static_assert_size!(Attribute, 32);
static_assert_size!(Block, 32);
static_assert_size!(Expr, 72);
static_assert_size!(ExprKind, 40);
static_assert_size!(Expr, 64);
static_assert_size!(ExprKind, 32);
static_assert_size!(Fn, 192);
static_assert_size!(FnDecl, 24);
static_assert_size!(FnHeader, 76);
static_assert_size!(FnSig, 96);
static_assert_size!(ForeignItem, 80);
static_assert_size!(ForeignItemKind, 16);
static_assert_size!(GenericArg, 24);
static_assert_size!(GenericBound, 88);
static_assert_size!(GenericArgs, 40);
static_assert_size!(GenericBound, 72);
static_assert_size!(GenericParam, 80);
static_assert_size!(Generics, 40);
static_assert_size!(Impl, 80);
static_assert_size!(Item, 152);
static_assert_size!(ItemKind, 88);
static_assert_size!(Lifetime, 16);
static_assert_size!(LitKind, 24);
static_assert_size!(Local, 96);
static_assert_size!(MetaItem, 72);
static_assert_size!(MetaItemKind, 40);
static_assert_size!(MetaItemLit, 40);
static_assert_size!(Param, 40);
static_assert_size!(Pat, 80);
static_assert_size!(PatKind, 56);
static_assert_size!(Pat, 64);
static_assert_size!(PatKind, 40);
static_assert_size!(Path, 24);
static_assert_size!(PathSegment, 24);
static_assert_size!(QSelf, 24);
static_assert_size!(Stmt, 32);
static_assert_size!(StmtKind, 16);
static_assert_size!(TraitImplHeader, 72);
static_assert_size!(TraitImplHeader, 56);
static_assert_size!(Ty, 64);
static_assert_size!(TyKind, 40);
// tidy-alphabetical-end
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ impl MetaItem {
iter.next();
}
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
Path { span, segments, tokens: None }
Box::new(Path { span, segments, tokens: None })
}
Some(TokenTree::Delimited(
_span,
Expand Down Expand Up @@ -741,7 +741,7 @@ fn mk_attr(
g: &AttrIdGenerator,
style: AttrStyle,
unsafety: Safety,
path: Path,
path: Box<Path>,
args: AttrArgs,
span: Span,
) -> Attribute {
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_ast/src/util/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
loop {
match &expr.kind {
Block(_, label) | ForLoop { label, .. } | Loop(_, label, _) | While(_, _, label) => {
Block(_, label)
| ForLoop(ast::ForLoop { label, .. })
| Loop(_, label, _)
| While(_, _, label) => {
return label.is_some();
}

Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,10 @@ macro_rules! common_visitor_and_walkers {
impl_visitable_list!(<$($lt)? $($mut)?>
ThinVec<AngleBracketedArg>,
ThinVec<Attribute>,
ThinVec<GenericBound>,
ThinVec<Ident>,
ThinVec<(Ident, Option<Ident>)>,
ThinVec<(NodeId, Path)>,
ThinVec<(NodeId, Box<Path>)>,
ThinVec<PathSegment>,
ThinVec<PreciseCapturingArg>,
ThinVec<Pat>,
Expand Down Expand Up @@ -999,7 +1001,7 @@ macro_rules! common_visitor_and_walkers {
visit_visitable!($($mut)? vis, head_expression, if_block, optional_else),
ExprKind::While(subexpression, block, opt_label) =>
visit_visitable!($($mut)? vis, subexpression, block, opt_label),
ExprKind::ForLoop { pat, iter, body, label, kind } =>
ExprKind::ForLoop(ForLoop { pat, iter, body, label, kind }) =>
visit_visitable!($($mut)? vis, pat, iter, body, label, kind),
ExprKind::Loop(block, opt_label, span) =>
visit_visitable!($($mut)? vis, block, opt_label, span),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
//
// This also needs special handling because the HirId of the returned `hir::Expr` will not
// correspond to the `e.id`, so `lower_expr_for` handles attribute lowering itself.
ExprKind::ForLoop { pat, iter, body, label, kind } => {
ExprKind::ForLoop(ForLoop { pat, iter, body, label, kind }) => {
return self.lower_expr_for(e, pat, iter, body, *label, *kind);
}
ExprKind::Closure(closure) => return self.lower_expr_closure_expr(e, closure),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
pub(super) fn lower_define_opaque(
&mut self,
hir_id: HirId,
define_opaque: &Option<ThinVec<(NodeId, Path)>>,
define_opaque: &Option<ThinVec<(NodeId, Box<Path>)>>,
) {
assert_eq!(self.define_opaque, None);
assert!(hir_id.is_owner());
Expand Down
Loading
Loading