Skip to content

Commit 20ba687

Browse files
committed
parser_item_mod: avoid cloning outer attributes
1 parent 73d5970 commit 20ba687

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

src/librustc_parse/config.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,9 @@ fn is_cfg(attr: &Attribute) -> bool {
562562

563563
/// Process the potential `cfg` attributes on a module.
564564
/// Also determine if the module should be included in this configuration.
565-
pub fn process_configure_mod(
566-
sess: &ParseSess,
567-
cfg_mods: bool,
568-
attrs: &[Attribute],
569-
) -> (bool, Vec<Attribute>) {
565+
pub fn process_configure_mod(sess: &ParseSess, cfg_mods: bool, attrs: &mut Vec<Attribute>) -> bool {
570566
// Don't perform gated feature checking.
571567
let mut strip_unconfigured = StripUnconfigured { sess, features: None };
572-
let mut attrs = attrs.to_owned();
573-
strip_unconfigured.process_cfg_attrs(&mut attrs);
574-
(!cfg_mods || strip_unconfigured.in_cfg(&attrs), attrs)
568+
strip_unconfigured.process_cfg_attrs(attrs);
569+
!cfg_mods || strip_unconfigured.in_cfg(&attrs)
575570
}

src/librustc_parse/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'a> Parser<'a> {
168168
self.parse_item_impl(unsafety, defaultness)?
169169
} else if self.eat_keyword(kw::Mod) {
170170
// MODULE ITEM
171-
self.parse_item_mod(&attrs[..])?
171+
self.parse_item_mod(attrs)?
172172
} else if self.eat_keyword(kw::Type) {
173173
// TYPE ITEM
174174
let (ident, ty, generics) = self.parse_type_alias()?;

src/librustc_parse/parser/module.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,34 @@ impl<'a> Parser<'a> {
4040
}
4141

4242
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
43-
pub(super) fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
44-
let (in_cfg, outer_attrs) =
45-
crate::config::process_configure_mod(self.sess, self.cfg_mods, outer_attrs);
43+
pub(super) fn parse_item_mod(&mut self, attrs: &mut Vec<Attribute>) -> PResult<'a, ItemInfo> {
44+
let in_cfg = crate::config::process_configure_mod(self.sess, self.cfg_mods, attrs);
4645

4746
let id_span = self.token.span;
4847
let id = self.parse_ident()?;
49-
if self.eat(&token::Semi) {
48+
let (module, mut inner_attrs) = if self.eat(&token::Semi) {
5049
if in_cfg && self.recurse_into_file_modules {
5150
// This mod is in an external file. Let's go get it!
5251
let ModulePathSuccess { path, directory_ownership } =
53-
self.submod_path(id, &outer_attrs, id_span)?;
54-
let (module, attrs) =
55-
self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?;
56-
Ok((id, ItemKind::Mod(module), Some(attrs)))
52+
self.submod_path(id, &attrs, id_span)?;
53+
self.eval_src_mod(path, directory_ownership, id.to_string(), id_span)?
5754
} else {
58-
let placeholder = ast::Mod { inner: DUMMY_SP, items: Vec::new(), inline: false };
59-
Ok((id, ItemKind::Mod(placeholder), None))
55+
(ast::Mod { inner: DUMMY_SP, items: Vec::new(), inline: false }, Vec::new())
6056
}
6157
} else {
6258
let old_directory = self.directory.clone();
63-
self.push_directory(id, &outer_attrs);
59+
self.push_directory(id, &attrs);
6460

6561
self.expect(&token::OpenDelim(token::Brace))?;
6662
let mod_inner_lo = self.token.span;
67-
let attrs = self.parse_inner_attributes()?;
63+
let inner_attrs = self.parse_inner_attributes()?;
6864
let module = self.parse_mod_items(&token::CloseDelim(token::Brace), mod_inner_lo)?;
6965

7066
self.directory = old_directory;
71-
Ok((id, ItemKind::Mod(module), Some(attrs)))
72-
}
67+
(module, inner_attrs)
68+
};
69+
attrs.append(&mut inner_attrs);
70+
Ok((id, ItemKind::Mod(module), None))
7371
}
7472

7573
/// Given a termination token, parses all of the items in a module.

0 commit comments

Comments
 (0)