Bugfix: The compiler were not inserting proper downcasts for generic types
This resulted in a codegen error in C++ mode, because the generic types were not defined in modules where calls requiring downcasts were used (generating a downcast forces the inclusion of the full definition of the involved types).
This commit is contained in:
parent
4ab1cfb0b0
commit
7297613900
4 changed files with 25 additions and 4 deletions
|
|
@ -1962,7 +1962,8 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
|
||||||
inc(m.genericMatches)
|
inc(m.genericMatches)
|
||||||
if arg.typ == nil:
|
if arg.typ == nil:
|
||||||
result = arg
|
result = arg
|
||||||
elif skipTypes(arg.typ, abstractVar-{tyTypeDesc}).kind == tyTuple:
|
elif skipTypes(arg.typ, abstractVar-{tyTypeDesc}).kind == tyTuple or
|
||||||
|
m.inheritancePenalty > 0:
|
||||||
result = implicitConv(nkHiddenSubConv, f, arg, m, c)
|
result = implicitConv(nkHiddenSubConv, f, arg, m, c)
|
||||||
elif arg.typ.isEmptyContainer:
|
elif arg.typ.isEmptyContainer:
|
||||||
result = arg.copyTree
|
result = arg.copyTree
|
||||||
|
|
|
||||||
|
|
@ -612,7 +612,7 @@ proc firstOrd*(t: PType): BiggestInt =
|
||||||
else:
|
else:
|
||||||
assert(t.n.sons[0].kind == nkSym)
|
assert(t.n.sons[0].kind == nkSym)
|
||||||
result = t.n.sons[0].sym.position
|
result = t.n.sons[0].sym.position
|
||||||
of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tyStatic:
|
of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tyStatic, tyInferred:
|
||||||
result = firstOrd(lastSon(t))
|
result = firstOrd(lastSon(t))
|
||||||
of tyOrdinal:
|
of tyOrdinal:
|
||||||
if t.len > 0: result = firstOrd(lastSon(t))
|
if t.len > 0: result = firstOrd(lastSon(t))
|
||||||
|
|
@ -651,7 +651,7 @@ proc lastOrd*(t: PType; fixedUnsigned = false): BiggestInt =
|
||||||
of tyEnum:
|
of tyEnum:
|
||||||
assert(t.n.sons[sonsLen(t.n) - 1].kind == nkSym)
|
assert(t.n.sons[sonsLen(t.n) - 1].kind == nkSym)
|
||||||
result = t.n.sons[sonsLen(t.n) - 1].sym.position
|
result = t.n.sons[sonsLen(t.n) - 1].sym.position
|
||||||
of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tyStatic:
|
of tyGenericInst, tyDistinct, tyTypeDesc, tyAlias, tyStatic, tyInferred:
|
||||||
result = lastOrd(lastSon(t))
|
result = lastOrd(lastSon(t))
|
||||||
of tyProxy: result = 0
|
of tyProxy: result = 0
|
||||||
of tyOrdinal:
|
of tyOrdinal:
|
||||||
|
|
|
||||||
14
tests/generics/module_with_generics.nim
Normal file
14
tests/generics/module_with_generics.nim
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
type
|
||||||
|
Base[T] = ref object {.inheritable.}
|
||||||
|
value*: T
|
||||||
|
|
||||||
|
Derived[T] = ref object of Base[T]
|
||||||
|
derivedValue*: T
|
||||||
|
|
||||||
|
proc makeDerived*[T](v: T): Derived[T] =
|
||||||
|
new result
|
||||||
|
result.value = v
|
||||||
|
|
||||||
|
proc setBaseValue*[T](a: Base[T], value: T) =
|
||||||
|
a.value = value
|
||||||
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
discard """
|
discard """
|
||||||
output: "seq[float]\n0"
|
output: "seq[float]\n0"
|
||||||
|
targets: "c cpp"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# https://github.com/nim-lang/Nim/issues/5602
|
# https://github.com/nim-lang/Nim/issues/5602
|
||||||
|
|
||||||
import typetraits
|
import typetraits, module_with_generics
|
||||||
|
|
||||||
type
|
type
|
||||||
Foo[T] = object of RootObj
|
Foo[T] = object of RootObj
|
||||||
|
|
@ -16,3 +17,8 @@ proc p[T](f: Foo[T]): T =
|
||||||
var s: Bar[float]
|
var s: Bar[float]
|
||||||
echo p(s).len # the bug was: p(s) should return seq[float], but returns float instead
|
echo p(s).len # the bug was: p(s) should return seq[float], but returns float instead
|
||||||
|
|
||||||
|
# Test overloading and code generation when
|
||||||
|
# downcasting is required for generic types:
|
||||||
|
var d = makeDerived(10)
|
||||||
|
setBaseValue(d, 20)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue