diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 95a7beada56ec..6f7e83c186210 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -220,7 +220,7 @@ proc genOptAsgnTuple(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) = optAsgnLoc(src, t, field), newflags) proc genOptAsgnObject(p: BProc, dest, src: TLoc, flags: TAssignmentFlags, - t: PNode) = + t: PNode, typ: PType) = if t == nil: return let newflags = if src.s == OnStatic: @@ -232,10 +232,11 @@ proc genOptAsgnObject(p: BProc, dest, src: TLoc, flags: TAssignmentFlags, case t.kind of nkSym: let field = t.sym + if field.loc.r == nil: fillObjectFields(p.module, typ) genAssignment(p, optAsgnLoc(dest, field.typ, field.loc.r), optAsgnLoc(src, field.typ, field.loc.r), newflags) of nkRecList: - for child in items(t): genOptAsgnObject(p, dest, src, newflags, child) + for child in items(t): genOptAsgnObject(p, dest, src, newflags, child, typ) else: discard proc genGenericAsgn(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) = @@ -315,9 +316,9 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) = genGenericAsgn(p, dest, src, flags) elif needsComplexAssignment(ty): if ty.sons[0].isNil and asgnComplexity(ty.n) <= 4: - discard getTypeDesc(p.module, dest.t) + discard getTypeDesc(p.module, ty) internalAssert ty.n != nil - genOptAsgnObject(p, dest, src, flags, ty.n) + genOptAsgnObject(p, dest, src, flags, ty.n, ty) else: genGenericAsgn(p, dest, src, flags) else: diff --git a/tests/ccgbugs/tgeneric_smallobj_asgn_opt.nim b/tests/ccgbugs/tgeneric_smallobj_asgn_opt.nim new file mode 100644 index 0000000000000..919dc3fc19064 --- /dev/null +++ b/tests/ccgbugs/tgeneric_smallobj_asgn_opt.nim @@ -0,0 +1,26 @@ +discard """ + output: '''false''' +""" + +# bug #5402 + +import lists + +type + Container[T] = ref object + obj: T + + ListOfContainers[T] = ref object + list: DoublyLinkedList[Container[T]] + +proc contains[T](this: ListOfContainers[T], obj: T): bool = + for item in this.list.items(): + if item.obj == obj: return true + return false + +proc newListOfContainers[T](): ListOfContainers[T] = + new(result) + result.list = initDoublyLinkedList[Container[T]]() + +let q = newListOfContainers[int64]() +echo q.contains(123)