You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are (at least) two related issues at play here. First, subtypes of array won't have their bounds checked. Second, range doesn't get checked in generics. As it stands, this behavior is both unintuitive and extremely unsafe, as impossible code gets through without erroring, or with unhelpful messages.
Example (both issues)
Somewhat incredibly, this both compiles and runs.
typeNaturalArray[N: static[Natural]] =array[N, int]
var a: NaturalArray[-1000]
echo a # output: []
However, this will fail to compile, as expected:
var b: array[-1000, int] # Error: Array length can't be negative, but was -1000
Expected Output
Error: cannot instantiate NaturalArray: invalid value -1000 for N
Some Amusing Very Serious Examples and Related Issues
Some more things that compile and run but shouldn't:
typeIntArray[N: static[int]] =array[N, int]
var c: IntArray[-1000]
echo c # []typeRangedArray[N: static[3..high(int)]] =array[N, int]
var d: RangedArray[1]
echo d # [0]
While we're here, these both makes the compiler hang forever, but only if you echo the variables afterwards:
typeFunnyArray[N] =array[N, int]
var e: FunnyArray[0.5]
echo e
typeHilariousArray[N] =array[N, int]
var f: HilariousArray["not even a number lmao"]
echo f
If you DON'T echo, you get this very helpful message:
Error: internal error: invalid kind for lastOrd(tyFloat)
No stack traceback available
To create a stacktrace, rerun compilation with './koch temp c <file>', see https://nim-lang.github.io/Nim/intern.html#debugging-the-compiler for details
Below will also give you that message, with or without echoing, as long as you leave out the static in the generic (related to #17362 ?):
typeBadRangedArray[N: range[3..high(int)]] =array[N, int] # Error: internal error: invalid kind for lastOrd(tyAnything)let g =BadRangedArray([1, 1, 1])
Ok I'll stop 😅.
Version
Nim Compiler Version 1.4.4 [Windows: amd64]
Compiled at 2021-02-23
Copyright (c) 2006-2020 by Andreas Rumpf
The text was updated successfully, but these errors were encountered:
Expands on #16969 and #16492.
There are (at least) two related issues at play here. First, subtypes of
array
won't have their bounds checked. Second,range
doesn't get checked in generics. As it stands, this behavior is both unintuitive and extremely unsafe, as impossible code gets through without erroring, or with unhelpful messages.Example (both issues)
Somewhat incredibly, this both compiles and runs.
However, this will fail to compile, as expected:
Expected Output
Some
AmusingVery Serious Examples and Related IssuesSome more things that compile and run but shouldn't:
While we're here, these both makes the compiler hang forever, but only if you echo the variables afterwards:
If you DON'T echo, you get this very helpful message:
Below will also give you that message, with or without echoing, as long as you leave out the static in the generic (related to #17362 ?):
Ok I'll stop 😅.
Version
The text was updated successfully, but these errors were encountered: