Skip to content

Commit 08776ef

Browse files
committed
Support module-level rustfmt::skip in sub-modules
1 parent aedff61 commit 08776ef

File tree

2 files changed

+11
-81
lines changed

2 files changed

+11
-81
lines changed

src/modules.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
252252
return Ok(None);
253253
}
254254
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.inner) {
255-
Some(m) => Ok(Some(SubModKind::External(
255+
Some((_, ref attrs)) if contains_skip(attrs) => Ok(None),
256+
Some((m, _)) => Ok(Some(SubModKind::External(
256257
path,
257258
DirectoryOwnership::Owned { relative: None },
258259
Cow::Owned(m),
@@ -290,10 +291,11 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
290291
}
291292
}
292293
match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.inner) {
293-
Some(m) if outside_mods_empty => {
294+
Some((_, ref attrs)) if contains_skip(attrs) => Ok(None),
295+
Some((m, _)) if outside_mods_empty => {
294296
Ok(Some(SubModKind::External(path, ownership, Cow::Owned(m))))
295297
}
296-
Some(m) => {
298+
Some((m, _)) => {
297299
mods_outside_ast.push((path.clone(), ownership, Cow::Owned(m)));
298300
if should_insert {
299301
mods_outside_ast.push((path, ownership, sub_mod.clone()));
@@ -377,7 +379,8 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
377379
}
378380
let m = match Parser::parse_file_as_module(self.parse_sess, &actual_path, sub_mod.inner)
379381
{
380-
Some(m) => m,
382+
Some((_, ref attrs)) if contains_skip(attrs) => continue,
383+
Some((m, _)) => m,
381384
None => continue,
382385
};
383386

src/syntux/parser.rs

+4-77
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
33

44
use rustc_ast::ast;
55
use rustc_ast::token::{DelimToken, TokenKind};
6-
use rustc_errors::{Diagnostic, PResult};
6+
use rustc_errors::Diagnostic;
77
use rustc_parse::{new_parser_from_file, parser::Parser as RawParser};
88
use rustc_span::{symbol::kw, Span};
99

@@ -102,91 +102,18 @@ impl<'a> Parser<'a> {
102102
rustc_expand::module::submod_path_from_attr(attrs, path)
103103
}
104104

105-
// FIXME(topecongiro) Use the method from libsyntax[1] once it become public.
106-
//
107-
// [1] https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/attr.rs
108-
fn parse_inner_attrs(parser: &mut RawParser<'a>) -> PResult<'a, Vec<ast::Attribute>> {
109-
let mut attrs: Vec<ast::Attribute> = vec![];
110-
loop {
111-
match parser.token.kind {
112-
TokenKind::Pound => {
113-
// Don't even try to parse if it's not an inner attribute.
114-
if !parser.look_ahead(1, |t| t == &TokenKind::Not) {
115-
break;
116-
}
117-
118-
let attr = parser.parse_attribute(true)?;
119-
assert_eq!(attr.style, ast::AttrStyle::Inner);
120-
attrs.push(attr);
121-
}
122-
TokenKind::DocComment(s) => {
123-
// we need to get the position of this token before we bump.
124-
let attr = rustc_ast::attr::mk_doc_comment(
125-
rustc_ast::util::comments::doc_comment_style(&s.as_str()),
126-
s,
127-
parser.token.span,
128-
);
129-
if attr.style == ast::AttrStyle::Inner {
130-
attrs.push(attr);
131-
parser.bump();
132-
} else {
133-
break;
134-
}
135-
}
136-
_ => break,
137-
}
138-
}
139-
Ok(attrs)
140-
}
141-
142-
fn parse_mod_items(parser: &mut RawParser<'a>, span: Span) -> PResult<'a, ast::Mod> {
143-
let mut items = vec![];
144-
while let Some(item) = parser.parse_item()? {
145-
items.push(item);
146-
}
147-
148-
// Handle extern mods that are empty files/files with only comments.
149-
if items.is_empty() {
150-
parser.parse_mod(&TokenKind::Eof)?;
151-
}
152-
153-
let hi = if parser.token.span.is_dummy() {
154-
span
155-
} else {
156-
parser.prev_token.span
157-
};
158-
159-
Ok(ast::Mod {
160-
inner: span.to(hi),
161-
items,
162-
inline: false,
163-
})
164-
}
165-
166105
pub(crate) fn parse_file_as_module(
167106
sess: &'a ParseSess,
168107
path: &Path,
169108
span: Span,
170-
) -> Option<ast::Mod> {
109+
) -> Option<(ast::Mod, Vec<ast::Attribute>)> {
171110
let result = catch_unwind(AssertUnwindSafe(|| {
172111
let mut parser = new_parser_from_file(sess.inner(), &path, Some(span));
173-
174-
let lo = parser.token.span;
175-
// FIXME(topecongiro) Format inner attributes (#3606).
176-
match Parser::parse_inner_attrs(&mut parser) {
177-
Ok(_attrs) => (),
112+
match parser.parse_mod(&TokenKind::Eof) {
113+
Ok(result) => Some(result),
178114
Err(mut e) => {
179115
e.cancel();
180116
sess.reset_errors();
181-
return None;
182-
}
183-
}
184-
185-
match Parser::parse_mod_items(&mut parser, lo) {
186-
Ok(m) => Some(m),
187-
Err(mut db) => {
188-
db.cancel();
189-
sess.reset_errors();
190117
None
191118
}
192119
}

0 commit comments

Comments
 (0)