Nim icon indicating copy to clipboard operation
Nim copied to clipboard

fully specialized generic failed at array construction

Open jangko opened this issue 7 years ago • 6 comments

type
  SBase[T, V] = ref object of RootObj
    val: T
    color: V
  SRC = ref object of SBase[string, int]
  SRD = ref object of SBase[string, int]

var a = SBase[string, int](val: "base", color: 1)
var b = SRC(val: "rc", color: 2)
var c = SRD(val: "rd", color: 3)

var x = [a, b, c] #ok
var y = [b, c, a] #failed
var z = [c, b, a] #failed

what need to be included in the test:

  • the above case
  • secondary form of ref -> e.g. from new(ref someobject)
  • ptr form, and it's secondary form
  • ~~ordinary object -> perhaps generate runtime error because of object slicing~~

see the evolution of this bug in #7601, #7818, and #7906

note: if possible, uncomment test at block test_t4799_6 of #4799

jangko avatar Jun 05 '18 02:06 jangko

a,b,c are not defined in the test

cooldome avatar Jun 05 '18 08:06 cooldome

a,b,c are not defined in the test

edited

jangko avatar Jun 05 '18 09:06 jangko

I think this one should close , relevant issues are closed

bung87 avatar Sep 20 '22 00:09 bung87

nope. the compiler(devel v1.7.1) still rejects the above example. inside the compiler, the type comparator related to array and perhaps seq, is still buggy.

jangko avatar Sep 20 '22 04:09 jangko

I think the result is expected, x is implicitly casting to base type, y and z can't casting from base type to derived type

bung87 avatar Sep 20 '22 04:09 bung87

ah, right. the example is misleading. it should be like this:

type
  SBase[T, V] = ref object of RootObj
    val: T
    color: V
  SRC = ref object of SBase[string, int]
  SRD = ref object of SBase[string, int]

var a = SBase[string, int](val: "base", color: 1)
var b = SRC(val: "rc", color: 2)
var c = SRD(val: "rd", color: 3)

var x = [a, b, c] #ok
var y = [b, c] #failed
var z = [c, b] #failed

jangko avatar Sep 21 '22 02:09 jangko

This works (and is normally what you're supposed to do):

type
  SBase[T, V] = ref object of RootObj
    val: T
    color: V
  SRC = ref object of SBase[string, int]
  SRD = ref object of SBase[string, int]

var a = SBase[string, int](val: "base", color: 1)
var b = SRC(val: "rc", color: 2)
var c = SRD(val: "rd", color: 3)

var x = [a, b, c]
var y = [SBase[string, int](b), c, a]
var z = [SBase[string, int](c), b, a]

The problem is the error message:

(13, 10) Error: type mismatch: got 'SRC' for 'b' but expected 'ref SBase[system.string, system.int]'

It performs type inference to find the common supertype of b, c, a and c, b, a and infers a correct array type, but doesn't actually update the b and c nodes to convert to their supertype. Maybe a final fitNode call to every array element.

metagn avatar Sep 16 '23 04:09 metagn