Skip to content

Commit

Permalink
Use AttrVec in more places.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nnethercote committed Aug 21, 2022
1 parent 6884e04 commit 05a0dfe
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 11 deletions.
5 changes: 1 addition & 4 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
}

/// Returns attributes that are within `outer_span`.
pub(crate) fn filter_inline_attrs(
attrs: &[ast::Attribute],
outer_span: Span,
) -> Vec<ast::Attribute> {
pub(crate) fn filter_inline_attrs(attrs: &[ast::Attribute], outer_span: Span) -> ast::AttrVec {
attrs
.iter()
.filter(|a| outer_span.lo() <= a.span.lo() && a.span.hi() <= outer_span.hi())
Expand Down
4 changes: 2 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub(crate) struct UseTree {
// Additional fields for top level use items.
// Should we have another struct for top-level use items rather than reusing this?
visibility: Option<ast::Visibility>,
attrs: Option<Vec<ast::Attribute>>,
attrs: Option<ast::AttrVec>,
}

impl PartialEq for UseTree {
Expand Down Expand Up @@ -417,7 +417,7 @@ impl UseTree {
list_item: Option<ListItem>,
visibility: Option<ast::Visibility>,
opt_lo: Option<BytePos>,
attrs: Option<Vec<ast::Attribute>>,
attrs: Option<ast::AttrVec>,
) -> UseTree {
let span = if let Some(lo) = opt_lo {
mk_sp(lo, a.span.hi())
Expand Down
8 changes: 4 additions & 4 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
pub(crate) struct Module<'a> {
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
pub(crate) items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
inner_attr: Vec<ast::Attribute>,
inner_attr: ast::AttrVec,
pub(crate) span: Span,
}

Expand All @@ -35,7 +35,7 @@ impl<'a> Module<'a> {
mod_span: Span,
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
mod_items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
mod_attrs: Cow<'a, Vec<ast::Attribute>>,
mod_attrs: Cow<'a, ast::AttrVec>,
) -> Self {
let inner_attr = mod_attrs
.iter()
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
module_item.item.span,
Some(Cow::Owned(sub_mod_kind.clone())),
Cow::Owned(vec![]),
Cow::Owned(vec![]),
Cow::Owned(ast::AttrVec::new()),
),
)?;
}
Expand All @@ -185,7 +185,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
span,
Some(Cow::Owned(sub_mod_kind.clone())),
Cow::Owned(vec![]),
Cow::Owned(vec![]),
Cow::Owned(ast::AttrVec::new()),
),
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'a> Parser<'a> {
sess: &'a ParseSess,
path: &Path,
span: Span,
) -> Result<(Vec<ast::Attribute>, Vec<ptr::P<ast::Item>>, Span), ParserError> {
) -> Result<(ast::AttrVec, Vec<ptr::P<ast::Item>>, Span), ParserError> {
let result = catch_unwind(AssertUnwindSafe(|| {
let mut parser = new_parser_from_file(sess.inner(), path, Some(span));
match parser.parse_mod(&TokenKind::Eof) {
Expand Down

0 comments on commit 05a0dfe

Please sign in to comment.