Skip to content

fix: incorrect error message for string literal suffixes#145602

Closed
notJoon wants to merge 7 commits intorust-lang:mainfrom
notJoon:fix/issue-144161
Closed

fix: incorrect error message for string literal suffixes#145602
notJoon wants to merge 7 commits intorust-lang:mainfrom
notJoon:fix/issue-144161

Conversation

@notJoon
Copy link
Copy Markdown
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 to handle raw string literals with suffixes by:

  1. Checking if a token is a raw string literal with a suffix before general literal parsing
  2. Using the existing LitKind::from_token_lit to validate and get the appropriate error

To prevent cascading errors, consume the next token if it's another string literal after an error is reported.

@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

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@notJoon notJoon marked this pull request as ready for review August 20, 2025 05:37
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Aug 20, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 20, 2025
@petrochenkov
Copy link
Copy Markdown
Contributor

This is incorrect, the string literal suffix is a semantic error, not a parsing error, and this code

#[cfg(false)]
fn check() {
    let _ = r#"raw"#suffix;
}

should compile successfully.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 21, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Aug 21, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@bors
Copy link
Copy Markdown
Collaborator

bors commented Aug 22, 2025

☔ The latest upstream changes (presumably #144689) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Copy Markdown
Member

@notJoon any updates on this? thanks

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Oct 17, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Oct 17, 2025

⚠️ Warning ⚠️

  • The following commits have merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

    You can start a rebase with the following commands:

    $ # rebase
    $ git pull --rebase https://github.com/rust-lang/rust.git master
    $ git push --force-with-lease
    

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Oct 17, 2025
@notJoon
Copy link
Copy Markdown
Contributor Author

notJoon commented Oct 17, 2025

@Dylan-DPC sorry for the delay, I'll take a look this asap. thanks for the remind

@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

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

Click to see the possible cause of the failure (guessed by this bot)
---- [ui] tests/ui/parser/bad-lit-suffixes.rs stdout ----
Saved the actual stderr to `/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/parser/bad-lit-suffixes/bad-lit-suffixes.stderr`
diff of stderr:

16 LL |     r#""#suffix;
17    |     ^^^^^^^^^^^ invalid suffix `suffix`
18 
- error: suffixes on string literals are invalid
-   --> $DIR/bad-lit-suffixes.rs:30:17
-    |
- LL | #[rustc_dummy = "string"suffix]
-    |                 ^^^^^^^^^^^^^^ invalid suffix `suffix`
- 
- error: suffixes on string literals are invalid
-   --> $DIR/bad-lit-suffixes.rs:34:14
-    |
- LL | #[must_use = "string"suffix]
-    |              ^^^^^^^^^^^^^^ invalid suffix `suffix`
- 
- error: suffixes on string literals are invalid
-   --> $DIR/bad-lit-suffixes.rs:39:15
-    |
- LL | #[link(name = "string"suffix)]
-    |               ^^^^^^^^^^^^^^ invalid suffix `suffix`
- 
- error: invalid suffix `suffix` for number literal
-   --> $DIR/bad-lit-suffixes.rs:43:41
-    |
- LL | #[rustc_layout_scalar_valid_range_start(0suffix)]
-    |                                         ^^^^^^^ invalid suffix `suffix`
-    |
-    = help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)
- 
45 warning: `extern` declarations without an explicit ABI are deprecated
46   --> $DIR/bad-lit-suffixes.rs:3:1
47    |


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args parser/bad-lit-suffixes.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/parser/bad-lit-suffixes.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/parser/bad-lit-suffixes" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: suffixes on string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:4:5
   |
LL |     "C"suffix //~ ERROR suffixes on string literals are invalid
   |     ^^^^^^^^^ invalid suffix `suffix`

error: suffixes on string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:8:5
   |
LL |     "C"suffix //~ ERROR suffixes on string literals are invalid
   |     ^^^^^^^^^ invalid suffix `suffix`

error: suffixes on string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:14:5
   |
LL |     r#""#suffix; //~ ERROR suffixes on string literals are invalid
   |     ^^^^^^^^^^^ invalid suffix `suffix`

warning: `extern` declarations without an explicit ABI are deprecated
##[warning]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:3:1
   |
LL | extern //~ WARN missing_abi
   | ^^^^^^ help: explicitly specify the "C" ABI: `extern "C"`
   |
   = note: `#[warn(missing_abi)]` on by default

warning: `extern` declarations without an explicit ABI are deprecated
##[warning]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:7:1
   |
LL | extern //~ WARN missing_abi
   | ^^^^^^ help: explicitly specify the "C" ABI: `extern "C"`

error: suffixes on string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:12:5
   |
LL |     ""suffix; //~ ERROR suffixes on string literals are invalid
   |     ^^^^^^^^ invalid suffix `suffix`

error: suffixes on byte string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:13:5
   |
LL |     b""suffix; //~ ERROR suffixes on byte string literals are invalid
   |     ^^^^^^^^^ invalid suffix `suffix`

error: suffixes on byte string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:15:5
   |
LL |     br#""#suffix; //~ ERROR suffixes on byte string literals are invalid
   |     ^^^^^^^^^^^^ invalid suffix `suffix`

error: suffixes on char literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:16:5
   |
LL |     'a'suffix; //~ ERROR suffixes on char literals are invalid
   |     ^^^^^^^^^ invalid suffix `suffix`

error: suffixes on byte literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:17:5
   |
LL |     b'a'suffix; //~ ERROR suffixes on byte literals are invalid
   |     ^^^^^^^^^^ invalid suffix `suffix`

error: invalid width `1024` for integer literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:19:5
   |
LL |     1234u1024; //~ ERROR invalid width `1024` for integer literal
   |     ^^^^^^^^^
   |
   = help: valid widths are 8, 16, 32, 64 and 128

error: invalid width `1024` for integer literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:20:5
   |
LL |     1234i1024; //~ ERROR invalid width `1024` for integer literal
   |     ^^^^^^^^^
   |
   = help: valid widths are 8, 16, 32, 64 and 128

error: invalid width `1024` for float literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:21:5
   |
LL |     1234f1024; //~ ERROR invalid width `1024` for float literal
   |     ^^^^^^^^^
   |
   = help: valid widths are 32 and 64

error: invalid width `1024` for float literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:22:5
   |
LL |     1234.5f1024; //~ ERROR invalid width `1024` for float literal
   |     ^^^^^^^^^^^
   |
   = help: valid widths are 32 and 64

error: invalid suffix `suffix` for number literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:24:5
   |
LL |     1234suffix; //~ ERROR invalid suffix `suffix` for number literal
   |     ^^^^^^^^^^ invalid suffix `suffix`
   |
   = help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)

error: invalid suffix `suffix` for number literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:25:5
   |
LL |     0b101suffix; //~ ERROR invalid suffix `suffix` for number literal
   |     ^^^^^^^^^^^ invalid suffix `suffix`
   |
   = help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)

error: invalid suffix `suffix` for float literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:26:5
   |
LL |     1.0suffix; //~ ERROR invalid suffix `suffix` for float literal
   |     ^^^^^^^^^ invalid suffix `suffix`
   |
   = help: valid suffixes are `f32` and `f64`

error: invalid suffix `suffix` for float literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:27:5
   |
LL |     1.0e10suffix; //~ ERROR invalid suffix `suffix` for float literal
   |     ^^^^^^^^^^^^ invalid suffix `suffix`
   |
   = help: valid suffixes are `f32` and `f64`

error: suffixes on string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:30:17
   |
LL | #[rustc_dummy = "string"suffix]
   |                 ^^^^^^^^^^^^^^ invalid suffix `suffix`

error: suffixes on string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:34:14
   |
LL | #[must_use = "string"suffix]
   |              ^^^^^^^^^^^^^^ invalid suffix `suffix`

error[E0539]: malformed `must_use` attribute input
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:34:1
   |
LL | #[must_use = "string"suffix]
   | ^^^^^^^^^^^^^--------------^
   |              |
   |              expected a string literal here
   |
   = note: for more information, visit <https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute>
help: try changing it to one of the following valid forms of the attribute
   |
LL - #[must_use = "string"suffix]
LL + #[must_use = "reason"]
   |
LL - #[must_use = "string"suffix]
LL + #[must_use]
   |

error: suffixes on string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:39:15
   |
LL | #[link(name = "string"suffix)]
   |               ^^^^^^^^^^^^^^ invalid suffix `suffix`

error: invalid suffix `suffix` for number literal
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:43:41
   |
LL | #[rustc_layout_scalar_valid_range_start(0suffix)]
   |                                         ^^^^^^^ invalid suffix `suffix`
   |
   = help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.)

error: suffixes on string literals are invalid
##[error]  --> /checkout/tests/ui/parser/bad-lit-suffixes.rs:48:25
   |
LL |     #[rustc_confusables("blah"suffix)]
   |                         ^^^^^^^^^^^^ invalid suffix `suffix`

error: aborting due to 22 previous errors; 2 warnings emitted

For more information about this error, try `rustc --explain E0539`.
------------------------------------------

@reddevilmidzy
Copy link
Copy Markdown
Member

@notJoon
Triage: I'm closing this due to inactivity. Please reopen when you are ready to continue with this. Thanks for your contribution.
@rustbot label: +S-inactive

@rustbot rustbot added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

has-merge-commits PR has merge commits, merge with caution. S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. 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

7 participants