Skip to content

Commit 4ed31b6

Browse files
authored
Merge pull request rust-lang#3322 from fyrchik/fix/3304
calculate statement first line properly
2 parents ecde43e + 1093945 commit 4ed31b6

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

Diff for: src/attr.rs

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ pub fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
3434
}
3535
}
3636

37+
pub fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
38+
match stmt.node {
39+
ast::StmtKind::Local(ref local) => local.span,
40+
ast::StmtKind::Item(ref item) => item.span,
41+
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span,
42+
ast::StmtKind::Mac(ref mac) => {
43+
let (ref mac, _, _) = **mac;
44+
mac.span
45+
}
46+
}
47+
}
48+
3749
/// Returns attributes that are within `outer_span`.
3850
pub fn filter_inline_attrs(attrs: &[ast::Attribute], outer_span: Span) -> Vec<ast::Attribute> {
3951
attrs

Diff for: src/visitor.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
113113
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
114114
let attrs = get_attrs_from_stmt(stmt);
115115
if contains_skip(attrs) {
116-
self.push_skipped_with_span(attrs, stmt.span());
116+
self.push_skipped_with_span(attrs, stmt.span(), get_span_without_attrs(stmt));
117117
} else {
118118
let shape = self.shape();
119119
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
@@ -123,7 +123,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
123123
ast::StmtKind::Mac(ref mac) => {
124124
let (ref mac, _macro_style, ref attrs) = **mac;
125125
if self.visit_attrs(attrs, ast::AttrStyle::Outer) {
126-
self.push_skipped_with_span(attrs, stmt.span());
126+
self.push_skipped_with_span(attrs, stmt.span(), get_span_without_attrs(stmt));
127127
} else {
128128
self.visit_mac(mac, None, MacroPosition::Statement);
129129
}
@@ -331,14 +331,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
331331
// For use items, skip rewriting attributes. Just check for a skip attribute.
332332
ast::ItemKind::Use(..) => {
333333
if contains_skip(attrs) {
334-
self.push_skipped_with_span(attrs.as_slice(), item.span());
334+
self.push_skipped_with_span(attrs.as_slice(), item.span(), item.span());
335335
return;
336336
}
337337
}
338338
// Module is inline, in this case we treat it like any other item.
339339
_ if !is_mod_decl(item) => {
340340
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
341-
self.push_skipped_with_span(item.attrs.as_slice(), item.span());
341+
self.push_skipped_with_span(item.attrs.as_slice(), item.span(), item.span());
342342
return;
343343
}
344344
}
@@ -357,7 +357,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
357357
}
358358
_ => {
359359
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
360-
self.push_skipped_with_span(item.attrs.as_slice(), item.span());
360+
self.push_skipped_with_span(item.attrs.as_slice(), item.span(), item.span());
361361
return;
362362
}
363363
}
@@ -474,7 +474,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
474474
skip_out_of_file_lines_range_visitor!(self, ti.span);
475475

476476
if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
477-
self.push_skipped_with_span(ti.attrs.as_slice(), ti.span());
477+
self.push_skipped_with_span(ti.attrs.as_slice(), ti.span(), ti.span());
478478
return;
479479
}
480480

@@ -518,7 +518,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
518518
skip_out_of_file_lines_range_visitor!(self, ii.span);
519519

520520
if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
521-
self.push_skipped_with_span(ii.attrs.as_slice(), ii.span());
521+
self.push_skipped_with_span(ii.attrs.as_slice(), ii.span(), ii.span());
522522
return;
523523
}
524524

@@ -592,16 +592,24 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
592592
self.push_rewrite_inner(span, rewrite);
593593
}
594594

595-
pub fn push_skipped_with_span(&mut self, attrs: &[ast::Attribute], item_span: Span) {
595+
pub fn push_skipped_with_span(
596+
&mut self,
597+
attrs: &[ast::Attribute],
598+
item_span: Span,
599+
main_span: Span,
600+
) {
596601
self.format_missing_with_indent(source!(self, item_span).lo());
597602
// do not take into account the lines with attributes as part of the skipped range
598603
let attrs_end = attrs
599604
.iter()
600605
.map(|attr| self.source_map.lookup_char_pos(attr.span().hi()).line)
601606
.max()
602607
.unwrap_or(1);
603-
// Add 1 to get the line past the last attribute
604-
let lo = attrs_end + 1;
608+
let first_line = self.source_map.lookup_char_pos(main_span.lo()).line;
609+
// Statement can start after some newlines and/or spaces
610+
// or it can be on the same line as the last attribute.
611+
// So here we need to take a minimum between the two.
612+
let lo = std::cmp::min(attrs_end + 1, first_line);
605613
self.push_rewrite_inner(item_span, None);
606614
let hi = self.line_number + 1;
607615
self.skipped_range.push((lo, hi));

Diff for: tests/target/issue-3304.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// rustfmt-error_on_line_overflow: true
2+
3+
#[rustfmt::skip] use one::two::three::four::five::six::seven::eight::night::ten::eleven::twelve::thirteen::fourteen::fiveteen;
4+
#[rustfmt::skip]
5+
6+
use one::two::three::four::five::six::seven::eight::night::ten::eleven::twelve::thirteen::fourteen::fiveteen;
7+
8+
macro_rules! test_macro {
9+
($($id:ident),*) => {};
10+
}
11+
12+
macro_rules! test_macro2 {
13+
($($id:ident),*) => {
14+
1
15+
};
16+
}
17+
18+
fn main() {
19+
#[rustfmt::skip] test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
20+
#[rustfmt::skip]
21+
22+
test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
23+
}
24+
25+
fn test_local() {
26+
#[rustfmt::skip] let x = test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
27+
#[rustfmt::skip]
28+
29+
let x = test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
30+
}
31+
32+
fn test_expr(_: [u32]) -> u32 {
33+
#[rustfmt::skip] test_expr([9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999]);
34+
#[rustfmt::skip]
35+
36+
test_expr([9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999])
37+
}
38+
39+
#[rustfmt::skip] mod test { use one::two::three::four::five::six::seven::eight::night::ten::eleven::twelve::thirteen::fourteen::fiveteen; }
40+
#[rustfmt::skip]
41+
42+
mod test { use one::two::three::four::five::six::seven::eight::night::ten::eleven::twelve::thirteen::fourteen::fiveteen; }

0 commit comments

Comments
 (0)