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

Format attributes on stmt and single line block #1814

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
55 changes: 34 additions & 21 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ fn combine_attr_and_expr(
String::new()
} else {
// Try to recover comments between the attributes and the expression if available.
let missing_snippet = context.snippet(mk_sp(
expr.attrs[expr.attrs.len() - 1].span.hi,
expr.span.lo,
));
// Some dirty hack involved here to recover missing `do` from rust parser.
let hi = if let ast::ExprKind::Catch(..) = expr.node {
let missing_span = mk_sp(expr.attrs[expr.attrs.len() - 1].span.hi, expr.span.lo);
let snippet = context.snippet(missing_span);
let do_pos = snippet.find_uncommented("do").unwrap();
missing_span.lo + BytePos(do_pos as u32)
} else {
expr.span.lo
};
let missing_snippet = context.snippet(mk_sp(expr.attrs[expr.attrs.len() - 1].span.hi, hi));
let comment_opening_pos = missing_snippet.chars().position(|c| c == '/');
let prefer_same_line = if let Some(pos) = comment_opening_pos {
!missing_snippet[..pos].contains('\n')
Expand Down Expand Up @@ -185,10 +191,11 @@ pub fn format_expr(
} else {
// Rewrite block without trying to put it in a single line.
if let rw @ Some(_) = rewrite_empty_block(context, block, shape) {
return rw;
rw
} else {
let prefix = try_opt!(block_prefix(context, block, shape));
rewrite_block_with_visitor(context, &prefix, block, shape)
}
let prefix = try_opt!(block_prefix(context, block, shape));
rewrite_block_with_visitor(context, &prefix, block, shape)
}
}
ExprType::SubExpression => block.rewrite(context, shape),
Expand Down Expand Up @@ -337,21 +344,31 @@ pub fn format_expr(
if let rewrite @ Some(_) =
rewrite_single_line_block(context, "do catch ", block, shape)
{
return rewrite;
rewrite
} else {
// 9 = `do catch `
let budget = shape.width.checked_sub(9).unwrap_or(0);
let shape = Shape::legacy(budget, shape.indent).add_offset(9);
Some(format!(
"do catch {}",
try_opt!(block.rewrite(&context, shape))
))
}
// 9 = `do catch `
let budget = shape.width.checked_sub(9).unwrap_or(0);
Some(format!(
"{}{}",
"do catch ",
try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent)))
))
}
};

expr_rw
.and_then(|expr_str| {
recover_comment_removed(expr_str, expr.span, context, shape)
// Unfortunately, we are missing `do` in span of catch from parser. We must add `do`
// manually when recovering missing comments.
recover_comment_removed(expr_str, expr.span, context, shape).map(|recoverd_str| {
if let ast::ExprKind::Catch(..) = expr.node {
if !recoverd_str.starts_with("do") {
return String::from("do ") + &recoverd_str;
}
}
recoverd_str
})
})
.and_then(|expr_str| {
combine_attr_and_expr(context, shape, expr, &expr_str)
Expand Down Expand Up @@ -948,11 +965,7 @@ impl Rewrite for ast::Stmt {

format_expr(
ex,
match self.node {
ast::StmtKind::Expr(_) => ExprType::SubExpression,
ast::StmtKind::Semi(_) => ExprType::Statement,
_ => unreachable!(),
},
ExprType::Statement,
context,
try_opt!(shape.sub_width(suffix.len())),
).map(|s| s + suffix)
Expand Down
8 changes: 7 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ impl Rewrite for ast::Local {
shape.width,
shape.indent
);
let mut result = "let ".to_owned();
let mut result = if self.attrs.is_empty() {
"let ".to_owned()
} else {
let attrs_str = try_opt!(self.attrs.rewrite(context, shape));
let indent_str = shape.indent.to_string(context.config);
format!("{}\n{}let ", attrs_str, indent_str)
};

// 4 = "let ".len()
let pat_shape = try_opt!(shape.offset_left(4));
Expand Down
20 changes: 14 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ impl Spanned for ast::Expr {

impl Spanned for ast::Stmt {
fn span(&self) -> Span {
match self.node {
// Cover attributes
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => {
mk_sp(expr.span().lo, self.span.hi)
}
_ => self.span,
let attrs = stmt_attrs(self);
if attrs.is_empty() {
self.span
} else {
mk_sp(attrs[0].span.lo, self.span.hi)
}
}
}
Expand Down Expand Up @@ -205,6 +204,15 @@ impl Spanned for ast::TyParamBound {
}
}

fn stmt_attrs(stmt: &ast::Stmt) -> &[ast::Attribute] {
match stmt.node {
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,
}
}

#[derive(Copy, Clone, Debug)]
pub struct Indent {
// Width of the block indent, in characters. Must be a multiple of
Expand Down
34 changes: 10 additions & 24 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use syntax::{ast, ptr, visit};
use syntax::codemap::{BytePos, CodeMap, Span};
use syntax::parse::ParseSess;

use {Indent, Shape};
use {Indent, Shape, Spanned};
use codemap::{LineRangeUtils, SpanUtils};
use comment::{contains_comment, FindUncommented};
use comment::rewrite_comment;
use config::{BraceStyle, Config};
use expr::{format_expr, ExprType};
use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_associated_type,
rewrite_static, rewrite_type_alias};
use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorTactic};
Expand Down Expand Up @@ -88,39 +87,26 @@ impl<'a> FmtVisitor<'a> {
Shape::indented(self.block_indent, self.config),
)
};
self.push_rewrite(stmt.span, rewrite);
self.push_rewrite(stmt.span(), rewrite);
}
ast::StmtKind::Expr(ref expr) => {
let rewrite = format_expr(
expr,
ExprType::Statement,
&self.get_context(),
Shape::indented(self.block_indent, self.config),
);
let span = if expr.attrs.is_empty() {
stmt.span
} else {
mk_sp(expr.attrs[0].span.lo, stmt.span.hi)
};
self.push_rewrite(span, rewrite)
}
ast::StmtKind::Semi(ref expr) => {
ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
let rewrite = stmt.rewrite(
&self.get_context(),
Shape::indented(self.block_indent, self.config),
);
let span = if expr.attrs.is_empty() {
stmt.span
} else {
mk_sp(expr.attrs[0].span.lo, stmt.span.hi)
};
self.push_rewrite(span, rewrite)
self.push_rewrite(stmt.span(), rewrite)
}
ast::StmtKind::Mac(ref mac) => {
let (ref mac, _macro_style, ref attrs) = **mac;
if contains_skip(attrs) {
self.push_rewrite(mac.span, None);
} else {
if !attrs.is_empty() {
let shape = Shape::indented(self.block_indent, self.config);
let attrs_rw = attrs.rewrite(&self.get_context(), shape);
let span = mk_sp(attrs[0].span.lo, attrs.last().unwrap().span.hi);
self.push_rewrite(span, attrs_rw);
}
self.visit_mac(mac, None, MacroPosition::Statement);
}
self.format_missing(stmt.span.hi);
Expand Down
23 changes: 23 additions & 0 deletions tests/source/attrib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,26 @@ fn issue_1799() {
// https://github.com/rust-lang/rust/issues/43336
Some( Err(error) ) ;
}

// #1813
fn attributes_on_statemetns() {
// Semi
# [ an_attribute ( rustfmt ) ]
foo(1);

// Local
# [ an_attribute ( rustfmt ) ]
let x = foo(a, b, c);

// Item
# [ an_attribute ( rustfmt ) ]
use foobar;

// Mac
# [ an_attribute ( rustfmt ) ]
vec![1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 1, 1, 2, 1];

// Expr
# [ an_attribute ( rustfmt ) ]
{}
}
3 changes: 3 additions & 0 deletions tests/source/catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ fn main() {
do catch {
// Regular do catch block
};

#[ an_attribute(rustfmt) ]
do catch { foo()? };
}
4 changes: 0 additions & 4 deletions tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,6 @@ fn handle_result(

if fmt_text != text {
let diff = make_diff(&text, &fmt_text, DIFF_CONTEXT_SIZE);
assert!(
!diff.is_empty(),
"Empty diff? Maybe due to a missing a newline at the end of a file?"
);
Copy link
Member

Choose a reason for hiding this comment

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

Should this be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a mistake, sorry.

failures.insert(file_name, diff);
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/target/attrib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,26 @@ fn issue_1799() {
// https://github.com/rust-lang/rust/issues/43336
Some(Err(error));
}

// #1813
fn attributes_on_statemetns() {
// Semi
#[an_attribute(rustfmt)]
foo(1);

// Local
#[an_attribute(rustfmt)]
let x = foo(a, b, c);

// Item
#[an_attribute(rustfmt)]
use foobar;

// Mac
#[an_attribute(rustfmt)]
vec![1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 1, 1, 2, 1];

// Expr
#[an_attribute(rustfmt)]
{}
}
3 changes: 3 additions & 0 deletions tests/target/catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ fn main() {
do catch {
// Regular do catch block
};

#[an_attribute(rustfmt)]
do catch { foo()? };
}