Skip to content

Commit 68b527e

Browse files
committed
Use ThinVec in ast::Generics and related types.
1 parent fe11492 commit 68b527e

File tree

12 files changed

+98
-96
lines changed

12 files changed

+98
-96
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3171,6 +3171,7 @@ version = "0.0.0"
31713171
dependencies = [
31723172
"rustc_ast",
31733173
"rustc_span",
3174+
"thin-vec",
31743175
]
31753176

31763177
[[package]]

Diff for: 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`).
@@ -1187,7 +1187,7 @@ impl Expr {
11871187
pub fn to_bound(&self) -> Option<GenericBound> {
11881188
match &self.kind {
11891189
ExprKind::Path(None, path) => Some(GenericBound::Trait(
1190-
PolyTraitRef::new(Vec::new(), path.clone(), self.span),
1190+
PolyTraitRef::new(ThinVec::new(), path.clone(), self.span),
11911191
TraitBoundModifier::None,
11921192
)),
11931193
_ => None,
@@ -1564,7 +1564,7 @@ pub enum ClosureBinder {
15641564
/// for<'a, 'b> |_: &'a (), _: &'b ()| { ... }
15651565
/// ^^^^^^ -- this
15661566
/// ```
1567-
generic_params: P<[GenericParam]>,
1567+
generic_params: ThinVec<GenericParam>,
15681568
},
15691569
}
15701570

@@ -2025,7 +2025,7 @@ impl Ty {
20252025
pub struct BareFnTy {
20262026
pub unsafety: Unsafe,
20272027
pub ext: Extern,
2028-
pub generic_params: Vec<GenericParam>,
2028+
pub generic_params: ThinVec<GenericParam>,
20292029
pub decl: P<FnDecl>,
20302030
/// Span of the `fn(...) -> ...` part.
20312031
pub decl_span: Span,
@@ -2611,7 +2611,7 @@ pub struct TraitRef {
26112611
#[derive(Clone, Encodable, Decodable, Debug)]
26122612
pub struct PolyTraitRef {
26132613
/// The `'a` in `for<'a> Foo<&'a T>`.
2614-
pub bound_generic_params: Vec<GenericParam>,
2614+
pub bound_generic_params: ThinVec<GenericParam>,
26152615

26162616
/// The `Foo<&'a T>` in `<'a> Foo<&'a T>`.
26172617
pub trait_ref: TraitRef,
@@ -2620,7 +2620,7 @@ pub struct PolyTraitRef {
26202620
}
26212621

26222622
impl PolyTraitRef {
2623-
pub fn new(generic_params: Vec<GenericParam>, path: Path, span: Span) -> Self {
2623+
pub fn new(generic_params: ThinVec<GenericParam>, path: Path, span: Span) -> Self {
26242624
PolyTraitRef {
26252625
bound_generic_params: generic_params,
26262626
trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
@@ -3075,15 +3075,15 @@ mod size_asserts {
30753075
static_assert_size!(Block, 48);
30763076
static_assert_size!(Expr, 104);
30773077
static_assert_size!(ExprKind, 72);
3078-
static_assert_size!(Fn, 192);
3078+
static_assert_size!(Fn, 176);
30793079
static_assert_size!(ForeignItem, 96);
30803080
static_assert_size!(ForeignItemKind, 24);
30813081
static_assert_size!(GenericArg, 24);
3082-
static_assert_size!(GenericBound, 72);
3083-
static_assert_size!(Generics, 72);
3084-
static_assert_size!(Impl, 184);
3085-
static_assert_size!(Item, 184);
3086-
static_assert_size!(ItemKind, 112);
3082+
static_assert_size!(GenericBound, 56);
3083+
static_assert_size!(Generics, 56);
3084+
static_assert_size!(Impl, 168);
3085+
static_assert_size!(Item, 168);
3086+
static_assert_size!(ItemKind, 96);
30873087
static_assert_size!(Lit, 48);
30883088
static_assert_size!(LitKind, 24);
30893089
static_assert_size!(Local, 72);

Diff for: 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
}

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ use rustc_span::hygiene::MacroKind;
6868
use rustc_span::source_map::DesugaringKind;
6969
use rustc_span::symbol::{kw, sym, Ident, Symbol};
7070
use rustc_span::{Span, DUMMY_SP};
71-
7271
use smallvec::SmallVec;
7372
use std::collections::hash_map::Entry;
73+
use thin_vec::ThinVec;
7474

7575
macro_rules! arena_vec {
7676
($this:expr; $($x:expr),*) => (
@@ -1183,7 +1183,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11831183
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
11841184
let bound = this.lower_poly_trait_ref(
11851185
&PolyTraitRef {
1186-
bound_generic_params: vec![],
1186+
bound_generic_params: ThinVec::new(),
11871187
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
11881188
span: t.span
11891189
},

Diff for: 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"

Diff for: 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

@@ -1708,7 +1708,7 @@ impl<'a> State<'a> {
17081708
self.ibox(INDENT_UNIT);
17091709
self.print_formal_generic_params(generic_params);
17101710
let generics = ast::Generics {
1711-
params: Vec::new(),
1711+
params: ThinVec::new(),
17121712
where_clause: ast::WhereClause {
17131713
has_where_token: false,
17141714
predicates: Vec::new(),

Diff for: compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ use rustc_span::Span;
175175
use std::cell::RefCell;
176176
use std::iter;
177177
use std::vec;
178-
use thin_vec::thin_vec;
178+
use thin_vec::{thin_vec, ThinVec};
179179
use ty::{Bounds, Path, Ref, Self_, Ty};
180180

181181
pub mod ty;
@@ -293,7 +293,7 @@ pub fn combine_substructure(
293293
}
294294

295295
struct TypeParameter {
296-
bound_generic_params: Vec<ast::GenericParam>,
296+
bound_generic_params: ThinVec<ast::GenericParam>,
297297
ty: P<ast::Ty>,
298298
}
299299

@@ -360,7 +360,7 @@ fn find_type_parameters(
360360
struct Visitor<'a, 'b> {
361361
cx: &'a ExtCtxt<'b>,
362362
ty_param_names: &'a [Symbol],
363-
bound_generic_params_stack: Vec<ast::GenericParam>,
363+
bound_generic_params_stack: ThinVec<ast::GenericParam>,
364364
type_params: Vec<TypeParameter>,
365365
}
366366

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

Diff for: 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
}

Diff for: compiler/rustc_parse/src/parser/expr.rs

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

20482048
self.sess.gated_spans.gate(sym::closure_lifetime_binder, span);
20492049

2050-
ClosureBinder::For { span, generic_params: P::from_vec(lifetime_defs) }
2050+
ClosureBinder::For { span, generic_params: lifetime_defs }
20512051
} else {
20522052
ClosureBinder::NotPresent
20532053
};

Diff for: 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,

Diff for: 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)