Skip to content

Commit 86ded88

Browse files
committed
Box ExprKind::Closure, ExprKind::MethodCall, and QSelf in expressions, types, and patterns.
1 parent a37499a commit 86ded88

File tree

37 files changed

+411
-318
lines changed

37 files changed

+411
-318
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,10 @@ pub enum PatKind {
720720

721721
/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
722722
/// The `bool` is `true` in the presence of a `..`.
723-
Struct(Option<QSelf>, Path, Vec<PatField>, /* recovered */ bool),
723+
Struct(Option<P<QSelf>>, Path, Vec<PatField>, /* recovered */ bool),
724724

725725
/// A tuple struct/variant pattern (`Variant(x, y, .., z)`).
726-
TupleStruct(Option<QSelf>, Path, Vec<P<Pat>>),
726+
TupleStruct(Option<P<QSelf>>, Path, Vec<P<Pat>>),
727727

728728
/// An or-pattern `A | B | C`.
729729
/// Invariant: `pats.len() >= 2`.
@@ -733,7 +733,7 @@ pub enum PatKind {
733733
/// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
734734
/// or associated constants. Qualified path patterns `<A>::B::C`/`<A as Trait>::B::C` can
735735
/// only legally refer to associated constants.
736-
Path(Option<QSelf>, Path),
736+
Path(Option<P<QSelf>>, Path),
737737

738738
/// A tuple pattern (`(a, b)`).
739739
Tuple(Vec<P<Pat>>),
@@ -1292,6 +1292,18 @@ impl Expr {
12921292
}
12931293
}
12941294

1295+
#[derive(Clone, Encodable, Decodable, Debug)]
1296+
pub struct Closure {
1297+
pub binder: ClosureBinder,
1298+
pub capture_clause: CaptureBy,
1299+
pub asyncness: Async,
1300+
pub movability: Movability,
1301+
pub fn_decl: P<FnDecl>,
1302+
pub body: P<Expr>,
1303+
/// The span of the argument block `|...|`.
1304+
pub fn_decl_span: Span,
1305+
}
1306+
12951307
/// Limit types of a range (inclusive or exclusive)
12961308
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug)]
12971309
pub enum RangeLimits {
@@ -1301,6 +1313,20 @@ pub enum RangeLimits {
13011313
Closed,
13021314
}
13031315

1316+
/// A method call (e.g. `x.foo::<Bar, Baz>(a, b, c)`).
1317+
#[derive(Clone, Encodable, Decodable, Debug)]
1318+
pub struct MethodCall {
1319+
/// The method name and its generic arguments, e.g. `foo::<Bar, Baz>`.
1320+
pub seg: PathSegment,
1321+
/// The receiver, e.g. `x`.
1322+
pub receiver: P<Expr>,
1323+
/// The arguments, e.g. `a, b, c`.
1324+
pub args: Vec<P<Expr>>,
1325+
/// The span of the function, without the dot and receiver e.g. `foo::<Bar,
1326+
/// Baz>(a, b, c)`.
1327+
pub span: Span,
1328+
}
1329+
13041330
#[derive(Clone, Encodable, Decodable, Debug)]
13051331
pub enum StructRest {
13061332
/// `..x`.
@@ -1313,7 +1339,7 @@ pub enum StructRest {
13131339

13141340
#[derive(Clone, Encodable, Decodable, Debug)]
13151341
pub struct StructExpr {
1316-
pub qself: Option<QSelf>,
1342+
pub qself: Option<P<QSelf>>,
13171343
pub path: Path,
13181344
pub fields: Vec<ExprField>,
13191345
pub rest: StructRest,
@@ -1334,17 +1360,8 @@ pub enum ExprKind {
13341360
/// This also represents calling the constructor of
13351361
/// tuple-like ADTs such as tuple structs and enum variants.
13361362
Call(P<Expr>, Vec<P<Expr>>),
1337-
/// A method call (`x.foo::<'static, Bar, Baz>(a, b, c, d)`)
1338-
///
1339-
/// The `PathSegment` represents the method name and its generic arguments
1340-
/// (within the angle brackets).
1341-
/// The standalone `Expr` is the receiver expression.
1342-
/// The vector of `Expr` is the arguments.
1343-
/// `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
1344-
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, x, [a, b, c, d])`.
1345-
/// This `Span` is the span of the function, without the dot and receiver
1346-
/// (e.g. `foo(a, b)` in `x.foo(a, b)`
1347-
MethodCall(PathSegment, P<Expr>, Vec<P<Expr>>, Span),
1363+
/// A method call (e.g. `x.foo::<Bar, Baz>(a, b, c)`).
1364+
MethodCall(Box<MethodCall>),
13481365
/// A tuple (e.g., `(a, b, c, d)`).
13491366
Tup(Vec<P<Expr>>),
13501367
/// A binary operation (e.g., `a + b`, `a * b`).
@@ -1383,9 +1400,7 @@ pub enum ExprKind {
13831400
/// A `match` block.
13841401
Match(P<Expr>, Vec<Arm>),
13851402
/// A closure (e.g., `move |a, b, c| a + b + c`).
1386-
///
1387-
/// The final span is the span of the argument block `|...|`.
1388-
Closure(ClosureBinder, CaptureBy, Async, Movability, P<FnDecl>, P<Expr>, Span),
1403+
Closure(Box<Closure>),
13891404
/// A block (`'label: { ... }`).
13901405
Block(P<Block>, Option<Label>),
13911406
/// An async block (`async move { ... }`).
@@ -1423,7 +1438,7 @@ pub enum ExprKind {
14231438
/// parameters (e.g., `foo::bar::<baz>`).
14241439
///
14251440
/// Optionally "qualified" (e.g., `<Vec<T> as SomeTrait>::SomeType`).
1426-
Path(Option<QSelf>, Path),
1441+
Path(Option<P<QSelf>>, Path),
14271442

14281443
/// A referencing operation (`&a`, `&mut a`, `&raw const a` or `&raw mut a`).
14291444
AddrOf(BorrowKind, Mutability, P<Expr>),
@@ -2022,7 +2037,7 @@ pub enum TyKind {
20222037
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
20232038
///
20242039
/// Type parameters are stored in the `Path` itself.
2025-
Path(Option<QSelf>, Path),
2040+
Path(Option<P<QSelf>>, Path),
20262041
/// A trait object type `Bound1 + Bound2 + Bound3`
20272042
/// where `Bound` is a trait or a lifetime.
20282043
TraitObject(GenericBounds, TraitObjectSyntax),
@@ -2151,7 +2166,7 @@ impl InlineAsmTemplatePiece {
21512166
#[derive(Clone, Encodable, Decodable, Debug)]
21522167
pub struct InlineAsmSym {
21532168
pub id: NodeId,
2154-
pub qself: Option<QSelf>,
2169+
pub qself: Option<P<QSelf>>,
21552170
pub path: Path,
21562171
}
21572172

@@ -3044,8 +3059,10 @@ mod size_asserts {
30443059
static_assert_size!(AssocItemKind, 32);
30453060
static_assert_size!(Attribute, 32);
30463061
static_assert_size!(Block, 48);
3047-
static_assert_size!(Expr, 104);
3048-
static_assert_size!(ExprKind, 72);
3062+
#[cfg(not(bootstrap))]
3063+
static_assert_size!(Expr, 88);
3064+
#[cfg(not(bootstrap))]
3065+
static_assert_size!(ExprKind, 56);
30493066
#[cfg(not(bootstrap))]
30503067
static_assert_size!(Fn, 184);
30513068
static_assert_size!(ForeignItem, 96);
@@ -3060,12 +3077,12 @@ mod size_asserts {
30603077
static_assert_size!(LitKind, 24);
30613078
static_assert_size!(Local, 72);
30623079
static_assert_size!(Param, 40);
3063-
static_assert_size!(Pat, 120);
3064-
static_assert_size!(PatKind, 96);
3080+
static_assert_size!(Pat, 104);
3081+
static_assert_size!(PatKind, 80);
30653082
static_assert_size!(Path, 40);
30663083
static_assert_size!(PathSegment, 24);
30673084
static_assert_size!(Stmt, 32);
30683085
static_assert_size!(StmtKind, 16);
3069-
static_assert_size!(Ty, 96);
3070-
static_assert_size!(TyKind, 72);
3086+
static_assert_size!(Ty, 80);
3087+
static_assert_size!(TyKind, 56);
30713088
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub trait MutVisitor: Sized {
188188
noop_visit_path(p, self);
189189
}
190190

191-
fn visit_qself(&mut self, qs: &mut Option<QSelf>) {
191+
fn visit_qself(&mut self, qs: &mut Option<P<QSelf>>) {
192192
noop_visit_qself(qs, self);
193193
}
194194

@@ -523,8 +523,9 @@ pub fn noop_visit_path<T: MutVisitor>(Path { segments, span, tokens }: &mut Path
523523
visit_lazy_tts(tokens, vis);
524524
}
525525

526-
pub fn noop_visit_qself<T: MutVisitor>(qself: &mut Option<QSelf>, vis: &mut T) {
527-
visit_opt(qself, |QSelf { ty, path_span, position: _ }| {
526+
pub fn noop_visit_qself<T: MutVisitor>(qself: &mut Option<P<QSelf>>, vis: &mut T) {
527+
visit_opt(qself, |qself| {
528+
let QSelf { ty, path_span, position: _ } = &mut **qself;
528529
vis.visit_ty(ty);
529530
vis.visit_span(path_span);
530531
})
@@ -1297,12 +1298,17 @@ pub fn noop_visit_expr<T: MutVisitor>(
12971298
vis.visit_expr(f);
12981299
visit_exprs(args, vis);
12991300
}
1300-
ExprKind::MethodCall(PathSegment { ident, id, args }, receiver, exprs, span) => {
1301+
ExprKind::MethodCall(box MethodCall {
1302+
seg: PathSegment { ident, id, args: seg_args },
1303+
receiver,
1304+
args: call_args,
1305+
span,
1306+
}) => {
13011307
vis.visit_ident(ident);
13021308
vis.visit_id(id);
1303-
visit_opt(args, |args| vis.visit_generic_args(args));
1309+
visit_opt(seg_args, |args| vis.visit_generic_args(args));
13041310
vis.visit_expr(receiver);
1305-
visit_exprs(exprs, vis);
1311+
visit_exprs(call_args, vis);
13061312
vis.visit_span(span);
13071313
}
13081314
ExprKind::Binary(_binop, lhs, rhs) => {
@@ -1347,12 +1353,20 @@ pub fn noop_visit_expr<T: MutVisitor>(
13471353
vis.visit_expr(expr);
13481354
arms.flat_map_in_place(|arm| vis.flat_map_arm(arm));
13491355
}
1350-
ExprKind::Closure(binder, _capture_by, asyncness, _movability, decl, body, span) => {
1356+
ExprKind::Closure(box Closure {
1357+
binder,
1358+
capture_clause: _,
1359+
asyncness,
1360+
movability: _,
1361+
fn_decl,
1362+
body,
1363+
fn_decl_span,
1364+
}) => {
13511365
vis.visit_closure_binder(binder);
13521366
vis.visit_asyncness(asyncness);
1353-
vis.visit_fn_decl(decl);
1367+
vis.visit_fn_decl(fn_decl);
13541368
vis.visit_expr(body);
1355-
vis.visit_span(span);
1369+
vis.visit_span(fn_decl_span);
13561370
}
13571371
ExprKind::Block(blk, label) => {
13581372
vis.visit_block(blk);

compiler/rustc_ast/src/util/classify.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
3636
| Binary(_, _, e)
3737
| Box(e)
3838
| Break(_, Some(e))
39-
| Closure(.., e, _)
4039
| Let(_, e, _)
4140
| Range(_, Some(e), _)
4241
| Ret(Some(e))
4342
| Unary(_, e)
4443
| Yield(Some(e)) => {
4544
expr = e;
4645
}
46+
Closure(closure) => {
47+
expr = &closure.body;
48+
}
4749
Async(..) | Block(..) | ForLoop(..) | If(..) | Loop(..) | Match(..) | Struct(..)
4850
| TryBlock(..) | While(..) => break Some(expr),
4951
_ => break None,

compiler/rustc_ast/src/util/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ pub fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
396396
contains_exterior_struct_lit(&x)
397397
}
398398

399-
ast::ExprKind::MethodCall(_, ref receiver, _, _) => {
399+
ast::ExprKind::MethodCall(box ast::MethodCall { ref receiver, .. }) => {
400400
// X { y: 1 }.bar(...)
401401
contains_exterior_struct_lit(&receiver)
402402
}

compiler/rustc_ast/src/visit.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,10 +795,10 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
795795
visitor.visit_expr(callee_expression);
796796
walk_list!(visitor, visit_expr, arguments);
797797
}
798-
ExprKind::MethodCall(ref segment, ref receiver, ref arguments, _span) => {
799-
visitor.visit_path_segment(segment);
798+
ExprKind::MethodCall(box MethodCall { ref seg, ref receiver, ref args, span: _ }) => {
799+
visitor.visit_path_segment(seg);
800800
visitor.visit_expr(receiver);
801-
walk_list!(visitor, visit_expr, arguments);
801+
walk_list!(visitor, visit_expr, args);
802802
}
803803
ExprKind::Binary(_, ref left_expression, ref right_expression) => {
804804
visitor.visit_expr(left_expression);
@@ -839,8 +839,16 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
839839
visitor.visit_expr(subexpression);
840840
walk_list!(visitor, visit_arm, arms);
841841
}
842-
ExprKind::Closure(ref binder, _, _, _, ref decl, ref body, _decl_span) => {
843-
visitor.visit_fn(FnKind::Closure(binder, decl, body), expression.span, expression.id)
842+
ExprKind::Closure(box Closure {
843+
ref binder,
844+
capture_clause: _,
845+
asyncness: _,
846+
movability: _,
847+
ref fn_decl,
848+
ref body,
849+
fn_decl_span: _,
850+
}) => {
851+
visitor.visit_fn(FnKind::Closure(binder, fn_decl, body), expression.span, expression.id)
844852
}
845853
ExprKind::Block(ref block, ref opt_label) => {
846854
walk_list!(visitor, visit_label, opt_label);

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
6060
hir::ExprKind::Call(f, self.lower_exprs(args))
6161
}
6262
}
63-
ExprKind::MethodCall(ref seg, ref receiver, ref args, span) => {
63+
ExprKind::MethodCall(box MethodCall { ref seg, ref receiver, ref args, span }) => {
6464
let hir_seg = self.arena.alloc(self.lower_path_segment(
6565
e.span,
6666
seg,
@@ -160,22 +160,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
160160
};
161161
self.lower_expr_await(dot_await_span, expr)
162162
}
163-
ExprKind::Closure(
163+
ExprKind::Closure(box Closure {
164164
ref binder,
165165
capture_clause,
166166
asyncness,
167167
movability,
168-
ref decl,
168+
ref fn_decl,
169169
ref body,
170170
fn_decl_span,
171-
) => {
171+
}) => {
172172
if let Async::Yes { closure_id, .. } = asyncness {
173173
self.lower_expr_async_closure(
174174
binder,
175175
capture_clause,
176176
e.id,
177177
closure_id,
178-
decl,
178+
fn_decl,
179179
body,
180180
fn_decl_span,
181181
)
@@ -185,7 +185,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
185185
capture_clause,
186186
e.id,
187187
movability,
188-
decl,
188+
fn_decl,
189189
body,
190190
fn_decl_span,
191191
)
@@ -1040,7 +1040,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10401040
fn extract_tuple_struct_path<'a>(
10411041
&mut self,
10421042
expr: &'a Expr,
1043-
) -> Option<(&'a Option<QSelf>, &'a Path)> {
1043+
) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
10441044
if let ExprKind::Path(qself, path) = &expr.kind {
10451045
// Does the path resolve to something disallowed in a tuple struct/variant pattern?
10461046
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
@@ -1062,7 +1062,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10621062
fn extract_unit_struct_path<'a>(
10631063
&mut self,
10641064
expr: &'a Expr,
1065-
) -> Option<(&'a Option<QSelf>, &'a Path)> {
1065+
) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
10661066
if let ExprKind::Path(qself, path) = &expr.kind {
10671067
// Does the path resolve to something disallowed in a unit struct/variant pattern?
10681068
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11891189
fn lower_path_ty(
11901190
&mut self,
11911191
t: &Ty,
1192-
qself: &Option<QSelf>,
1192+
qself: &Option<ptr::P<QSelf>>,
11931193
path: &Path,
11941194
param_mode: ParamMode,
11951195
itctx: &ImplTraitContext,

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1919
pub(crate) fn lower_qpath(
2020
&mut self,
2121
id: NodeId,
22-
qself: &Option<QSelf>,
22+
qself: &Option<ptr::P<QSelf>>,
2323
p: &Path,
2424
param_mode: ParamMode,
2525
itctx: &ImplTraitContext,

0 commit comments

Comments
 (0)