Nim icon indicating copy to clipboard operation
Nim copied to clipboard

ordinary object slicing in varargs generate bad codegen

Open jangko opened this issue 6 years ago • 2 comments

this is an offshoot from #4799

type
  Vehicle = object of RootObj
    tire: int
  Car = object of Vehicle
  Bike = object of Vehicle

proc testVehicle(x: varargs[Vehicle]): string =
  result = ""
  for c in x:
    result.add $c.tire

var v = Vehicle(tire: 3)
var c = Car(tire: 4)
var b = Bike(tire: 2)
echo testVehicle b, c, v

if compiled, it should success, but will fail at runtime with ObjectAssignmentError see #7712 and #7637

currently, it generate bad codegen

jangko avatar Jun 05 '18 02:06 jangko

This is not a bug, this works as expected. Object slicing does not depend on the actual existence of additional fields.

Araq avatar Jun 06 '18 14:06 Araq

sorry for my broken English, I should say: after #4799 fixed, this will become codegen bug. the situation before #4799 fixed is the code cannot be compiled at all. the slicing problem is a probability issue, best to anticipate it.

jangko avatar Jun 06 '18 15:06 jangko