Skip to content

bump rustfmt 1x to rustc-ap v651 #4100

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

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 150 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

30 changes: 16 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,45 @@ ignore = "0.4.11"
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`
# for more information.
rustc-workspace-hack = "1.0.0"

[dependencies.rustc_ast]
package = "rustc-ap-rustc_ast"
version = "650.0.0"

[dependencies.rustc_ast_pretty]
package = "rustc-ap-rustc_ast_pretty"
version = "642.0.0"
version = "650.0.0"

[dependencies.rustc_data_structures]
package = "rustc-ap-rustc_data_structures"
version = "642.0.0"
version = "650.0.0"

[dependencies.rustc_errors]
package = "rustc-ap-rustc_errors"
version = "642.0.0"
version = "650.0.0"

[dependencies.rustc_expand]
package = "rustc-ap-rustc_expand"
version = "650.0.0"

[dependencies.rustc_parse]
package = "rustc-ap-rustc_parse"
version = "642.0.0"
version = "650.0.0"

[dependencies.rustc_session]
package = "rustc-ap-rustc_session"
version = "642.0.0"
version = "650.0.0"

[dependencies.rustc_span]
package = "rustc-ap-rustc_span"
version = "642.0.0"
version = "650.0.0"

[dependencies.rustc_target]
package = "rustc-ap-rustc_target"
version = "642.0.0"

[dependencies.syntax]
package = "rustc-ap-syntax"
version = "642.0.0"

[dev-dependencies]
lazy_static = "1.0.0"
version = "650.0.0"
16 changes: 9 additions & 7 deletions src/attr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Format attributes and meta items.

use rustc_ast::ast;
use rustc_span::{symbol::sym, BytePos, Span, DUMMY_SP};
use syntax::ast;

use self::doc_comment::DocCommentFormatter;
use crate::comment::{contains_comment, rewrite_doc_comment, CommentStyle};
Expand All @@ -18,12 +18,13 @@ use crate::utils::{count_newlines, mk_sp};
mod doc_comment;

/// Returns attributes on the given statement.
pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
pub(crate) fn get_attrs_from_stmt(stmt: &ast::Stmt) -> Option<&[ast::Attribute]> {
match stmt.kind {
ast::StmtKind::Local(ref local) => &local.attrs,
ast::StmtKind::Item(ref item) => &item.attrs,
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => &expr.attrs,
ast::StmtKind::Mac(ref mac) => &mac.2,
ast::StmtKind::Local(ref local) => Some(&local.attrs),
ast::StmtKind::Item(ref item) => Some(&item.attrs),
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => Some(&expr.attrs),
ast::StmtKind::MacCall(ref mac) => Some(&mac.2),
ast::StmtKind::Empty => None,
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thanks! I didn't know that existed. Have updated to leverage that accordingly


Expand All @@ -32,10 +33,11 @@ pub(crate) fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
ast::StmtKind::Local(ref local) => local.span,
ast::StmtKind::Item(ref item) => item.span,
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span,
ast::StmtKind::Mac(ref mac) => {
ast::StmtKind::MacCall(ref mac) => {
let (ref mac, _, _) = **mac;
mac.span()
}
ast::StmtKind::Empty => stmt.span,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
use std::borrow::Cow;
use std::cmp::min;

use rustc_ast::{ast, ptr};
use rustc_span::{BytePos, Span};
use syntax::{ast, ptr};

use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar};
use crate::config::IndentStyle;
Expand Down Expand Up @@ -403,7 +403,7 @@ impl Chain {

fn convert_try(expr: &ast::Expr, context: &RewriteContext<'_>) -> ast::Expr {
match expr.kind {
ast::ExprKind::Mac(ref mac) if context.config.use_try_shorthand() => {
ast::ExprKind::MacCall(ref mac) if context.config.use_try_shorthand() => {
if let Some(subexpr) = convert_try_mac(mac, context) {
subexpr
} else {
Expand Down
24 changes: 14 additions & 10 deletions src/closures.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_ast::{ast, ptr};
use rustc_span::Span;
use syntax::{ast, ptr};

use crate::attr::get_attrs_from_stmt;
use crate::config::lists::*;
Expand All @@ -25,7 +25,7 @@ use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};

pub(crate) fn rewrite_closure(
capture: ast::CaptureBy,
is_async: &ast::IsAsync,
is_async: &ast::Async,
movability: ast::Movability,
fn_decl: &ast::FnDecl,
body: &ast::Expr,
Expand All @@ -43,14 +43,14 @@ 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));
}

let result = match fn_decl.output {
ast::FunctionRetTy::Default(_) if !context.inside_macro() => {
ast::FnRetTy::Default(_) if !context.inside_macro() => {
try_rewrite_without_block(body, &prefix, context, shape, body_shape)
}
_ => None,
Expand Down Expand Up @@ -105,14 +105,18 @@ fn get_inner_expr<'a>(

// Figure out if a block is necessary.
fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -> bool {
let has_attributes = block.stmts.first().map_or(false, |first_stmt| {
!get_attrs_from_stmt(first_stmt).is_empty()
});
let has_attributes = block
.stmts
.first()
.map_or(false, |first_stmt| match get_attrs_from_stmt(first_stmt) {
Some(attrs) => !attrs.is_empty(),
None => false,
});

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 @@ -214,7 +218,7 @@ fn rewrite_closure_block(
// Return type is (prefix, extra_offset)
fn rewrite_closure_fn_decl(
capture: ast::CaptureBy,
asyncness: &ast::IsAsync,
asyncness: &ast::Async,
movability: ast::Movability,
fn_decl: &ast::FnDecl,
body: &ast::Expr,
Expand Down Expand Up @@ -305,7 +309,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
39 changes: 19 additions & 20 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::borrow::Cow;
use std::cmp::min;

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

use crate::chains::rewrite_chain;
use crate::closures;
Expand Down Expand Up @@ -199,7 +199,7 @@ pub(crate) fn format_expr(
ast::ExprKind::Try(..) | ast::ExprKind::Field(..) | ast::ExprKind::MethodCall(..) => {
rewrite_chain(expr, context, shape)
}
ast::ExprKind::Mac(ref mac) => {
ast::ExprKind::MacCall(ref mac) => {
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
wrap_str(
context.snippet(expr.span).to_owned(),
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 @@ -1134,47 +1134,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 Expand Up @@ -1313,9 +1312,9 @@ pub(crate) fn can_be_overflowed_expr(
context.config.overflow_delimited_expr()
|| (context.use_block_indent() && args_len == 1)
}
ast::ExprKind::Mac(ref mac) => {
ast::ExprKind::MacCall(ref mac) => {
match (
syntax::ast::MacDelimiter::from_token(mac.args.delim()),
rustc_ast::ast::MacDelimiter::from_token(mac.args.delim()),
context.config.overflow_delimited_expr(),
) {
(Some(ast::MacDelimiter::Bracket), true)
Expand All @@ -1341,7 +1340,7 @@ pub(crate) fn can_be_overflowed_expr(

pub(crate) fn is_nested_call(expr: &ast::Expr) -> bool {
match expr.kind {
ast::ExprKind::Call(..) | ast::ExprKind::Mac(..) => true,
ast::ExprKind::Call(..) | ast::ExprKind::MacCall(..) => true,
ast::ExprKind::AddrOf(_, _, ref expr)
| ast::ExprKind::Box(ref expr)
| ast::ExprKind::Try(ref expr)
Expand Down
Loading