rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.23k stars 12.71k forks source link

Unclosed string literals cause unhelpful diagnostic #117559

Open bend-n opened 1 year ago

bend-n commented 1 year ago

Code

fn main() { "a"-a"; }

Current output

error: prefix `a` is unknown
 --> src/main.rs:1:17
  |
1 | fn main() { "a"-a"; }
  |                 ^ unknown prefix
  |
  = note: prefixed identifiers and literals are reserved since Rust 2021
help: consider inserting whitespace here
  |
1 | fn main() { "a"-a "; }
  |                  +

error[E0765]: unterminated double quote string
 --> src/main.rs:1:18
  |
1 | fn main() { "a"-a"; }
  |                  ^^^^

Desired output

error[E0765]: unterminated double quote string
 --> src/main.rs:1:18
  |
1 | fn main() { "a"-a"; }
  |               ^  ^^^^ here
  |               help: you may have meant to close this string instead: `"a""`
  = note: prefixed identifiers and literals are reserved since Rust 2021

Rationale and extra context

Confusing error when dealing with long unclosed string literals and command line options.

Other cases

No response

Anything else?

No response

gurry commented 1 year ago

@rustbot claim

gurry commented 1 year ago

To reproduce with only rustc without using cargo you will need to specify --edition 2021 e.g. rustc <file>.rs --edition 2021

gurry commented 1 year ago

It is hard for the compiler to tell whether you merely forgot a starting quote before a or you actually meant to use a as a string prefix. So it is emitting errors for both scenarios, which I feel is reasonable.

There is one shortcoming though. The consider inserting whitespace here suggestion makes sense only in a macro invocation i.e. if the code were like this.: my_macro!("a"-a"). It is not helpful in other cases and therefore we could suppress it. However, given that the error originates in the lexer/StringReader, adding such smarts so early in the compiler pipeline may be too much effort.

So all things considered I feel we should leave this behaviour as is.

(For the background on unknown prefixes and the suggestion to insert whitespace see RFC 3101: https://rust-lang.github.io/rfcs/3101-reserved_prefixes.html)

gurry commented 12 months ago

@rustbot release-assignment

aplatypus commented 11 months ago

Hi gang, I get this error on program litteral strings. It makes No sense really because the bytes between one quote(") and the final quote(") must be literal character encoding (UTF-8) normally.

Heres is an example of the error I saw:

source

   let matches = Command::new("Typester")
        .version("0.1.0")
        .author("Alex E")
        .about("Convert Rust types to Typescript types")
        .arg
        (
            Arg::new("input")
                .short('i')
                .long("input")
                .required(true)
                .help("The Rust file to process (including extension)"),
        )

output errror

  --> src/main.rs:48:23
   |
48 |         .author("Alex E")
   |                       ^ unknown prefix
   |
   = note: prefixed identifiers and literals are reserved since Rust 2021
help: consider inserting whitespace here
   |
48 |         .author("Alex E ")

Now; I have done teh normal quote and braket matching for this kind of thing. I'm quite concerned now that text withing a quoted srtring has some "influence" on other bits of code.

Imh Litteral meas litteral -- NO, 'interpretation' is required on string data.

Anyhoo ... this error itself seems spurious -- There are no interpolations or text enpansionhs neededed hear -- Why / HOW can it 'be' a problem

Thanks, William

aplatypus commented 11 months ago

Follow-up post . . . Eventually I discovered a missing quote character in the .rs file. Hard to detect once I had found it; so mea cuppa the fault here was mine. Girls and Boys -- check your quotes!!!

bend-n commented 11 months ago

^ case of how unhelpful the diagnostic is.