Skip to content

Commit 70fe5f0

Browse files
committed
Auto merge of #101562 - nnethercote:shrink-ast-Expr-harder, r=petrochenkov
Shrink `ast::Expr` harder r? `@ghost`
2 parents fd3bfb3 + 67d5cc0 commit 70fe5f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+486
-383
lines changed

Cargo.lock

+3
Original file line numberDiff line numberDiff line change
@@ -3476,6 +3476,7 @@ dependencies = [
34763476
"rustc_session",
34773477
"rustc_span",
34783478
"smallvec",
3479+
"thin-vec",
34793480
"tracing",
34803481
]
34813482

@@ -3916,6 +3917,7 @@ dependencies = [
39163917
"rustc_macros",
39173918
"rustc_session",
39183919
"rustc_span",
3920+
"thin-vec",
39193921
"tracing",
39203922
"unicode-normalization",
39213923
"unicode-width",
@@ -4051,6 +4053,7 @@ dependencies = [
40514053
"rustc_session",
40524054
"rustc_span",
40534055
"smallvec",
4056+
"thin-vec",
40544057
"tracing",
40554058
]
40564059

compiler/rustc_ast/src/ast.rs

+48-33
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_span::{Span, DUMMY_SP};
3636
use std::convert::TryFrom;
3737
use std::fmt;
3838
use std::mem;
39-
use thin_vec::ThinVec;
39+
use thin_vec::{thin_vec, ThinVec};
4040

4141
/// A "Label" is an identifier of some point in sources,
4242
/// e.g. in the following code:
@@ -90,7 +90,7 @@ pub struct Path {
9090
pub span: Span,
9191
/// The segments in the path: the things separated by `::`.
9292
/// Global paths begin with `kw::PathRoot`.
93-
pub segments: Vec<PathSegment>,
93+
pub segments: ThinVec<PathSegment>,
9494
pub tokens: Option<LazyAttrTokenStream>,
9595
}
9696

@@ -114,7 +114,7 @@ impl Path {
114114
// Convert a span and an identifier to the corresponding
115115
// one-segment path.
116116
pub fn from_ident(ident: Ident) -> Path {
117-
Path { segments: vec![PathSegment::from_ident(ident)], span: ident.span, tokens: None }
117+
Path { segments: thin_vec![PathSegment::from_ident(ident)], span: ident.span, tokens: None }
118118
}
119119

120120
pub fn is_global(&self) -> bool {
@@ -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,28 +3046,28 @@ 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, 72);
3050+
static_assert_size!(ExprKind, 40);
30363051
static_assert_size!(Fn, 184);
30373052
static_assert_size!(ForeignItem, 96);
30383053
static_assert_size!(ForeignItemKind, 24);
30393054
static_assert_size!(GenericArg, 24);
3040-
static_assert_size!(GenericBound, 88);
3055+
static_assert_size!(GenericBound, 72);
30413056
static_assert_size!(Generics, 72);
3042-
static_assert_size!(Impl, 200);
3057+
static_assert_size!(Impl, 184);
30433058
static_assert_size!(Item, 184);
30443059
static_assert_size!(ItemKind, 112);
30453060
static_assert_size!(Lit, 48);
30463061
static_assert_size!(LitKind, 24);
30473062
static_assert_size!(Local, 72);
30483063
static_assert_size!(Param, 40);
3049-
static_assert_size!(Pat, 120);
3050-
static_assert_size!(Path, 40);
3064+
static_assert_size!(Pat, 88);
3065+
static_assert_size!(Path, 24);
30513066
static_assert_size!(PathSegment, 24);
3052-
static_assert_size!(PatKind, 96);
3067+
static_assert_size!(PatKind, 64);
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, 64);
3071+
static_assert_size!(TyKind, 40);
30573072
// tidy-alphabetical-end
30583073
}

compiler/rustc_ast/src/attr/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@ use crate::token::{self, CommentKind, Delimiter, Token};
1010
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
1111
use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
1212
use crate::util::comments;
13-
1413
use rustc_data_structures::sync::WorkerLocal;
1514
use rustc_index::bit_set::GrowableBitSet;
1615
use rustc_span::source_map::BytePos;
1716
use rustc_span::symbol::{sym, Ident, Symbol};
1817
use rustc_span::Span;
19-
2018
use std::cell::Cell;
2119
use std::iter;
2220
#[cfg(debug_assertions)]
2321
use std::ops::BitXor;
2422
#[cfg(debug_assertions)]
2523
use std::sync::atomic::{AtomicU32, Ordering};
24+
use thin_vec::thin_vec;
2625

2726
pub struct MarkedAttrs(GrowableBitSet<AttrId>);
2827

@@ -471,12 +470,12 @@ impl MetaItem {
471470
tokens.peek()
472471
{
473472
tokens.next();
474-
vec![PathSegment::from_ident(Ident::new(name, span))]
473+
thin_vec![PathSegment::from_ident(Ident::new(name, span))]
475474
} else {
476475
break 'arm Path::from_ident(Ident::new(name, span));
477476
}
478477
} else {
479-
vec![PathSegment::path_root(span)]
478+
thin_vec![PathSegment::path_root(span)]
480479
};
481480
loop {
482481
if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =

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);

0 commit comments

Comments
 (0)