Skip to content

Commit 4143b10

Browse files
committed
Use ThinVec in various AST types.
This commit changes the sequence parsers to produce `ThinVec`, which triggers numerous conversions.
1 parent 6a56c3a commit 4143b10

File tree

52 files changed

+353
-290
lines changed

Some content is hidden

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

52 files changed

+353
-290
lines changed

Cargo.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3689,6 +3689,7 @@ dependencies = [
36893689
"rustc_session",
36903690
"rustc_span",
36913691
"rustc_target",
3692+
"thin-vec",
36923693
"tracing",
36933694
]
36943695

@@ -4072,6 +4073,7 @@ dependencies = [
40724073
"rustc_trait_selection",
40734074
"rustc_type_ir",
40744075
"smallvec",
4076+
"thin-vec",
40754077
"tracing",
40764078
]
40774079

@@ -4128,6 +4130,7 @@ dependencies = [
41284130
"rustc_serialize",
41294131
"rustc_session",
41304132
"rustc_span",
4133+
"thin-vec",
41314134
"tracing",
41324135
]
41334136

@@ -4826,7 +4829,6 @@ dependencies = [
48264829
"serde_json",
48274830
"smallvec",
48284831
"tempfile",
4829-
"thin-vec",
48304832
"tracing",
48314833
"tracing-subscriber",
48324834
"tracing-tree",

compiler/rustc_ast/src/ast.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub struct ParenthesizedArgs {
253253
pub span: Span,
254254

255255
/// `(A, B)`
256-
pub inputs: Vec<P<Ty>>,
256+
pub inputs: ThinVec<P<Ty>>,
257257

258258
/// ```text
259259
/// Foo(A, B) -> C
@@ -503,7 +503,7 @@ pub enum MetaItemKind {
503503
/// List meta item.
504504
///
505505
/// E.g., `#[derive(..)]`, where the field represents the `..`.
506-
List(Vec<NestedMetaItem>),
506+
List(ThinVec<NestedMetaItem>),
507507

508508
/// Name value meta item.
509509
///
@@ -581,7 +581,7 @@ impl Pat {
581581
// A tuple pattern `(P0, .., Pn)` can be reparsed as `(T0, .., Tn)`
582582
// assuming `T0` to `Tn` are all syntactically valid as types.
583583
PatKind::Tuple(pats) => {
584-
let mut tys = Vec::with_capacity(pats.len());
584+
let mut tys = ThinVec::with_capacity(pats.len());
585585
// FIXME(#48994) - could just be collected into an Option<Vec>
586586
for pat in pats {
587587
tys.push(pat.to_ty()?);
@@ -725,11 +725,11 @@ pub enum PatKind {
725725
Struct(Option<P<QSelf>>, Path, Vec<PatField>, /* recovered */ bool),
726726

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

730730
/// An or-pattern `A | B | C`.
731731
/// Invariant: `pats.len() >= 2`.
732-
Or(Vec<P<Pat>>),
732+
Or(ThinVec<P<Pat>>),
733733

734734
/// A possibly qualified path pattern.
735735
/// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
@@ -738,7 +738,7 @@ pub enum PatKind {
738738
Path(Option<P<QSelf>>, Path),
739739

740740
/// A tuple pattern (`(a, b)`).
741-
Tuple(Vec<P<Pat>>),
741+
Tuple(ThinVec<P<Pat>>),
742742

743743
/// A `box` pattern.
744744
Box(P<Pat>),
@@ -753,7 +753,7 @@ pub enum PatKind {
753753
Range(Option<P<Expr>>, Option<P<Expr>>, Spanned<RangeEnd>),
754754

755755
/// A slice pattern `[a, b, c]`.
756-
Slice(Vec<P<Pat>>),
756+
Slice(ThinVec<P<Pat>>),
757757

758758
/// A rest pattern `..`.
759759
///
@@ -1204,7 +1204,7 @@ impl Expr {
12041204
ExprKind::Array(exprs) if exprs.len() == 1 => exprs[0].to_ty().map(TyKind::Slice)?,
12051205

12061206
ExprKind::Tup(exprs) => {
1207-
let tys = exprs.iter().map(|expr| expr.to_ty()).collect::<Option<Vec<_>>>()?;
1207+
let tys = exprs.iter().map(|expr| expr.to_ty()).collect::<Option<ThinVec<_>>>()?;
12081208
TyKind::Tup(tys)
12091209
}
12101210

@@ -1337,7 +1337,7 @@ pub struct MethodCall {
13371337
/// The receiver, e.g. `x`.
13381338
pub receiver: P<Expr>,
13391339
/// The arguments, e.g. `a, b, c`.
1340-
pub args: Vec<P<Expr>>,
1340+
pub args: ThinVec<P<Expr>>,
13411341
/// The span of the function, without the dot and receiver e.g. `foo::<Bar,
13421342
/// Baz>(a, b, c)`.
13431343
pub span: Span,
@@ -1366,7 +1366,7 @@ pub enum ExprKind {
13661366
/// A `box x` expression.
13671367
Box(P<Expr>),
13681368
/// An array (`[a, b, c, d]`)
1369-
Array(Vec<P<Expr>>),
1369+
Array(ThinVec<P<Expr>>),
13701370
/// Allow anonymous constants from an inline `const` block
13711371
ConstBlock(AnonConst),
13721372
/// A function call
@@ -1375,11 +1375,11 @@ pub enum ExprKind {
13751375
/// and the second field is the list of arguments.
13761376
/// This also represents calling the constructor of
13771377
/// tuple-like ADTs such as tuple structs and enum variants.
1378-
Call(P<Expr>, Vec<P<Expr>>),
1378+
Call(P<Expr>, ThinVec<P<Expr>>),
13791379
/// A method call (e.g. `x.foo::<Bar, Baz>(a, b, c)`).
13801380
MethodCall(Box<MethodCall>),
13811381
/// A tuple (e.g., `(a, b, c, d)`).
1382-
Tup(Vec<P<Expr>>),
1382+
Tup(ThinVec<P<Expr>>),
13831383
/// A binary operation (e.g., `a + b`, `a * b`).
13841384
Binary(BinOp, P<Expr>, P<Expr>),
13851385
/// A unary operation (e.g., `!x`, `*x`).
@@ -2078,7 +2078,7 @@ pub enum TyKind {
20782078
/// The never type (`!`).
20792079
Never,
20802080
/// A tuple (`(A, B, C, D,...)`).
2081-
Tup(Vec<P<Ty>>),
2081+
Tup(ThinVec<P<Ty>>),
20822082
/// A path (`module::module::...::Type`), optionally
20832083
/// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
20842084
///
@@ -2363,7 +2363,7 @@ impl Param {
23632363
/// which contains metadata about function safety, asyncness, constness and ABI.
23642364
#[derive(Clone, Encodable, Decodable, Debug)]
23652365
pub struct FnDecl {
2366-
pub inputs: Vec<Param>,
2366+
pub inputs: ThinVec<Param>,
23672367
pub output: FnRetTy,
23682368
}
23692369

@@ -2532,7 +2532,7 @@ pub enum UseTreeKind {
25322532
/// `use prefix` or `use prefix as rename`
25332533
Simple(Option<Ident>),
25342534
/// `use prefix::{...}`
2535-
Nested(Vec<(UseTree, NodeId)>),
2535+
Nested(ThinVec<(UseTree, NodeId)>),
25362536
/// `use prefix::*`
25372537
Glob,
25382538
}
@@ -2695,11 +2695,11 @@ pub enum VariantData {
26952695
/// Struct variant.
26962696
///
26972697
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
2698-
Struct(Vec<FieldDef>, bool),
2698+
Struct(ThinVec<FieldDef>, bool),
26992699
/// Tuple variant.
27002700
///
27012701
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
2702-
Tuple(Vec<FieldDef>, NodeId),
2702+
Tuple(ThinVec<FieldDef>, NodeId),
27032703
/// Unit variant.
27042704
///
27052705
/// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
@@ -3122,8 +3122,8 @@ mod size_asserts {
31223122
static_assert_size!(GenericBound, 56);
31233123
static_assert_size!(Generics, 40);
31243124
static_assert_size!(Impl, 136);
3125-
static_assert_size!(Item, 152);
3126-
static_assert_size!(ItemKind, 80);
3125+
static_assert_size!(Item, 144);
3126+
static_assert_size!(ItemKind, 72);
31273127
static_assert_size!(LitKind, 24);
31283128
static_assert_size!(Local, 72);
31293129
static_assert_size!(MetaItemLit, 40);

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::iter;
2020
use std::ops::BitXor;
2121
#[cfg(debug_assertions)]
2222
use std::sync::atomic::{AtomicU32, Ordering};
23-
use thin_vec::thin_vec;
23+
use thin_vec::{thin_vec, ThinVec};
2424

2525
pub struct MarkedAttrs(GrowableBitSet<AttrId>);
2626

@@ -135,7 +135,7 @@ impl Attribute {
135135
}
136136
}
137137

138-
pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
138+
pub fn meta_item_list(&self) -> Option<ThinVec<NestedMetaItem>> {
139139
match &self.kind {
140140
AttrKind::Normal(normal) => normal.item.meta_item_list(),
141141
AttrKind::DocComment(..) => None,
@@ -216,7 +216,7 @@ impl AttrItem {
216216
self.args.span().map_or(self.path.span, |args_span| self.path.span.to(args_span))
217217
}
218218

219-
fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
219+
fn meta_item_list(&self) -> Option<ThinVec<NestedMetaItem>> {
220220
match &self.args {
221221
AttrArgs::Delimited(args) if args.delim == MacDelimiter::Parenthesis => {
222222
MetaItemKind::list_from_tokens(args.tokens.clone())
@@ -375,9 +375,9 @@ impl MetaItemKind {
375375
}
376376
}
377377

378-
fn list_from_tokens(tokens: TokenStream) -> Option<Vec<NestedMetaItem>> {
378+
fn list_from_tokens(tokens: TokenStream) -> Option<ThinVec<NestedMetaItem>> {
379379
let mut tokens = tokens.into_trees().peekable();
380-
let mut result = Vec::new();
380+
let mut result = ThinVec::new();
381381
while tokens.peek().is_some() {
382382
let item = NestedMetaItem::from_tokens(&mut tokens)?;
383383
result.push(item);

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ pub fn visit_exprs<T: MutVisitor>(exprs: &mut Vec<P<Expr>>, vis: &mut T) {
369369
exprs.flat_map_in_place(|expr| vis.filter_map_expr(expr))
370370
}
371371

372+
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
373+
pub fn visit_thin_exprs<T: MutVisitor>(exprs: &mut ThinVec<P<Expr>>, vis: &mut T) {
374+
exprs.flat_map_in_place(|expr| vis.filter_map_expr(expr))
375+
}
376+
372377
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
373378
pub fn visit_bounds<T: MutVisitor>(bounds: &mut GenericBounds, vis: &mut T) {
374379
visit_vec(bounds, |bound| vis.visit_param_bound(bound));
@@ -485,7 +490,7 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
485490
vis.visit_fn_decl(decl);
486491
vis.visit_span(decl_span);
487492
}
488-
TyKind::Tup(tys) => visit_vec(tys, |ty| vis.visit_ty(ty)),
493+
TyKind::Tup(tys) => visit_thin_vec(tys, |ty| vis.visit_ty(ty)),
489494
TyKind::Paren(ty) => vis.visit_ty(ty),
490495
TyKind::Path(qself, path) => {
491496
vis.visit_qself(qself);
@@ -584,7 +589,7 @@ pub fn noop_visit_parenthesized_parameter_data<T: MutVisitor>(
584589
vis: &mut T,
585590
) {
586591
let ParenthesizedArgs { inputs, output, span, .. } = args;
587-
visit_vec(inputs, |input| vis.visit_ty(input));
592+
visit_thin_vec(inputs, |input| vis.visit_ty(input));
588593
noop_visit_fn_ret_ty(output, vis);
589594
vis.visit_span(span);
590595
}
@@ -647,7 +652,7 @@ pub fn noop_visit_meta_item<T: MutVisitor>(mi: &mut MetaItem, vis: &mut T) {
647652
let MetaItem { path: _, kind, span } = mi;
648653
match kind {
649654
MetaItemKind::Word => {}
650-
MetaItemKind::List(mis) => visit_vec(mis, |mi| vis.visit_meta_list_item(mi)),
655+
MetaItemKind::List(mis) => visit_thin_vec(mis, |mi| vis.visit_meta_list_item(mi)),
651656
MetaItemKind::NameValue(_s) => {}
652657
}
653658
vis.visit_span(span);
@@ -1236,7 +1241,7 @@ pub fn noop_visit_pat<T: MutVisitor>(pat: &mut P<Pat>, vis: &mut T) {
12361241
PatKind::TupleStruct(qself, path, elems) => {
12371242
vis.visit_qself(qself);
12381243
vis.visit_path(path);
1239-
visit_vec(elems, |elem| vis.visit_pat(elem));
1244+
visit_thin_vec(elems, |elem| vis.visit_pat(elem));
12401245
}
12411246
PatKind::Path(qself, path) => {
12421247
vis.visit_qself(qself);
@@ -1255,7 +1260,7 @@ pub fn noop_visit_pat<T: MutVisitor>(pat: &mut P<Pat>, vis: &mut T) {
12551260
vis.visit_span(span);
12561261
}
12571262
PatKind::Tuple(elems) | PatKind::Slice(elems) | PatKind::Or(elems) => {
1258-
visit_vec(elems, |elem| vis.visit_pat(elem))
1263+
visit_thin_vec(elems, |elem| vis.visit_pat(elem))
12591264
}
12601265
PatKind::Paren(inner) => vis.visit_pat(inner),
12611266
PatKind::MacCall(mac) => vis.visit_mac_call(mac),
@@ -1312,18 +1317,18 @@ pub fn noop_visit_expr<T: MutVisitor>(
13121317
) {
13131318
match kind {
13141319
ExprKind::Box(expr) => vis.visit_expr(expr),
1315-
ExprKind::Array(exprs) => visit_exprs(exprs, vis),
1320+
ExprKind::Array(exprs) => visit_thin_exprs(exprs, vis),
13161321
ExprKind::ConstBlock(anon_const) => {
13171322
vis.visit_anon_const(anon_const);
13181323
}
13191324
ExprKind::Repeat(expr, count) => {
13201325
vis.visit_expr(expr);
13211326
vis.visit_anon_const(count);
13221327
}
1323-
ExprKind::Tup(exprs) => visit_exprs(exprs, vis),
1328+
ExprKind::Tup(exprs) => visit_thin_exprs(exprs, vis),
13241329
ExprKind::Call(f, args) => {
13251330
vis.visit_expr(f);
1326-
visit_exprs(args, vis);
1331+
visit_thin_exprs(args, vis);
13271332
}
13281333
ExprKind::MethodCall(box MethodCall {
13291334
seg: PathSegment { ident, id, args: seg_args },
@@ -1335,7 +1340,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
13351340
vis.visit_id(id);
13361341
visit_opt(seg_args, |args| vis.visit_generic_args(args));
13371342
vis.visit_method_receiver_expr(receiver);
1338-
visit_exprs(call_args, vis);
1343+
visit_thin_exprs(call_args, vis);
13391344
vis.visit_span(span);
13401345
}
13411346
ExprKind::Binary(_binop, lhs, rhs) => {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_session::errors::report_lit_error;
1818
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
1919
use rustc_span::symbol::{sym, Ident, Symbol};
2020
use rustc_span::DUMMY_SP;
21-
use thin_vec::thin_vec;
21+
use thin_vec::{thin_vec, ThinVec};
2222

2323
impl<'hir> LoweringContext<'_, 'hir> {
2424
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
@@ -367,7 +367,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
367367
fn lower_legacy_const_generics(
368368
&mut self,
369369
mut f: Expr,
370-
args: Vec<AstP<Expr>>,
370+
args: ThinVec<AstP<Expr>>,
371371
legacy_args_idx: &[usize],
372372
) -> hir::ExprKind<'hir> {
373373
let ExprKind::Path(None, path) = &mut f.kind else {

compiler/rustc_ast_passes/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
itertools = "0.10.1"
8-
tracing = "0.1"
8+
rustc_ast = { path = "../rustc_ast" }
99
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1010
rustc_attr = { path = "../rustc_attr" }
1111
rustc_data_structures = { path = "../rustc_data_structures" }
@@ -16,4 +16,5 @@ rustc_parse = { path = "../rustc_parse" }
1616
rustc_session = { path = "../rustc_session" }
1717
rustc_span = { path = "../rustc_span" }
1818
rustc_target = { path = "../rustc_target" }
19-
rustc_ast = { path = "../rustc_ast" }
19+
thin-vec = "0.2.12"
20+
tracing = "0.1"

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use rustc_span::source_map::Spanned;
1010
use rustc_span::symbol::sym;
1111
use rustc_span::Span;
1212
use rustc_target::spec::abi;
13+
use thin_vec::ThinVec;
14+
use tracing::debug;
1315

1416
use crate::errors::ForbiddenLifetimeBound;
1517

@@ -250,7 +252,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
250252

251253
ast::ItemKind::Struct(..) => {
252254
for attr in self.sess.filter_by_name(&i.attrs, sym::repr) {
253-
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
255+
for item in attr.meta_item_list().unwrap_or_else(ThinVec::new) {
254256
if item.has_name(sym::simd) {
255257
gate_feature_post!(
256258
&self,

compiler/rustc_ast_passes/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#![feature(let_chains)]
1212
#![recursion_limit = "256"]
1313

14-
#[macro_use]
15-
extern crate tracing;
16-
1714
pub mod ast_validation;
1815
mod errors;
1916
pub mod feature_gate;

compiler/rustc_ast_pretty/src/pprust/tests.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::*;
33
use rustc_ast as ast;
44
use rustc_span::create_default_session_globals_then;
55
use rustc_span::symbol::Ident;
6+
use thin_vec::ThinVec;
67

78
fn fun_to_string(
89
decl: &ast::FnDecl,
@@ -27,8 +28,10 @@ fn test_fun_to_string() {
2728
create_default_session_globals_then(|| {
2829
let abba_ident = Ident::from_str("abba");
2930

30-
let decl =
31-
ast::FnDecl { inputs: Vec::new(), output: ast::FnRetTy::Default(rustc_span::DUMMY_SP) };
31+
let decl = ast::FnDecl {
32+
inputs: ThinVec::new(),
33+
output: ast::FnRetTy::Default(rustc_span::DUMMY_SP),
34+
};
3235
let generics = ast::Generics::default();
3336
assert_eq!(
3437
fun_to_string(&decl, ast::FnHeader::default(), abba_ident, &generics),

0 commit comments

Comments
 (0)