summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/semfields.nim6
-rw-r--r--lib/core/typeinfo.nim9
-rw-r--r--tests/fields/tfielditerator2.nim20
-rw-r--r--tests/stdlib/tmarshal.nim16
4 files changed, 39 insertions, 12 deletions
diff --git a/compiler/semfields.nim b/compiler/semfields.nim
index 70dead8ab..a6ade64d5 100644
--- a/compiler/semfields.nim
+++ b/compiler/semfields.nim
@@ -147,7 +147,11 @@ proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
     var fc: TFieldsCtx
     fc.m = m
     fc.c = c
-    semForObjectFields(fc, tupleTypeA.n, n, stmts)
+    var t = tupleTypeA
+    while t.kind == tyObject:
+      semForObjectFields(fc, t.n, n, stmts)
+      if t.sons[0] == nil: break
+      t = skipTypes(t.sons[0], abstractPtrs)
   dec(c.p.nestedLoopCounter)
   # for TR macros this 'while true: ...; break' loop is pretty bad, so
   # we avoid it now if we can:
diff --git a/lib/core/typeinfo.nim b/lib/core/typeinfo.nim
index 1b9b95c24..0046924a1 100644
--- a/lib/core/typeinfo.nim
+++ b/lib/core/typeinfo.nim
@@ -269,9 +269,14 @@ iterator fields*(x: TAny): tuple[name: string, any: TAny] =
   # XXX BUG: does not work yet, however is questionable anyway
   when false:
     if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
-  var n = t.node
   var ret: seq[tuple[name: cstring, any: TAny]] = @[]
-  fieldsAux(p, n, ret)
+  if t.kind == tyObject:
+    while true:
+      fieldsAux(p, t.node, ret)
+      t = t.base
+      if t.isNil: break
+  else:
+    fieldsAux(p, t.node, ret)
   for name, any in items(ret):
     yield ($name, any)
 
diff --git a/tests/fields/tfielditerator2.nim b/tests/fields/tfielditerator2.nim
index 76fa568f2..70ab9e2ab 100644
--- a/tests/fields/tfielditerator2.nim
+++ b/tests/fields/tfielditerator2.nim
@@ -5,16 +5,19 @@ a char: false
 an int: 5
 an int: 6
 a string: abc
-false
-true
-true
-false
-true
+a string: I'm root!
+CMP false
+CMP true
+CMP true
+CMP false
+CMP true
+CMP true
 a: a
 b: b
 x: 5
 y: 6
 z: abc
+thaRootMan: I'm root!
 myDisc: enC
 c: Z
 enC
@@ -23,7 +26,9 @@ Z
 """
 
 type
-  TMyObj = object
+  SomeRootObj = object of RootObj
+    thaRootMan: string
+  TMyObj = object of SomeRootObj
     a, b: char
     x, y: int
     z: string
@@ -41,6 +46,7 @@ proc p(x: string) = echo "a string: ", x
 
 proc myobj(a, b: char, x, y: int, z: string): TMyObj =
   result.a = a; result.b = b; result.x = x; result.y = y; result.z = z
+  result.thaRootMan = "I'm root!"
 
 var x = myobj('a', 'b', 5, 6, "abc")
 var y = myobj('A', 'b', 5, 9, "abc")
@@ -49,7 +55,7 @@ for f in fields(x):
   p f
 
 for a, b in fields(x, y):
-  echo a == b
+  echo "CMP ", a == b
 
 for key, val in fieldPairs(x):
   echo key, ": ", val
diff --git a/tests/stdlib/tmarshal.nim b/tests/stdlib/tmarshal.nim
index 5471d347a..1b83aab53 100644
--- a/tests/stdlib/tmarshal.nim
+++ b/tests/stdlib/tmarshal.nim
@@ -1,10 +1,10 @@
 discard """
-  output: ""
+  output: '''{"age": 12, "name": "Cletus"}'''
 """
 
 import marshal
 
-template testit(x: expr) = echo($$to[type(x)]($$x))
+template testit(x: expr) = discard $$to[type(x)]($$x)
 
 var x: array[0..4, array[0..4, string]] = [

   ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], 

@@ -63,3 +63,15 @@ testit(test7)
 var test6: set[char] = {'A'..'Z', '_'}
 testit(test6)
 
+
+# bug #1352
+
+type
+  Entity = object of RootObj
+    name: string
+
+  Person = object of Entity
+    age: int
+
+var instance1 = Person(name: "Cletus", age: 12)
+echo($$instance1)