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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion crates/parser/src/lexed_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ impl<'a> Converter<'a> {
}
}

/// Simple check for recent unterminated string literal
fn has_recent_unterminated_string(&self) -> bool {
let start = self.offset.saturating_sub(500);
let recent_text = &self.res.text[start..self.offset];
recent_text.chars().filter(|&c| c == '"').count() % 2 == 1
}

fn finalize_with_eof(mut self) -> LexedStr<'a> {
self.res.push(EOF, self.offset);
self.res
Expand Down Expand Up @@ -267,7 +274,15 @@ impl<'a> Converter<'a> {
rustc_lexer::TokenKind::Unknown => ERROR,
rustc_lexer::TokenKind::UnknownPrefix if token_text == "builtin" => IDENT,
rustc_lexer::TokenKind::UnknownPrefix => {
errors.push("unknown literal prefix".into());
let error_msg = if self.has_recent_unterminated_string() {
format!(
"unknown literal prefix `{}` (note: check for unterminated string literal)",
token_text
)
} else {
"unknown literal prefix".into()
};
errors.push(error_msg);
IDENT
}
rustc_lexer::TokenKind::Eof => EOF,
Expand Down
Loading