Skip to content

[internal error] on trying to format overlong string literal #4414

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

Closed
moxian opened this issue Sep 4, 2020 · 2 comments
Closed

[internal error] on trying to format overlong string literal #4414

moxian opened this issue Sep 4, 2020 · 2 comments
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate

Comments

@moxian
Copy link
Contributor

moxian commented Sep 4, 2020

Describe the bug

Have a file with the following contents:

fn some() {
    let results = 
    foo.bar(r#"
            a aaaaaaaaaaaaa aaaaa aaa aaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaa aaa aaaaaaaaaaaaaaaaaaaa
    "#
    , );
}

(the specific content of the string literal does not seen to matter, but the length does).

Run rustfmt filename.rs

Get:

> rustfmt stuff.rs
error[internal]: left behind trailing whitespace
 --> \\?\D:\path\to\stuff.rs:2:2:17
  |
2 |     let results =
  |                  ^
  |

warning: rustfmt has failed to format. See previous 1 errors.

Note that the mere presence of the string literal anywhere in the file breaks formatting for the entire file, not just for the containing block (but that might be WAI)

Expected behavior

no [internal error]; other lines get formatted just fine, even if this one cannot

Meta

  • rustfmt version:rustfmt 1.4.20-beta (48f6c32 2020-08-09)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: doesn't matter. (both rustfmt path\to\file.rs and the vscode integration observe the same result)
@moxian moxian added the bug Panic, non-idempotency, invalid code, etc. label Sep 4, 2020
@calebcartwright
Copy link
Member

Thank you for reaching out @moxian. This is a duplicate (#3863) of a known/existing issues with formatting chains when one or more of the chain elements would exceed max_width when formatted. In these cases, rustfmt leaves the original formatting in place and defers to the developer to properly format, because any formatting rustfmt would apply would violate a rule (max width in this case)

Your snippet above contains a chain due to the foo.bar(... on line 3, and the string arg exceeds the max_width value of 100. Your original snippet has a trailing space after the = on line 2, so when rustfmt bails on formatting the chain and reverts to the original snippet, your original trailing space is maintained which triggers the trailing whitespace warning.

To get rid of the warning, you should remove the trailing whitespace after the =. To allow rustfmt to be able to format the chain, you could try moving the raw string lit out of the chain foo.bar(some_new_var) or if you don't need a raw string lit, use a regular string and try setting format_strings = true in your rustfmt config file to allow rustfmt to split the string to stay within the max_width limit.

Note that the mere presence of the string literal anywhere in the file breaks formatting for the entire file, not just for the containing block (but that might be WAI)

Hmmm. I'm not able to reproduce this locally (although I do observe this behavior on the Playground).

When I run rustfmt against this:

fn some() {
    let results = 
    foo.bar(r#"
            a aaaaaaaaaaaaa aaaaa aaa aaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaa aaa aaaaaaaaaaaaaaaaaaaa
    "#
    , );
}

fn           foo (   ) {  



println!("bar")         ; }

I get this output:

Diff in /home/caleb/dev/rustfmt-sandbox/chains-8/src/lib.rs at line 6:
     , );
 }
 
-fn           foo (   ) {  
-
-
-
-println!("bar")         ; }
+fn foo() {
+    println!("bar");
+}
+
error[internal]: left behind trailing whitespace
 --> /home/caleb/dev/rustfmt-sandbox/chains-8/src/lib.rs:2:2:17
  |
2 |     let results = 
  |                  ^
  |

warning: rustfmt has failed to format. See previous 1 errors.

@calebcartwright
Copy link
Member

Closing as a duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate
Projects
None yet
Development

No branches or pull requests

2 participants