Skip to content

Commit dd7aff5

Browse files
committed
Use ThinVec in ast::Generics and related types.
1 parent 06228d6 commit dd7aff5

File tree

12 files changed

+97
-97
lines changed

12 files changed

+97
-97
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3698,6 +3698,7 @@ version = "0.0.0"
36983698
dependencies = [
36993699
"rustc_ast",
37003700
"rustc_span",
3701+
"thin-vec",
37013702
]
37023703

37033704
[[package]]

compiler/rustc_ast/src/ast.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,15 @@ impl GenericParam {
384384
/// a function, enum, trait, etc.
385385
#[derive(Clone, Encodable, Decodable, Debug)]
386386
pub struct Generics {
387-
pub params: Vec<GenericParam>,
387+
pub params: ThinVec<GenericParam>,
388388
pub where_clause: WhereClause,
389389
pub span: Span,
390390
}
391391

392392
impl Default for Generics {
393393
/// Creates an instance of `Generics`.
394394
fn default() -> Generics {
395-
Generics { params: Vec::new(), where_clause: Default::default(), span: DUMMY_SP }
395+
Generics { params: ThinVec::new(), where_clause: Default::default(), span: DUMMY_SP }
396396
}
397397
}
398398

@@ -441,7 +441,7 @@ impl WherePredicate {
441441
pub struct WhereBoundPredicate {
442442
pub span: Span,
443443
/// Any generics from a `for` binding.
444-
pub bound_generic_params: Vec<GenericParam>,
444+
pub bound_generic_params: ThinVec<GenericParam>,
445445
/// The type being bounded.
446446
pub bounded_ty: P<Ty>,
447447
/// Trait and lifetime bounds (`Clone + Send + 'static`).
@@ -1169,7 +1169,7 @@ impl Expr {
11691169
pub fn to_bound(&self) -> Option<GenericBound> {
11701170
match &self.kind {
11711171
ExprKind::Path(None, path) => Some(GenericBound::Trait(
1172-
PolyTraitRef::new(Vec::new(), path.clone(), self.span),
1172+
PolyTraitRef::new(ThinVec::new(), path.clone(), self.span),
11731173
TraitBoundModifier::None,
11741174
)),
11751175
_ => None,
@@ -1574,7 +1574,7 @@ pub enum ClosureBinder {
15741574
/// for<'a, 'b> |_: &'a (), _: &'b ()| { ... }
15751575
/// ^^^^^^ -- this
15761576
/// ```
1577-
generic_params: P<[GenericParam]>,
1577+
generic_params: ThinVec<GenericParam>,
15781578
},
15791579
}
15801580

@@ -2056,7 +2056,7 @@ impl Ty {
20562056
pub struct BareFnTy {
20572057
pub unsafety: Unsafe,
20582058
pub ext: Extern,
2059-
pub generic_params: Vec<GenericParam>,
2059+
pub generic_params: ThinVec<GenericParam>,
20602060
pub decl: P<FnDecl>,
20612061
/// Span of the `fn(...) -> ...` part.
20622062
pub decl_span: Span,
@@ -2636,7 +2636,7 @@ pub struct TraitRef {
26362636
#[derive(Clone, Encodable, Decodable, Debug)]
26372637
pub struct PolyTraitRef {
26382638
/// The `'a` in `for<'a> Foo<&'a T>`.
2639-
pub bound_generic_params: Vec<GenericParam>,
2639+
pub bound_generic_params: ThinVec<GenericParam>,
26402640

26412641
/// The `Foo<&'a T>` in `<'a> Foo<&'a T>`.
26422642
pub trait_ref: TraitRef,
@@ -2645,7 +2645,7 @@ pub struct PolyTraitRef {
26452645
}
26462646

26472647
impl PolyTraitRef {
2648-
pub fn new(generic_params: Vec<GenericParam>, path: Path, span: Span) -> Self {
2648+
pub fn new(generic_params: ThinVec<GenericParam>, path: Path, span: Span) -> Self {
26492649
PolyTraitRef {
26502650
bound_generic_params: generic_params,
26512651
trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
@@ -3115,15 +3115,15 @@ mod size_asserts {
31153115
static_assert_size!(Block, 48);
31163116
static_assert_size!(Expr, 72);
31173117
static_assert_size!(ExprKind, 40);
3118-
static_assert_size!(Fn, 184);
3118+
static_assert_size!(Fn, 168);
31193119
static_assert_size!(ForeignItem, 96);
31203120
static_assert_size!(ForeignItemKind, 24);
31213121
static_assert_size!(GenericArg, 24);
3122-
static_assert_size!(GenericBound, 72);
3123-
static_assert_size!(Generics, 72);
3124-
static_assert_size!(Impl, 184);
3125-
static_assert_size!(Item, 184);
3126-
static_assert_size!(ItemKind, 112);
3122+
static_assert_size!(GenericBound, 56);
3123+
static_assert_size!(Generics, 56);
3124+
static_assert_size!(Impl, 168);
3125+
static_assert_size!(Item, 168);
3126+
static_assert_size!(ItemKind, 96);
31273127
static_assert_size!(LitKind, 24);
31283128
static_assert_size!(Local, 72);
31293129
static_assert_size!(MetaItemLit, 40);

compiler/rustc_ast/src/mut_visit.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,7 @@ pub fn noop_visit_closure_binder<T: MutVisitor>(binder: &mut ClosureBinder, vis:
839839
match binder {
840840
ClosureBinder::NotPresent => {}
841841
ClosureBinder::For { span: _, generic_params } => {
842-
let mut vec = std::mem::take(generic_params).into_vec();
843-
vec.flat_map_in_place(|param| vis.flat_map_generic_param(param));
844-
*generic_params = P::from_vec(vec);
842+
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
845843
}
846844
}
847845
}

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ use rustc_span::hygiene::MacroKind;
6666
use rustc_span::source_map::DesugaringKind;
6767
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6868
use rustc_span::{Span, DUMMY_SP};
69-
7069
use smallvec::SmallVec;
7170
use std::collections::hash_map::Entry;
71+
use thin_vec::ThinVec;
7272

7373
macro_rules! arena_vec {
7474
($this:expr; $($x:expr),*) => (
@@ -1207,7 +1207,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12071207
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
12081208
let bound = this.lower_poly_trait_ref(
12091209
&PolyTraitRef {
1210-
bound_generic_params: vec![],
1210+
bound_generic_params: ThinVec::new(),
12111211
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
12121212
span: t.span
12131213
},

compiler/rustc_ast_pretty/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ edition = "2021"
88
[dependencies]
99
rustc_ast = { path = "../rustc_ast" }
1010
rustc_span = { path = "../rustc_span" }
11+
thin-vec = "0.2.12"

compiler/rustc_ast_pretty/src/pprust/state.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod item;
44

55
use crate::pp::Breaks::{Consistent, Inconsistent};
66
use crate::pp::{self, Breaks};
7-
7+
use rustc_ast::attr::AttrIdGenerator;
88
use rustc_ast::ptr::P;
99
use rustc_ast::token::{self, BinOpToken, CommentKind, Delimiter, Nonterminal, Token, TokenKind};
1010
use rustc_ast::tokenstream::{TokenStream, TokenTree};
@@ -20,9 +20,8 @@ use rustc_span::edition::Edition;
2020
use rustc_span::source_map::{SourceMap, Spanned};
2121
use rustc_span::symbol::{kw, sym, Ident, IdentPrinter, Symbol};
2222
use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
23-
24-
use rustc_ast::attr::AttrIdGenerator;
2523
use std::borrow::Cow;
24+
use thin_vec::ThinVec;
2625

2726
pub use self::delimited::IterDelimited;
2827

@@ -1722,7 +1721,7 @@ impl<'a> State<'a> {
17221721
self.ibox(INDENT_UNIT);
17231722
self.print_formal_generic_params(generic_params);
17241723
let generics = ast::Generics {
1725-
params: Vec::new(),
1724+
params: ThinVec::new(),
17261725
where_clause: ast::WhereClause {
17271726
has_where_token: false,
17281727
predicates: Vec::new(),

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ use std::cell::RefCell;
177177
use std::iter;
178178
use std::ops::Not;
179179
use std::vec;
180-
use thin_vec::thin_vec;
180+
use thin_vec::{thin_vec, ThinVec};
181181
use ty::{Bounds, Path, Ref, Self_, Ty};
182182

183183
pub mod ty;
@@ -318,7 +318,7 @@ pub fn combine_substructure(
318318
}
319319

320320
struct TypeParameter {
321-
bound_generic_params: Vec<ast::GenericParam>,
321+
bound_generic_params: ThinVec<ast::GenericParam>,
322322
ty: P<ast::Ty>,
323323
}
324324

@@ -385,7 +385,7 @@ fn find_type_parameters(
385385
struct Visitor<'a, 'b> {
386386
cx: &'a ExtCtxt<'b>,
387387
ty_param_names: &'a [Symbol],
388-
bound_generic_params_stack: Vec<ast::GenericParam>,
388+
bound_generic_params_stack: ThinVec<ast::GenericParam>,
389389
type_params: Vec<TypeParameter>,
390390
}
391391

@@ -422,7 +422,7 @@ fn find_type_parameters(
422422
let mut visitor = Visitor {
423423
cx,
424424
ty_param_names,
425-
bound_generic_params_stack: Vec::new(),
425+
bound_generic_params_stack: ThinVec::new(),
426426
type_params: Vec::new(),
427427
};
428428
visit::Visitor::visit_ty(&mut visitor, ty);
@@ -594,7 +594,7 @@ impl<'a> TraitDef<'a> {
594594
let span = generics.span.with_ctxt(ctxt);
595595

596596
// Create the generic parameters
597-
let params: Vec<_> = generics
597+
let params: ThinVec<_> = generics
598598
.params
599599
.iter()
600600
.map(|param| match &param.kind {

compiler/rustc_expand/src/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'a> ExtCtxt<'a> {
125125

126126
pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
127127
ast::PolyTraitRef {
128-
bound_generic_params: Vec::new(),
128+
bound_generic_params: ThinVec::new(),
129129
trait_ref: self.trait_ref(path),
130130
span,
131131
}

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ impl<'a> Parser<'a> {
20952095

20962096
self.sess.gated_spans.gate(sym::closure_lifetime_binder, span);
20972097

2098-
ClosureBinder::For { span, generic_params: P::from_vec(lifetime_defs) }
2098+
ClosureBinder::For { span, generic_params: lifetime_defs }
20992099
} else {
21002100
ClosureBinder::NotPresent
21012101
};

compiler/rustc_parse/src/parser/generics.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_ast::{
1414
use rustc_errors::{Applicability, PResult};
1515
use rustc_span::symbol::{kw, Ident};
1616
use rustc_span::Span;
17+
use thin_vec::ThinVec;
1718

1819
enum PredicateOrStructBody {
1920
Predicate(ast::WherePredicate),
@@ -121,8 +122,8 @@ impl<'a> Parser<'a> {
121122

122123
/// Parses a (possibly empty) list of lifetime and type parameters, possibly including
123124
/// a trailing comma and erroneous trailing attributes.
124-
pub(super) fn parse_generic_params(&mut self) -> PResult<'a, Vec<ast::GenericParam>> {
125-
let mut params = Vec::new();
125+
pub(super) fn parse_generic_params(&mut self) -> PResult<'a, ThinVec<ast::GenericParam>> {
126+
let mut params = ThinVec::new();
126127
let mut done = false;
127128
while !done {
128129
let attrs = self.parse_outer_attributes()?;
@@ -251,7 +252,7 @@ impl<'a> Parser<'a> {
251252
self.expect_gt()?;
252253
(params, span_lo.to(self.prev_token.span))
253254
} else {
254-
(vec![], self.prev_token.span.shrink_to_hi())
255+
(ThinVec::new(), self.prev_token.span.shrink_to_hi())
255256
};
256257
Ok(ast::Generics {
257258
params,

compiler/rustc_parse/src/parser/ty.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_errors::{Applicability, PResult};
2121
use rustc_span::source_map::Span;
2222
use rustc_span::symbol::{kw, sym, Ident};
2323
use rustc_span::Symbol;
24-
use thin_vec::thin_vec;
24+
use thin_vec::{thin_vec, ThinVec};
2525

2626
/// Any `?` or `~const` modifiers that appear at the start of a bound.
2727
struct BoundModifiers {
@@ -273,7 +273,7 @@ impl<'a> Parser<'a> {
273273
TyKind::Infer
274274
} else if self.check_fn_front_matter(false, Case::Sensitive) {
275275
// Function pointer type
276-
self.parse_ty_bare_fn(lo, Vec::new(), None, recover_return_sign)?
276+
self.parse_ty_bare_fn(lo, ThinVec::new(), None, recover_return_sign)?
277277
} else if self.check_keyword(kw::For) {
278278
// Function pointer type or bound list (trait object type) starting with a poly-trait.
279279
// `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T`
@@ -352,7 +352,7 @@ impl<'a> Parser<'a> {
352352
match ty.kind {
353353
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
354354
TyKind::Path(None, path) if maybe_bounds => {
355-
self.parse_remaining_bounds_path(Vec::new(), path, lo, true)
355+
self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true)
356356
}
357357
TyKind::TraitObject(bounds, TraitObjectSyntax::None)
358358
if maybe_bounds && bounds.len() == 1 && !trailing_plus =>
@@ -378,7 +378,7 @@ impl<'a> Parser<'a> {
378378

379379
fn parse_remaining_bounds_path(
380380
&mut self,
381-
generic_params: Vec<GenericParam>,
381+
generic_params: ThinVec<GenericParam>,
382382
path: ast::Path,
383383
lo: Span,
384384
parse_plus: bool,
@@ -511,7 +511,7 @@ impl<'a> Parser<'a> {
511511
fn parse_ty_bare_fn(
512512
&mut self,
513513
lo: Span,
514-
mut params: Vec<GenericParam>,
514+
mut params: ThinVec<GenericParam>,
515515
param_insertion_point: Option<Span>,
516516
recover_return_sign: RecoverReturnSign,
517517
) -> PResult<'a, TyKind> {
@@ -545,13 +545,13 @@ impl<'a> Parser<'a> {
545545
fn recover_fn_ptr_with_generics(
546546
&mut self,
547547
lo: Span,
548-
params: &mut Vec<GenericParam>,
548+
params: &mut ThinVec<GenericParam>,
549549
param_insertion_point: Option<Span>,
550550
) -> PResult<'a, ()> {
551551
let generics = self.parse_generics()?;
552552
let arity = generics.params.len();
553553

554-
let mut lifetimes: Vec<_> = generics
554+
let mut lifetimes: ThinVec<_> = generics
555555
.params
556556
.into_iter()
557557
.filter(|param| matches!(param.kind, ast::GenericParamKind::Lifetime))
@@ -662,7 +662,7 @@ impl<'a> Parser<'a> {
662662
})))
663663
} else if allow_plus == AllowPlus::Yes && self.check_plus() {
664664
// `Trait1 + Trait2 + 'a`
665-
self.parse_remaining_bounds_path(Vec::new(), path, lo, true)
665+
self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true)
666666
} else {
667667
// Just a type path.
668668
Ok(TyKind::Path(None, path))
@@ -993,7 +993,7 @@ impl<'a> Parser<'a> {
993993
}
994994

995995
/// Optionally parses `for<$generic_params>`.
996-
pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<GenericParam>> {
996+
pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, ThinVec<GenericParam>> {
997997
if self.eat_keyword(kw::For) {
998998
self.expect_lt()?;
999999
let params = self.parse_generic_params()?;
@@ -1002,7 +1002,7 @@ impl<'a> Parser<'a> {
10021002
// parameters, and the lifetime parameters must not have bounds.
10031003
Ok(params)
10041004
} else {
1005-
Ok(Vec::new())
1005+
Ok(ThinVec::new())
10061006
}
10071007
}
10081008

@@ -1012,7 +1012,7 @@ impl<'a> Parser<'a> {
10121012
fn recover_fn_trait_with_lifetime_params(
10131013
&mut self,
10141014
fn_path: &mut ast::Path,
1015-
lifetime_defs: &mut Vec<GenericParam>,
1015+
lifetime_defs: &mut ThinVec<GenericParam>,
10161016
) -> PResult<'a, ()> {
10171017
let fn_path_segment = fn_path.segments.last_mut().unwrap();
10181018
let generic_args = if let Some(p_args) = &fn_path_segment.args {
@@ -1072,7 +1072,7 @@ impl<'a> Parser<'a> {
10721072
kind: ast::GenericParamKind::Lifetime,
10731073
colon_span: None,
10741074
})
1075-
.collect::<Vec<GenericParam>>();
1075+
.collect::<ThinVec<GenericParam>>();
10761076
lifetime_defs.append(&mut generic_params);
10771077

10781078
let generic_args_span = generic_args.span();

0 commit comments

Comments
 (0)