Skip to content

feat: hint at unterminated strings in unknown prefix errors #20425

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

Merged

Conversation

avrabe
Copy link
Contributor

@avrabe avrabe commented Aug 11, 2025

Hi! This is my first contribution to rust-analyzer - I hope I'm following the right approach.

Problem

When rust-analyzer encounters "unknown literal prefix" errors, they can sometimes be caused by unterminated string literals earlier in the code. This makes debugging confusing because the error points to a distant location rather than the actual missing quote.

For example:

fn main() {
    hello("world);
    // a bunch of code was here
    env("FLAGS", "-help")
}

Solution

Added simple quote balance detection to provide helpful hints when this situation occurs:

  • When an "unknown literal prefix" error happens, check for unbalanced quotes in the preceding 500 characters
  • If odd number of quotes found, add a note suggesting to check for unterminated string literals
  • If even number, show normal error message

Before:

error: unknown literal prefix `help`

After:

error: unknown literal prefix `help` (note: check for unterminated string literal)

Implementation

  • File: crates/parser/src/lexed_str.rs
  • Lines added: 8 (very minimal change)
  • Performance: Only checks recent text when error occurs
  • Safety: Non-breaking, only enhances existing error messages

The implementation follows rust-analyzer's style of simple, reliable improvements. I tried to keep it as minimal as possible while still being helpful.

I'd really appreciate any feedback on the approach or implementation! Thank you for considering this contribution.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 11, 2025
@avrabe avrabe force-pushed the feat/unterminated-string-hints branch from 5be12e4 to 5555867 Compare August 11, 2025 04:34
@ChayimFriedman2
Copy link
Contributor

Seems reasonable, but please add a test. You can see other lexer tests at crates/parser/test_data/lexer/err/.

@avrabe avrabe force-pushed the feat/unterminated-string-hints branch 5 times, most recently from 929cda7 to 604c622 Compare August 14, 2025 05:59
@avrabe
Copy link
Contributor Author

avrabe commented Aug 14, 2025

@ChayimFriedman2 Thanks for the review. Added a test. Had to update the detection.

@avrabe avrabe force-pushed the feat/unterminated-string-hints branch 2 times, most recently from 2e145c7 to 2d94eea Compare August 14, 2025 17:30
When encountering 'unknown literal prefix' errors, check for unbalanced
quotes in recent code and suggest checking for unterminated string literals.
@avrabe avrabe force-pushed the feat/unterminated-string-hints branch from 2d94eea to e7c3fe1 Compare August 14, 2025 17:30
Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Aug 14, 2025
Merged via the queue into rust-lang:master with commit 70fcdd5 Aug 14, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants