Skip to content

Raw strings w/ too many terminating hashes are not exclusively handled #140618

Open
@md-tr

Description

@md-tr

Code

fn main() {
    r#"ok!"###;
}

Current output

error: reserved multi-hash token is forbidden
 --> src/main.rs:2:13
  |
2 |     r#"ok!"###;
  |             ^^
  |
  = note: sequences of two or more # are reserved for future use since Rust 2024
help: consider inserting whitespace here
  |
2 |     r#"ok!"## #;
  |              +

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `##`
 --> src/main.rs:2:13
  |
2 |     r#"ok!"###;
  |             ^^ expected one of `.`, `;`, `?`, `}`, or an operator

error: could not compile `playground` (bin "playground") due to 2 previous errors

Desired output

error: too many `#` when terminating raw string
 --> src/main.rs:2:13
  |
2 |     r#"ok!"###;
  |     --------^^ help: remove the extra `#`
  |     |
  |     this raw string started with 1 `#`

error: could not compile `playground` (bin "playground") due to 1 previous error

Rationale and extra context

rustc is already capable of warning the user if a raw string is found to be finalized with one more octothorpe (#) than expected. To avoid confusion, the compiler reserves multi-hash tokens, ensuring that ### and #"foo"# are not interpreted as three separate tokens in macro invocations (see here). However, in my opinion, raw strings whose terminators are followed by one or more '#' characters may not create an edge case. They must already be recognized by the compiler as a "malformed token."

Rust Version

rustc 1.88.0-nightly (4824c2bb7 2025-05-02)
binary: rustc
commit-hash: 4824c2bb7445cb2478aab0190c268c939d77a0f6
commit-date: 2025-05-02
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions