summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2018-09-15 00:09:50 +0200
committerLemonBoy <thatlemon@gmail.com>2018-09-15 11:55:11 +0200
commit6af6ca6351f29d180f86a8521cf2f46f0e6934a7 (patch)
treeea7c9a2b04602ec2e5c39ea54d1f981c84d9041d
parent0c04b80651ff591beadb873c8814168e66e6b722 (diff)
downloadNim-6af6ca6351f29d180f86a8521cf2f46f0e6934a7.tar.gz
Fix codegen for set[T] parameters
Sometimes sets are materialized as arrays and we must treat them as
such: the CPP backend is pickier than the C one and would sometimes
produce invalid code.

Fixes #8967
-rw-r--r--compiler/ccgexprs.nim3
-rw-r--r--compiler/ccgtypes.nim15
-rw-r--r--tests/ccgbugs/t8967.nim10
3 files changed, 20 insertions, 8 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index 56ecf5ba3..a7a28a51c 100644
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -1596,10 +1596,11 @@ proc genSwap(p: BProc, e: PNode, d: var TLoc) =
   genAssignment(p, a, b, {})
   genAssignment(p, b, tmp, {})
 
-proc rdSetElemLoc(conf: ConfigRef; a: TLoc, setType: PType): Rope =
+proc rdSetElemLoc(conf: ConfigRef; a: TLoc, typ: PType): Rope =
   # read a location of an set element; it may need a subtraction operation
   # before the set operation
   result = rdCharLoc(a)
+  let setType = typ.skipTypes(abstractPtrs)
   assert(setType.kind == tySet)
   if firstOrd(conf, setType) != 0:
     result = "($1- $2)" % [result, rope(firstOrd(conf, setType))]
diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim
index dd79f4846..52a4a72f2 100644
--- a/compiler/ccgtypes.nim
+++ b/compiler/ccgtypes.nim
@@ -162,9 +162,9 @@ proc mapType(conf: ConfigRef; typ: PType): TCTypeKind =
     var base = skipTypes(typ.lastSon, typedescInst)
     case base.kind
     of tyOpenArray, tyArray, tyVarargs: result = ctPtrToArray
-    #of tySet:
-    #  if mapSetType(base) == ctArray: result = ctPtrToArray
-    #  else: result = ctPtr
+    of tySet:
+      if mapSetType(conf, base) == ctArray: result = ctPtrToArray
+      else: result = ctPtr
     # XXX for some reason this breaks the pegs module
     else: result = ctPtr
   of tyPointer: result = ctPtr
@@ -641,10 +641,11 @@ proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet): Rope =
                     compileToCpp(m): "&" else: "*"
     var et = origTyp.skipTypes(abstractInst).lastSon
     var etB = et.skipTypes(abstractInst)
-    if etB.kind in {tyArray, tyOpenArray, tyVarargs}:
-      # this is correct! sets have no proper base type, so we treat
-      # ``var set[char]`` in `getParamTypeDesc`
-      et = elemType(etB)
+    if mapType(m.config, t) == ctPtrToArray:
+      if etB.kind == tySet:
+        et = getSysType(m.g.graph, unknownLineInfo(), tyUInt8)
+      else:
+        et = elemType(etB)
       etB = et.skipTypes(abstractInst)
       star[0] = '*'
     case etB.kind
diff --git a/tests/ccgbugs/t8967.nim b/tests/ccgbugs/t8967.nim
new file mode 100644
index 000000000..e342b7eae
--- /dev/null
+++ b/tests/ccgbugs/t8967.nim
@@ -0,0 +1,10 @@
+discard """
+  targets: "c cpp"
+"""
+
+import marshal
+
+let orig: set[char] = {'A'..'Z'}
+let m = $$orig
+let old = to[set[char]](m)
+doAssert orig - old == {}