Skip to content

Commit 05a0dfe

Browse files
committed
Use AttrVec in more places.
In some places we use `Vec<Attribute>` and some places we use `ThinVec<Attribute>` (a.k.a. `AttrVec`). This results in various points where we have to convert between `Vec` and `ThinVec`. This commit changes the places that use `Vec<Attribute>` to use `AttrVec`. A lot of this is mechanical and boring, but there are some interesting parts: - It adds a few new methods to `ThinVec`. - It implements `MapInPlace` for `ThinVec`, and introduces a macro to avoid the repetition of this trait for `Vec`, `SmallVec`, and `ThinVec`. Overall, it makes the code a little nicer, and has little effect on performance. But it is a precursor to removing `rustc_data_structures::thin_vec::ThinVec` and replacing it with `thin_vec::ThinVec`, which is implemented more efficiently.
1 parent 6884e04 commit 05a0dfe

File tree

4 files changed

+8
-11
lines changed

4 files changed

+8
-11
lines changed

src/attr.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
4949
}
5050

5151
/// Returns attributes that are within `outer_span`.
52-
pub(crate) fn filter_inline_attrs(
53-
attrs: &[ast::Attribute],
54-
outer_span: Span,
55-
) -> Vec<ast::Attribute> {
52+
pub(crate) fn filter_inline_attrs(attrs: &[ast::Attribute], outer_span: Span) -> ast::AttrVec {
5653
attrs
5754
.iter()
5855
.filter(|a| outer_span.lo() <= a.span.lo() && a.span.hi() <= outer_span.hi())

src/imports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub(crate) struct UseTree {
116116
// Additional fields for top level use items.
117117
// Should we have another struct for top-level use items rather than reusing this?
118118
visibility: Option<ast::Visibility>,
119-
attrs: Option<Vec<ast::Attribute>>,
119+
attrs: Option<ast::AttrVec>,
120120
}
121121

122122
impl PartialEq for UseTree {
@@ -417,7 +417,7 @@ impl UseTree {
417417
list_item: Option<ListItem>,
418418
visibility: Option<ast::Visibility>,
419419
opt_lo: Option<BytePos>,
420-
attrs: Option<Vec<ast::Attribute>>,
420+
attrs: Option<ast::AttrVec>,
421421
) -> UseTree {
422422
let span = if let Some(lo) = opt_lo {
423423
mk_sp(lo, a.span.hi())

src/modules.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
2626
pub(crate) struct Module<'a> {
2727
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
2828
pub(crate) items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
29-
inner_attr: Vec<ast::Attribute>,
29+
inner_attr: ast::AttrVec,
3030
pub(crate) span: Span,
3131
}
3232

@@ -35,7 +35,7 @@ impl<'a> Module<'a> {
3535
mod_span: Span,
3636
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
3737
mod_items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
38-
mod_attrs: Cow<'a, Vec<ast::Attribute>>,
38+
mod_attrs: Cow<'a, ast::AttrVec>,
3939
) -> Self {
4040
let inner_attr = mod_attrs
4141
.iter()
@@ -158,7 +158,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
158158
module_item.item.span,
159159
Some(Cow::Owned(sub_mod_kind.clone())),
160160
Cow::Owned(vec![]),
161-
Cow::Owned(vec![]),
161+
Cow::Owned(ast::AttrVec::new()),
162162
),
163163
)?;
164164
}
@@ -185,7 +185,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
185185
span,
186186
Some(Cow::Owned(sub_mod_kind.clone())),
187187
Cow::Owned(vec![]),
188-
Cow::Owned(vec![]),
188+
Cow::Owned(ast::AttrVec::new()),
189189
),
190190
)?;
191191
}

src/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'a> Parser<'a> {
109109
sess: &'a ParseSess,
110110
path: &Path,
111111
span: Span,
112-
) -> Result<(Vec<ast::Attribute>, Vec<ptr::P<ast::Item>>, Span), ParserError> {
112+
) -> Result<(ast::AttrVec, Vec<ptr::P<ast::Item>>, Span), ParserError> {
113113
let result = catch_unwind(AssertUnwindSafe(|| {
114114
let mut parser = new_parser_from_file(sess.inner(), path, Some(span));
115115
match parser.parse_mod(&TokenKind::Eof) {

0 commit comments

Comments
 (0)