Nim
Nim copied to clipboard
ordinary object slicing in varargs generate bad codegen
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
This is not a bug, this works as expected. Object slicing does not depend on the actual existence of additional fields.
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.