Skip to content

Fix for 4824 - multi comments cause internal error #5502

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,14 @@ impl<'a> FmtVisitor<'a> {
let first_line = &subslice[..offset];
self.push_str(first_line);
self.push_str(&comment_indent.to_string_with_newline(self.config));

let other_lines = &subslice[offset + 1..];
let comment_str =
rewrite_comment(other_lines, false, comment_shape, self.config)
.unwrap_or_else(|| String::from(other_lines));
let comment_str = rewrite_comment(
other_lines.trim_start(),
false,
comment_shape,
self.config,
)
.unwrap_or_else(|| other_lines.to_string());
self.push_str(&comment_str);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.push_str(&self.block_indent.to_string_with_newline(config));

// put the other lines below it, shaping it as needed
let other_lines = &sub_slice[offset + 1..];
let other_lines = &sub_slice[offset + 1..].trim_start();
let comment_str =
rewrite_comment(other_lines, false, comment_shape, config);
match comment_str {
Expand Down
71 changes: 71 additions & 0 deletions tests/source/issue-4824/one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// rustfmt-version: One

// Original issue test case

#[cfg(windows)]
fn main() {
use common_util::get_version_for_rc;
use std::env;
use winres::{VersionInfo, WindowsResource};

println!("cargo:rerun-if-changed=favicon.ico");
println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
/*let profile = env::var("PROFILE").unwrap();
if profile == "release"*/ {
let mut res = WindowsResource::new();
res.set_icon("favicon.ico");
let version = get_version_for_rc!();
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
res.set_version_info(VersionInfo::FILEVERSION, version);
res.compile().unwrap();
}
}

#[cfg(not(windows))]
fn main() {}

// Other test cases

fn main() {
let a = 1; // One line comment
/* Block comment */
true
}

fn main() {
let a = 1; /* Block comment */
// One line comment
true
}

fn main() {
let a = 1; // One line comment
/* Block comment */
}

fn main() {
let a = 1; /* Block comment */
// One line comment
}

fn main() {
// One line comment
/* Block comment */
true
}

fn main() {
/* Block comment */
// One line comment
true
}

fn main() {
// One line comment
/* Block comment */
}

fn main() {
/* Block comment */
// One line comment
}
71 changes: 71 additions & 0 deletions tests/source/issue-4824/two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// rustfmt-version: Two

// Original issue test case

#[cfg(windows)]
fn main() {
use common_util::get_version_for_rc;
use std::env;
use winres::{VersionInfo, WindowsResource};

println!("cargo:rerun-if-changed=favicon.ico");
println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
/*let profile = env::var("PROFILE").unwrap();
if profile == "release"*/ {
let mut res = WindowsResource::new();
res.set_icon("favicon.ico");
let version = get_version_for_rc!();
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
res.set_version_info(VersionInfo::FILEVERSION, version);
res.compile().unwrap();
}
}

#[cfg(not(windows))]
fn main() {}

// Other test cases

fn main() {
let a = 1; // One line comment
/* Block comment */
true
}

fn main() {
let a = 1; /* Block comment */
// One line comment
true
}

fn main() {
let a = 1; // One line comment
/* Block comment */
}

fn main() {
let a = 1; /* Block comment */
// One line comment
}

fn main() {
// One line comment
/* Block comment */
true
}

fn main() {
/* Block comment */
// One line comment
true
}

fn main() {
// One line comment
/* Block comment */
}

fn main() {
/* Block comment */
// One line comment
}
72 changes: 72 additions & 0 deletions tests/target/issue-4824/one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// rustfmt-version: One

// Original issue test case

#[cfg(windows)]
fn main() {
use common_util::get_version_for_rc;
use std::env;
use winres::{VersionInfo, WindowsResource};

println!("cargo:rerun-if-changed=favicon.ico");
println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
/*let profile = env::var("PROFILE").unwrap();
if profile == "release"*/
{
let mut res = WindowsResource::new();
res.set_icon("favicon.ico");
let version = get_version_for_rc!();
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
res.set_version_info(VersionInfo::FILEVERSION, version);
res.compile().unwrap();
}
}

#[cfg(not(windows))]
fn main() {}

// Other test cases

fn main() {
let a = 1; // One line comment
/* Block comment */
true
}

fn main() {
let a = 1; /* Block comment */
// One line comment
true
}

fn main() {
let a = 1; // One line comment
/* Block comment */
}

fn main() {
let a = 1; /* Block comment */
// One line comment
}

fn main() {
// One line comment
/* Block comment */
true
}

fn main() {
/* Block comment */
// One line comment
true
}

fn main() {
// One line comment
/* Block comment */
}

fn main() {
/* Block comment */
// One line comment
}
72 changes: 72 additions & 0 deletions tests/target/issue-4824/two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// rustfmt-version: Two

// Original issue test case

#[cfg(windows)]
fn main() {
use common_util::get_version_for_rc;
use std::env;
use winres::{VersionInfo, WindowsResource};

println!("cargo:rerun-if-changed=favicon.ico");
println!("cargo:rerun-if-changed=Cargo.toml"); // rerun when version changed
/*let profile = env::var("PROFILE").unwrap();
if profile == "release"*/
{
let mut res = WindowsResource::new();
res.set_icon("favicon.ico");
let version = get_version_for_rc!();
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
res.set_version_info(VersionInfo::FILEVERSION, version);
res.compile().unwrap();
}
}

#[cfg(not(windows))]
fn main() {}

// Other test cases

fn main() {
let a = 1; // One line comment
/* Block comment */
true
}

fn main() {
let a = 1; /* Block comment */
// One line comment
true
}

fn main() {
let a = 1; // One line comment
/* Block comment */
}

fn main() {
let a = 1; /* Block comment */
// One line comment
}

fn main() {
// One line comment
/* Block comment */
true
}

fn main() {
/* Block comment */
// One line comment
true
}

fn main() {
// One line comment
/* Block comment */
}

fn main() {
/* Block comment */
// One line comment
}