Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntux module #3880

Merged
merged 12 commits into from
Oct 25, 2019
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ignore = "0.4.6"
annotate-snippets = { version = "0.6", features = ["ansi_term"] }
structopt = "0.3"
rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" }
lazy_static = "1.0.0"

# A noop dependency that changes in the Rust repository, it's a bit of a hack.
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
Expand All @@ -74,5 +75,3 @@ version = "610.0.0"
package = "rustc-ap-syntax_pos"
version = "610.0.0"

[dev-dependencies]
lazy_static = "1.0.0"
6 changes: 3 additions & 3 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) fn rewrite_closure(

if let ast::ExprKind::Block(ref block, _) = body.kind {
// The body of the closure is an empty block.
if block.stmts.is_empty() && !block_contains_comment(block, context.source_map) {
if block.stmts.is_empty() && !block_contains_comment(context, block) {
return body
.rewrite(context, shape)
.map(|s| format!("{} {}", prefix, s));
Expand Down Expand Up @@ -112,7 +112,7 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -
is_unsafe_block(block)
|| block.stmts.len() > 1
|| has_attributes
|| block_contains_comment(block, context.source_map)
|| block_contains_comment(context, block)
|| prefix.contains('\n')
}

Expand Down Expand Up @@ -304,7 +304,7 @@ pub(crate) fn rewrite_last_closure(
ast::ExprKind::Block(ref block, _)
if !is_unsafe_block(block)
&& !context.inside_macro()
&& is_simple_block(block, Some(&body.attrs), context.source_map) =>
&& is_simple_block(context, block, Some(&body.attrs)) =>
{
stmt_expr(&block.stmts[0]).unwrap_or(body)
}
Expand Down
4 changes: 2 additions & 2 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,10 +1556,10 @@ pub(crate) fn recover_comment_removed(
// We missed some comments. Warn and keep the original text.
if context.config.error_on_unformatted() {
context.report.append(
context.source_map.span_to_filename(span).into(),
context.parse_sess.span_to_filename(span),
vec![FormattingError::from_span(
span,
&context.source_map,
&context.parse_sess,
ErrorKind::LostComment,
)],
);
Expand Down
27 changes: 13 additions & 14 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cmp::min;

use itertools::Itertools;
use syntax::parse::token::{DelimToken, LitKind};
use syntax::source_map::{BytePos, SourceMap, Span};
use syntax::source_map::{BytePos, Span};
use syntax::{ast, ptr};

use crate::chains::rewrite_chain;
Expand Down Expand Up @@ -430,7 +430,7 @@ fn rewrite_empty_block(
return None;
}

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

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

if let ast::ExprKind::Block(ref else_node, _) = else_block.kind {
if !is_simple_block(self.block, None, context.source_map)
|| !is_simple_block(else_node, None, context.source_map)
if !is_simple_block(context, self.block, None)
|| !is_simple_block(context, else_node, None)
|| pat_expr_str.contains('\n')
{
return None;
Expand Down Expand Up @@ -1105,47 +1105,46 @@ fn extract_comment(span: Span, context: &RewriteContext<'_>, shape: Shape) -> Op
}
}

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

// Checks that a block contains no statements, an expression and no comments or
// attributes.
// FIXME: incorrectly returns false when comment is contained completely within
// the expression.
pub(crate) fn is_simple_block(
context: &RewriteContext<'_>,
block: &ast::Block,
attrs: Option<&[ast::Attribute]>,
source_map: &SourceMap,
) -> bool {
(block.stmts.len() == 1
&& stmt_is_expr(&block.stmts[0])
&& !block_contains_comment(block, source_map)
&& !block_contains_comment(context, block)
&& attrs.map_or(true, |a| a.is_empty()))
}

/// Checks whether a block contains at most one statement or expression, and no
/// comments or attributes.
pub(crate) fn is_simple_block_stmt(
context: &RewriteContext<'_>,
block: &ast::Block,
attrs: Option<&[ast::Attribute]>,
source_map: &SourceMap,
) -> bool {
block.stmts.len() <= 1
&& !block_contains_comment(block, source_map)
&& !block_contains_comment(context, block)
&& attrs.map_or(true, |a| a.is_empty())
}

/// Checks whether a block contains no statements, expressions, comments, or
/// inner attributes.
pub(crate) fn is_empty_block(
context: &RewriteContext<'_>,
block: &ast::Block,
attrs: Option<&[ast::Attribute]>,
source_map: &SourceMap,
) -> bool {
block.stmts.is_empty()
&& !block_contains_comment(block, source_map)
&& !block_contains_comment(context, block)
&& attrs.map_or(true, |a| inner_attributes(a).is_empty())
}

Expand Down
Loading