nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

fully specialized generic failed at array construction #7955

Open jangko opened 6 years ago

jangko commented 6 years ago
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:

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

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

cooldome commented 6 years ago

a,b,c are not defined in the test

jangko commented 6 years ago

a,b,c are not defined in the test

edited

bung87 commented 2 years ago

I think this one should close , relevant issues are closed

jangko commented 2 years ago

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.

bung87 commented 2 years ago

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

jangko commented 2 years ago

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
metagn commented 1 year ago

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.