Skip to content

Commit ade273d

Browse files
committed
Use ThinVec in ast::Generics and related types.
1 parent 8738cfd commit ade273d

File tree

12 files changed

+98
-96
lines changed

12 files changed

+98
-96
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,7 @@ version = "0.0.0"
36113611
dependencies = [
36123612
"rustc_ast",
36133613
"rustc_span",
3614+
"thin-vec",
36143615
]
36153616

36163617
[[package]]

compiler/rustc_ast/src/ast.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ impl GenericParam {
414414
/// a function, enum, trait, etc.
415415
#[derive(Clone, Encodable, Decodable, Debug)]
416416
pub struct Generics {
417-
pub params: Vec<GenericParam>,
417+
pub params: ThinVec<GenericParam>,
418418
pub where_clause: WhereClause,
419419
pub span: Span,
420420
}
@@ -423,7 +423,7 @@ impl Default for Generics {
423423
/// Creates an instance of `Generics`.
424424
fn default() -> Generics {
425425
Generics {
426-
params: Vec::new(),
426+
params: ThinVec::new(),
427427
where_clause: WhereClause {
428428
has_where_token: false,
429429
predicates: Vec::new(),
@@ -473,7 +473,7 @@ impl WherePredicate {
473473
pub struct WhereBoundPredicate {
474474
pub span: Span,
475475
/// Any generics from a `for` binding.
476-
pub bound_generic_params: Vec<GenericParam>,
476+
pub bound_generic_params: ThinVec<GenericParam>,
477477
/// The type being bounded.
478478
pub bounded_ty: P<Ty>,
479479
/// Trait and lifetime bounds (`Clone + Send + 'static`).
@@ -1154,7 +1154,7 @@ impl Expr {
11541154
pub fn to_bound(&self) -> Option<GenericBound> {
11551155
match &self.kind {
11561156
ExprKind::Path(None, path) => Some(GenericBound::Trait(
1157-
PolyTraitRef::new(Vec::new(), path.clone(), self.span),
1157+
PolyTraitRef::new(ThinVec::new(), path.clone(), self.span),
11581158
TraitBoundModifier::None,
11591159
)),
11601160
_ => None,
@@ -1531,7 +1531,7 @@ pub enum ClosureBinder {
15311531
/// for<'a, 'b> |_: &'a (), _: &'b ()| { ... }
15321532
/// ^^^^^^ -- this
15331533
/// ```
1534-
generic_params: P<[GenericParam]>,
1534+
generic_params: ThinVec<GenericParam>,
15351535
},
15361536
}
15371537

@@ -1992,7 +1992,7 @@ impl Ty {
19921992
pub struct BareFnTy {
19931993
pub unsafety: Unsafe,
19941994
pub ext: Extern,
1995-
pub generic_params: Vec<GenericParam>,
1995+
pub generic_params: ThinVec<GenericParam>,
19961996
pub decl: P<FnDecl>,
19971997
/// Span of the `fn(...) -> ...` part.
19981998
pub decl_span: Span,
@@ -2578,7 +2578,7 @@ pub struct TraitRef {
25782578
#[derive(Clone, Encodable, Decodable, Debug)]
25792579
pub struct PolyTraitRef {
25802580
/// The `'a` in `for<'a> Foo<&'a T>`.
2581-
pub bound_generic_params: Vec<GenericParam>,
2581+
pub bound_generic_params: ThinVec<GenericParam>,
25822582

25832583
/// The `Foo<&'a T>` in `<'a> Foo<&'a T>`.
25842584
pub trait_ref: TraitRef,
@@ -2587,7 +2587,7 @@ pub struct PolyTraitRef {
25872587
}
25882588

25892589
impl PolyTraitRef {
2590-
pub fn new(generic_params: Vec<GenericParam>, path: Path, span: Span) -> Self {
2590+
pub fn new(generic_params: ThinVec<GenericParam>, path: Path, span: Span) -> Self {
25912591
PolyTraitRef {
25922592
bound_generic_params: generic_params,
25932593
trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
@@ -3042,14 +3042,14 @@ mod size_asserts {
30423042
static_assert_size!(Block, 48);
30433043
static_assert_size!(Expr, 104);
30443044
static_assert_size!(ExprKind, 72);
3045-
static_assert_size!(Fn, 192);
3045+
static_assert_size!(Fn, 176);
30463046
static_assert_size!(ForeignItem, 96);
30473047
static_assert_size!(ForeignItemKind, 24);
3048-
static_assert_size!(GenericBound, 72);
3049-
static_assert_size!(Generics, 72);
3050-
static_assert_size!(Impl, 184);
3051-
static_assert_size!(Item, 184);
3052-
static_assert_size!(ItemKind, 112);
3048+
static_assert_size!(GenericBound, 56);
3049+
static_assert_size!(Generics, 56);
3050+
static_assert_size!(Impl, 168);
3051+
static_assert_size!(Item, 168);
3052+
static_assert_size!(ItemKind, 96);
30533053
static_assert_size!(Lit, 48);
30543054
static_assert_size!(LitKind, 24);
30553055
static_assert_size!(Pat, 104);

compiler/rustc_ast/src/mut_visit.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,7 @@ pub fn noop_visit_closure_binder<T: MutVisitor>(binder: &mut ClosureBinder, vis:
829829
match binder {
830830
ClosureBinder::NotPresent => {}
831831
ClosureBinder::For { span: _, generic_params } => {
832-
let mut vec = std::mem::take(generic_params).into_vec();
833-
vec.flat_map_in_place(|param| vis.flat_map_generic_param(param));
834-
*generic_params = P::from_vec(vec);
832+
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
835833
}
836834
}
837835
}

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ use rustc_span::hygiene::MacroKind;
6363
use rustc_span::source_map::DesugaringKind;
6464
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6565
use rustc_span::{Span, DUMMY_SP};
66-
6766
use smallvec::SmallVec;
6867
use std::collections::hash_map::Entry;
68+
use thin_vec::ThinVec;
6969

7070
macro_rules! arena_vec {
7171
($this:expr; $($x:expr),*) => (
@@ -1196,7 +1196,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11961196
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
11971197
let bound = this.lower_poly_trait_ref(
11981198
&PolyTraitRef {
1199-
bound_generic_params: vec![],
1199+
bound_generic_params: ThinVec::new(),
12001200
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
12011201
span: t.span
12021202
},

compiler/rustc_ast_pretty/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ doctest = false
99
[dependencies]
1010
rustc_span = { path = "../rustc_span" }
1111
rustc_ast = { path = "../rustc_ast" }
12+
thin-vec = "0.2.8"

compiler/rustc_ast_pretty/src/pprust/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use rustc_span::edition::Edition;
2121
use rustc_span::source_map::{SourceMap, Spanned};
2222
use rustc_span::symbol::{kw, sym, Ident, IdentPrinter, Symbol};
2323
use rustc_span::{BytePos, FileName, Span};
24-
2524
use std::borrow::Cow;
25+
use thin_vec::ThinVec;
2626

2727
pub use self::delimited::IterDelimited;
2828

@@ -1714,7 +1714,7 @@ impl<'a> State<'a> {
17141714
self.ibox(INDENT_UNIT);
17151715
self.print_formal_generic_params(generic_params);
17161716
let generics = ast::Generics {
1717-
params: Vec::new(),
1717+
params: ThinVec::new(),
17181718
where_clause: ast::WhereClause {
17191719
has_where_token: false,
17201720
predicates: Vec::new(),

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ use rustc_span::Span;
173173
use std::cell::RefCell;
174174
use std::iter;
175175
use std::vec;
176-
use thin_vec::thin_vec;
176+
use thin_vec::{thin_vec, ThinVec};
177177
use ty::{Bounds, Path, Ref, Self_, Ty};
178178

179179
pub mod ty;
@@ -291,7 +291,7 @@ pub fn combine_substructure(
291291
}
292292

293293
struct TypeParameter {
294-
bound_generic_params: Vec<ast::GenericParam>,
294+
bound_generic_params: ThinVec<ast::GenericParam>,
295295
ty: P<ast::Ty>,
296296
}
297297

@@ -358,7 +358,7 @@ fn find_type_parameters(
358358
struct Visitor<'a, 'b> {
359359
cx: &'a ExtCtxt<'b>,
360360
ty_param_names: &'a [Symbol],
361-
bound_generic_params_stack: Vec<ast::GenericParam>,
361+
bound_generic_params_stack: ThinVec<ast::GenericParam>,
362362
type_params: Vec<TypeParameter>,
363363
}
364364

@@ -397,7 +397,7 @@ fn find_type_parameters(
397397
let mut visitor = Visitor {
398398
cx,
399399
ty_param_names,
400-
bound_generic_params_stack: Vec::new(),
400+
bound_generic_params_stack: ThinVec::new(),
401401
type_params: Vec::new(),
402402
};
403403
visit::Visitor::visit_ty(&mut visitor, ty);

compiler/rustc_expand/src/build.rs

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

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

compiler/rustc_parse/src/parser/expr.rs

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

20522052
self.sess.gated_spans.gate(sym::closure_lifetime_binder, span);
20532053

2054-
ClosureBinder::For { span, generic_params: P::from_vec(lifetime_defs) }
2054+
ClosureBinder::For { span, generic_params: lifetime_defs }
20552055
} else {
20562056
ClosureBinder::NotPresent
20572057
};

compiler/rustc_parse/src/parser/generics.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_ast::token;
44
use rustc_ast::{self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, WhereClause};
55
use rustc_errors::{Applicability, PResult};
66
use rustc_span::symbol::kw;
7+
use thin_vec::ThinVec;
78

89
impl<'a> Parser<'a> {
910
/// Parses bounds of a lifetime parameter `BOUND + BOUND + BOUND`, possibly with trailing `+`.
@@ -76,8 +77,8 @@ impl<'a> Parser<'a> {
7677

7778
/// Parses a (possibly empty) list of lifetime and type parameters, possibly including
7879
/// a trailing comma and erroneous trailing attributes.
79-
pub(super) fn parse_generic_params(&mut self) -> PResult<'a, Vec<ast::GenericParam>> {
80-
let mut params = Vec::new();
80+
pub(super) fn parse_generic_params(&mut self) -> PResult<'a, ThinVec<ast::GenericParam>> {
81+
let mut params = ThinVec::new();
8182
let mut done = false;
8283
while !done {
8384
let attrs = self.parse_outer_attributes()?;
@@ -195,7 +196,7 @@ impl<'a> Parser<'a> {
195196
self.expect_gt()?;
196197
(params, span_lo.to(self.prev_token.span))
197198
} else {
198-
(vec![], self.prev_token.span.shrink_to_hi())
199+
(ThinVec::new(), self.prev_token.span.shrink_to_hi())
199200
};
200201
Ok(ast::Generics {
201202
params,

compiler/rustc_parse/src/parser/ty.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_ast::{
1111
use rustc_errors::{pluralize, struct_span_err, Applicability, PResult};
1212
use rustc_span::source_map::Span;
1313
use rustc_span::symbol::{kw, sym};
14+
use thin_vec::ThinVec;
1415

1516
/// Any `?` or `~const` modifiers that appear at the start of a bound.
1617
struct BoundModifiers {
@@ -269,7 +270,7 @@ impl<'a> Parser<'a> {
269270
TyKind::Infer
270271
} else if self.check_fn_front_matter(false) {
271272
// Function pointer type
272-
self.parse_ty_bare_fn(lo, Vec::new(), recover_return_sign)?
273+
self.parse_ty_bare_fn(lo, ThinVec::new(), recover_return_sign)?
273274
} else if self.check_keyword(kw::For) {
274275
// Function pointer type or bound list (trait object type) starting with a poly-trait.
275276
// `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T`
@@ -343,7 +344,7 @@ impl<'a> Parser<'a> {
343344
match ty.kind {
344345
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
345346
TyKind::Path(None, path) if maybe_bounds => {
346-
self.parse_remaining_bounds_path(Vec::new(), path, lo, true)
347+
self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true)
347348
}
348349
TyKind::TraitObject(bounds, TraitObjectSyntax::None)
349350
if maybe_bounds && bounds.len() == 1 && !trailing_plus =>
@@ -370,7 +371,7 @@ impl<'a> Parser<'a> {
370371

371372
fn parse_remaining_bounds_path(
372373
&mut self,
373-
generic_params: Vec<GenericParam>,
374+
generic_params: ThinVec<GenericParam>,
374375
path: ast::Path,
375376
lo: Span,
376377
parse_plus: bool,
@@ -515,7 +516,7 @@ impl<'a> Parser<'a> {
515516
fn parse_ty_bare_fn(
516517
&mut self,
517518
lo: Span,
518-
params: Vec<GenericParam>,
519+
params: ThinVec<GenericParam>,
519520
recover_return_sign: RecoverReturnSign,
520521
) -> PResult<'a, TyKind> {
521522
let inherited_vis = rustc_ast::Visibility {
@@ -605,7 +606,7 @@ impl<'a> Parser<'a> {
605606
})))
606607
} else if allow_plus == AllowPlus::Yes && self.check_plus() {
607608
// `Trait1 + Trait2 + 'a`
608-
self.parse_remaining_bounds_path(Vec::new(), path, lo, true)
609+
self.parse_remaining_bounds_path(ThinVec::new(), path, lo, true)
609610
} else {
610611
// Just a type path.
611612
Ok(TyKind::Path(None, path))
@@ -877,7 +878,7 @@ impl<'a> Parser<'a> {
877878
}
878879

879880
/// Optionally parses `for<$generic_params>`.
880-
pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec<GenericParam>> {
881+
pub(super) fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, ThinVec<GenericParam>> {
881882
if self.eat_keyword(kw::For) {
882883
self.expect_lt()?;
883884
let params = self.parse_generic_params()?;
@@ -886,7 +887,7 @@ impl<'a> Parser<'a> {
886887
// parameters, and the lifetime parameters must not have bounds.
887888
Ok(params)
888889
} else {
889-
Ok(Vec::new())
890+
Ok(ThinVec::new())
890891
}
891892
}
892893

0 commit comments

Comments
 (0)