summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2013-11-30 03:15:14 +0100
committerAraq <rumpf_a@web.de>2013-11-30 03:15:14 +0100
commitb3d759ca6d01d569534d71f0821148fa64a1fd2c (patch)
treeba83492798bea4039781a7066bd63dc731013522
parent2dcbc6493a8bcebc7dc9364b4a834853e1263639 (diff)
downloadNim-b3d759ca6d01d569534d71f0821148fa64a1fd2c.tar.gz
fixes #681
-rw-r--r--compiler/sem.nim20
-rw-r--r--tests/run/tvarious1.nim14
2 files changed, 32 insertions, 2 deletions
diff --git a/compiler/sem.nim b/compiler/sem.nim
index eda444252..3ace623bc 100644
--- a/compiler/sem.nim
+++ b/compiler/sem.nim
@@ -90,6 +90,24 @@ proc commonType*(x, y: PType): PType =
     let idx = ord(b.kind in {tyArray, tyArrayConstr})
     if a.sons[idx].kind == tyEmpty: return y
     #elif b.sons[idx].kind == tyEmpty: return x
+  elif a.kind == tyRange and b.kind == tyRange:
+    # consider:  (range[0..3], range[0..4]) here. We should make that
+    # range[0..4]. But then why is (range[0..4], 6) not range[0..6]?
+    # But then why is (2,4) not range[2..4]? But I think this would break
+    # too much code. So ... it's the same range or the base type. This means
+    #  type(if b: 0 else 1) == int and not range[0..1]. For now. In the long
+    # run people expect ranges to work properly within a tuple.
+    if not sameType(a, b):
+      result = skipTypes(a, {tyRange}).skipIntLit
+    when false:
+      if a.kind != tyRange and b.kind == tyRange:
+        # XXX This really needs a better solution, but a proper fix now breaks
+        # code.
+        result = a #.skipIntLit
+      elif a.kind == tyRange and b.kind != tyRange:
+        result = b #.skipIntLit
+      elif a.kind in IntegralTypes and a.n != nil:
+        result = a #.skipIntLit
   else:
     var k = tyNone
     if a.kind in {tyRef, tyPtr}:
@@ -103,7 +121,7 @@ proc commonType*(x, y: PType): PType =
       if result.isNil: return x
       if k != tyNone:
         let r = result
-        result = NewType(k, r.owner)
+        result = newType(k, r.owner)
         result.addSonSkipIntLit(r)
 
 proc isTopLevel(c: PContext): bool {.inline.} = 
diff --git a/tests/run/tvarious1.nim b/tests/run/tvarious1.nim
index 9dd4af606..6e4612ae3 100644
--- a/tests/run/tvarious1.nim
+++ b/tests/run/tvarious1.nim
@@ -2,7 +2,8 @@ discard """
   file: "tlenopenarray.nim"
   output: '''1
 0
-Whopie'''
+Whopie
+12'''
 """
 
 echo len([1_000_000]) #OUT 1
@@ -27,3 +28,14 @@ var w = TWidget(names: initQueue[string]())
 add(w.names, "Whopie")
 
 for n in w.names: echo(n)
+
+# bug #681
+
+type TSomeRange = object
+  hour: range[0..23]
+
+var value: string
+var val12 = TSomeRange(hour: 12)
+
+value = $(if val12.hour > 12: val12.hour - 12 else: val12.hour)
+echo value