Skip to content

Commit c7e2bfd

Browse files
AlexWaygoodntBre
andauthored
[ty] continue and break statements outside loops are syntax errors (#20944)
Co-authored-by: Brent Westbrook <[email protected]>
1 parent c424007 commit c7e2bfd

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

crates/ty_python_semantic/resources/mdtest/diagnostics/semantic_syntax_errors.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,25 @@ def f():
354354
x = 1
355355
global x # error: [invalid-syntax] "name `x` is used prior to global declaration"
356356
```
357+
358+
## `break` and `continue` outside a loop
359+
360+
<!-- snapshot-diagnostics -->
361+
362+
```py
363+
break # error: [invalid-syntax]
364+
continue # error: [invalid-syntax]
365+
366+
for x in range(42):
367+
break # fine
368+
continue # fine
369+
370+
def _():
371+
break # error: [invalid-syntax]
372+
continue # error: [invalid-syntax]
373+
374+
class Fine:
375+
# this is invalid syntax despite it being in an eager-nested scope!
376+
break # error: [invalid-syntax]
377+
continue # error: [invalid-syntax]
378+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
source: crates/ty_test/src/lib.rs
3+
expression: snapshot
4+
---
5+
---
6+
mdtest name: semantic_syntax_errors.md - Semantic syntax error diagnostics - `break` and `continue` outside a loop
7+
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/semantic_syntax_errors.md
8+
---
9+
10+
# Python source files
11+
12+
## mdtest_snippet.py
13+
14+
```
15+
1 | break # error: [invalid-syntax]
16+
2 | continue # error: [invalid-syntax]
17+
3 |
18+
4 | for x in range(42):
19+
5 | break # fine
20+
6 | continue # fine
21+
7 |
22+
8 | def _():
23+
9 | break # error: [invalid-syntax]
24+
10 | continue # error: [invalid-syntax]
25+
11 |
26+
12 | class Fine:
27+
13 | # this is invalid syntax despite it being in an eager-nested scope!
28+
14 | break # error: [invalid-syntax]
29+
15 | continue # error: [invalid-syntax]
30+
```
31+
32+
# Diagnostics
33+
34+
```
35+
error[invalid-syntax]: `break` outside loop
36+
--> src/mdtest_snippet.py:1:1
37+
|
38+
1 | break # error: [invalid-syntax]
39+
| ^^^^^
40+
2 | continue # error: [invalid-syntax]
41+
|
42+
43+
```
44+
45+
```
46+
error[invalid-syntax]: `continue` outside loop
47+
--> src/mdtest_snippet.py:2:1
48+
|
49+
1 | break # error: [invalid-syntax]
50+
2 | continue # error: [invalid-syntax]
51+
| ^^^^^^^^
52+
3 |
53+
4 | for x in range(42):
54+
|
55+
56+
```
57+
58+
```
59+
error[invalid-syntax]: `break` outside loop
60+
--> src/mdtest_snippet.py:9:9
61+
|
62+
8 | def _():
63+
9 | break # error: [invalid-syntax]
64+
| ^^^^^
65+
10 | continue # error: [invalid-syntax]
66+
|
67+
68+
```
69+
70+
```
71+
error[invalid-syntax]: `continue` outside loop
72+
--> src/mdtest_snippet.py:10:9
73+
|
74+
8 | def _():
75+
9 | break # error: [invalid-syntax]
76+
10 | continue # error: [invalid-syntax]
77+
| ^^^^^^^^
78+
11 |
79+
12 | class Fine:
80+
|
81+
82+
```
83+
84+
```
85+
error[invalid-syntax]: `break` outside loop
86+
--> src/mdtest_snippet.py:14:9
87+
|
88+
12 | class Fine:
89+
13 | # this is invalid syntax despite it being in an eager-nested scope!
90+
14 | break # error: [invalid-syntax]
91+
| ^^^^^
92+
15 | continue # error: [invalid-syntax]
93+
|
94+
95+
```
96+
97+
```
98+
error[invalid-syntax]: `continue` outside loop
99+
--> src/mdtest_snippet.py:15:9
100+
|
101+
13 | # this is invalid syntax despite it being in an eager-nested scope!
102+
14 | break # error: [invalid-syntax]
103+
15 | continue # error: [invalid-syntax]
104+
| ^^^^^^^^
105+
|
106+
107+
```

crates/ty_python_semantic/src/semantic_index/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2787,7 +2787,7 @@ impl SemanticSyntaxContext for SemanticIndexBuilder<'_, '_> {
27872787
}
27882788

27892789
fn in_loop_context(&self) -> bool {
2790-
true
2790+
self.current_scope_info().current_loop.is_some()
27912791
}
27922792
}
27932793

0 commit comments

Comments
 (0)