Skip to content

Commit 67fb831

Browse files
committed
Factor out Module.ast_mod_kind
1 parent d6826bb commit 67fb831

File tree

1 file changed

+21
-62
lines changed

1 file changed

+21
-62
lines changed

src/modules.rs

Lines changed: 21 additions & 62 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
),
@@ -153,14 +149,13 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
153149
visitor.visit_item(item);
154150
for module_item in visitor.mods() {
155151
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = module_item.item.kind {
152+
let items = match sub_mod_kind {
153+
ast::ModKind::Loaded(items, ..) => Cow::Owned(items.clone()),
154+
_ => Cow::Owned(ThinVec::new()),
155+
};
156156
self.visit_sub_mod(
157157
&module_item.item,
158-
Module::new(
159-
module_item.item.span,
160-
Some(Cow::Owned(sub_mod_kind.clone())),
161-
Cow::Owned(ThinVec::new()),
162-
&[],
163-
),
158+
Module::new(module_item.item.span, items, &[]),
164159
false,
165160
)?;
166161
}
@@ -180,17 +175,11 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
180175
}
181176

182177
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
183-
let span = item.span;
184-
self.visit_sub_mod(
185-
&item,
186-
Module::new(
187-
span,
188-
Some(Cow::Owned(sub_mod_kind.clone())),
189-
Cow::Owned(ThinVec::new()),
190-
&[],
191-
),
192-
false,
193-
)?;
178+
let items = match sub_mod_kind {
179+
ast::ModKind::Loaded(items, ..) => Cow::Owned(items.clone()),
180+
_ => Cow::Owned(ThinVec::new()),
181+
};
182+
self.visit_sub_mod(&item, Module::new(item.span, items, &[]), false)?;
194183
}
195184
}
196185
Ok(())
@@ -207,17 +196,11 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
207196
}
208197

209198
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
210-
let span = item.span;
211-
self.visit_sub_mod(
212-
item,
213-
Module::new(
214-
span,
215-
Some(Cow::Borrowed(sub_mod_kind)),
216-
Cow::Owned(ThinVec::new()),
217-
&[],
218-
),
219-
true,
220-
)?;
199+
let items = match sub_mod_kind {
200+
ast::ModKind::Loaded(items, ..) => Cow::Borrowed(items),
201+
_ => Cow::Owned(ThinVec::new()),
202+
};
203+
self.visit_sub_mod(item, Module::new(item.span, items, &[]), true)?;
221204
}
222205
}
223206
Ok(())
@@ -322,15 +305,11 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
322305
self.directory = directory;
323306
}
324307
if from_ast {
325-
if let Some(Cow::Borrowed(ast::ModKind::Loaded(items, _, _))) = &sub_mod.ast_mod_kind {
308+
if let Cow::Borrowed(items) = sub_mod.items {
326309
self.visit_mod_from_ast(items)?;
327310
}
328311
} else {
329-
if let Some(Cow::Owned(ast::ModKind::Loaded(items, _, _))) = &sub_mod.ast_mod_kind {
330-
self.visit_mod_outside_ast(items)?;
331-
} else if let Cow::Owned(items) = &sub_mod.items {
332-
self.visit_mod_outside_ast(items)?;
333-
}
312+
self.visit_mod_outside_ast(&sub_mod.items)?;
334313
}
335314
Ok(())
336315
}
@@ -355,12 +334,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
355334
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
356335
path,
357336
DirectoryOwnership::Owned { relative: None },
358-
Module::new(
359-
span,
360-
Some(Cow::Owned(ast::ModKind::Unloaded)),
361-
Cow::Owned(items),
362-
&attrs,
363-
),
337+
Module::new(span, Cow::Owned(items), &attrs),
364338
))),
365339
Err(ParserError::ParseError) => Err(ModuleResolutionError {
366340
module: mod_name.to_string(),
@@ -405,24 +379,14 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
405379
Ok(Some(SubModKind::External(
406380
file_path,
407381
dir_ownership,
408-
Module::new(
409-
span,
410-
Some(Cow::Owned(ast::ModKind::Unloaded)),
411-
Cow::Owned(items),
412-
&attrs,
413-
),
382+
Module::new(span, Cow::Owned(items), &attrs),
414383
)))
415384
}
416385
Ok((attrs, items, span)) => {
417386
mods_outside_ast.push((
418387
file_path.clone(),
419388
dir_ownership,
420-
Module::new(
421-
span,
422-
Some(Cow::Owned(ast::ModKind::Unloaded)),
423-
Cow::Owned(items),
424-
&attrs,
425-
),
389+
Module::new(span, Cow::Owned(items), &attrs),
426390
));
427391
if should_insert {
428392
mods_outside_ast.push((file_path, dir_ownership, sub_mod.clone()));
@@ -545,12 +509,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
545509
result.push((
546510
actual_path,
547511
DirectoryOwnership::Owned { relative: None },
548-
Module::new(
549-
span,
550-
Some(Cow::Owned(ast::ModKind::Unloaded)),
551-
Cow::Owned(items),
552-
&attrs,
553-
),
512+
Module::new(span, Cow::Owned(items), &attrs),
554513
))
555514
}
556515
result

0 commit comments

Comments
 (0)