summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorcooldome <cdome@bk.ru>2018-07-30 11:00:06 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-07-30 11:00:06 +0200
commit404f0d64afa5cb18c54c52d261609f60a00cdb46 (patch)
tree2dbf07dfb74dc7831af2b0f447c659fef6703d09
parentc3a9ac4d352a84fb47da0a4d5fc6f963b651bbec (diff)
downloadNim-404f0d64afa5cb18c54c52d261609f60a00cdb46.tar.gz
Add sizeof for arrays of integral types (#8445)
-rw-r--r--compiler/semfold.nim2
-rw-r--r--tests/misc/tsizeof.nim22
2 files changed, 21 insertions, 3 deletions
diff --git a/compiler/semfold.nim b/compiler/semfold.nim
index 2f495bc7f..d2abfac13 100644
--- a/compiler/semfold.nim
+++ b/compiler/semfold.nim
@@ -609,7 +609,7 @@ proc getConstExpr(m: PSym, n: PNode; g: ModuleGraph): PNode =
         if computeSize(g.config, a.typ) < 0:
           localError(g.config, a.info, "cannot evaluate 'sizeof' because its type is not defined completely")
           result = nil
-        elif skipTypes(a.typ, typedescInst+{tyRange}).kind in
+        elif skipTypes(a.typ, typedescInst+{tyRange, tyArray}).kind in
              IntegralTypes+NilableTypes+{tySet}:
           #{tyArray,tyObject,tyTuple}:
           result = newIntNodeT(getSize(g.config, a.typ), n, g)
diff --git a/tests/misc/tsizeof.nim b/tests/misc/tsizeof.nim
index 4afd48472..0597535f9 100644
--- a/tests/misc/tsizeof.nim
+++ b/tests/misc/tsizeof.nim
@@ -1,5 +1,7 @@
-# Test the sizeof proc
-
+discard """
+  file: "tsize.nim"
+  output: "40 3 12 32"
+"""
 type
   TMyRecord {.final.} = object
     x, y: int
@@ -7,4 +9,20 @@ type
     r: float
     s: string
 
+  TMyEnum = enum
+    tmOne, tmTwo, tmThree, tmFour
+
+  TMyArray1 = array[3, uint8]
+  TMyArray2 = array[1..3, int32]
+  TMyArray3 = array[TMyEnum, float64]
+
+const 
+  mysize1 = sizeof(TMyArray1)
+  mysize2 = sizeof(TMyArray2)
+  mysize3 = sizeof(TMyArray3)
+
 write(stdout, sizeof(TMyRecord))
+echo ' ', mysize1, ' ', mysize2, ' ',mysize3
+
+
+
f='#n188'>188 189 190 191 192