Skip to content

Commit 1a1121b

Browse files
committed
Return inner attributes
1 parent 3eb864e commit 1a1121b

File tree

2 files changed

+8
-83
lines changed

2 files changed

+8
-83
lines changed

rustfmt-core/rustfmt-lib/src/formatting/modules.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
261261
Ok(m) => Ok(Some(SubModKind::External(
262262
path,
263263
DirectoryOwnership::Owned { relative: None },
264-
Cow::Owned(m),
264+
Cow::Owned(m.0),
265265
))),
266266
Err(ParserError::ParseError) => Err(ModuleResolutionError {
267267
module: mod_name.to_string(),
@@ -301,10 +301,10 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
301301
}
302302
match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.inner) {
303303
Ok(m) if outside_mods_empty => {
304-
Ok(Some(SubModKind::External(path, ownership, Cow::Owned(m))))
304+
Ok(Some(SubModKind::External(path, ownership, Cow::Owned(m.0))))
305305
}
306306
Ok(m) => {
307-
mods_outside_ast.push((path.clone(), ownership, Cow::Owned(m)));
307+
mods_outside_ast.push((path.clone(), ownership, Cow::Owned(m.0)));
308308
if should_insert {
309309
mods_outside_ast.push((path, ownership, sub_mod.clone()));
310310
}
@@ -403,7 +403,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
403403
result.push((
404404
actual_path,
405405
DirectoryOwnership::Owned { relative: None },
406-
Cow::Owned(m),
406+
Cow::Owned(m.0),
407407
))
408408
}
409409
result

rustfmt-core/rustfmt-lib/src/formatting/syntux/parser.rs

+4-79
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,79 +102,15 @@ 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-
) -> Result<ast::Mod, ParserError> {
109+
) -> Result<(ast::Mod, Vec<ast::Attribute>), ParserError> {
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
if sess.can_reset_errors() {
@@ -183,17 +119,6 @@ impl<'a> Parser<'a> {
183119
return None;
184120
}
185121
}
186-
187-
match Parser::parse_mod_items(&mut parser, lo) {
188-
Ok(m) => Some(m),
189-
Err(mut db) => {
190-
db.cancel();
191-
if sess.can_reset_errors() {
192-
sess.reset_errors();
193-
}
194-
None
195-
}
196-
}
197122
}));
198123
match result {
199124
Ok(Some(m)) => {

0 commit comments

Comments
 (0)