Skip to content

Commit c367f7e

Browse files
committed
Factor out Module.ast_mod_kind
1 parent 98c9938 commit c367f7e

File tree

1 file changed

+16
-56
lines changed

1 file changed

+16
-56
lines changed

src/modules.rs

Lines changed: 16 additions & 56 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

@@ -139,7 +136,6 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
139136
root_filename,
140137
Module::new(
141138
mk_sp(snippet_provider.start_pos(), snippet_provider.end_pos()),
142-
None,
143139
Cow::Borrowed(&krate.items),
144140
&krate.attrs,
145141
),
@@ -167,17 +163,11 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
167163
}
168164

169165
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
170-
let span = item.span;
171-
self.visit_sub_mod(
172-
&item,
173-
Module::new(
174-
span,
175-
Some(Cow::Owned(sub_mod_kind.clone())),
176-
Cow::Owned(ThinVec::new()),
177-
&[],
178-
),
179-
false,
180-
)?;
166+
let items = match sub_mod_kind {
167+
ast::ModKind::Loaded(items, ..) => Cow::Owned(items.clone()),
168+
_ => Cow::Owned(ThinVec::new()),
169+
};
170+
self.visit_sub_mod(&item, Module::new(item.span, items, &[]), false)?;
181171
}
182172
}
183173
Ok(())
@@ -194,17 +184,11 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
194184
}
195185

196186
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
197-
let span = item.span;
198-
self.visit_sub_mod(
199-
item,
200-
Module::new(
201-
span,
202-
Some(Cow::Borrowed(sub_mod_kind)),
203-
Cow::Owned(ThinVec::new()),
204-
&[],
205-
),
206-
true,
207-
)?;
187+
let items = match sub_mod_kind {
188+
ast::ModKind::Loaded(items, ..) => Cow::Borrowed(items),
189+
_ => Cow::Owned(ThinVec::new()),
190+
};
191+
self.visit_sub_mod(item, Module::new(item.span, items, &[]), true)?;
208192
}
209193
}
210194
Ok(())
@@ -309,15 +293,11 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
309293
self.directory = directory;
310294
}
311295
if from_ast {
312-
if let Some(Cow::Borrowed(ast::ModKind::Loaded(items, _, _))) = &sub_mod.ast_mod_kind {
296+
if let Cow::Borrowed(items) = sub_mod.items {
313297
self.visit_mod_from_ast(items)?;
314298
}
315299
} else {
316-
if let Some(Cow::Owned(ast::ModKind::Loaded(items, _, _))) = &sub_mod.ast_mod_kind {
317-
self.visit_mod_outside_ast(items)?;
318-
} else if let Cow::Owned(items) = &sub_mod.items {
319-
self.visit_mod_outside_ast(items)?;
320-
}
300+
self.visit_mod_outside_ast(&sub_mod.items)?;
321301
}
322302
Ok(())
323303
}
@@ -342,12 +322,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
342322
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
343323
path,
344324
DirectoryOwnership::Owned { relative: None },
345-
Module::new(
346-
span,
347-
Some(Cow::Owned(ast::ModKind::Unloaded)),
348-
Cow::Owned(items),
349-
&attrs,
350-
),
325+
Module::new(span, Cow::Owned(items), &attrs),
351326
))),
352327
Err(ParserError::ParseError) => Err(ModuleResolutionError {
353328
module: mod_name.to_string(),
@@ -392,24 +367,14 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
392367
Ok(Some(SubModKind::External(
393368
file_path,
394369
dir_ownership,
395-
Module::new(
396-
span,
397-
Some(Cow::Owned(ast::ModKind::Unloaded)),
398-
Cow::Owned(items),
399-
&attrs,
400-
),
370+
Module::new(span, Cow::Owned(items), &attrs),
401371
)))
402372
}
403373
Ok((attrs, items, span)) => {
404374
mods_outside_ast.push((
405375
file_path.clone(),
406376
dir_ownership,
407-
Module::new(
408-
span,
409-
Some(Cow::Owned(ast::ModKind::Unloaded)),
410-
Cow::Owned(items),
411-
&attrs,
412-
),
377+
Module::new(span, Cow::Owned(items), &attrs),
413378
));
414379
if should_insert {
415380
mods_outside_ast.push((file_path, dir_ownership, sub_mod.clone()));
@@ -532,12 +497,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
532497
result.push((
533498
actual_path,
534499
DirectoryOwnership::Owned { relative: None },
535-
Module::new(
536-
span,
537-
Some(Cow::Owned(ast::ModKind::Unloaded)),
538-
Cow::Owned(items),
539-
&attrs,
540-
),
500+
Module::new(span, Cow::Owned(items), &attrs),
541501
))
542502
}
543503
result

0 commit comments

Comments
 (0)