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
Merged
Show file tree
Hide file tree
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
30 changes: 29 additions & 1 deletion crates/parser/src/lexed_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ impl<'a> Converter<'a> {
}
}

/// Check for likely unterminated string by analyzing STRING token content
fn has_likely_unterminated_string(&self) -> bool {
let Some(last_idx) = self.res.kind.len().checked_sub(1) else { return false };

for i in (0..=last_idx).rev().take(5) {
if self.res.kind[i] == STRING {
let start = self.res.start[i] as usize;
let end = self.res.start.get(i + 1).map(|&s| s as usize).unwrap_or(self.offset);
let content = &self.res.text[start..end];

if content.contains('(') && (content.contains("//") || content.contains(";\n")) {
return true;
}
}
}
false
}

fn finalize_with_eof(mut self) -> LexedStr<'a> {
self.res.push(EOF, self.offset);
self.res
Expand Down Expand Up @@ -267,7 +285,17 @@ 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 has_unterminated = self.has_likely_unterminated_string();

let error_msg = if has_unterminated {
format!(
"unknown literal prefix `{}` (note: check for unterminated string literal)",
token_text
)
} else {
"unknown literal prefix".to_owned()
};
errors.push(error_msg);
IDENT
}
rustc_lexer::TokenKind::Eof => EOF,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FN_KW "fn"
WHITESPACE " "
IDENT "main"
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
L_CURLY "{"
WHITESPACE "\n "
IDENT "hello"
L_PAREN "("
STRING "\"world);\n // a bunch of code was here\n env(\"FLAGS"
STRING "\", \""
MINUS "-"
IDENT "help" error: unknown literal prefix `help` (note: check for unterminated string literal)
STRING "\")\n}" error: Missing trailing `"` symbol to terminate the string literal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
hello("world);
// a bunch of code was here
env("FLAGS", "-help")
}
Loading