Skip to content

Commit 4fd6386

Browse files
authored
closure complain (#1154)
1 parent eae7e1b commit 4fd6386

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/nimony/derefs.nim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ proc trProcPragmas(c: var Context; n: var Cursor) =
350350
takeParRi c, n
351351

352352
proc trProcDecl(c: var Context; n: var Cursor) =
353-
c.typeCache.openScope()
353+
c.typeCache.openScope(ProcScope)
354354
takeToken c, n
355355
let symId = n.symId
356356
var isGeneric = false
@@ -714,7 +714,15 @@ proc trFor(c: var Context; n: var Cursor) =
714714
proc tr(c: var Context; n: var Cursor; e: Expects) =
715715
case n.kind
716716
of Symbol:
717-
trLocation c, n, e
717+
let localInfo = c.typeCache.getLocalInfo(n.symId)
718+
if localInfo.crossedProc:
719+
let info = n.info
720+
c.dest.buildTree ErrT, info:
721+
c.dest.addSubtree n
722+
c.dest.add strToken(pool.strings.getOrIncl("cannot access local variable `" & asNimCode(n) & "` from another routine; closures are not supported"), info)
723+
skip n
724+
else:
725+
trLocation c, n, e
718726
of IntLit, UIntLit, FloatLit, CharLit, StringLit:
719727
if e.wantMutable:
720728
# Consider `fvar(returnsVar(someLet))`: We must not allow this.

src/nimony/typenav.nim

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ proc isGeneratedType*(s: string): bool =
3131
type
3232
LocalInfo* = object
3333
kind*: SymKind
34+
crossedProc*: bool
3435
typ*: Cursor
36+
ScopeKind* = enum
37+
OtherScope, ProcScope, UnusedScope
3538
TypeScope* {.acyclic.} = ref object
3639
locals: Table[SymId, LocalInfo]
3740
parent: TypeScope
41+
kind: ScopeKind
3842

3943
TypeCache* = object
4044
builtins*: BuiltinTypes
@@ -47,8 +51,8 @@ proc createTypeCache*(): TypeCache =
4751
proc registerLocal*(c: var TypeCache; s: SymId; kind: SymKind; typ: Cursor) =
4852
c.current.locals[s] = LocalInfo(kind: kind, typ: typ)
4953

50-
proc openScope*(c: var TypeCache) =
51-
c.current = TypeScope(locals: initTable[SymId, LocalInfo](), parent: c.current)
54+
proc openScope*(c: var TypeCache; kind = OtherScope) =
55+
c.current = TypeScope(locals: initTable[SymId, LocalInfo](), parent: c.current, kind: kind)
5256

5357
proc closeScope*(c: var TypeCache) =
5458
c.current = c.current.parent
@@ -87,11 +91,17 @@ proc getInitValueImpl(c: var TypeCache; s: SymId): Cursor =
8791

8892
proc getLocalInfo*(c: var TypeCache; s: SymId): LocalInfo =
8993
var it {.cursor.} = c.current
94+
var crossedProc = false
95+
var compareTo = UnusedScope
9096
while it != nil:
9197
var res = it.locals.getOrDefault(s)
98+
if it.kind == compareTo:
99+
crossedProc = true
92100
if res.kind != NoSym:
101+
res.crossedProc = crossedProc
93102
return res
94103
it = it.parent
104+
compareTo = ProcScope
95105
return default(LocalInfo)
96106

97107
proc getInitValue*(c: var TypeCache; s: SymId): Cursor =

0 commit comments

Comments
 (0)