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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

avrabe
Copy link

@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() {
    println\!("hello world   // missing closing quote
    let result = zero_function();  // error reported here: unknown literal prefix `zero`
}

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 `zero`

After:

error: unknown literal prefix `zero` (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 2 times, most recently from d7dc953 to c16075a Compare August 11, 2025 15:14
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 c16075a to e5b6382 Compare August 11, 2025 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants