Skip to content

Commit b14b7ba

Browse files
committed
Use ThinVec in ast::Block.
1 parent 4143b10 commit b14b7ba

File tree

18 files changed

+92
-81
lines changed

18 files changed

+92
-81
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ pub enum NestedMetaItem {
531531
#[derive(Clone, Encodable, Decodable, Debug)]
532532
pub struct Block {
533533
/// The statements in the block.
534-
pub stmts: Vec<Stmt>,
534+
pub stmts: ThinVec<Stmt>,
535535
pub id: NodeId,
536536
/// Distinguishes between `unsafe { ... }` and `{ ... }`.
537537
pub rules: BlockCheckMode,
@@ -3112,7 +3112,7 @@ mod size_asserts {
31123112
static_assert_size!(AssocItem, 104);
31133113
static_assert_size!(AssocItemKind, 32);
31143114
static_assert_size!(Attribute, 32);
3115-
static_assert_size!(Block, 48);
3115+
static_assert_size!(Block, 32);
31163116
static_assert_size!(Expr, 72);
31173117
static_assert_size!(ExprKind, 40);
31183118
static_assert_size!(Fn, 152);

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn expand(
3939
let span = ecx.with_def_site_ctxt(item.span);
4040

4141
// Generate item statements for the allocator methods.
42-
let stmts = vec![generate_handler(ecx, item.ident, span, sig_span)];
42+
let stmts = thin_vec![generate_handler(ecx, item.ident, span, sig_span)];
4343

4444
// Generate anonymous constant serving as container for the allocator methods.
4545
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ impl<'cx, 'a> Context<'cx, 'a> {
8383

8484
let Self { best_case_captures, capture_decls, cx, local_bind_decls, span, .. } = self;
8585

86-
let mut assert_then_stmts = Vec::with_capacity(2);
86+
let mut assert_then_stmts = ThinVec::with_capacity(2);
8787
assert_then_stmts.extend(best_case_captures);
8888
assert_then_stmts.push(self.cx.stmt_expr(panic));
8989
let assert_then = self.cx.block(span, assert_then_stmts);
9090

91-
let mut stmts = Vec::with_capacity(4);
91+
let mut stmts = ThinVec::with_capacity(4);
9292
stmts.push(initial_imports);
9393
stmts.extend(capture_decls.into_iter().map(|c| c.decl));
9494
stmts.extend(local_bind_decls);
@@ -389,7 +389,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
389389
let local_bind_path = self.cx.expr_path(Path::from_ident(local_bind));
390390
let rslt = if self.is_consumed {
391391
let ret = self.cx.stmt_expr(local_bind_path);
392-
self.cx.expr_block(self.cx.block(self.span, vec![try_capture_call, ret]))
392+
self.cx.expr_block(self.cx.block(self.span, thin_vec![try_capture_call, ret]))
393393
} else {
394394
self.best_case_captures.push(try_capture_call);
395395
local_bind_path

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_data_structures::fx::FxHashSet;
66
use rustc_expand::base::{Annotatable, ExtCtxt};
77
use rustc_span::symbol::{kw, sym, Ident};
88
use rustc_span::Span;
9-
use thin_vec::thin_vec;
9+
use thin_vec::{thin_vec, ThinVec};
1010

1111
pub fn expand_deriving_clone(
1212
cx: &mut ExtCtxt<'_>,
@@ -100,7 +100,7 @@ fn cs_clone_simple(
100100
substr: &Substructure<'_>,
101101
is_union: bool,
102102
) -> BlockOrExpr {
103-
let mut stmts = Vec::new();
103+
let mut stmts = ThinVec::new();
104104
let mut seen_type_names = FxHashSet::default();
105105
let mut process_variant = |variant: &VariantData| {
106106
for field in variant.fields() {

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::fx::FxHashSet;
77
use rustc_expand::base::{Annotatable, ExtCtxt};
88
use rustc_span::symbol::sym;
99
use rustc_span::Span;
10-
use thin_vec::thin_vec;
10+
use thin_vec::{thin_vec, ThinVec};
1111

1212
pub fn expand_deriving_eq(
1313
cx: &mut ExtCtxt<'_>,
@@ -56,7 +56,7 @@ fn cs_total_eq_assert(
5656
trait_span: Span,
5757
substr: &Substructure<'_>,
5858
) -> BlockOrExpr {
59-
let mut stmts = Vec::new();
59+
let mut stmts = ThinVec::new();
6060
let mut seen_type_names = FxHashSet::default();
6161
let mut process_variant = |variant: &ast::VariantData| {
6262
for field in variant.fields() {

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
187187
args.push(cx.expr_ident(span, Ident::new(sym::values, span)));
188188
let expr = cx.expr_call_global(span, fn_path_debug_internal, args);
189189

190-
let mut stmts = Vec::with_capacity(3);
190+
let mut stmts = ThinVec::with_capacity(2);
191191
if is_struct {
192192
stmts.push(names_let.unwrap());
193193
}

compiler/rustc_builtin_macros/src/deriving/encodable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn encodable_substructure(
169169
Struct(_, fields) => {
170170
let fn_emit_struct_field_path =
171171
cx.def_site_path(&[sym::rustc_serialize, sym::Encoder, sym::emit_struct_field]);
172-
let mut stmts = Vec::new();
172+
let mut stmts = ThinVec::new();
173173
for (i, &FieldInfo { name, ref self_expr, span, .. }) in fields.iter().enumerate() {
174174
let name = match name {
175175
Some(id) => id.name,
@@ -237,7 +237,7 @@ fn encodable_substructure(
237237
let fn_emit_enum_variant_arg_path: Vec<_> =
238238
cx.def_site_path(&[sym::rustc_serialize, sym::Encoder, sym::emit_enum_variant_arg]);
239239

240-
let mut stmts = Vec::new();
240+
let mut stmts = ThinVec::new();
241241
if !fields.is_empty() {
242242
let last = fields.len() - 1;
243243
for (i, &FieldInfo { ref self_expr, span, .. }) in fields.iter().enumerate() {
@@ -293,7 +293,7 @@ fn encodable_substructure(
293293
fn_emit_enum_path,
294294
thin_vec![encoder, cx.expr_str(trait_span, substr.type_ident.name), blk],
295295
);
296-
BlockOrExpr::new_mixed(vec![me], Some(expr))
296+
BlockOrExpr::new_mixed(thin_vec![me], Some(expr))
297297
}
298298

299299
_ => cx.bug("expected Struct or EnumMatching in derive(Encodable)"),

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,18 @@ struct TypeParameter {
328328
/// avoiding the insertion of any unnecessary blocks.
329329
///
330330
/// The statements come before the expression.
331-
pub struct BlockOrExpr(Vec<ast::Stmt>, Option<P<Expr>>);
331+
pub struct BlockOrExpr(ThinVec<ast::Stmt>, Option<P<Expr>>);
332332

333333
impl BlockOrExpr {
334-
pub fn new_stmts(stmts: Vec<ast::Stmt>) -> BlockOrExpr {
334+
pub fn new_stmts(stmts: ThinVec<ast::Stmt>) -> BlockOrExpr {
335335
BlockOrExpr(stmts, None)
336336
}
337337

338338
pub fn new_expr(expr: P<Expr>) -> BlockOrExpr {
339-
BlockOrExpr(vec![], Some(expr))
339+
BlockOrExpr(ThinVec::new(), Some(expr))
340340
}
341341

342-
pub fn new_mixed(stmts: Vec<ast::Stmt>, expr: Option<P<Expr>>) -> BlockOrExpr {
342+
pub fn new_mixed(stmts: ThinVec<ast::Stmt>, expr: Option<P<Expr>>) -> BlockOrExpr {
343343
BlockOrExpr(stmts, expr)
344344
}
345345

@@ -355,7 +355,7 @@ impl BlockOrExpr {
355355
fn into_expr(self, cx: &ExtCtxt<'_>, span: Span) -> P<Expr> {
356356
if self.0.is_empty() {
357357
match self.1 {
358-
None => cx.expr_block(cx.block(span, vec![])),
358+
None => cx.expr_block(cx.block(span, ThinVec::new())),
359359
Some(expr) => expr,
360360
}
361361
} else if self.0.len() == 1
@@ -1146,7 +1146,7 @@ impl<'a> MethodDef<'a> {
11461146
// There is no sensible code to be generated for *any* deriving on a
11471147
// zero-variant enum. So we just generate a failing expression.
11481148
if variants.is_empty() {
1149-
return BlockOrExpr(vec![], Some(deriving::call_unreachable(cx, span)));
1149+
return BlockOrExpr(ThinVec::new(), Some(deriving::call_unreachable(cx, span)));
11501150
}
11511151

11521152
let prefixes = iter::once("__self".to_string())
@@ -1182,7 +1182,7 @@ impl<'a> MethodDef<'a> {
11821182
let other_selflike_exprs = tag_exprs;
11831183
let tag_field = FieldInfo { span, name: None, self_expr, other_selflike_exprs };
11841184

1185-
let tag_let_stmts: Vec<_> = iter::zip(&tag_idents, &selflike_args)
1185+
let tag_let_stmts: ThinVec<_> = iter::zip(&tag_idents, &selflike_args)
11861186
.map(|(&ident, selflike_arg)| {
11871187
let variant_value = deriving::call_intrinsic(
11881188
cx,
@@ -1362,7 +1362,7 @@ impl<'a> MethodDef<'a> {
13621362
tag_let_stmts.append(&mut tag_check_plus_match.0);
13631363
BlockOrExpr(tag_let_stmts, tag_check_plus_match.1)
13641364
} else {
1365-
BlockOrExpr(vec![], Some(get_match_expr(selflike_args)))
1365+
BlockOrExpr(ThinVec::new(), Some(get_match_expr(selflike_args)))
13661366
}
13671367
}
13681368

@@ -1599,7 +1599,7 @@ impl<'a> TraitDef<'a> {
15991599
} else {
16001600
// Wrap the expression in `{...}`, causing a copy.
16011601
field_expr = cx.expr_block(
1602-
cx.block(struct_field.span, vec![cx.stmt_expr(field_expr)]),
1602+
cx.block(struct_field.span, thin_vec![cx.stmt_expr(field_expr)]),
16031603
);
16041604
}
16051605
}

compiler/rustc_builtin_macros/src/deriving/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn hash_substructure(
7272
}
7373
EnumTag(tag_field, match_expr) => {
7474
assert!(tag_field.other_selflike_exprs.is_empty());
75-
let stmts = vec![call_hash(tag_field.span, tag_field.self_expr.clone())];
75+
let stmts = thin_vec![call_hash(tag_field.span, tag_field.self_expr.clone())];
7676
(stmts, match_expr.clone())
7777
}
7878
_ => cx.span_bug(trait_span, "impossible substructure in `derive(Hash)`"),

compiler/rustc_builtin_macros/src/deriving/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_ast::{GenericArg, Impl, ItemKind, MetaItem};
66
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier};
77
use rustc_span::symbol::{sym, Ident, Symbol};
88
use rustc_span::Span;
9-
use thin_vec::ThinVec;
9+
use thin_vec::{thin_vec, ThinVec};
1010

1111
macro path_local($x:ident) {
1212
generic::ty::Path::new_local(sym::$x)
@@ -107,7 +107,7 @@ fn call_unreachable(cx: &ExtCtxt<'_>, span: Span) -> P<ast::Expr> {
107107
let call = cx.expr_call_global(span, path, ThinVec::new());
108108

109109
cx.expr_block(P(ast::Block {
110-
stmts: vec![cx.stmt_expr(call)],
110+
stmts: thin_vec![cx.stmt_expr(call)],
111111
id: ast::DUMMY_NODE_ID,
112112
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
113113
span,
@@ -212,7 +212,7 @@ fn inject_impl_of_structural_trait(
212212

213213
fn assert_ty_bounds(
214214
cx: &mut ExtCtxt<'_>,
215-
stmts: &mut Vec<ast::Stmt>,
215+
stmts: &mut ThinVec<ast::Stmt>,
216216
ty: P<ast::Ty>,
217217
span: Span,
218218
assert_path: &[Symbol],

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
375375
});
376376

377377
let block = cx.expr_block(
378-
cx.block(span, vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
378+
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
379379
);
380380

381381
let anon_constant = cx.item_const(

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
317317

318318
// If no test runner is provided we need to import the test crate
319319
let main_body = if cx.test_runner.is_none() {
320-
ecx.block(sp, vec![test_extern_stmt, call_test_main])
320+
ecx.block(sp, thin_vec![test_extern_stmt, call_test_main])
321321
} else {
322-
ecx.block(sp, vec![call_test_main])
322+
ecx.block(sp, thin_vec![call_test_main])
323323
};
324324

325325
let decl = ecx.fn_decl(ThinVec::new(), ast::FnRetTy::Ty(main_ret_ty));

compiler/rustc_expand/src/build.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ impl<'a> ExtCtxt<'a> {
221221
pub fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
222222
self.block(
223223
expr.span,
224-
vec![ast::Stmt {
224+
thin_vec![ast::Stmt {
225225
id: ast::DUMMY_NODE_ID,
226226
span: expr.span,
227227
kind: ast::StmtKind::Expr(expr),
228228
}],
229229
)
230230
}
231-
pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
231+
pub fn block(&self, span: Span, stmts: ThinVec<ast::Stmt>) -> P<ast::Block> {
232232
P(ast::Block {
233233
stmts,
234234
id: ast::DUMMY_NODE_ID,
@@ -567,7 +567,12 @@ impl<'a> ExtCtxt<'a> {
567567
self.lambda(span, vec![ident], body)
568568
}
569569

570-
pub fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>, ident: Ident) -> P<ast::Expr> {
570+
pub fn lambda_stmts_1(
571+
&self,
572+
span: Span,
573+
stmts: ThinVec<ast::Stmt>,
574+
ident: Ident,
575+
) -> P<ast::Expr> {
571576
self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
572577
}
573578

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl<'a> Parser<'a> {
708708
err.delay_as_bug();
709709
self.restore_snapshot(snapshot);
710710
let mut tail = self.mk_block(
711-
vec![self.mk_stmt_err(expr.span)],
711+
thin_vec![self.mk_stmt_err(expr.span)],
712712
s,
713713
lo.to(self.prev_token.span),
714714
);

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ impl<'a> Parser<'a> {
16011601

16021602
// Replace `'label: non_block_expr` with `'label: {non_block_expr}` in order to suppress future errors about `break 'label`.
16031603
let stmt = self.mk_stmt(span, StmtKind::Expr(expr));
1604-
let blk = self.mk_block(vec![stmt], BlockCheckMode::Default, span);
1604+
let blk = self.mk_block(thin_vec![stmt], BlockCheckMode::Default, span);
16051605
self.mk_expr(span, ExprKind::Block(blk, label))
16061606
});
16071607

@@ -2481,7 +2481,7 @@ impl<'a> Parser<'a> {
24812481
self.sess
24822482
.emit_err(errors::MissingExpressionInForLoop { span: expr.span.shrink_to_lo() });
24832483
let err_expr = self.mk_expr(expr.span, ExprKind::Err);
2484-
let block = self.mk_block(vec![], BlockCheckMode::Default, self.prev_token.span);
2484+
let block = self.mk_block(thin_vec![], BlockCheckMode::Default, self.prev_token.span);
24852485
return Ok(self.mk_expr(
24862486
lo.to(self.prev_token.span),
24872487
ExprKind::ForLoop(pat, err_expr, block, opt_label),

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use rustc_ast::{StmtKind, DUMMY_NODE_ID};
2020
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
2121
use rustc_span::source_map::{BytePos, Span};
2222
use rustc_span::symbol::{kw, sym};
23-
2423
use std::mem;
24+
use thin_vec::{thin_vec, ThinVec};
2525

2626
impl<'a> Parser<'a> {
2727
/// Parses a statement. This stops just before trailing semicolons on everything but items.
@@ -544,7 +544,7 @@ impl<'a> Parser<'a> {
544544
s: BlockCheckMode,
545545
recover: AttemptLocalParseRecovery,
546546
) -> PResult<'a, P<Block>> {
547-
let mut stmts = vec![];
547+
let mut stmts = ThinVec::new();
548548
let mut snapshot = None;
549549
while !self.eat(&token::CloseDelim(Delimiter::Brace)) {
550550
if self.token == token::Eof {
@@ -662,7 +662,12 @@ impl<'a> Parser<'a> {
662662
Ok(Some(stmt))
663663
}
664664

665-
pub(super) fn mk_block(&self, stmts: Vec<Stmt>, rules: BlockCheckMode, span: Span) -> P<Block> {
665+
pub(super) fn mk_block(
666+
&self,
667+
stmts: ThinVec<Stmt>,
668+
rules: BlockCheckMode,
669+
span: Span,
670+
) -> P<Block> {
666671
P(Block {
667672
stmts,
668673
id: DUMMY_NODE_ID,
@@ -682,6 +687,6 @@ impl<'a> Parser<'a> {
682687
}
683688

684689
pub(super) fn mk_block_err(&self, span: Span) -> P<Block> {
685-
self.mk_block(vec![self.mk_stmt_err(span)], BlockCheckMode::Default, span)
690+
self.mk_block(thin_vec![self.mk_stmt_err(span)], BlockCheckMode::Default, span)
686691
}
687692
}

src/tools/rustfmt/src/closures.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_ast::{ast, ptr};
22
use rustc_span::Span;
3+
use thin_vec::thin_vec;
34

45
use crate::attr::get_attrs_from_stmt;
56
use crate::config::lists::*;
@@ -150,7 +151,7 @@ fn rewrite_closure_with_block(
150151
}
151152

152153
let block = ast::Block {
153-
stmts: vec![ast::Stmt {
154+
stmts: thin_vec![ast::Stmt {
154155
id: ast::NodeId::root(),
155156
kind: ast::StmtKind::Expr(ptr::P(body.clone())),
156157
span: body.span,

0 commit comments

Comments
 (0)