* fixes #7601, array construction of ptr generics * add more test
This commit is contained in:
parent
822d2b5085
commit
07d6ff4be4
3 changed files with 71 additions and 2 deletions
|
|
@ -153,8 +153,9 @@ proc commonType*(x, y: PType): PType =
|
||||||
if a.kind in {tyRef, tyPtr}:
|
if a.kind in {tyRef, tyPtr}:
|
||||||
k = a.kind
|
k = a.kind
|
||||||
if b.kind != a.kind: return x
|
if b.kind != a.kind: return x
|
||||||
a = a.lastSon
|
# bug #7601, array construction of ptr generic
|
||||||
b = b.lastSon
|
a = a.lastSon.skipTypes({tyGenericInst})
|
||||||
|
b = b.lastSon.skipTypes({tyGenericInst})
|
||||||
if a.kind == tyObject and b.kind == tyObject:
|
if a.kind == tyObject and b.kind == tyObject:
|
||||||
result = commonSuperclass(a, b)
|
result = commonSuperclass(a, b)
|
||||||
# this will trigger an error later:
|
# this will trigger an error later:
|
||||||
|
|
|
||||||
51
tests/array/tarraycons_ptr_generic.nim
Normal file
51
tests/array/tarraycons_ptr_generic.nim
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
discard """
|
||||||
|
output: '''apple
|
||||||
|
banana
|
||||||
|
Fruit
|
||||||
|
2
|
||||||
|
4
|
||||||
|
3
|
||||||
|
none
|
||||||
|
skin
|
||||||
|
paper
|
||||||
|
'''
|
||||||
|
"""
|
||||||
|
type
|
||||||
|
Fruit = object of RootObj
|
||||||
|
name: string
|
||||||
|
Apple = object of Fruit
|
||||||
|
Banana = object of Fruit
|
||||||
|
|
||||||
|
var
|
||||||
|
ir = Fruit(name: "Fruit")
|
||||||
|
ia = Apple(name: "apple")
|
||||||
|
ib = Banana(name: "banana")
|
||||||
|
|
||||||
|
let x = [ia.addr, ib.addr, ir.addr]
|
||||||
|
for c in x: echo c.name
|
||||||
|
|
||||||
|
type
|
||||||
|
Vehicle[T] = object of RootObj
|
||||||
|
tire: T
|
||||||
|
Car[T] = object of Vehicle[T]
|
||||||
|
Bike[T] = object of Vehicle[T]
|
||||||
|
|
||||||
|
var v = Vehicle[int](tire: 3)
|
||||||
|
var c = Car[int](tire: 4)
|
||||||
|
var b = Bike[int](tire: 2)
|
||||||
|
|
||||||
|
let y = [b.addr, c.addr, v.addr]
|
||||||
|
for c in y: echo c.tire
|
||||||
|
|
||||||
|
type
|
||||||
|
Book[T] = ref object of RootObj
|
||||||
|
cover: T
|
||||||
|
Hard[T] = ref object of Book[T]
|
||||||
|
Soft[T] = ref object of Book[T]
|
||||||
|
|
||||||
|
var bn = Book[string](cover: "none")
|
||||||
|
var hs = Hard[string](cover: "skin")
|
||||||
|
var bp = Soft[string](cover: "paper")
|
||||||
|
|
||||||
|
let z = [bn, hs, bp]
|
||||||
|
for c in z: echo c.cover
|
||||||
17
tests/array/tarraycons_ptr_generic2.nim
Normal file
17
tests/array/tarraycons_ptr_generic2.nim
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
discard """
|
||||||
|
file: "tarraycons_ptr_generic2.nim"
|
||||||
|
line: 17
|
||||||
|
errormsg: "type mismatch: got <ptr Hard[system.string]> but expected 'Book[system.string]'"
|
||||||
|
"""
|
||||||
|
|
||||||
|
type
|
||||||
|
Book[T] = ref object of RootObj
|
||||||
|
cover: T
|
||||||
|
Hard[T] = ref object of Book[T]
|
||||||
|
Soft[T] = ref object of Book[T]
|
||||||
|
|
||||||
|
var bn = Book[string](cover: "none")
|
||||||
|
var hs = Hard[string](cover: "skin")
|
||||||
|
var bp = Soft[string](cover: "paper")
|
||||||
|
|
||||||
|
let z = [bn, hs.addr, bp]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue