Skip to content

fix: incorrect error message for string literal suffixes #145602

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

notJoon
Copy link
Contributor

@notJoon notJoon commented Aug 19, 2025

Description

Fixes #144161

When a raw string literal has an invalid suffix (like r#"test"#suffix), the compiler was not reporting the appropriate error message "suffixes on string literals are invalid". This was particularly problematic when the suffix was immediately followed by another token, as in r#" \\ "#r"\\ ".

Changes

Modified parse_expr_lit in compiler/rustc_parse/src/parser/expr.rs to validate literals using LitKind::from_token_lit and report errors appropriately.

By doing so,

  • Single point of error reporting prevents duplicates
  • Preserves the original token for attribute parsing

When modifying the lexer directly to detect invalid suffixes, the two problems mentioned above additionally occurred.

Also, modified it to consume the next token if it's a string literal after reporting a suffix error. This prevents the parser from generating the cascading error.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 19, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
tidy check
Checking tidy rustdoc_json...
No error code explanation was removed!
tidy: Skipping binary file check, read-only filesystem
##[error]tidy error: /checkout/tests/ui/parser/raw-string-suffix-invalid.rs:2: line longer than 100 chars
some tidy checks failed
Command `/checkout/obj/build/aarch64-unknown-linux-gnu/stage1-tools-bin/rust-tidy /checkout /checkout/obj/build/aarch64-unknown-linux-gnu/stage0/bin/cargo /checkout/obj/build 4 npm` failed with exit code 1
Created at: src/bootstrap/src/core/build_steps/tool.rs:1583:23
Executed at: src/bootstrap/src/core/build_steps/test.rs:1225:29

Command has failed. Rerun with -v to see more details.
Bootstrap failed while executing `--stage 2 test --skip tests --skip coverage-map --skip coverage-run --skip library --skip tidyselftest`
Build completed unsuccessfully in 0:00:52
  local time: Tue Aug 19 15:20:11 UTC 2025
  network time: Tue, 19 Aug 2025 15:20:11 GMT
##[error]Process completed with exit code 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong error for string literal suffix when stuck together
3 participants