forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
track introduced locals in vmgen for eval check (nim-lang#24674)
fixes nim-lang#8758, fixes nim-lang#10828, fixes nim-lang#12172, fixes nim-lang#21610, fixes nim-lang#23803, fixes nim-lang#24633, fixes nim-lang#24634, succeeds nim-lang#24085 We simply track the symbol ID of every traversed `var`/`let` definition in `vmgen`, then these symbols are always considered evaluable in the current `vmgen` context. The set of symbols is reset before every generation, but both tests worked properly without doing this including the nested `const`, so maybe it's already done in some way I'm not seeing.
- Loading branch information
Showing
7 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
block: # issue #8758 | ||
template baz() = | ||
var i = 0 | ||
|
||
proc foo() = | ||
static: | ||
var i = 0 | ||
baz() | ||
|
||
block: # issue #10828 | ||
proc test(i: byte): bool = | ||
const SET = block: # No issues when defined outside proc | ||
var s: set[byte] | ||
for i in 0u8 .. 255u8: incl(s, i) | ||
s | ||
return i in SET | ||
doAssert test(0) | ||
doAssert test(127) | ||
doAssert test(255) | ||
|
||
block: # issue #12172 | ||
const TEST = block: | ||
var test: array[5, string] | ||
for i in low(test)..high(test): | ||
test[i] = $i | ||
test | ||
proc test = | ||
const TEST2 = block: | ||
var test: array[5, string] # Error here | ||
for i in low(test)..high(test): | ||
test[i] = $i | ||
test | ||
doAssert TEST == TEST2 | ||
doAssert TEST == @["0", "1", "2", "3", "4"] | ||
doAssert TEST2 == @["0", "1", "2", "3", "4"] | ||
test() | ||
|
||
block: # issue #21610 | ||
func stuff(): int = | ||
const r = block: | ||
var r = 1 # Error: cannot evaluate at compile time: r | ||
for i in 2..10: | ||
r *= i | ||
r | ||
r | ||
doAssert stuff() == 3628800 | ||
|
||
block: # issue #23803 | ||
func foo1(c: int): int {.inline.} = | ||
const arr = block: | ||
var res: array[0..99, int] | ||
res[42] = 43 | ||
res | ||
arr[c] | ||
doAssert foo1(41) == 0 | ||
doAssert foo1(42) == 43 | ||
doAssert foo1(43) == 0 | ||
|
||
# works | ||
func foo2(c: int): int {.inline.} = | ||
func initArr(): auto = | ||
var res: array[0..99, int] | ||
res[42] = 43 | ||
res | ||
const arr = initArr() | ||
arr[c] | ||
doAssert foo2(41) == 0 | ||
doAssert foo2(42) == 43 | ||
doAssert foo2(43) == 0 | ||
|
||
# also works | ||
const globalArr = block: | ||
var res: array[0..99, int] | ||
res[42] = 43 | ||
res | ||
func foo3(c: int): int {.inline.} = globalArr[c] | ||
doAssert foo3(41) == 0 | ||
doAssert foo3(42) == 43 | ||
doAssert foo3(43) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# issue #24634 | ||
|
||
type J = object | ||
|
||
template m(u: J): int = | ||
let v = u | ||
0 | ||
|
||
proc g() = | ||
const x = J() | ||
const _ = m(x) | ||
|
||
g() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# issue #24633 | ||
|
||
import std/sequtils | ||
|
||
proc f(a: static openArray[int]) = | ||
const s1 = a.mapIt(it) | ||
const s2 = a.toSeq() | ||
|
||
f([1,2,3]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
proc test = | ||
const TEST = block: | ||
let i = 1 | ||
const j = i + 1 #[tt.Error | ||
^ cannot evaluate at compile time: i]# | ||
j |