Skip to content

Commit 6b7ca2f

Browse files
committed
Box ExprKind::{Closure,MethodCall}, and QSelf in expressions, types, and patterns.
1 parent bebd57a commit 6b7ca2f

File tree

37 files changed

+409
-318
lines changed

37 files changed

+409
-318
lines changed

compiler/rustc_ast/src/ast.rs

+42-27
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,10 @@ pub enum PatKind {
718718

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

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

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

736736
/// A tuple pattern (`(a, b)`).
737737
Tuple(Vec<P<Pat>>),
@@ -1272,6 +1272,18 @@ impl Expr {
12721272
}
12731273
}
12741274

1275+
#[derive(Clone, Encodable, Decodable, Debug)]
1276+
pub struct Closure {
1277+
pub binder: ClosureBinder,
1278+
pub capture_clause: CaptureBy,
1279+
pub asyncness: Async,
1280+
pub movability: Movability,
1281+
pub fn_decl: P<FnDecl>,
1282+
pub body: P<Expr>,
1283+
/// The span of the argument block `|...|`.
1284+
pub fn_decl_span: Span,
1285+
}
1286+
12751287
/// Limit types of a range (inclusive or exclusive)
12761288
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug)]
12771289
pub enum RangeLimits {
@@ -1281,6 +1293,20 @@ pub enum RangeLimits {
12811293
Closed,
12821294
}
12831295

1296+
/// A method call (e.g. `x.foo::<Bar, Baz>(a, b, c)`).
1297+
#[derive(Clone, Encodable, Decodable, Debug)]
1298+
pub struct MethodCall {
1299+
/// The method name and its generic arguments, e.g. `foo::<Bar, Baz>`.
1300+
pub seg: PathSegment,
1301+
/// The receiver, e.g. `x`.
1302+
pub receiver: P<Expr>,
1303+
/// The arguments, e.g. `a, b, c`.
1304+
pub args: Vec<P<Expr>>,
1305+
/// The span of the function, without the dot and receiver e.g. `foo::<Bar,
1306+
/// Baz>(a, b, c)`.
1307+
pub span: Span,
1308+
}
1309+
12841310
#[derive(Clone, Encodable, Decodable, Debug)]
12851311
pub enum StructRest {
12861312
/// `..x`.
@@ -1293,7 +1319,7 @@ pub enum StructRest {
12931319

12941320
#[derive(Clone, Encodable, Decodable, Debug)]
12951321
pub struct StructExpr {
1296-
pub qself: Option<QSelf>,
1322+
pub qself: Option<P<QSelf>>,
12971323
pub path: Path,
12981324
pub fields: Vec<ExprField>,
12991325
pub rest: StructRest,
@@ -1314,17 +1340,8 @@ pub enum ExprKind {
13141340
/// This also represents calling the constructor of
13151341
/// tuple-like ADTs such as tuple structs and enum variants.
13161342
Call(P<Expr>, Vec<P<Expr>>),
1317-
/// A method call (`x.foo::<'static, Bar, Baz>(a, b, c, d)`)
1318-
///
1319-
/// The `PathSegment` represents the method name and its generic arguments
1320-
/// (within the angle brackets).
1321-
/// The standalone `Expr` is the receiver expression.
1322-
/// The vector of `Expr` is the arguments.
1323-
/// `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
1324-
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, x, [a, b, c, d])`.
1325-
/// This `Span` is the span of the function, without the dot and receiver
1326-
/// (e.g. `foo(a, b)` in `x.foo(a, b)`
1327-
MethodCall(PathSegment, P<Expr>, Vec<P<Expr>>, Span),
1343+
/// A method call (e.g. `x.foo::<Bar, Baz>(a, b, c)`).
1344+
MethodCall(Box<MethodCall>),
13281345
/// A tuple (e.g., `(a, b, c, d)`).
13291346
Tup(Vec<P<Expr>>),
13301347
/// A binary operation (e.g., `a + b`, `a * b`).
@@ -1363,9 +1380,7 @@ pub enum ExprKind {
13631380
/// A `match` block.
13641381
Match(P<Expr>, Vec<Arm>),
13651382
/// A closure (e.g., `move |a, b, c| a + b + c`).
1366-
///
1367-
/// The final span is the span of the argument block `|...|`.
1368-
Closure(ClosureBinder, CaptureBy, Async, Movability, P<FnDecl>, P<Expr>, Span),
1383+
Closure(Box<Closure>),
13691384
/// A block (`'label: { ... }`).
13701385
Block(P<Block>, Option<Label>),
13711386
/// An async block (`async move { ... }`).
@@ -1403,7 +1418,7 @@ pub enum ExprKind {
14031418
/// parameters (e.g., `foo::bar::<baz>`).
14041419
///
14051420
/// Optionally "qualified" (e.g., `<Vec<T> as SomeTrait>::SomeType`).
1406-
Path(Option<QSelf>, Path),
1421+
Path(Option<P<QSelf>>, Path),
14071422

14081423
/// A referencing operation (`&a`, `&mut a`, `&raw const a` or `&raw mut a`).
14091424
AddrOf(BorrowKind, Mutability, P<Expr>),
@@ -2006,7 +2021,7 @@ pub enum TyKind {
20062021
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
20072022
///
20082023
/// Type parameters are stored in the `Path` itself.
2009-
Path(Option<QSelf>, Path),
2024+
Path(Option<P<QSelf>>, Path),
20102025
/// A trait object type `Bound1 + Bound2 + Bound3`
20112026
/// where `Bound` is a trait or a lifetime.
20122027
TraitObject(GenericBounds, TraitObjectSyntax),
@@ -2138,7 +2153,7 @@ impl InlineAsmTemplatePiece {
21382153
#[derive(Clone, Encodable, Decodable, Debug)]
21392154
pub struct InlineAsmSym {
21402155
pub id: NodeId,
2141-
pub qself: Option<QSelf>,
2156+
pub qself: Option<P<QSelf>>,
21422157
pub path: Path,
21432158
}
21442159

@@ -3031,8 +3046,8 @@ mod size_asserts {
30313046
static_assert_size!(AssocItemKind, 32);
30323047
static_assert_size!(Attribute, 32);
30333048
static_assert_size!(Block, 48);
3034-
static_assert_size!(Expr, 104);
3035-
static_assert_size!(ExprKind, 72);
3049+
static_assert_size!(Expr, 88);
3050+
static_assert_size!(ExprKind, 56);
30363051
static_assert_size!(Fn, 184);
30373052
static_assert_size!(ForeignItem, 96);
30383053
static_assert_size!(ForeignItemKind, 24);
@@ -3046,13 +3061,13 @@ mod size_asserts {
30463061
static_assert_size!(LitKind, 24);
30473062
static_assert_size!(Local, 72);
30483063
static_assert_size!(Param, 40);
3049-
static_assert_size!(Pat, 120);
3064+
static_assert_size!(Pat, 104);
30503065
static_assert_size!(Path, 40);
30513066
static_assert_size!(PathSegment, 24);
3052-
static_assert_size!(PatKind, 96);
3067+
static_assert_size!(PatKind, 80);
30533068
static_assert_size!(Stmt, 32);
30543069
static_assert_size!(StmtKind, 16);
3055-
static_assert_size!(Ty, 96);
3056-
static_assert_size!(TyKind, 72);
3070+
static_assert_size!(Ty, 80);
3071+
static_assert_size!(TyKind, 56);
30573072
// tidy-alphabetical-end
30583073
}

compiler/rustc_ast/src/mut_visit.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub trait MutVisitor: Sized {
194194
noop_visit_path(p, self);
195195
}
196196

197-
fn visit_qself(&mut self, qs: &mut Option<QSelf>) {
197+
fn visit_qself(&mut self, qs: &mut Option<P<QSelf>>) {
198198
noop_visit_qself(qs, self);
199199
}
200200

@@ -529,8 +529,9 @@ pub fn noop_visit_path<T: MutVisitor>(Path { segments, span, tokens }: &mut Path
529529
visit_lazy_tts(tokens, vis);
530530
}
531531

532-
pub fn noop_visit_qself<T: MutVisitor>(qself: &mut Option<QSelf>, vis: &mut T) {
533-
visit_opt(qself, |QSelf { ty, path_span, position: _ }| {
532+
pub fn noop_visit_qself<T: MutVisitor>(qself: &mut Option<P<QSelf>>, vis: &mut T) {
533+
visit_opt(qself, |qself| {
534+
let QSelf { ty, path_span, position: _ } = &mut **qself;
534535
vis.visit_ty(ty);
535536
vis.visit_span(path_span);
536537
})
@@ -1303,12 +1304,17 @@ pub fn noop_visit_expr<T: MutVisitor>(
13031304
vis.visit_expr(f);
13041305
visit_exprs(args, vis);
13051306
}
1306-
ExprKind::MethodCall(PathSegment { ident, id, args }, receiver, exprs, span) => {
1307+
ExprKind::MethodCall(box MethodCall {
1308+
seg: PathSegment { ident, id, args: seg_args },
1309+
receiver,
1310+
args: call_args,
1311+
span,
1312+
}) => {
13071313
vis.visit_ident(ident);
13081314
vis.visit_id(id);
1309-
visit_opt(args, |args| vis.visit_generic_args(args));
1315+
visit_opt(seg_args, |args| vis.visit_generic_args(args));
13101316
vis.visit_method_receiver_expr(receiver);
1311-
visit_exprs(exprs, vis);
1317+
visit_exprs(call_args, vis);
13121318
vis.visit_span(span);
13131319
}
13141320
ExprKind::Binary(_binop, lhs, rhs) => {
@@ -1353,12 +1359,20 @@ pub fn noop_visit_expr<T: MutVisitor>(
13531359
vis.visit_expr(expr);
13541360
arms.flat_map_in_place(|arm| vis.flat_map_arm(arm));
13551361
}
1356-
ExprKind::Closure(binder, _capture_by, asyncness, _movability, decl, body, span) => {
1362+
ExprKind::Closure(box Closure {
1363+
binder,
1364+
capture_clause: _,
1365+
asyncness,
1366+
movability: _,
1367+
fn_decl,
1368+
body,
1369+
fn_decl_span,
1370+
}) => {
13571371
vis.visit_closure_binder(binder);
13581372
vis.visit_asyncness(asyncness);
1359-
vis.visit_fn_decl(decl);
1373+
vis.visit_fn_decl(fn_decl);
13601374
vis.visit_expr(body);
1361-
vis.visit_span(span);
1375+
vis.visit_span(fn_decl_span);
13621376
}
13631377
ExprKind::Block(blk, label) => {
13641378
vis.visit_block(blk);

compiler/rustc_ast/src/util/classify.rs

+3-1
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

+1-1
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

+13-5
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,10 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
798798
visitor.visit_expr(callee_expression);
799799
walk_list!(visitor, visit_expr, arguments);
800800
}
801-
ExprKind::MethodCall(ref segment, ref receiver, ref arguments, _span) => {
802-
visitor.visit_path_segment(segment);
801+
ExprKind::MethodCall(box MethodCall { ref seg, ref receiver, ref args, span: _ }) => {
802+
visitor.visit_path_segment(seg);
803803
visitor.visit_expr(receiver);
804-
walk_list!(visitor, visit_expr, arguments);
804+
walk_list!(visitor, visit_expr, args);
805805
}
806806
ExprKind::Binary(_, ref left_expression, ref right_expression) => {
807807
visitor.visit_expr(left_expression);
@@ -842,8 +842,16 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
842842
visitor.visit_expr(subexpression);
843843
walk_list!(visitor, visit_arm, arms);
844844
}
845-
ExprKind::Closure(ref binder, _, _, _, ref decl, ref body, _decl_span) => {
846-
visitor.visit_fn(FnKind::Closure(binder, decl, body), expression.span, expression.id)
845+
ExprKind::Closure(box Closure {
846+
ref binder,
847+
capture_clause: _,
848+
asyncness: _,
849+
movability: _,
850+
ref fn_decl,
851+
ref body,
852+
fn_decl_span: _,
853+
}) => {
854+
visitor.visit_fn(FnKind::Closure(binder, fn_decl, body), expression.span, expression.id)
847855
}
848856
ExprKind::Block(ref block, ref opt_label) => {
849857
walk_list!(visitor, visit_label, opt_label);

compiler/rustc_ast_lowering/src/expr.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
6161
hir::ExprKind::Call(f, self.lower_exprs(args))
6262
}
6363
}
64-
ExprKind::MethodCall(ref seg, ref receiver, ref args, span) => {
64+
ExprKind::MethodCall(box MethodCall { ref seg, ref receiver, ref args, span }) => {
6565
let hir_seg = self.arena.alloc(self.lower_path_segment(
6666
e.span,
6767
seg,
@@ -172,22 +172,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
172172
};
173173
self.lower_expr_await(dot_await_span, expr)
174174
}
175-
ExprKind::Closure(
175+
ExprKind::Closure(box Closure {
176176
ref binder,
177177
capture_clause,
178178
asyncness,
179179
movability,
180-
ref decl,
180+
ref fn_decl,
181181
ref body,
182182
fn_decl_span,
183-
) => {
183+
}) => {
184184
if let Async::Yes { closure_id, .. } = asyncness {
185185
self.lower_expr_async_closure(
186186
binder,
187187
capture_clause,
188188
e.id,
189189
closure_id,
190-
decl,
190+
fn_decl,
191191
body,
192192
fn_decl_span,
193193
)
@@ -197,7 +197,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
197197
capture_clause,
198198
e.id,
199199
movability,
200-
decl,
200+
fn_decl,
201201
body,
202202
fn_decl_span,
203203
)
@@ -1077,7 +1077,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10771077
fn extract_tuple_struct_path<'a>(
10781078
&mut self,
10791079
expr: &'a Expr,
1080-
) -> Option<(&'a Option<QSelf>, &'a Path)> {
1080+
) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
10811081
if let ExprKind::Path(qself, path) = &expr.kind {
10821082
// Does the path resolve to something disallowed in a tuple struct/variant pattern?
10831083
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
@@ -1097,7 +1097,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10971097
fn extract_unit_struct_path<'a>(
10981098
&mut self,
10991099
expr: &'a Expr,
1100-
) -> Option<(&'a Option<QSelf>, &'a Path)> {
1100+
) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
11011101
if let ExprKind::Path(qself, path) = &expr.kind {
11021102
// Does the path resolve to something disallowed in a unit struct/variant pattern?
11031103
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12071207
fn lower_path_ty(
12081208
&mut self,
12091209
t: &Ty,
1210-
qself: &Option<QSelf>,
1210+
qself: &Option<ptr::P<QSelf>>,
12111211
path: &Path,
12121212
param_mode: ParamMode,
12131213
itctx: &ImplTraitContext,

compiler/rustc_ast_lowering/src/path.rs

+1-1
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)