Skip to content

Commit f0bab60

Browse files
chore: apply rustc mod parsing changes
1 parent 71db3b6 commit f0bab60

File tree

5 files changed

+127
-67
lines changed

5 files changed

+127
-67
lines changed

src/formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ fn format_file(
178178
original_snippet: Option<String>,
179179
is_macro_def: bool,
180180
) -> Result<(), OperationError> {
181-
let snippet_provider = parse_session.snippet_provider(module.as_ref().inner);
181+
let snippet_provider = parse_session.snippet_provider(module.span);
182182
let mut visitor = FmtVisitor::from_parse_sess(
183183
&parse_session,
184184
config,

src/formatting/items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,8 +3404,8 @@ pub(crate) fn rewrite_extern_crate(
34043404
/// Returns `true` for `mod foo;`, false for `mod foo { .. }`.
34053405
pub(crate) fn is_mod_decl(item: &ast::Item) -> bool {
34063406
match item.kind {
3407-
ast::ItemKind::Mod(ref m) => m.inner.hi() != item.span.hi(),
3408-
_ => false,
3407+
ast::ItemKind::Mod(_, ast::ModKind::Loaded(_, ast::Inline::Yes, _)) => false,
3408+
_ => true,
34093409
}
34103410
}
34113411

src/formatting/modules.rs

Lines changed: 105 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast::ast;
66
use rustc_ast::attr::HasAttrs;
77
use rustc_ast::visit::Visitor;
88
use rustc_span::symbol::{self, sym, Symbol};
9+
use rustc_span::Span;
910
use thiserror::Error;
1011

1112
use crate::config::FileName;
@@ -39,9 +40,12 @@ pub(crate) fn get_mod_inner_attrs<'a>(
3940
/// Represents module with its inner attributes.
4041
#[derive(Debug, Clone)]
4142
pub(crate) struct Module<'a> {
42-
ast_mod: Cow<'a, ast::Mod>,
43+
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
44+
pub(crate) items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
45+
attrs: Cow<'a, Vec<ast::Attribute>>,
4346
ast_item: Option<Cow<'a, ast::Item>>,
4447
inner_attr: Vec<ast::Attribute>,
48+
pub(crate) span: Span,
4549
}
4650

4751
impl<'a> Module<'a> {
@@ -67,21 +71,30 @@ impl<'a> Module<'a> {
6771
}
6872

6973
pub(crate) fn new(
70-
ast_mod: Cow<'a, ast::Mod>,
74+
mod_span: Span,
75+
ast_mod_kind: Option<Cow<'a, ast::ModKind>>,
7176
ast_item: Option<Cow<'a, ast::Item>>,
72-
attrs: &[ast::Attribute],
77+
mod_items: Cow<'a, Vec<rustc_ast::ptr::P<ast::Item>>>,
78+
mod_attrs: Cow<'a, Vec<ast::Attribute>>,
7379
) -> Self {
74-
let inner_attr = attrs
80+
let inner_attr = mod_attrs
7581
.iter()
7682
.filter(|attr| attr.style == ast::AttrStyle::Inner)
7783
.cloned()
7884
.collect();
7985
Module {
86+
ast_mod_kind,
8087
ast_item,
81-
ast_mod,
88+
items: mod_items,
89+
attrs: mod_attrs,
8290
inner_attr,
91+
span: mod_span,
8392
}
8493
}
94+
95+
pub(crate) fn outside_ast_mod_span(&self) -> Option<Span> {
96+
self.ast_item.as_ref().map(|item| item.span)
97+
}
8598
}
8699

87100
impl<'a> HasAttrs for Module<'a> {
@@ -93,12 +106,6 @@ impl<'a> HasAttrs for Module<'a> {
93106
}
94107
}
95108

96-
impl<'a> AsRef<ast::Mod> for Module<'a> {
97-
fn as_ref(&self) -> &ast::Mod {
98-
&self.ast_mod
99-
}
100-
}
101-
102109
/// Maps each module to the corresponding file.
103110
pub(crate) struct ModResolver<'ast, 'sess> {
104111
parse_sess: &'sess ParseSess,
@@ -164,11 +171,17 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
164171
_ => PathBuf::new(),
165172
};
166173

167-
self.visit_mod_from_ast(&krate.module)?;
174+
self.visit_mod_from_ast(&krate.items)?;
168175

169176
self.file_map.insert(
170177
root_filename,
171-
Module::new(Cow::Borrowed(&krate.module), None, &krate.attrs),
178+
Module::new(
179+
krate.span,
180+
None,
181+
None,
182+
Cow::Borrowed(&krate.items),
183+
Cow::Borrowed(&krate.attrs),
184+
),
172185
);
173186
Ok(self.file_map)
174187
}
@@ -178,51 +191,63 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
178191
let mut visitor = visitor::CfgIfVisitor::new(self.parse_sess);
179192
visitor.visit_item(&item);
180193
for module_item in visitor.mods() {
181-
if let ast::ItemKind::Mod(ref sub_mod) = module_item.item.kind {
194+
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = module_item.item.kind {
182195
self.visit_sub_mod(Module::new(
183-
Cow::Owned(sub_mod.clone()),
196+
module_item.item.span,
197+
Some(Cow::Owned(sub_mod_kind.clone())),
184198
Some(Cow::Owned(module_item.item)),
185-
&[],
199+
Cow::Owned(vec![]),
200+
Cow::Owned(vec![]),
186201
))?;
187202
}
188203
}
189204
Ok(())
190205
}
191206

192207
/// Visit modules defined inside macro calls.
193-
fn visit_mod_outside_ast(&mut self, module: ast::Mod) -> Result<(), ModuleResolutionError> {
194-
for item in module.items {
208+
fn visit_mod_outside_ast(
209+
&mut self,
210+
items: Vec<rustc_ast::ptr::P<ast::Item>>,
211+
) -> Result<(), ModuleResolutionError> {
212+
for item in items {
195213
if is_cfg_if(&item) {
196214
self.visit_cfg_if(Cow::Owned(item.into_inner()))?;
197215
continue;
198216
}
199217

200-
if let ast::ItemKind::Mod(ref sub_mod) = item.kind {
218+
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
201219
self.visit_sub_mod(Module::new(
202-
Cow::Owned(sub_mod.clone()),
220+
item.span,
221+
Some(Cow::Owned(sub_mod_kind.clone())),
203222
Some(Cow::Owned(item.into_inner())),
204-
&[],
223+
Cow::Owned(vec![]),
224+
Cow::Owned(vec![]),
205225
))?;
206226
}
207227
}
208228
Ok(())
209229
}
210230

211231
/// Visit modules from AST.
212-
fn visit_mod_from_ast(&mut self, module: &'ast ast::Mod) -> Result<(), ModuleResolutionError> {
213-
for item in &module.items {
232+
fn visit_mod_from_ast(
233+
&mut self,
234+
items: &'ast Vec<rustc_ast::ptr::P<ast::Item>>,
235+
) -> Result<(), ModuleResolutionError> {
236+
for item in items {
214237
if is_cfg_if(item) {
215238
let result = self.visit_cfg_if(Cow::Borrowed(item));
216239
if result.is_err() && self.recursive {
217240
return result;
218241
}
219242
}
220243

221-
if let ast::ItemKind::Mod(ref sub_mod) = item.kind {
244+
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = item.kind {
222245
let result = self.visit_sub_mod(Module::new(
223-
Cow::Borrowed(sub_mod),
246+
item.span,
247+
Some(Cow::Borrowed(sub_mod_kind)),
224248
Some(Cow::Borrowed(item)),
225-
&item.attrs,
249+
Cow::Owned(vec![]),
250+
Cow::Borrowed(&item.attrs),
226251
));
227252
if result.is_err() && self.recursive {
228253
return result;
@@ -330,9 +355,12 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
330355
if let Some(directory) = directory {
331356
self.directory = directory;
332357
}
333-
match sub_mod.ast_mod {
334-
Cow::Borrowed(sub_mod) => self.visit_mod_from_ast(sub_mod),
335-
Cow::Owned(sub_mod) => self.visit_mod_outside_ast(sub_mod),
358+
match (sub_mod.ast_mod_kind, sub_mod.items) {
359+
(Some(Cow::Borrowed(ast::ModKind::Loaded(items, ast::Inline::No, _))), _) => {
360+
self.visit_mod_from_ast(&items)
361+
}
362+
(Some(Cow::Owned(..)), Cow::Owned(items)) => self.visit_mod_outside_ast(items),
363+
(_, _) => Ok(()),
336364
}
337365
}
338366

@@ -351,12 +379,21 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
351379
if self.parse_sess.is_file_parsed(&path) {
352380
return Ok(None);
353381
}
354-
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.ast_mod.inner)
355-
{
356-
Ok(m) => Ok(Some(SubModKind::External(
382+
return match Parser::parse_file_as_module(
383+
self.parse_sess,
384+
&path,
385+
sub_mod.outside_ast_mod_span(),
386+
) {
387+
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
357388
path,
358389
DirectoryOwnership::Owned { relative: None },
359-
Module::new(Cow::Owned(m.0), sub_mod.ast_item.clone(), &m.1),
390+
Module::new(
391+
span,
392+
Some(Cow::Owned(ast::ModKind::Unloaded)),
393+
sub_mod.ast_item.clone(),
394+
Cow::Owned(items),
395+
Cow::Owned(attrs),
396+
),
360397
))),
361398
Err(ParserError::ParseError) => Err(ModuleResolutionError {
362399
module: sub_mod.name(),
@@ -394,17 +431,35 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
394431
return Ok(Some(SubModKind::MultiExternal(mods_outside_ast)));
395432
}
396433
}
397-
match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.ast_mod.inner) {
398-
Ok(m) if outside_mods_empty => Ok(Some(SubModKind::External(
399-
path,
400-
ownership,
401-
Module::new(Cow::Owned(m.0), sub_mod.ast_item.clone(), &m.1),
402-
))),
403-
Ok(m) => {
434+
match Parser::parse_file_as_module(
435+
self.parse_sess,
436+
&path,
437+
sub_mod.outside_ast_mod_span(),
438+
) {
439+
Ok((attrs, items, span)) if outside_mods_empty => {
440+
Ok(Some(SubModKind::External(
441+
path,
442+
ownership,
443+
Module::new(
444+
span,
445+
Some(Cow::Owned(ast::ModKind::Unloaded)),
446+
sub_mod.ast_item.clone(),
447+
Cow::Owned(items),
448+
Cow::Owned(attrs),
449+
),
450+
)))
451+
}
452+
Ok((attrs, items, span)) => {
404453
mods_outside_ast.push((
405454
path.clone(),
406455
ownership,
407-
Module::new(Cow::Owned(m.0), sub_mod.ast_item.clone(), &m.1),
456+
Module::new(
457+
span,
458+
Some(Cow::Owned(ast::ModKind::Unloaded)),
459+
sub_mod.ast_item.clone(),
460+
Cow::Owned(items),
461+
Cow::Owned(attrs),
462+
),
408463
));
409464
if should_insert {
410465
mods_outside_ast.push((path, ownership, sub_mod.clone()));
@@ -494,10 +549,10 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
494549
continue;
495550
}
496551

497-
let m = match Parser::parse_file_as_module(
552+
let (attrs, items, span) = match Parser::parse_file_as_module(
498553
self.parse_sess,
499554
&actual_path,
500-
sub_mod.ast_mod.inner,
555+
sub_mod.outside_ast_mod_span(),
501556
) {
502557
Ok(m) => m,
503558
Err(..) => continue,
@@ -506,7 +561,13 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
506561
result.push((
507562
actual_path,
508563
DirectoryOwnership::Owned { relative: None },
509-
Module::new(Cow::Owned(m.0), sub_mod.ast_item.clone(), &m.1),
564+
Module::new(
565+
span,
566+
Some(Cow::Owned(ast::ModKind::Unloaded)),
567+
sub_mod.ast_item.clone(),
568+
Cow::Owned(items),
569+
Cow::Owned(attrs),
570+
),
510571
))
511572
}
512573
result

src/formatting/syntux/parser.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::panic::{catch_unwind, AssertUnwindSafe};
22
use std::path::{Path, PathBuf};
33

4-
use rustc_ast::ast;
54
use rustc_ast::token::{DelimToken, TokenKind};
5+
use rustc_ast::{ast, ptr};
66
use rustc_errors::Diagnostic;
77
use rustc_parse::{
88
new_parser_from_file,
@@ -123,11 +123,11 @@ impl<'a> Parser<'a> {
123123
pub(crate) fn parse_file_as_module(
124124
sess: &'a ParseSess,
125125
path: &Path,
126-
span: Span,
127-
) -> Result<(ast::Mod, Vec<ast::Attribute>), ParserError> {
126+
span: Option<Span>,
127+
) -> Result<(Vec<ast::Attribute>, Vec<ptr::P<ast::Item>>, Span), ParserError> {
128128
let result = catch_unwind(AssertUnwindSafe(|| {
129-
let mut parser = new_parser_from_file(sess.inner(), &path, Some(span));
130-
match parser.parse_mod(&TokenKind::Eof, ast::Unsafe::No) {
129+
let mut parser = new_parser_from_file(sess.inner(), &path, span);
130+
match parser.parse_mod(&TokenKind::Eof) {
131131
Ok(result) => Some(result),
132132
Err(mut e) => {
133133
sess.emit_or_cancel_diagnostic(&mut e);

0 commit comments

Comments
 (0)