Skip to content

Commit 4a00fa8

Browse files
refactor: update ast::Mac tokenstream detection
1 parent a609afb commit 4a00fa8

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

rustfmt-core/rustfmt-lib/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
3434
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span,
3535
ast::StmtKind::Mac(ref mac) => {
3636
let (ref mac, _, _) = **mac;
37-
mac.span
37+
mac.span()
3838
}
3939
}
4040
}

rustfmt-core/rustfmt-lib/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,8 +1336,8 @@ pub(crate) fn can_be_overflowed_expr(
13361336
|| (context.use_block_indent() && args_len == 1)
13371337
}
13381338
ast::ExprKind::Mac(ref mac) => {
1339-
match (mac.delim, context.config.overflow_delimited_expr()) {
1340-
(ast::MacDelimiter::Bracket, true) | (ast::MacDelimiter::Brace, true) => true,
1339+
match (syntax::ast::MacDelimiter::from_token(mac.args.delim()), context.config.overflow_delimited_expr()) {
1340+
(Some(ast::MacDelimiter::Bracket), true) | (Some(ast::MacDelimiter::Brace), true) => true,
13411341
_ => context.use_block_indent() && args_len == 1,
13421342
}
13431343
}

rustfmt-core/rustfmt-lib/src/macros.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,13 @@ fn rewrite_macro_inner(
262262
original_style
263263
};
264264

265-
let ts: TokenStream = mac.stream();
266-
let has_comment = contains_comment(context.snippet(mac.span));
265+
let ts: TokenStream = match *mac.args {
266+
ast::MacArgs::Empty => TokenStream::default(),
267+
ast::MacArgs::Delimited(_, _, token_stream) => token_stream,
268+
ast::MacArgs::Eq(_, token_stream) => token_stream,
269+
};
270+
271+
let has_comment = contains_comment(context.snippet(mac.span()));
267272
if ts.is_empty() && !has_comment {
268273
return match style {
269274
DelimToken::Paren if position == MacroPosition::Item => {
@@ -297,7 +302,7 @@ fn rewrite_macro_inner(
297302
} else if let Some(arg) = parse_macro_arg(&mut parser) {
298303
arg_vec.push(arg);
299304
} else {
300-
return return_macro_parse_failure_fallback(context, shape.indent, mac.span);
305+
return return_macro_parse_failure_fallback(context, shape.indent, mac.span());
301306
}
302307

303308
match parser.token.kind {
@@ -321,16 +326,16 @@ fn rewrite_macro_inner(
321326
return return_macro_parse_failure_fallback(
322327
context,
323328
shape.indent,
324-
mac.span,
329+
mac.span(),
325330
);
326331
}
327332
}
328333
}
329334
}
330-
return return_macro_parse_failure_fallback(context, shape.indent, mac.span);
335+
return return_macro_parse_failure_fallback(context, shape.indent, mac.span());
331336
}
332337
_ if arg_vec.last().map_or(false, MacroArg::is_item) => continue,
333-
_ => return return_macro_parse_failure_fallback(context, shape.indent, mac.span),
338+
_ => return return_macro_parse_failure_fallback(context, shape.indent, mac.span()),
334339
}
335340

336341
parser.bump();
@@ -350,7 +355,7 @@ fn rewrite_macro_inner(
350355
shape,
351356
style,
352357
position,
353-
mac.span,
358+
mac.span(),
354359
);
355360
}
356361

@@ -367,7 +372,7 @@ fn rewrite_macro_inner(
367372
&macro_name,
368373
arg_vec.iter(),
369374
shape,
370-
mac.span,
375+
mac.span(),
371376
context.config.width_heuristics().fn_call_width,
372377
if trailing_comma {
373378
Some(SeparatorTactic::Always)
@@ -404,7 +409,7 @@ fn rewrite_macro_inner(
404409
let rewrite = rewrite_array(
405410
macro_name,
406411
arg_vec.iter(),
407-
mac.span,
412+
mac.span(),
408413
context,
409414
shape,
410415
force_trailing_comma,
@@ -422,7 +427,7 @@ fn rewrite_macro_inner(
422427
// For macro invocations with braces, always put a space between
423428
// the `macro_name!` and `{ /* macro_body */ }` but skip modifying
424429
// anything in between the braces (for now).
425-
let snippet = context.snippet(mac.span).trim_start_matches(|c| c != '{');
430+
let snippet = context.snippet(mac.span()).trim_start_matches(|c| c != '{');
426431
match trim_left_preserve_layout(snippet, shape.indent, &context.config) {
427432
Some(macro_body) => Some(format!("{} {}", macro_name, macro_body)),
428433
None => Some(format!("{} {}", macro_name, snippet)),
@@ -486,8 +491,12 @@ pub(crate) fn rewrite_macro_def(
486491
if snippet.as_ref().map_or(true, |s| s.ends_with(';')) {
487492
return snippet;
488493
}
489-
490-
let mut parser = MacroParser::new(def.stream().into_trees());
494+
let ts: TokenStream = match *def.body {
495+
ast::MacArgs::Empty => TokenStream::default(),
496+
ast::MacArgs::Delimited(_, _, token_stream) => token_stream,
497+
ast::MacArgs::Eq(_, token_stream) => token_stream,
498+
};
499+
let mut parser = MacroParser::new(ts.into_trees());
491500
let parsed_def = match parser.parse() {
492501
Some(def) => def,
493502
None => return snippet,
@@ -1192,17 +1201,21 @@ pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> O
11921201
// https://github.com/rust-lang/rust/pull/62672
11931202
let path = &pprust::path_to_string(&mac.path);
11941203
if path == "try" || path == "r#try" {
1195-
let ts: TokenStream = mac.tts.clone();
1204+
let ts: TokenStream = match *mac.args {
1205+
ast::MacArgs::Empty => TokenStream::default(),
1206+
ast::MacArgs::Delimited(_, _, token_stream) => token_stream,
1207+
ast::MacArgs::Eq(_, token_stream) => token_stream,
1208+
};
11961209
let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect());
11971210

11981211
let mut kind = parser.parse_expr().ok()?;
11991212
// take the end pos of mac so that the Try expression includes the closing parenthesis of
12001213
// the try! macro
1201-
kind.span = mk_sp(kind.span.lo(), mac.span.hi());
1214+
kind.span = mk_sp(kind.span.lo(), mac.span().hi());
12021215
Some(ast::Expr {
12031216
id: ast::NodeId::root(), // dummy value
12041217
kind: ast::ExprKind::Try(kind),
1205-
span: mac.span, // incorrect span, but shouldn't matter too much
1218+
span: mac.span(), // incorrect span, but shouldn't matter too much
12061219
attrs: ast::AttrVec::new(),
12071220
})
12081221
} else {
@@ -1211,7 +1224,7 @@ pub(crate) fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext<'_>) -> O
12111224
}
12121225

12131226
fn macro_style(mac: &ast::Mac, context: &RewriteContext<'_>) -> DelimToken {
1214-
let snippet = context.snippet(mac.span);
1227+
let snippet = context.snippet(mac.span());
12151228
let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
12161229
let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
12171230
let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,14 @@ impl<'a> Parser<'a> {
283283
mac: &'a ast::Mac,
284284
base_dir: &Directory,
285285
) -> Result<Vec<ast::Item>, &'static str> {
286+
let token_stream: syntax::tokenstream::TokenStream = match *mac.args {
287+
ast::MacArgs::Empty => syntax::tokenstream::TokenStream::default(),
288+
ast::MacArgs::Delimited(_, _, token_stream) => token_stream,
289+
ast::MacArgs::Eq(_, token_stream) => token_stream,
290+
};
286291
let mut parser = rustc_parse::stream_to_parser_with_base_dir(
287292
sess.inner(),
288-
mac.tts.clone(),
293+
token_stream.clone(),
289294
base_dir.to_syntax_directory(),
290295
);
291296

rustfmt-core/rustfmt-lib/src/visitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
656656
}
657657

658658
fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPosition) {
659-
skip_out_of_file_lines_range_visitor!(self, mac.span);
659+
skip_out_of_file_lines_range_visitor!(self, mac.span());
660660

661661
// 1 = ;
662662
let shape = self.shape().saturating_sub_width(1);
663663
let rewrite = self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
664-
self.push_rewrite(mac.span, rewrite);
664+
self.push_rewrite(mac.span(), rewrite);
665665
}
666666

667667
pub(crate) fn push_str(&mut self, s: &str) {

0 commit comments

Comments
 (0)