Skip to content

Commit 6106aef

Browse files
committed
Fix rust-lang#4824 - multi comments cause internal error
1 parent 949da52 commit 6106aef

File tree

6 files changed

+289
-4
lines changed

6 files changed

+289
-4
lines changed

Diff for: src/missed_spans.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,10 @@ impl<'a> FmtVisitor<'a> {
278278
let first_line = &subslice[..offset];
279279
self.push_str(first_line);
280280
self.push_str(&comment_indent.to_string_with_newline(self.config));
281-
282-
let other_lines = &subslice[offset + 1..];
281+
let other_lines = &subslice[offset + 1..].trim_start();
283282
let comment_str =
284283
rewrite_comment(other_lines, false, comment_shape, self.config)
285-
.unwrap_or_else(|| String::from(other_lines));
284+
.unwrap_or_else(|| other_lines.to_string());
286285
self.push_str(&comment_str);
287286
}
288287
}

Diff for: src/visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
313313
self.push_str(&self.block_indent.to_string_with_newline(config));
314314

315315
// put the other lines below it, shaping it as needed
316-
let other_lines = &sub_slice[offset + 1..];
316+
let other_lines = &sub_slice[offset + 1..].trim_start();
317317
let comment_str =
318318
rewrite_comment(other_lines, false, comment_shape, config);
319319
match comment_str {

Diff for: tests/source/issue-4824/one.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// rustfmt-version: One
2+
3+
// Original issue test case
4+
5+
#[cfg(windows)]
6+
fn main() {
7+
use common_util::get_version_for_rc;
8+
use std::env;
9+
use winres::{VersionInfo, WindowsResource};
10+
11+
println!("cargo:rerun-if-changed=favicon.ico");
12+
println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
13+
/*let profile = env::var("PROFILE").unwrap();
14+
if profile == "release"*/ {
15+
let mut res = WindowsResource::new();
16+
res.set_icon("favicon.ico");
17+
let version = get_version_for_rc!();
18+
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
19+
res.set_version_info(VersionInfo::FILEVERSION, version);
20+
res.compile().unwrap();
21+
}
22+
}
23+
24+
#[cfg(not(windows))]
25+
fn main() {}
26+
27+
// Other test cases
28+
29+
fn main() {
30+
let a = 1; // One line comment
31+
/* Block comment */
32+
true
33+
}
34+
35+
fn main() {
36+
let a = 1; /* Block comment */
37+
// One line comment
38+
true
39+
}
40+
41+
fn main() {
42+
let a = 1; // One line comment
43+
/* Block comment */
44+
}
45+
46+
fn main() {
47+
let a = 1; /* Block comment */
48+
// One line comment
49+
}
50+
51+
fn main() {
52+
// One line comment
53+
/* Block comment */
54+
true
55+
}
56+
57+
fn main() {
58+
/* Block comment */
59+
// One line comment
60+
true
61+
}
62+
63+
fn main() {
64+
// One line comment
65+
/* Block comment */
66+
}
67+
68+
fn main() {
69+
/* Block comment */
70+
// One line comment
71+
}

Diff for: tests/source/issue-4824/two.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// rustfmt-version: Two
2+
3+
// Original issue test case
4+
5+
#[cfg(windows)]
6+
fn main() {
7+
use common_util::get_version_for_rc;
8+
use std::env;
9+
use winres::{VersionInfo, WindowsResource};
10+
11+
println!("cargo:rerun-if-changed=favicon.ico");
12+
println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
13+
/*let profile = env::var("PROFILE").unwrap();
14+
if profile == "release"*/ {
15+
let mut res = WindowsResource::new();
16+
res.set_icon("favicon.ico");
17+
let version = get_version_for_rc!();
18+
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
19+
res.set_version_info(VersionInfo::FILEVERSION, version);
20+
res.compile().unwrap();
21+
}
22+
}
23+
24+
#[cfg(not(windows))]
25+
fn main() {}
26+
27+
// Other test cases
28+
29+
fn main() {
30+
let a = 1; // One line comment
31+
/* Block comment */
32+
true
33+
}
34+
35+
fn main() {
36+
let a = 1; /* Block comment */
37+
// One line comment
38+
true
39+
}
40+
41+
fn main() {
42+
let a = 1; // One line comment
43+
/* Block comment */
44+
}
45+
46+
fn main() {
47+
let a = 1; /* Block comment */
48+
// One line comment
49+
}
50+
51+
fn main() {
52+
// One line comment
53+
/* Block comment */
54+
true
55+
}
56+
57+
fn main() {
58+
/* Block comment */
59+
// One line comment
60+
true
61+
}
62+
63+
fn main() {
64+
// One line comment
65+
/* Block comment */
66+
}
67+
68+
fn main() {
69+
/* Block comment */
70+
// One line comment
71+
}

Diff for: tests/target/issue-4824/one.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// rustfmt-version: One
2+
3+
// Original issue test case
4+
5+
#[cfg(windows)]
6+
fn main() {
7+
use common_util::get_version_for_rc;
8+
use std::env;
9+
use winres::{VersionInfo, WindowsResource};
10+
11+
println!("cargo:rerun-if-changed=favicon.ico");
12+
println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
13+
/*let profile = env::var("PROFILE").unwrap();
14+
if profile == "release"*/
15+
{
16+
let mut res = WindowsResource::new();
17+
res.set_icon("favicon.ico");
18+
let version = get_version_for_rc!();
19+
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
20+
res.set_version_info(VersionInfo::FILEVERSION, version);
21+
res.compile().unwrap();
22+
}
23+
}
24+
25+
#[cfg(not(windows))]
26+
fn main() {}
27+
28+
// Other test cases
29+
30+
fn main() {
31+
let a = 1; // One line comment
32+
/* Block comment */
33+
true
34+
}
35+
36+
fn main() {
37+
let a = 1; /* Block comment */
38+
// One line comment
39+
true
40+
}
41+
42+
fn main() {
43+
let a = 1; // One line comment
44+
/* Block comment */
45+
}
46+
47+
fn main() {
48+
let a = 1; /* Block comment */
49+
// One line comment
50+
}
51+
52+
fn main() {
53+
// One line comment
54+
/* Block comment */
55+
true
56+
}
57+
58+
fn main() {
59+
/* Block comment */
60+
// One line comment
61+
true
62+
}
63+
64+
fn main() {
65+
// One line comment
66+
/* Block comment */
67+
}
68+
69+
fn main() {
70+
/* Block comment */
71+
// One line comment
72+
}

Diff for: tests/target/issue-4824/two.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// rustfmt-version: Two
2+
3+
// Original issue test case
4+
5+
#[cfg(windows)]
6+
fn main() {
7+
use common_util::get_version_for_rc;
8+
use std::env;
9+
use winres::{VersionInfo, WindowsResource};
10+
11+
println!("cargo:rerun-if-changed=favicon.ico");
12+
println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
13+
/*let profile = env::var("PROFILE").unwrap();
14+
if profile == "release"*/
15+
{
16+
let mut res = WindowsResource::new();
17+
res.set_icon("favicon.ico");
18+
let version = get_version_for_rc!();
19+
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
20+
res.set_version_info(VersionInfo::FILEVERSION, version);
21+
res.compile().unwrap();
22+
}
23+
}
24+
25+
#[cfg(not(windows))]
26+
fn main() {}
27+
28+
// Other test cases
29+
30+
fn main() {
31+
let a = 1; // One line comment
32+
/* Block comment */
33+
true
34+
}
35+
36+
fn main() {
37+
let a = 1; /* Block comment */
38+
// One line comment
39+
true
40+
}
41+
42+
fn main() {
43+
let a = 1; // One line comment
44+
/* Block comment */
45+
}
46+
47+
fn main() {
48+
let a = 1; /* Block comment */
49+
// One line comment
50+
}
51+
52+
fn main() {
53+
// One line comment
54+
/* Block comment */
55+
true
56+
}
57+
58+
fn main() {
59+
/* Block comment */
60+
// One line comment
61+
true
62+
}
63+
64+
fn main() {
65+
// One line comment
66+
/* Block comment */
67+
}
68+
69+
fn main() {
70+
/* Block comment */
71+
// One line comment
72+
}

0 commit comments

Comments
 (0)