test case haul for old generic/template/macro issues (#22564)

* test case haul for old generic/template/macro issues

closes #12582, closes #19552, closes #2465, closes #4596, closes #15246,
closes #12683, closes #7889, closes #4547, closes #12415, closes #2002,
closes #1771, closes #5121

The test for #5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.

* fix template gensym test
This commit is contained in:
metagn
2023-08-27 12:27:47 +03:00
committed by GitHub
parent a108a451c5
commit c19fd69b69
9 changed files with 248 additions and 34 deletions

View File

@@ -101,3 +101,14 @@ echo @(b.arr[0].arr), @(b.arr[1].arr)
let y = b
echo @(y.arr[0].arr), @(y.arr[1].arr)
import macros
block: # issue #5121
type
A = object
AConst[X] = A
macro dumpType(t: typedesc): untyped =
result = newTree(nnkTupleConstr, newLit $t.getType[1].typeKind, newLit t.getType[1].treeRepr)
doAssert dumpType(A) == ("ntyObject", "Sym \"A\"")

View File

@@ -78,3 +78,65 @@ block:
doAssert x.data.len == 5
var y: Leb128Buf[uint16]
doAssert y.data.len == 3
import macros
block: # issue #12415
macro isSomePointerImpl(t: typedesc): bool =
var impl = t.getTypeInst[1].getTypeImpl
if impl.kind == nnkDistinctTy:
impl = impl[0].getTypeImpl
if impl.kind in {nnkPtrTy,nnkRefTy}:
result = newLit(true)
elif impl.kind == nnkSym and impl.eqIdent("pointer"):
result = newLit(true)
else:
result = newLit(false)
proc isSomePointer[T](t: typedesc[T]): bool {.compileTime.} =
isSomePointerImpl(t)
type
Option[T] = object
## An optional type that stores its value and state separately in a boolean.
when isSomePointer(typedesc(T)):
val: T
else:
val: T
has: bool
var x: Option[ref int]
doAssert not compiles(x.has)
var y: Option[int]
doAssert compiles(y.has)
block: # issue #2002
proc isNillable(T: typedesc): bool =
when compiles((let v: T = nil)):
return true
else:
return false
type
Foo[T] = object
when isNillable(T):
nillable: float
else:
notnillable: int
var val1: Foo[ref int]
doAssert compiles(val1.nillable)
doAssert not compiles(val1.notnillable)
var val2: Foo[int]
doAssert not compiles(val2.nillable)
doAssert compiles(val2.notnillable)
block: # issue #1771
type
Foo[X, T] = object
bar: array[X.low..X.high, T]
proc test[X, T](f: Foo[X, T]): T =
f.bar[X.low]
var a: Foo[range[0..2], float]
doAssert test(a) == 0.0