Skip to content

Commit f0b1898

Browse files
committed
Simplify find_external_module arguments
Defer creating a Module until needed. This is possible now that we are using arenas, so ast Items have the 'ast lifetime.
1 parent dbd7c1d commit f0b1898

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

src/modules.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ impl<'a> Module<'a> {
5151
}
5252
}
5353

54+
pub(crate) fn from_item(item: &'a ast::Item) -> Module<'a> {
55+
let mod_kind = match &item.kind {
56+
ast::ItemKind::Mod(_, mod_kind) => Some(mod_kind),
57+
_ => None,
58+
};
59+
let items = match &item.kind {
60+
ast::ItemKind::Mod(_, ast::ModKind::Loaded(items, ..)) => &**items,
61+
_ => &[],
62+
};
63+
Module::new(item.span, mod_kind, items, &item.attrs)
64+
}
65+
5466
pub(crate) fn attrs(&self) -> &[ast::Attribute] {
5567
&self.inner_attr
5668
}
@@ -185,7 +197,7 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
185197
if is_mod_decl(item) {
186198
// mod foo;
187199
// Look for an extern file.
188-
let Some(kind) = self.find_external_module(item.ident, &item.attrs, &sub_mod)? else {
200+
let Some(kind) = self.find_external_module(item)? else {
189201
return Ok(());
190202
};
191203
self.insert_sub_mod(kind.clone())?;
@@ -264,19 +276,18 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
264276
/// Find a file path in the filesystem which corresponds to the given module.
265277
fn find_external_module(
266278
&self,
267-
mod_name: symbol::Ident,
268-
attrs: &[ast::Attribute],
269-
sub_mod: &Module<'ast>,
279+
item: &'ast ast::Item,
270280
) -> Result<Option<SubModKind<'ast>>, ModuleResolutionError> {
281+
let mod_name = item.ident;
271282
let relative = match self.directory.ownership {
272283
DirectoryOwnership::Owned { relative } => relative,
273284
DirectoryOwnership::UnownedViaBlock => None,
274285
};
275-
if let Some(path) = Parser::submod_path_from_attr(attrs, &self.directory.path) {
286+
if let Some(path) = Parser::submod_path_from_attr(&item.attrs, &self.directory.path) {
276287
if self.parse_sess.is_file_parsed(&path) {
277288
return Ok(None);
278289
}
279-
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.span) {
290+
return match Parser::parse_file_as_module(self.parse_sess, &path, item.span) {
280291
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
281292
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
282293
path,
@@ -300,7 +311,7 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
300311
}
301312

302313
// Look for nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
303-
let mut mods_outside_ast = self.find_mods_outside_of_ast(attrs, sub_mod);
314+
let mut mods_outside_ast = self.find_mods_outside_of_ast(item);
304315

305316
match self
306317
.parse_sess
@@ -320,12 +331,16 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
320331
return Ok(None);
321332
} else {
322333
if should_insert {
323-
mods_outside_ast.push((file_path, dir_ownership, sub_mod.clone()));
334+
mods_outside_ast.push((
335+
file_path,
336+
dir_ownership,
337+
Module::from_item(item),
338+
));
324339
}
325340
return Ok(Some(SubModKind::MultiExternal(mods_outside_ast)));
326341
}
327342
}
328-
match Parser::parse_file_as_module(self.parse_sess, &file_path, sub_mod.span) {
343+
match Parser::parse_file_as_module(self.parse_sess, &file_path, item.span) {
329344
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
330345
Ok((attrs, items, span)) if outside_mods_empty => {
331346
Ok(Some(SubModKind::External(
@@ -351,7 +366,11 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
351366
),
352367
));
353368
if should_insert {
354-
mods_outside_ast.push((file_path, dir_ownership, sub_mod.clone()));
369+
mods_outside_ast.push((
370+
file_path,
371+
dir_ownership,
372+
Module::from_item(item),
373+
));
355374
}
356375
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
357376
}
@@ -365,7 +384,11 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
365384
}),
366385
Err(..) => {
367386
if should_insert {
368-
mods_outside_ast.push((file_path, dir_ownership, sub_mod.clone()));
387+
mods_outside_ast.push((
388+
file_path,
389+
dir_ownership,
390+
Module::from_item(item),
391+
));
369392
}
370393
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
371394
}
@@ -446,12 +469,11 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
446469

447470
fn find_mods_outside_of_ast(
448471
&self,
449-
attrs: &[ast::Attribute],
450-
sub_mod: &Module<'ast>,
472+
item: &'ast ast::Item,
451473
) -> Vec<(PathBuf, DirectoryOwnership, Module<'ast>)> {
452474
// Filter nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
453475
let mut path_visitor = visitor::PathVisitor::default();
454-
for attr in attrs.iter() {
476+
for attr in item.attrs.iter() {
455477
if let Some(meta) = attr.meta() {
456478
path_visitor.visit_meta_item(&meta)
457479
}
@@ -467,12 +489,12 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
467489
result.push((
468490
actual_path,
469491
DirectoryOwnership::Owned { relative: None },
470-
sub_mod.clone(),
492+
Module::from_item(item),
471493
));
472494
continue;
473495
}
474496
let (attrs, items, span) =
475-
match Parser::parse_file_as_module(self.parse_sess, &actual_path, sub_mod.span) {
497+
match Parser::parse_file_as_module(self.parse_sess, &actual_path, item.span) {
476498
Ok((ref attrs, _, _)) if contains_skip(attrs) => continue,
477499
Ok(m) => m,
478500
Err(..) => continue,

0 commit comments

Comments
 (0)