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:
Zahary Karadjov 2018-05-03 19:42:20 +03:00 • committed by Andreas Rumpf
commit 7297613900
4 changed files with 25 additions and 4 deletions

View 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

View file

@ -1,10 +1,11 @@
discard """
output: "seq[float]\n0"
targets: "c cpp"
"""
# https://github.com/nim-lang/Nim/issues/5602
import typetraits
import typetraits, module_with_generics
type
Foo[T] = object of RootObj
@ -16,3 +17,8 @@ proc p[T](f: Foo[T]): T =
var s: Bar[float]
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)