Skip to content

Commit ba2f38d

Browse files
committed
Factor out Module.ast_mod_kind
At this point it is just redundant with Module.items.
1 parent 3a5b794 commit ba2f38d

File tree

2 files changed

+7
-55
lines changed

2 files changed

+7
-55
lines changed

src/formatting.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,8 @@ fn format_project<T: FormatHandler>(
133133

134134
let mut context = FormatContext::new(&krate, report, parse_session, config, handler);
135135
let item_arena = TypedArena::default();
136-
let mod_kind_arena = TypedArena::default();
137136
let files = modules::ModResolver::new(
138137
&item_arena,
139-
&mod_kind_arena,
140138
&context.parse_session,
141139
directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaBlock),
142140
!input_is_stdin && !config.skip_children(),

src/modules.rs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
2424
/// Represents module with its inner attributes.
2525
#[derive(Debug, Clone)]
2626
pub(crate) struct Module<'a> {
27-
ast_mod_kind: Option<&'a ast::ModKind>,
2827
pub(crate) items: &'a [rustc_ast::ptr::P<ast::Item>],
2928
inner_attr: ast::AttrVec,
3029
pub(crate) span: Span,
@@ -33,7 +32,6 @@ pub(crate) struct Module<'a> {
3332
impl<'a> Module<'a> {
3433
pub(crate) fn new(
3534
mod_span: Span,
36-
ast_mod_kind: Option<&'a ast::ModKind>,
3735
mod_items: &'a [rustc_ast::ptr::P<ast::Item>],
3836
mod_attrs: &[ast::Attribute],
3937
) -> Self {
@@ -46,20 +44,15 @@ impl<'a> Module<'a> {
4644
items: mod_items,
4745
inner_attr,
4846
span: mod_span,
49-
ast_mod_kind,
5047
}
5148
}
5249

5350
pub(crate) fn from_item(item: &'a ast::Item) -> Module<'a> {
54-
let mod_kind = match &item.kind {
55-
ast::ItemKind::Mod(_, mod_kind) => Some(mod_kind),
56-
_ => None,
57-
};
5851
let items = match &item.kind {
5952
ast::ItemKind::Mod(_, ast::ModKind::Loaded(items, ..)) => &**items,
6053
_ => &[],
6154
};
62-
Module::new(item.span, mod_kind, items, &item.attrs)
55+
Module::new(item.span, items, &item.attrs)
6356
}
6457

6558
pub(crate) fn attrs(&self) -> &[ast::Attribute] {
@@ -70,7 +63,6 @@ impl<'a> Module<'a> {
7063
/// Maps each module to the corresponding file.
7164
pub(crate) struct ModResolver<'ast, 'sess> {
7265
item_arena: &'ast TypedArena<P<ast::Item>>,
73-
mod_kind_arena: &'ast TypedArena<ast::ModKind>,
7466
parse_sess: &'sess ParseSess,
7567
directory: Directory,
7668
file_map: FileModMap<'ast>,
@@ -114,14 +106,12 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
114106
/// Creates a new `ModResolver`.
115107
pub(crate) fn new(
116108
item_arena: &'ast TypedArena<P<ast::Item>>,
117-
mod_kind_arena: &'ast TypedArena<ast::ModKind>,
118109
parse_sess: &'sess ParseSess,
119110
directory_ownership: DirectoryOwnership,
120111
recursive: bool,
121112
) -> Self {
122113
ModResolver {
123114
item_arena,
124-
mod_kind_arena,
125115
directory: Directory {
126116
path: PathBuf::new(),
127117
ownership: directory_ownership,
@@ -154,7 +144,6 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
154144
root_filename,
155145
Module::new(
156146
mk_sp(snippet_provider.start_pos(), snippet_provider.end_pos()),
157-
None,
158147
&krate.items,
159148
&krate.attrs,
160149
),
@@ -243,36 +232,21 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
243232
path: mod_path.parent().unwrap().to_path_buf(),
244233
ownership: directory_ownership,
245234
};
246-
self.with_directory(directory, |this| {
247-
this.visit_sub_mod_after_directory_update(sub_mod)
248-
})?;
235+
self.with_directory(directory, |this| this.visit_items(&sub_mod.items))?;
249236
}
250237
SubModKind::MultiExternal(mods) => {
251238
for (mod_path, directory_ownership, sub_mod) in mods {
252239
let directory = Directory {
253240
path: mod_path.parent().unwrap().to_path_buf(),
254241
ownership: directory_ownership,
255242
};
256-
self.with_directory(directory, |this| {
257-
this.visit_sub_mod_after_directory_update(sub_mod)
258-
})?;
243+
self.with_directory(directory, |this| this.visit_items(&sub_mod.items))?;
259244
}
260245
}
261246
}
262247
Ok(())
263248
}
264249

265-
fn visit_sub_mod_after_directory_update(
266-
&mut self,
267-
sub_mod: Module<'ast>,
268-
) -> Result<(), ModuleResolutionError> {
269-
if let Some(ast::ModKind::Loaded(items, _, _)) = sub_mod.ast_mod_kind {
270-
self.visit_items(items)
271-
} else {
272-
self.visit_items(&sub_mod.items)
273-
}
274-
}
275-
276250
/// Find a file path in the filesystem which corresponds to the given module.
277251
fn find_external_module(
278252
&self,
@@ -292,12 +266,7 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
292266
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
293267
path,
294268
DirectoryOwnership::Owned { relative: None },
295-
Module::new(
296-
span,
297-
Some(self.mod_kind_arena.alloc(ast::ModKind::Unloaded)),
298-
self.item_arena.alloc_from_iter(items),
299-
&attrs,
300-
),
269+
Module::new(span, self.item_arena.alloc_from_iter(items), &attrs),
301270
))),
302271
Err(ParserError::ParseError) => Err(ModuleResolutionError {
303272
module: mod_name.to_string(),
@@ -346,24 +315,14 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
346315
Ok(Some(SubModKind::External(
347316
file_path,
348317
dir_ownership,
349-
Module::new(
350-
span,
351-
Some(self.mod_kind_arena.alloc(ast::ModKind::Unloaded)),
352-
self.item_arena.alloc_from_iter(items),
353-
&attrs,
354-
),
318+
Module::new(span, self.item_arena.alloc_from_iter(items), &attrs),
355319
)))
356320
}
357321
Ok((attrs, items, span)) => {
358322
mods_outside_ast.push((
359323
file_path.clone(),
360324
dir_ownership,
361-
Module::new(
362-
span,
363-
Some(self.mod_kind_arena.alloc(ast::ModKind::Unloaded)),
364-
self.item_arena.alloc_from_iter(items),
365-
&attrs,
366-
),
325+
Module::new(span, self.item_arena.alloc_from_iter(items), &attrs),
367326
));
368327
if should_insert {
369328
mods_outside_ast.push((
@@ -503,12 +462,7 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
503462
result.push((
504463
actual_path,
505464
DirectoryOwnership::Owned { relative: None },
506-
Module::new(
507-
span,
508-
Some(self.mod_kind_arena.alloc(ast::ModKind::Unloaded)),
509-
self.item_arena.alloc_from_iter(items),
510-
&attrs,
511-
),
465+
Module::new(span, self.item_arena.alloc_from_iter(items), &attrs),
512466
))
513467
}
514468
result

0 commit comments

Comments
 (0)