diff options
author | Araq <rumpf_a@web.de> | 2016-12-20 22:42:25 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2016-12-20 22:42:25 +0100 |
commit | c166394024a1f985c08bd4143936112f0ec07e25 (patch) | |
tree | 679c82248d7a3846808357018a9c1dcce8547736 | |
parent | f484d1b20b2ce5be13a2e7019e71d8ff6868f43a (diff) | |
download | Nim-c166394024a1f985c08bd4143936112f0ec07e25.tar.gz |
fixes #5131
-rw-r--r-- | compiler/vmdeps.nim | 2 | ||||
-rw-r--r-- | tests/macros/tgettype3.nim | 48 |
2 files changed, 49 insertions, 1 deletions
diff --git a/compiler/vmdeps.nim b/compiler/vmdeps.nim index bf02e228c..bda2710dc 100644 --- a/compiler/vmdeps.nim +++ b/compiler/vmdeps.nim @@ -174,7 +174,7 @@ proc mapTypeToAstX(t: PType; info: TLineInfo; for i in 1 .. < t.len-1: result.add mapTypeToAst(t.sons[i], info) else: - result = mapTypeToAst(t.lastSon, info) + result = mapTypeToAstX(t.lastSon, info, inst, allowRecursion) of tyGenericBody, tyOrdinal, tyUserTypeClassInst: result = mapTypeToAst(t.lastSon, info) of tyDistinct: diff --git a/tests/macros/tgettype3.nim b/tests/macros/tgettype3.nim new file mode 100644 index 000000000..786d09d8b --- /dev/null +++ b/tests/macros/tgettype3.nim @@ -0,0 +1,48 @@ +discard """ + output: "vec2" +""" +# bug #5131 + +import macros + +type + vecBase[I: static[int], T] = distinct array[I, T] + vec2* = vecBase[2, float32] + +proc isRange(n: NimNode, rangeLen: int = -1): bool = + if n.kind == nnkBracketExpr and $(n[0]) == "range": + if rangeLen == -1: + result = true + elif n[2].intVal - n[1].intVal + 1 == rangeLen: + result = true + +proc getTypeName(t: NimNode, skipVar = false): string = + case t.kind + of nnkBracketExpr: + if $(t[0]) == "array" and t[1].isRange(2) and $(t[2]) == "float32": + result = "vec2" + elif $(t[0]) == "array" and t[1].isRange(3) and $(t[2]) == "float32": + result = "vec3" + elif $(t[0]) == "array" and t[1].isRange(4) and $(t[2]) == "float32": + result = "vec4" + elif $(t[0]) == "distinct": + result = getTypeName(t[1], skipVar) + of nnkSym: + case $t + of "vecBase": result = getTypeName(getType(t), skipVar) + of "float32": result = "float" + else: + result = $t + of nnkVarTy: + result = getTypeName(t[0]) + if not skipVar: + result = "inout " & result + else: + echo "UNKNOWN TYPE: ", treeRepr(t) + assert(false, "Unknown type") + +macro typeName(t: typed): string = + result = newLit(getTypeName(getType(t))) + +var tt : vec2 +echo typeName(tt) |