test case haul before 2.2 (#24119)

closes #4774, closes #7385, closes #10019, closes #12405, closes #12732,
closes #13270, closes #13799, closes #15247, closes #16128, closes
#16175, closes #16774, closes #17527, closes #20880, closes #21346
This commit is contained in:
metagn
2024-09-17 04:50:10 +03:00
committed by GitHub
parent 651fdbe586
commit fe55dcb2be
16 changed files with 281 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
# issue #16128
import std/[tables, hashes]
type
NodeId*[L] = object
isSource: bool
index: Table[NodeId[L], seq[NodeId[L]]]
func hash*[L](id: NodeId[L]): Hash = discard
func `==`[L](a, b: NodeId[L]): bool = discard
proc makeIndex*[T, L](tree: T) =
var parent = NodeId[L]()
var tmp: Table[NodeId[L], seq[NodeId[L]]]
tmp[parent] = @[parent]
proc simpleTreeDiff*[T, L](source, target: T) =
# Swapping these two lines makes error disappear
var m: Table[NodeId[L], NodeId[L]]
makeIndex[T, L](target)
var tmp: Table[string, seq[string]] # removing this forward declaration also removes error
proc diff(x1, x2: string): auto =
simpleTreeDiff[int, string](12, 12)

View File

@@ -44,3 +44,15 @@ block: # constant condition after dynamic one
doAssert y.a is int
var z: Foo[float]
doAssert z.a is string
block: # issue #4774, but not with threads
const hasThreadSupport = not defined(js)
when hasThreadSupport:
type Channel[T] = object
value: T
type
SomeObj[T] = object
when hasThreadSupport:
channel: ptr Channel[T]
var x: SomeObj[int]
doAssert compiles(x.channel) == hasThreadSupport

View File

@@ -451,3 +451,67 @@ block: # real version of above
proc foo[T](x: T, a = Opt.none(int)) = discard
foo(1, a = Opt.none(int))
foo(1)
block: # issue #20880
type
Child[n: static int] = object
data: array[n, int]
Parent[n: static int] = object
child: Child[3*n]
const n = 3
doAssert $(typeof Parent[n*3]()) == "Parent[9]"
doAssert $(typeof Parent[1]().child) == "Child[3]"
doAssert Parent[1]().child.data.len == 3
{.experimental: "dynamicBindSym".}
block: # issue #16774
type SecretWord = distinct uint64
const WordBitWidth = 8 * sizeof(uint64)
func wordsRequired(bits: int): int {.compileTime.} =
## Compute the number of limbs required
# from the **announced** bit length
(bits + WordBitWidth - 1) div WordBitWidth
type
Curve = enum BLS12_381
BigInt[bits: static int] = object
limbs: array[bits.wordsRequired, SecretWord]
const BLS12_381_Modulus = default(BigInt[381])
macro Mod(C: static Curve): untyped =
## Get the Modulus associated to a curve
result = bindSym($C & "_Modulus")
macro getCurveBitwidth(C: static Curve): untyped =
result = nnkDotExpr.newTree(
getAST(Mod(C)),
ident"bits"
)
type Fp[C: static Curve] = object
## Finite Fields / Modular arithmetic
## modulo the curve modulus
mres: BigInt[getCurveBitwidth(C)]
var x: Fp[BLS12_381]
doAssert x.mres.limbs.len == wordsRequired(getCurveBitWidth(BLS12_381))
# minimized, as if we haven't tested it already:
macro makeIntLit(c: static int): untyped =
result = newLit(c)
type Test[T: static int] = object
myArray: array[makeIntLit(T), int]
var y: Test[2]
doAssert y.myArray.len == 2
var z: Test[4]
doAssert z.myArray.len == 4
block: # issue #16175
type
Thing[D: static uint] = object
when D == 0:
kid: char
else:
kid: Thing[D-1]
var t2 = Thing[3]()
doAssert t2.kid is Thing[2.uint]
doAssert t2.kid.kid is Thing[1.uint]
doAssert t2.kid.kid.kid is Thing[0.uint]
doAssert t2.kid.kid.kid.kid is char
var s = Thing[1]()
doAssert s.kid is Thing[0.uint]
doAssert s.kid.kid is char

21
tests/lent/tvm.nim Normal file
View File

@@ -0,0 +1,21 @@
block: # issue #17527
iterator items2[IX, T](a: array[IX, T]): lent T {.inline.} =
var i = low(IX)
if i <= high(IX):
while true:
yield a[i]
if i >= high(IX): break
inc(i)
proc main() =
var s: seq[string] = @[]
for i in 0..<3:
for (key, val) in items2([("any", "bar")]):
s.add $(i, key, val)
doAssert s == @[
"(0, \"any\", \"bar\")",
"(1, \"any\", \"bar\")",
"(2, \"any\", \"bar\")"
]
static: main()

View File

@@ -0,0 +1,2 @@
proc count*(s: string): int =
s.len

View File

@@ -0,0 +1 @@
var count*: int = 10

View File

@@ -0,0 +1 @@
const count* = 3.142

View File

@@ -0,0 +1,10 @@
# issue #12732
import std/macros
const getPrivate3_tmp* = 0
const foobar1* = 0 # comment this or make private and it'll compile fine
macro foobar4*(): untyped =
newLit "abc"
template currentPkgDir2*: string = foobar4()
macro currentPkgDir2*(dir: string): untyped =
newLit "abc2"

View File

@@ -0,0 +1,8 @@
# issue #15247
import mdisambsym1, mdisambsym2, mdisambsym3
proc twice(n: int): int =
n*2
doAssert twice(count) == 20

View File

@@ -0,0 +1,5 @@
# issue #12732
import mmacroamb
const s0 = currentPkgDir2 #[tt.Error
^ ambiguous identifier: 'currentPkgDir2' -- use one of the following:]#

View File

@@ -0,0 +1,13 @@
block: # issue #13799
type
X[A, B] = object
a: A
b: B
Y[A] = X[A, int]
template s(T: type X): X = T()
template t[A, B](T: type X[A, B]): X[A, B] = T()
proc works1(): Y[int] = s(X[int, int])
proc works2(): Y[int] = t(X[int, int])
proc works3(): Y[int] = t(Y[int])
proc broken(): Y[int] = s(Y[int])

View File

@@ -16,3 +16,26 @@ block: # bug #8568
proc g(a: D|E): string = "foo D|E"
proc g(a: D): string = "foo D"
doAssert g(D[int]()) == "foo D"
type Obj1[T] = object
v: T
converter toObj1[T](t: T): Obj1[T] = return Obj1[T](v: t)
block: # issue #10019
proc fun1[T](elements: seq[T]): string = "fun1 seq"
proc fun1(o: object|tuple): string = "fun1 object|tuple"
proc fun2[T](elements: openArray[T]): string = "fun2 openarray"
proc fun2(o: object): string = "fun2 object"
proc fun_bug[T](elements: openArray[T]): string = "fun_bug openarray"
proc fun_bug(o: object|tuple):string = "fun_bug object|tuple"
proc main() =
var x = @["hello", "world"]
block:
# no ambiguity error shown here even though this would compile if we remove either 1st or 2nd overload of fun1
doAssert fun1(x) == "fun1 seq"
block:
# ditto
doAssert fun2(x) == "fun2 openarray"
block:
# Error: ambiguous call; both t0065.fun_bug(elements: openarray[T])[declared in t0065.nim(17, 5)] and t0065.fun_bug(o: object or tuple)[declared in t0065.nim(20, 5)] match for: (array[0..1, string])
doAssert fun_bug(x) == "fun_bug openarray"
main()

View File

@@ -0,0 +1,19 @@
import macros
block: # issue #7385
type CustomSeq[T] = object
data: seq[T]
macro `[]`[T](s: CustomSeq[T], args: varargs[untyped]): untyped =
## The end goal is to replace the joker "_" by something else
result = newIntLitNode(10)
proc foo1(): CustomSeq[int] =
result.data.newSeq(10)
# works since no overload matches first argument with type `CustomSeq`
# except magic `[]`, which always matches without checking arguments
doAssert result[_] == 10
doAssert foo1() == CustomSeq[int](data: newSeq[int](10))
proc foo2[T](): CustomSeq[T] =
result.data.newSeq(10)
# works fine with generic return type
doAssert result[_] == 10
doAssert foo2[int]() == CustomSeq[int](data: newSeq[int](10))

View File

@@ -43,3 +43,13 @@ block: # ditto but may be wrong minimization
# alternative version, also causes instantiation issue
proc baz[T](x: typeof(foo[T]())) = discard
baz[int](Foo[int]())
block: # issue #21346
type K[T] = object
template s[T](x: int) = doAssert T is K[K[int]]
proc b1(n: bool | bool) = s[K[K[int]]](3)
proc b2(n: bool) = s[K[K[int]]](3)
template b3(n: bool) = s[K[K[int]]](3)
b1(false) # Error: cannot instantiate K; got: <T> but expected: <T>
b2(false) # Builds, on its own
b3(false)

View File

@@ -67,3 +67,14 @@ block: # issue #24099, modified to work but using float32
## Compares colors with given accuracy.
abs(a[0] - b[0]) < e and abs(a[1] - b[1]) < e and abs(a[2] - b[2]) < e
doAssert ColorRGBU([1.float32, 1, 1]) ~= ColorRGBU([1.float32, 1, 1])
block: # issue #13270
type
A = object
B = object
proc f(a: A) = discard
proc g[T](value: T, cb: (proc(a: T)) = f) =
cb value
g A()
# This should fail because there is no f(a: B) overload available
doAssert not compiles(g B())

View File

@@ -0,0 +1,55 @@
# issue #12405
import std/[marshal, streams, times, tables, os, assertions]
type AiredEpisodeState * = ref object
airedAt * : DateTime
tvShowId * : string
seasonNumber * : int
number * : int
title * : string
type ShowsWatchlistState * = ref object
aired * : seq[AiredEpisodeState]
type UiState * = ref object
shows: ShowsWatchlistState
# Helpers to marshal and unmarshal
proc load * ( state : var UiState, file : string ) =
var strm = newFileStream( file, fmRead )
strm.load( state )
strm.close()
proc store * ( state : UiState, file : string ) =
var strm = newFileStream( file, fmWrite )
strm.store( state )
strm.close()
# 1. We fill the state initially
var state : UiState = UiState( shows: ShowsWatchlistState( aired: @[] ) )
# VERY IMPORTANT: For some reason, small numbers (like 2 or 3) don't trigger the bug. Anything above 7 or 8 on my machine triggers though
for i in 0..30:
var episode = AiredEpisodeState( airedAt: now(), tvShowId: "1", seasonNumber: 1, number: 1, title: "string" )
state.shows.aired.add( episode )
# 2. Store it in a file with the marshal module, and then load it back up
store( state, "tmarshalsegfault_data" )
load( state, "tmarshalsegfault_data" )
removeFile("tmarshalsegfault_data")
# 3. VERY IMPORTANT: Without this line, for some reason, everything works fine
state.shows.aired[ 0 ] = AiredEpisodeState( airedAt: now(), tvShowId: "1", seasonNumber: 1, number: 1, title: "string" )
# 4. And formatting the airedAt date will now trigger the exception
var s = ""
for ep in state.shows.aired:
let x = $ep.seasonNumber & "x" & $ep.number & " (" & $ep.airedAt & ")"
if s.len == 0: s = x
else: doAssert s == x