summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2015-02-24 19:35:52 +0100
committerAndreas Rumpf <rumpf_a@web.de>2015-02-24 19:35:52 +0100
commit819a2a03acb6bd47c4205be900f8b3f20e17c583 (patch)
tree288ec58d5d087843ce5024fecc6c51f6b497c452
parentf4a0400de285feec8fe07a41b5a0191728614227 (diff)
parent79384ea729075a2242923bf28ee1d3513a174d9b (diff)
downloadNim-819a2a03acb6bd47c4205be900f8b3f20e17c583.tar.gz
Merge pull request #2184 from oderwat/feat-caseofwhen
Allow empty sets in case/of branches.
-rw-r--r--compiler/semtypes.nim8
-rw-r--r--tests/casestmt/tcase_emptyset_when.nim24
2 files changed, 30 insertions, 2 deletions
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index d9c7b6c92..32a384f97 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -452,6 +452,7 @@ proc semCaseBranchSetElem(c: PContext, t, b: PNode,
 
 proc semCaseBranch(c: PContext, t, branch: PNode, branchIndex: int, 
                    covered: var BiggestInt) = 
+  
   for i in countup(0, sonsLen(branch) - 2): 
     var b = branch.sons[i]
     if b.kind == nkRange:
@@ -461,8 +462,11 @@ proc semCaseBranch(c: PContext, t, branch: PNode, branchIndex: int,
     else:
       # constant sets and arrays are allowed:
       var r = semConstExpr(c, b)
-      # for ``{}`` we want to trigger the type mismatch in ``fitNode``:
-      if r.kind notin {nkCurly, nkBracket} or len(r) == 0:
+      if r.kind in {nkCurly, nkBracket} and len(r) == 0  and sonsLen(branch)==2:
+        # discarding ``{}`` and ``[]`` branches silently
+        delSon(branch, 0)
+        return
+      elif r.kind notin {nkCurly, nkBracket} or len(r) == 0:
         checkMinSonsLen(t, 1)
         branch.sons[i] = skipConv(fitNode(c, t.sons[0].typ, r))
         inc(covered)
diff --git a/tests/casestmt/tcase_emptyset_when.nim b/tests/casestmt/tcase_emptyset_when.nim
new file mode 100644
index 000000000..e9b1ec2df
--- /dev/null
+++ b/tests/casestmt/tcase_emptyset_when.nim
@@ -0,0 +1,24 @@
+discard """
+  file: "tcaseofwhen.nim"
+  outputsub: "compiles for 1\ni am always two\ndefault for 3\nset is 4 not 5\narray is 6 not 7\ndefault for 8"
+  exitcode: "0"
+"""
+
+proc whenCase(a: int) =
+  case a
+  of (when compiles(whenCase(1)): 1 else: {}): echo "compiles for 1"
+  of {}: echo "me not fail"
+  of 2: echo "i am always two"
+  of []: echo "me neither"
+  of {4,5}: echo "set is 4 not 5"
+  of [6,7]: echo "array is 6 not 7"
+  of (when compiles(neverCompilesIBet()): 3 else: {}): echo "compiles for 3"
+  #of {},[]: echo "me neither"
+  else: echo "default for ", a
+
+whenCase(1)
+whenCase(2)
+whenCase(3)
+whenCase(4)
+whenCase(6)
+whenCase(8)