Skip to content

Commit b213f3b

Browse files
committed
Factor out Module.ast_mod_kind
1 parent 62e1906 commit b213f3b

File tree

1 file changed

+10
-46
lines changed

1 file changed

+10
-46
lines changed

src/modules.rs

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
2525
/// Represents module with its inner attributes.
2626
#[derive(Debug, Clone)]
2727
pub(crate) struct Module<'a> {
28-
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
2928
pub(crate) items: Cow<'a, ThinVec<rustc_ast::ptr::P<ast::Item>>>,
3029
inner_attr: ast::AttrVec,
3130
pub(crate) span: Span,
@@ -34,7 +33,6 @@ pub(crate) struct Module<'a> {
3433
impl<'a> Module<'a> {
3534
pub(crate) fn new(
3635
mod_span: Span,
37-
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
3836
mod_items: Cow<'a, ThinVec<rustc_ast::ptr::P<ast::Item>>>,
3937
mod_attrs: &[ast::Attribute],
4038
) -> Self {
@@ -47,7 +45,6 @@ impl<'a> Module<'a> {
4745
items: mod_items,
4846
inner_attr,
4947
span: mod_span,
50-
ast_mod_kind,
5148
}
5249
}
5350

@@ -57,10 +54,6 @@ impl<'a> Module<'a> {
5754

5855
pub(crate) fn to_owned(&self) -> Module<'static> {
5956
Module {
60-
ast_mod_kind: self
61-
.ast_mod_kind
62-
.as_ref()
63-
.map(|i| Cow::Owned((**i).clone())),
6457
items: Cow::Owned((&*self.items).clone()),
6558
inner_attr: self.inner_attr.clone(),
6659
span: self.span,
@@ -151,7 +144,6 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
151144
root_filename,
152145
Module::new(
153146
mk_sp(snippet_provider.start_pos(), snippet_provider.end_pos()),
154-
None,
155147
Cow::Borrowed(&krate.items),
156148
&krate.attrs,
157149
),
@@ -175,15 +167,11 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
175167
if is_cfg_if(item) {
176168
self.visit_cfg_if(item)?;
177169
} else if let ast::ItemKind::Mod(_, sub_mod_kind) = &item.kind {
178-
self.visit_sub_mod(
179-
&item,
180-
Module::new(
181-
item.span,
182-
Some(Cow::Borrowed(sub_mod_kind)),
183-
Cow::Owned(ThinVec::new()),
184-
&[],
185-
),
186-
)?;
170+
let items = match sub_mod_kind {
171+
ast::ModKind::Loaded(items, ..) => Cow::Borrowed(items),
172+
_ => Cow::Owned(ThinVec::new()),
173+
};
174+
self.visit_sub_mod(item, Module::new(item.span, items, &[]))?;
187175
}
188176
}
189177
Ok(())
@@ -284,11 +272,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
284272
if let Some(directory) = directory {
285273
self.directory = directory;
286274
}
287-
if let Some(ast::ModKind::Loaded(items, _, _)) = sub_mod.ast_mod_kind.as_deref() {
288-
self.visit_mod(&items)
289-
} else {
290-
self.visit_mod(&sub_mod.items)
291-
}
275+
self.visit_mod(&sub_mod.items)
292276
}
293277

294278
/// Find a file path in the filesystem which corresponds to the given module.
@@ -311,12 +295,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
311295
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
312296
path,
313297
DirectoryOwnership::Owned { relative: None },
314-
Module::new(
315-
span,
316-
Some(Cow::Owned(ast::ModKind::Unloaded)),
317-
Cow::Owned(items),
318-
&attrs,
319-
),
298+
Module::new(span, Cow::Owned(items), &attrs),
320299
))),
321300
Err(ParserError::ParseError) => Err(ModuleResolutionError {
322301
module: mod_name.to_string(),
@@ -361,24 +340,14 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
361340
Ok(Some(SubModKind::External(
362341
file_path,
363342
dir_ownership,
364-
Module::new(
365-
span,
366-
Some(Cow::Owned(ast::ModKind::Unloaded)),
367-
Cow::Owned(items),
368-
&attrs,
369-
),
343+
Module::new(span, Cow::Owned(items), &attrs),
370344
)))
371345
}
372346
Ok((attrs, items, span)) => {
373347
mods_outside_ast.push((
374348
file_path.clone(),
375349
dir_ownership,
376-
Module::new(
377-
span,
378-
Some(Cow::Owned(ast::ModKind::Unloaded)),
379-
Cow::Owned(items),
380-
&attrs,
381-
),
350+
Module::new(span, Cow::Owned(items), &attrs),
382351
));
383352
if should_insert {
384353
mods_outside_ast.push((file_path, dir_ownership, sub_mod.to_owned()));
@@ -501,12 +470,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
501470
result.push((
502471
actual_path,
503472
DirectoryOwnership::Owned { relative: None },
504-
Module::new(
505-
span,
506-
Some(Cow::Owned(ast::ModKind::Unloaded)),
507-
Cow::Owned(items),
508-
&attrs,
509-
),
473+
Module::new(span, Cow::Owned(items), &attrs),
510474
))
511475
}
512476
result

0 commit comments

Comments
 (0)