Skip to content

Commit

Permalink
handle typedesc params in VM (nim-lang#22581)
Browse files Browse the repository at this point in the history
* handle typedesc params in VM

fixes nim-lang#15760

* add test

* fix getType(typedesc) test
  • Loading branch information
metagn authored Aug 30, 2023
1 parent d7634c1 commit 2e4e2f8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
8 changes: 7 additions & 1 deletion compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,7 @@ proc checkCanEval(c: PCtx; n: PNode) =
# little hack ahead for bug #12612: assume gensym'ed variables
# are in the right scope:
if sfGenSym in s.flags and c.prc.sym == nil: discard
elif s.kind == skParam and s.typ.kind == tyTypeDesc: discard
else: cannotEval(c, n)
elif s.kind in {skProc, skFunc, skConverter, skMethod,
skIterator} and sfForward in s.flags:
Expand Down Expand Up @@ -2102,8 +2103,13 @@ proc gen(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags = {}) =
let s = n.sym
checkCanEval(c, n)
case s.kind
of skVar, skForVar, skTemp, skLet, skParam, skResult:
of skVar, skForVar, skTemp, skLet, skResult:
genRdVar(c, n, dest, flags)
of skParam:
if s.typ.kind == tyTypeDesc:
genTypeLit(c, s.typ, dest)
else:
genRdVar(c, n, dest, flags)
of skProc, skFunc, skConverter, skMacro, skTemplate, skMethod, skIterator:
# 'skTemplate' is only allowed for 'getAst' support:
if s.kind == skIterator and s.typ.callConv == TCallingConvention.ccClosure:
Expand Down
18 changes: 18 additions & 0 deletions tests/vm/ttypedesc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
block: # issue #15760
type
Banana = object
SpecialBanana = object

proc getName(_: type Banana): string = "Banana"
proc getName(_: type SpecialBanana): string = "SpecialBanana"

proc x[T](): string =
const n = getName(T) # this one works
result = n

proc y(T: type): string =
const n = getName(T) # this one failed to compile
result = n

doAssert x[SpecialBanana]() == "SpecialBanana"
doAssert y(SpecialBanana) == "SpecialBanana"
5 changes: 3 additions & 2 deletions tests/vm/tvmmisc.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# bug #4462
import macros
import os

# bug #4462
block:
proc foo(t: typedesc) {.compileTime.} =
assert sameType(getType(t), getType(int))
assert sameType(getType(t), getType(typedesc[int]))
assert sameType(getType(t), getType(type int))

static:
foo(int)
Expand Down

0 comments on commit 2e4e2f8

Please sign in to comment.