Sitelet https://github.com/starkware-libs/cairo/commit/1ed257ac2761fc21ed54f38ddffc8b09637a68b9
Skip to content

Commit 1ed257a

Browse files
authored
fix(parser): stop block recovery at a following control-flow statement (#10227)
1 parent b5c1cc6 commit 1ed257a

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

  • crates/cairo-lang-parser/src

‎crates/cairo-lang-parser/src/parser_test_data/diagnostics/if‎

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ error[E1002]: Missing tokens. Expected an expression.
4040
if 0 == if x {1} else {2} {
4141
^
4242

43-
error[E1000]: Skipped tokens. Expected: '{'.
44-
--> dummy_file.cairo:2:13
43+
error[E1001]: Missing token '{'.
44+
--> dummy_file.cairo:2:12
4545
if 0 == if x {1} else {2} {
46-
^^^^
46+
^
47+
48+
error[E1001]: Missing token '}'.
49+
--> dummy_file.cairo:4:2
50+
}
51+
^
4752

4853
//! > ==========================================================================
4954

@@ -315,3 +320,57 @@ error[E1000]: Skipped tokens. Expected: statement.
315320
--> dummy_file.cairo:7:44
316321
if x += 3 && let x = 10 || false < > { += }
317322
^^
323+
324+
//! > ==========================================================================
325+
326+
//! > Test broken if header does not swallow a following control-flow statement
327+
328+
//! > test_comments
329+
// Recovering the broken `if` block must stop at the following `while`, leaving it
330+
// as its own statement rather than consuming its `{ ... }` as the `if` body.
331+
332+
//! > test_runner_name
333+
get_diagnostics
334+
335+
//! > cairo_code
336+
fn f(a: felt252, b: bool) {
337+
if a ==
338+
while b { let _x = 1; }
339+
let _y = 2;
340+
}
341+
342+
//! > expected_diagnostics
343+
error[E1002]: Missing tokens. Expected an expression.
344+
--> dummy_file.cairo:2:12
345+
if a ==
346+
^
347+
348+
error[E1001]: Missing token '{'.
349+
--> dummy_file.cairo:2:12
350+
if a ==
351+
^
352+
353+
error[E1001]: Missing token '}'.
354+
--> dummy_file.cairo:5:2
355+
}
356+
^
357+
358+
//! > ==========================================================================
359+
360+
//! > Test bad if condition with well-formed braces keeps the inner statement.
361+
362+
//! > test_runner_name
363+
get_diagnostics
364+
365+
//! > cairo_code
366+
fn f(b: bool) {
367+
if {
368+
while b {}
369+
}
370+
}
371+
372+
//! > expected_diagnostics
373+
error[E1002]: Missing tokens. Expected an expression.
374+
--> dummy_file.cairo:2:7
375+
if {
376+
^

‎crates/cairo-lang-parser/src/recovery.rs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ pub(crate) use module_item_kw;
102102

103103
macro_rules! block {
104104
() => {
105-
SyntaxKind::TerminalLet | SyntaxKind::TerminalMatch | SyntaxKind::TerminalReturn
105+
SyntaxKind::TerminalLet
106+
| SyntaxKind::TerminalMatch
107+
| SyntaxKind::TerminalReturn
108+
| SyntaxKind::TerminalBreak
109+
| SyntaxKind::TerminalContinue
110+
| SyntaxKind::TerminalIf
111+
| SyntaxKind::TerminalWhile
112+
| SyntaxKind::TerminalLoop
113+
| SyntaxKind::TerminalFor
106114
};
107115
}
108116
pub(crate) use block;

0 commit comments

Comments
 (0)