Skip to content
Closed
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
11 changes: 11 additions & 0 deletions src/nimony/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,11 @@ proc semWhile(c: var SemContext; it: var Item) =
takeToken c, it.n
semBoolExpr c, it.n
inc c.routine.inLoop
let oldBreakInLoop = c.routine.breakInLoop
c.routine.breakInLoop = true
withNewScope c:
semStmt c, it.n, true
c.routine.breakInLoop = oldBreakInLoop
dec c.routine.inLoop
takeParRi c, it.n
producesVoid c, info, it.typ
Expand All @@ -1217,6 +1220,8 @@ proc semBlock(c: var SemContext; it: var Item) =
takeToken c, it.n

inc c.routine.inBlock
let oldBreakInLoop = c.routine.breakInLoop
c.routine.breakInLoop = false
withNewScope c:
if it.n.kind == DotToken:
takeToken c, it.n
Expand All @@ -1227,6 +1232,7 @@ proc semBlock(c: var SemContext; it: var Item) =
publish c, delayed.s.name, declStart

semStmtBranch c, it, true
c.routine.breakInLoop = oldBreakInLoop
dec c.routine.inBlock

takeParRi c, it.n
Expand All @@ -1242,6 +1248,8 @@ proc semBreak(c: var SemContext; it: var Item) =
else:
if it.n.kind == DotToken:
wantDot c, it.n
if not c.routine.breakInLoop:
buildErr c, info, "Using an unnamed break in a block is not allowed"
else:
let labelInfo = it.n.info
var a = Item(n: it.n, typ: c.types.autoType)
Expand Down Expand Up @@ -2600,7 +2608,10 @@ proc semFor(c: var SemContext; it: var Item) =
takeTree c.dest, it.n # don't touch the body
else:
inc c.routine.inLoop
let oldBreakInLoop = c.routine.breakInLoop
c.routine.breakInLoop = true
semStmt c, it.n, true
c.routine.breakInLoop = oldBreakInLoop
dec c.routine.inLoop

takeParRi c, it.n
Expand Down
1 change: 1 addition & 0 deletions src/nimony/semdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import std / [tables, sets, os, syncio, formatfloat, assertions]
include ".." / lib / nifprelude
import ".." / lib / [symparser, nifindexes]
import nimony_model, symtabs, builtintypes, decls, programs, magics, reporters, nifconfig, xints

Check warning on line 12 in src/nimony/semdata.nim

View workflow job for this annotation

GitHub Actions / linux-i386-nim-devel (master)

imported and not used: 'reporters' [UnusedImport]

Check warning on line 12 in src/nimony/semdata.nim

View workflow job for this annotation

GitHub Actions / linux-amd64-nim-devel (master)

imported and not used: 'reporters' [UnusedImport]

Check warning on line 12 in src/nimony/semdata.nim

View workflow job for this annotation

GitHub Actions / macos-arm64-nim-devel (master)

imported and not used: 'reporters' [UnusedImport]

Check warning on line 12 in src/nimony/semdata.nim

View workflow job for this annotation

GitHub Actions / windows-amd64-nim-devel (master)

imported and not used: 'reporters' [UnusedImport]

import ".." / gear2 / modnames

Expand All @@ -19,6 +19,7 @@
kind*: SymKind
hasDefer*: bool
inGeneric*, inLoop*, inBlock*, inInst*: int
breakInLoop*: bool # whether we are in a loop without block
returnType*: TypeCursor
pragmas*: set[PragmaKind]
resId*: SymId
Expand Down
1 change: 1 addition & 0 deletions src/nimony/semdecls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ proc semProcImpl(c: var SemContext; it: var Item; kind: SymKind; pass: PassKind;
if kind == TemplateY:
inc c.routine.inLoop
inc c.routine.inGeneric
c.routine.breakInLoop = true

try:
c.openScope() # open parameter scope
Expand Down
6 changes: 3 additions & 3 deletions tests/nimony/nosystem/t1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ proc foo_block* =
var s = 12
break lab

block:
var s = 13
break
# block:
# var s = 13
# break

block lab:
var s = 14
Expand Down
24 changes: 21 additions & 3 deletions tests/nimony/sysbasics/tblocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ proc foo =
foo()

proc main =
block endLess:
let s = 12
break
# block endLess:
# let s = 12
# break

block endLess:
let s = 12
Expand All @@ -37,3 +37,21 @@ proc test(): int {.discardable.} =

block:
test()


for i in 1..2: # works
break

block: # works
for i in 1..2:
break

block: # works
block:
discard 12 + 3
for i in 1..2:
break

block named: # works
if true:
break named
2 changes: 2 additions & 0 deletions tests/nimony/sysbasics/tunnamedblock.msgs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/nimony/sysbasics/tunnamedblock.nim(2, 3) Error: Using an unnamed break in a block is not allowed
tests/nimony/sysbasics/tunnamedblock.nim(6, 5) Error: Using an unnamed break in a block is not allowed
6 changes: 6 additions & 0 deletions tests/nimony/sysbasics/tunnamedblock.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
block: # errors
break

for i in 1..2: # errors
block:
break
Loading