Skip to content

Commit e7c3fe1

Browse files
committed
feat: hint at unterminated strings in unknown prefix errors
When encountering 'unknown literal prefix' errors, check for unbalanced quotes in recent code and suggest checking for unterminated string literals.
1 parent 92ba1cc commit e7c3fe1

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

crates/parser/src/lexed_str.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ impl<'a> Converter<'a> {
149149
}
150150
}
151151

152+
/// Check for likely unterminated string by analyzing STRING token content
153+
fn has_likely_unterminated_string(&self) -> bool {
154+
let Some(last_idx) = self.res.kind.len().checked_sub(1) else { return false };
155+
156+
for i in (0..=last_idx).rev().take(5) {
157+
if self.res.kind[i] == STRING {
158+
let start = self.res.start[i] as usize;
159+
let end = self.res.start.get(i + 1).map(|&s| s as usize).unwrap_or(self.offset);
160+
let content = &self.res.text[start..end];
161+
162+
if content.contains('(') && (content.contains("//") || content.contains(";\n")) {
163+
return true;
164+
}
165+
}
166+
}
167+
false
168+
}
169+
152170
fn finalize_with_eof(mut self) -> LexedStr<'a> {
153171
self.res.push(EOF, self.offset);
154172
self.res
@@ -267,7 +285,17 @@ impl<'a> Converter<'a> {
267285
rustc_lexer::TokenKind::Unknown => ERROR,
268286
rustc_lexer::TokenKind::UnknownPrefix if token_text == "builtin" => IDENT,
269287
rustc_lexer::TokenKind::UnknownPrefix => {
270-
errors.push("unknown literal prefix".into());
288+
let has_unterminated = self.has_likely_unterminated_string();
289+
290+
let error_msg = if has_unterminated {
291+
format!(
292+
"unknown literal prefix `{}` (note: check for unterminated string literal)",
293+
token_text
294+
)
295+
} else {
296+
"unknown literal prefix".to_owned()
297+
};
298+
errors.push(error_msg);
271299
IDENT
272300
}
273301
rustc_lexer::TokenKind::Eof => EOF,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FN_KW "fn"
2+
WHITESPACE " "
3+
IDENT "main"
4+
L_PAREN "("
5+
R_PAREN ")"
6+
WHITESPACE " "
7+
L_CURLY "{"
8+
WHITESPACE "\n "
9+
IDENT "hello"
10+
L_PAREN "("
11+
STRING "\"world);\n // a bunch of code was here\n env(\"FLAGS"
12+
STRING "\", \""
13+
MINUS "-"
14+
IDENT "help" error: unknown literal prefix `help` (note: check for unterminated string literal)
15+
STRING "\")\n}" error: Missing trailing `"` symbol to terminate the string literal
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
hello("world);
3+
// a bunch of code was here
4+
env("FLAGS", "-help")
5+
}

0 commit comments

Comments
 (0)