fixes #3338
This commit is contained in:
parent
3f24a7ff3e
commit
d93507fd2e
4 changed files with 51 additions and 4 deletions
|
|
@ -239,14 +239,20 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable,
|
||||||
pushInfoContext(info)
|
pushInfoContext(info)
|
||||||
var entry = TInstantiation.new
|
var entry = TInstantiation.new
|
||||||
entry.sym = result
|
entry.sym = result
|
||||||
newSeq(entry.concreteTypes, gp.len)
|
# we need to compare both the generic types and the concrete types:
|
||||||
|
# generic[void](), generic[int]()
|
||||||
|
# see ttypeor.nim test.
|
||||||
var i = 0
|
var i = 0
|
||||||
|
newSeq(entry.concreteTypes, fn.typ.len+gp.len)
|
||||||
for s in instantiateGenericParamList(c, gp, pt):
|
for s in instantiateGenericParamList(c, gp, pt):
|
||||||
addDecl(c, s)
|
addDecl(c, s)
|
||||||
entry.concreteTypes[i] = s.typ
|
entry.concreteTypes[i] = s.typ
|
||||||
inc i
|
inc i
|
||||||
pushProcCon(c, result)
|
pushProcCon(c, result)
|
||||||
instantiateProcType(c, pt, result, info)
|
instantiateProcType(c, pt, result, info)
|
||||||
|
for j in 0 .. result.typ.len-1:
|
||||||
|
entry.concreteTypes[i] = result.typ.sons[j]
|
||||||
|
inc i
|
||||||
if tfTriggersCompileTime in result.typ.flags:
|
if tfTriggersCompileTime in result.typ.flags:
|
||||||
incl(result.flags, sfCompileTime)
|
incl(result.flags, sfCompileTime)
|
||||||
n.sons[genericParamsPos] = ast.emptyNode
|
n.sons[genericParamsPos] = ast.emptyNode
|
||||||
|
|
|
||||||
|
|
@ -75,8 +75,12 @@ proc searchInstTypes*(key: PType): PType =
|
||||||
proc cacheTypeInst*(inst: PType) =
|
proc cacheTypeInst*(inst: PType) =
|
||||||
# XXX: add to module's generics
|
# XXX: add to module's generics
|
||||||
# update the refcount
|
# update the refcount
|
||||||
let genericTyp = inst.sons[0]
|
let gt = inst.sons[0]
|
||||||
genericTyp.sym.typeInstCache.safeAdd(inst)
|
let t = if gt.kind == tyGenericBody: gt.lastSon else: gt
|
||||||
|
if t.kind in {tyStatic, tyGenericParam, tyIter} + tyTypeClasses:
|
||||||
|
return
|
||||||
|
gt.sym.typeInstCache.safeAdd(inst)
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
TReplTypeVars* {.final.} = object
|
TReplTypeVars* {.final.} = object
|
||||||
|
|
|
||||||
|
|
@ -991,7 +991,9 @@ proc compareTypes*(x, y: PType,
|
||||||
var c = initSameTypeClosure()
|
var c = initSameTypeClosure()
|
||||||
c.cmp = cmp
|
c.cmp = cmp
|
||||||
c.flags = flags
|
c.flags = flags
|
||||||
result = sameTypeAux(x, y, c)
|
if x == y: result = true
|
||||||
|
elif x.isNil or y.isNil: result = false
|
||||||
|
else: result = sameTypeAux(x, y, c)
|
||||||
|
|
||||||
proc inheritanceDiff*(a, b: PType): int =
|
proc inheritanceDiff*(a, b: PType): int =
|
||||||
# | returns: 0 iff `a` == `b`
|
# | returns: 0 iff `a` == `b`
|
||||||
|
|
|
||||||
35
tests/metatype/ttypeor.nim
Normal file
35
tests/metatype/ttypeor.nim
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
discard """
|
||||||
|
output: '''Foo
|
||||||
|
Bar'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
# bug #3338
|
||||||
|
|
||||||
|
type
|
||||||
|
Base[T] = Foo[T] | Bar[T]
|
||||||
|
|
||||||
|
Foo[T] = ref object
|
||||||
|
x: T
|
||||||
|
|
||||||
|
Bar[T] = ref object
|
||||||
|
x: T
|
||||||
|
|
||||||
|
proc test[T](ks: Foo[T], x, y: T): T =
|
||||||
|
echo("Foo")
|
||||||
|
return x + y + ks.x
|
||||||
|
|
||||||
|
proc test[T](ks: Bar[T], x, y: T): T =
|
||||||
|
echo("Bar")
|
||||||
|
return x
|
||||||
|
|
||||||
|
proc add[T](ksa: Base[T]) =
|
||||||
|
var test = ksa.test(5, 10)
|
||||||
|
ksa.x = test
|
||||||
|
|
||||||
|
var t1 = Foo[int32]()
|
||||||
|
t1.add()
|
||||||
|
doAssert t1.x == 15
|
||||||
|
|
||||||
|
var t2 = Bar[int32]()
|
||||||
|
t2.add()
|
||||||
|
doAssert t2.x == 5
|
||||||
Loading…
Add table
Add a link
Reference in a new issue