Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bd5dff4

Browse files
refactor: backport syntux mod
1 parent 9699c96 commit bd5dff4

22 files changed

+959
-766
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ ignore = "0.4.11"
5656
annotate-snippets = { version = "0.6", features = ["ansi_term"] }
5757
structopt = "0.3"
5858
rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" }
59+
lazy_static = "1.0.0"
5960

6061
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
6162
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
@@ -93,6 +94,3 @@ version = "647.0.0"
9394
[dependencies.syntax]
9495
package = "rustc-ap-rustc_ast"
9596
version = "647.0.0"
96-
97-
[dev-dependencies]
98-
lazy_static = "1.0.0"

src/closures.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn rewrite_closure(
4343

4444
if let ast::ExprKind::Block(ref block, _) = body.kind {
4545
// The body of the closure is an empty block.
46-
if block.stmts.is_empty() && !block_contains_comment(block, context.source_map) {
46+
if block.stmts.is_empty() && !block_contains_comment(context, block) {
4747
return body
4848
.rewrite(context, shape)
4949
.map(|s| format!("{} {}", prefix, s));
@@ -116,7 +116,7 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -
116116
is_unsafe_block(block)
117117
|| block.stmts.len() > 1
118118
|| has_attributes
119-
|| block_contains_comment(block, context.source_map)
119+
|| block_contains_comment(context, block)
120120
|| prefix.contains('\n')
121121
}
122122

@@ -309,7 +309,7 @@ pub(crate) fn rewrite_last_closure(
309309
ast::ExprKind::Block(ref block, _)
310310
if !is_unsafe_block(block)
311311
&& !context.inside_macro()
312-
&& is_simple_block(block, Some(&body.attrs), context.source_map) =>
312+
&& is_simple_block(context, block, Some(&body.attrs)) =>
313313
{
314314
stmt_expr(&block.stmts[0]).unwrap_or(body)
315315
}

src/comment.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,10 +1556,10 @@ pub(crate) fn recover_comment_removed(
15561556
// We missed some comments. Warn and keep the original text.
15571557
if context.config.error_on_unformatted() {
15581558
context.report.append(
1559-
context.source_map.span_to_filename(span).into(),
1559+
context.parse_sess.span_to_filename(span),
15601560
vec![FormattingError::from_span(
15611561
span,
1562-
&context.source_map,
1562+
&context.parse_sess,
15631563
ErrorKind::LostComment,
15641564
)],
15651565
);

src/expr.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22
use std::cmp::min;
33

44
use itertools::Itertools;
5-
use rustc_span::{source_map::SourceMap, BytePos, Span};
5+
use rustc_span::{BytePos, Span};
66
use syntax::token::{DelimToken, LitKind};
77
use syntax::{ast, ptr};
88

@@ -430,7 +430,7 @@ fn rewrite_empty_block(
430430
return None;
431431
}
432432

433-
if !block_contains_comment(block, context.source_map) && shape.width >= 2 {
433+
if !block_contains_comment(context, block) && shape.width >= 2 {
434434
return Some(format!("{}{}{{}}", prefix, label_str));
435435
}
436436

@@ -487,7 +487,7 @@ fn rewrite_single_line_block(
487487
label: Option<ast::Label>,
488488
shape: Shape,
489489
) -> Option<String> {
490-
if is_simple_block(block, attrs, context.source_map) {
490+
if is_simple_block(context, block, attrs) {
491491
let expr_shape = shape.offset_left(last_line_width(prefix))?;
492492
let expr_str = block.stmts[0].rewrite(context, expr_shape)?;
493493
let label_str = rewrite_label(label);
@@ -750,8 +750,8 @@ impl<'a> ControlFlow<'a> {
750750
let fixed_cost = self.keyword.len() + " { } else { }".len();
751751

752752
if let ast::ExprKind::Block(ref else_node, _) = else_block.kind {
753-
if !is_simple_block(self.block, None, context.source_map)
754-
|| !is_simple_block(else_node, None, context.source_map)
753+
if !is_simple_block(context, self.block, None)
754+
|| !is_simple_block(context, else_node, None)
755755
|| pat_expr_str.contains('\n')
756756
{
757757
return None;
@@ -1134,47 +1134,46 @@ fn extract_comment(span: Span, context: &RewriteContext<'_>, shape: Shape) -> Op
11341134
}
11351135
}
11361136

1137-
pub(crate) fn block_contains_comment(block: &ast::Block, source_map: &SourceMap) -> bool {
1138-
let snippet = source_map.span_to_snippet(block.span).unwrap();
1139-
contains_comment(&snippet)
1137+
pub(crate) fn block_contains_comment(context: &RewriteContext<'_>, block: &ast::Block) -> bool {
1138+
contains_comment(context.snippet(block.span))
11401139
}
11411140

11421141
// Checks that a block contains no statements, an expression and no comments or
11431142
// attributes.
11441143
// FIXME: incorrectly returns false when comment is contained completely within
11451144
// the expression.
11461145
pub(crate) fn is_simple_block(
1146+
context: &RewriteContext<'_>,
11471147
block: &ast::Block,
11481148
attrs: Option<&[ast::Attribute]>,
1149-
source_map: &SourceMap,
11501149
) -> bool {
11511150
block.stmts.len() == 1
11521151
&& stmt_is_expr(&block.stmts[0])
1153-
&& !block_contains_comment(block, source_map)
1152+
&& !block_contains_comment(context, block)
11541153
&& attrs.map_or(true, |a| a.is_empty())
11551154
}
11561155

11571156
/// Checks whether a block contains at most one statement or expression, and no
11581157
/// comments or attributes.
11591158
pub(crate) fn is_simple_block_stmt(
1159+
context: &RewriteContext<'_>,
11601160
block: &ast::Block,
11611161
attrs: Option<&[ast::Attribute]>,
1162-
source_map: &SourceMap,
11631162
) -> bool {
11641163
block.stmts.len() <= 1
1165-
&& !block_contains_comment(block, source_map)
1164+
&& !block_contains_comment(context, block)
11661165
&& attrs.map_or(true, |a| a.is_empty())
11671166
}
11681167

11691168
/// Checks whether a block contains no statements, expressions, comments, or
11701169
/// inner attributes.
11711170
pub(crate) fn is_empty_block(
1171+
context: &RewriteContext<'_>,
11721172
block: &ast::Block,
11731173
attrs: Option<&[ast::Attribute]>,
1174-
source_map: &SourceMap,
11751174
) -> bool {
11761175
block.stmts.is_empty()
1177-
&& !block_contains_comment(block, source_map)
1176+
&& !block_contains_comment(context, block)
11781177
&& attrs.map_or(true, |a| inner_attributes(a).is_empty())
11791178
}
11801179

0 commit comments

Comments
 (0)