mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-10 05:38:10 +00:00
@@ -20,7 +20,7 @@ proc int64Literal(i: BiggestInt): PRope =
|
||||
proc uint64Literal(i: uint64): PRope = toRope($i & "ULL")
|
||||
|
||||
proc intLiteral(i: BiggestInt): PRope =
|
||||
if (i > low(int32)) and (i <= high(int32)):
|
||||
if i > low(int32) and i <= high(int32):
|
||||
result = toRope(i)
|
||||
elif i == low(int32):
|
||||
# Nim has the same bug for the same reasons :-)
|
||||
@@ -39,7 +39,7 @@ proc int32Literal(i: int): PRope =
|
||||
proc genHexLiteral(v: PNode): PRope =
|
||||
# hex literals are unsigned in C
|
||||
# so we don't generate hex literals any longer.
|
||||
if not (v.kind in {nkIntLit..nkUInt64Lit}):
|
||||
if v.kind notin {nkIntLit..nkUInt64Lit}:
|
||||
internalError(v.info, "genHexLiteral")
|
||||
result = intLiteral(v.intVal)
|
||||
|
||||
@@ -224,9 +224,9 @@ proc optAsgnLoc(a: TLoc, t: PType, field: PRope): TLoc =
|
||||
proc genOptAsgnTuple(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
|
||||
let newflags =
|
||||
if src.k == locData:
|
||||
flags + { needToCopy }
|
||||
flags + {needToCopy}
|
||||
elif tfShallow in dest.t.flags:
|
||||
flags - { needToCopy }
|
||||
flags - {needToCopy}
|
||||
else:
|
||||
flags
|
||||
for i in 0 .. <dest.t.len:
|
||||
@@ -240,9 +240,9 @@ proc genOptAsgnObject(p: BProc, dest, src: TLoc, flags: TAssignmentFlags,
|
||||
if t == nil: return
|
||||
let newflags =
|
||||
if src.k == locData:
|
||||
flags + { needToCopy }
|
||||
flags + {needToCopy}
|
||||
elif tfShallow in dest.t.flags:
|
||||
flags - { needToCopy }
|
||||
flags - {needToCopy}
|
||||
else:
|
||||
flags
|
||||
case t.kind
|
||||
@@ -328,7 +328,9 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
|
||||
linefmt(p, cpsStmts, "$1 = $2;$n", rdLoc(dest), rdLoc(src))
|
||||
of tyObject:
|
||||
# XXX: check for subtyping?
|
||||
if needsComplexAssignment(ty):
|
||||
if not isObjLackingTypeField(ty):
|
||||
genGenericAsgn(p, dest, src, flags)
|
||||
elif needsComplexAssignment(ty):
|
||||
if ty.sons[0].isNil and asgnComplexity(ty.n) <= 4:
|
||||
discard getTypeDesc(p.module, ty)
|
||||
internalAssert ty.n != nil
|
||||
|
||||
@@ -70,13 +70,17 @@ proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) =
|
||||
GenericSeqSize),
|
||||
mt.base, shallow)
|
||||
of tyObject:
|
||||
# we need to copy m_type field for tyObject, as it could be empty for
|
||||
# sequence reallocations:
|
||||
var pint = cast[ptr PNimType](dest)
|
||||
pint[] = cast[ptr PNimType](src)[]
|
||||
if mt.base != nil:
|
||||
genericAssignAux(dest, src, mt.base, shallow)
|
||||
genericAssignAux(dest, src, mt.node, shallow)
|
||||
# we need to copy m_type field for tyObject, as it could be empty for
|
||||
# sequence reallocations:
|
||||
var pint = cast[ptr PNimType](dest)
|
||||
# We need to copy the *static* type not the dynamic type:
|
||||
# if p of TB:
|
||||
# var tbObj = TB(p)
|
||||
# tbObj of TC # needs to be false!
|
||||
pint[] = mt # cast[ptr PNimType](src)[]
|
||||
of tyTuple:
|
||||
genericAssignAux(dest, src, mt.node, shallow)
|
||||
of tyArray, tyArrayConstr:
|
||||
|
||||
55
tests/objects/tinherentedvalues.nim
Normal file
55
tests/objects/tinherentedvalues.nim
Normal file
@@ -0,0 +1,55 @@
|
||||
discard """
|
||||
output: '''tbObj of TC false
|
||||
false
|
||||
true
|
||||
5
|
||||
false'''
|
||||
"""
|
||||
|
||||
# bug #1053
|
||||
type
|
||||
TA = object of TObject
|
||||
a: int
|
||||
|
||||
TB = object of TA
|
||||
b: int
|
||||
|
||||
TC = object of TB
|
||||
c: int
|
||||
|
||||
proc test(p: TA) =
|
||||
#echo "p of TB ", p of TB
|
||||
if p of TB:
|
||||
var tbObj = TB(p)
|
||||
|
||||
# tbObj is actually no longer compatible with TC:
|
||||
echo "tbObj of TC ", tbObj of TC
|
||||
|
||||
var v = TC()
|
||||
v.a = 1
|
||||
v.b = 2
|
||||
v.c = 3
|
||||
test(v)
|
||||
|
||||
|
||||
# bug #924
|
||||
type
|
||||
MyObject = object of TObject
|
||||
x: int
|
||||
|
||||
var
|
||||
asd: MyObject
|
||||
|
||||
proc isMyObject(obj: TObject) =
|
||||
echo obj of MyObject
|
||||
if obj of MyObject:
|
||||
let a = MyObject(obj)
|
||||
echo a.x
|
||||
|
||||
asd.x = 5
|
||||
|
||||
var asdCopy = TObject(asd)
|
||||
echo asdCopy of MyObject
|
||||
|
||||
isMyObject(asd)
|
||||
isMyObject(asdCopy)
|
||||
Reference in New Issue
Block a user