Skip to content

Commit 7e6fa9e

Browse files
authored
fixes #23564; hasCustomPragma skips alises types (#24994)
fixes #23564 perhaps handle generic aliases (tyGenericInst for aliases types) if needed
1 parent 3ce38f2 commit 7e6fa9e

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

compiler/vm.nim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
18731873
regs[ra].node = opMapTypeInstToAst(c.cache, regs[rb].node.sym.typ, c.debug[pc], c.idgen)
18741874
else:
18751875
stackTrace(c, tos, pc, "node has no type")
1876-
else:
1876+
of 3:
18771877
# getTypeImpl opcode:
18781878
ensureKind(rkNode)
18791879
if regs[rb].kind == rkNode and regs[rb].node.typ != nil:
@@ -1882,6 +1882,15 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
18821882
regs[ra].node = opMapTypeImplToAst(c.cache, regs[rb].node.sym.typ, c.debug[pc], c.idgen)
18831883
else:
18841884
stackTrace(c, tos, pc, "node has no type")
1885+
else:
1886+
# getTypeInstSkipAlias opcode:
1887+
ensureKind(rkNode)
1888+
if regs[rb].kind == rkNode and regs[rb].node.typ != nil:
1889+
regs[ra].node = opMapTypeInstToAst(c.cache, regs[rb].node.typ, c.debug[pc], c.idgen, skipAlias = true)
1890+
elif regs[rb].kind == rkNode and regs[rb].node.kind == nkSym and regs[rb].node.sym.typ != nil:
1891+
regs[ra].node = opMapTypeInstToAst(c.cache, regs[rb].node.sym.typ, c.debug[pc], c.idgen, skipAlias = true)
1892+
else:
1893+
stackTrace(c, tos, pc, "node has no type")
18851894
of opcNGetSize:
18861895
decodeBImm(rkInt)
18871896
let n = regs[rb].node

compiler/vmdeps.nim

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ proc atomicTypeX(s: PSym; info: TLineInfo): PNode =
4242
result.info = info
4343

4444
proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator;
45-
inst=false; allowRecursionX=false): PNode
45+
inst=false; allowRecursionX=false; skipAlias = false): PNode
4646

4747
proc mapTypeToBracketX(cache: IdentCache; name: string; m: TMagic; t: PType; info: TLineInfo;
4848
idgen: IdGenerator;
@@ -70,7 +70,7 @@ proc objectNode(cache: IdentCache; n: PNode; idgen: IdGenerator): PNode =
7070

7171
proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
7272
idgen: IdGenerator;
73-
inst=false; allowRecursionX=false): PNode =
73+
inst=false; allowRecursionX=false; skipAlias = false): PNode =
7474
var allowRecursion = allowRecursionX
7575
template atomicType(name, m): untyped = atomicTypeX(cache, name, m, t, info, idgen)
7676
template atomicType(s): untyped = atomicTypeX(s, info)
@@ -91,7 +91,8 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
9191
id
9292
template newIdentDefs(s): untyped = newIdentDefs(s, s.typ)
9393

94-
if inst and not allowRecursion and t.sym != nil:
94+
if inst and not allowRecursion and t.sym != nil and
95+
not (skipAlias and t.kind == tyAlias):
9596
# getTypeInst behavior: return symbol
9697
return atomicType(t.sym)
9798

@@ -124,7 +125,7 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
124125
if t.base != nil:
125126
result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
126127
result.add atomicType("typeDesc", mTypeDesc)
127-
result.add mapTypeToAst(t.base, info)
128+
result.add mapTypeToAstX(cache, t.base, info, idgen, inst, skipAlias = skipAlias)
128129
else:
129130
result = atomicType("typeDesc", mTypeDesc)
130131
of tyGenericInvocation:
@@ -153,7 +154,7 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
153154
else:
154155
result = mapTypeToAst(t.typeBodyImpl, info)
155156
of tyAlias:
156-
result = mapTypeToAstX(cache, t.skipModifier, info, idgen, inst, allowRecursion)
157+
result = mapTypeToAstX(cache, t.skipModifier, info, idgen, inst, allowRecursion, skipAlias = skipAlias)
157158
of tyOrdinal:
158159
result = mapTypeToAst(t.skipModifier, info)
159160
of tyDistinct:
@@ -325,8 +326,9 @@ proc opMapTypeToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGene
325326

326327
# the "Inst" version includes generic parameters in the resulting type tree
327328
# and also tries to look like the corresponding Nim type declaration
328-
proc opMapTypeInstToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator): PNode =
329-
result = mapTypeToAstX(cache, t, info, idgen, inst=true, allowRecursionX=false)
329+
proc opMapTypeInstToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator; skipAlias = false): PNode =
330+
# skipAlias: skips aliases and typedesc
331+
result = mapTypeToAstX(cache, t, info, idgen, inst=true, allowRecursionX=false, skipAlias = skipAlias)
330332

331333
# the "Impl" version includes generic parameters in the resulting type tree
332334
# and also tries to look like the corresponding Nim type implementation

compiler/vmgen.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,8 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags = {}, m: TMag
13371337
of "getType": 0
13381338
of "typeKind": 1
13391339
of "getTypeInst": 2
1340-
else: 3 # "getTypeImpl"
1340+
of "getTypeImpl": 3 # "getTypeImpl"
1341+
else: 4 # getTypeInstSkipAlias
13411342
c.gABC(n, opcNGetType, dest, tmp, rc)
13421343
c.freeTemp(tmp)
13431344
#genUnaryABC(c, n, dest, opcNGetType)

lib/core/macros.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,11 +1575,14 @@ proc extractTypeImpl(n: NimNode): NimNode =
15751575
result = n[2]
15761576
else: error("Invalid node to retrieve type implementation of: " & $n.kind)
15771577

1578+
1579+
proc getTypeInstSkipAlias(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.}
1580+
15781581
proc customPragmaNode(n: NimNode): NimNode =
15791582
result = nil
15801583
expectKind(n, {nnkSym, nnkDotExpr, nnkBracketExpr, nnkTypeOfExpr, nnkType, nnkCheckedFieldExpr})
1581-
let
1582-
typ = n.getTypeInst()
1584+
1585+
let typ = n.getTypeInstSkipAlias()
15831586

15841587
if typ.kind == nnkBracketExpr and typ.len > 1 and typ[1].kind == nnkProcTy:
15851588
return typ[1][1]

tests/pragmas/tcustom_pragma.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,14 @@ block: # https://forum.nim-lang.org/t/12522, backticks
538538
type Test = object
539539
field {.`mypragma`.}: int
540540
doAssert Test().field.hasCustomPragma(mypragma)
541+
542+
543+
block:
544+
template p {.pragma.}
545+
546+
func foo[T0](v: T0): bool =
547+
type T = T0
548+
T.hasCustomPragma(p)
549+
550+
type X {.p.} = object
551+
doAssert foo(X())

0 commit comments

Comments
 (0)