Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inconsistent internal representation of generic objects array construction #7818

Closed
jangko opened this issue May 12, 2018 · 0 comments
Closed

Comments

@jangko
Copy link
Contributor

jangko commented May 12, 2018

the deeper I dig into #4799, the more I scratch my head. here is another meta-bug I think I need to isolate and fixed separately from #4799.

I know this one may looks like #7712 and #7637 because on surface it looks like object slicing. Indeed it sliced the object. But the problem highlighted here is the inconsistent internal representation of array construction of generic objects.

I will use macro to demonstrate the problem and to avoid object slicing at runtime. The problem itself not of macros module, but it originate from commonType algorithm of array construction semcheck.

import macros

type
  Vehicle[T] = object of RootObj
    tire: T
  Car[T] = object of Vehicle[T]
  Bike[T] = object of Vehicle[T]

macro peek(n: typed): untyped =
  echo getTypeImpl(n).treeRepr
  echo "---------"

var v = Vehicle[int](tire: 3)
var c = Car[int](tire: 4)
var b = Bike[int](tire: 2)

peek([v, c, b])
peek([c, b, v]) # change elements position

output:

BracketExpr
  Sym "array"
  Infix
    Ident ".."
    IntLit 0
    IntLit 2
  BracketExpr        # expected output: Vehicle[int]
    Sym "Vehicle"    
    Sym "int"
---------
BracketExpr
  Sym "array"
  Infix
    Ident ".."
    IntLit 0
    IntLit 2
  Sym "Vehicle"   # lost it's generic identity: Vehicle[?]
---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant