summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAudun Wilhelmsen <skyfex@gmail.com>2015-01-03 00:10:07 +0100
committerAudun Wilhelmsen <skyfex@gmail.com>2015-01-03 00:10:07 +0100
commit7592c9cf22524332ef4b60bf050c046254685e9f (patch)
treebe89f11421d98c14d18dd009e443075bdf852b7f
parentc461f5a8c6cbc753f47393de61e713b25e743661 (diff)
downloadNim-7592c9cf22524332ef4b60bf050c046254685e9f.tar.gz
Added safe implicit conversion of uint8 to int16..int64, uint16 to int32..int64 etc.
Added two new tests for unsigned conversions and comparisons between signed numbers.
-rw-r--r--compiler/sigmatch.nim26
-rw-r--r--tests/misc/tunsignedcomp.nim134
-rw-r--r--tests/misc/tunsignedconv.nim52
3 files changed, 199 insertions, 13 deletions
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index b58818a29..e0c11ee3c 100644
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -260,7 +260,7 @@ proc concreteType(c: TCandidate, t: PType): PType =
   else:
     result = t                # Note: empty is valid here
   
-proc handleRange(f, a: PType, min, max: TTypeKind): TTypeRelation = 
+proc handleRange(f, a: PType, validconv: set[TTypeKind]): TTypeRelation = 
   if a.kind == f.kind: 
     result = isEqual
   else:
@@ -274,9 +274,9 @@ proc handleRange(f, a: PType, min, max: TTypeKind): TTypeRelation =
       # integer literal in the proper range; we want ``i16 + 4`` to stay an
       # ``int16`` operation so we declare the ``4`` pseudo-equal to int16
       result = isFromIntLit
-    elif f.kind == tyInt and k in {tyInt8..tyInt32}:
+    elif f.kind == tyInt and k in {tyInt8..tyInt32, tyUint8..tyUInt16}:
       result = isIntConv
-    elif k >= min and k <= max: 
+    elif k in validconv: 
       result = isConvertible
     elif a.kind == tyRange and a.sons[0].kind in {tyInt..tyInt64, 
                                                   tyUInt8..tyUInt32} and
@@ -641,16 +641,16 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
         result = isIntConv
       elif isConvertibleToRange(skipTypes(f, {tyRange}), a):
         result = isConvertible  # a convertible to f
-  of tyInt:      result = handleRange(f, a, tyInt8, tyInt32)
-  of tyInt8:     result = handleRange(f, a, tyInt8, tyInt8)
-  of tyInt16:    result = handleRange(f, a, tyInt8, tyInt16)
-  of tyInt32:    result = handleRange(f, a, tyInt8, tyInt32)
-  of tyInt64:    result = handleRange(f, a, tyInt, tyInt64)
-  of tyUInt:     result = handleRange(f, a, tyUInt8, tyUInt32)
-  of tyUInt8:    result = handleRange(f, a, tyUInt8, tyUInt8)
-  of tyUInt16:   result = handleRange(f, a, tyUInt8, tyUInt16)
-  of tyUInt32:   result = handleRange(f, a, tyUInt8, tyUInt32)
-  of tyUInt64:   result = handleRange(f, a, tyUInt, tyUInt64)
+  of tyInt:      result = handleRange(f, a, {tyInt8..tyInt32,tyUInt8..tyUInt16})
+  of tyInt8:     result = handleRange(f, a, {tyInt8})
+  of tyInt16:    result = handleRange(f, a, {tyInt8..tyInt16,tyUInt8})
+  of tyInt32:    result = handleRange(f, a, {tyInt8..tyInt32,tyUInt8..tyUInt16})
+  of tyInt64:    result = handleRange(f, a, {tyInt..tyInt64,tyUInt8..tyUInt32})
+  of tyUInt:     result = handleRange(f, a, {tyUInt8..tyUInt32})
+  of tyUInt8:    result = handleRange(f, a, {tyUInt8})
+  of tyUInt16:   result = handleRange(f, a, {tyUInt8..tyUInt16})
+  of tyUInt32:   result = handleRange(f, a, {tyUInt8..tyUInt32})
+  of tyUInt64:   result = handleRange(f, a, {tyUInt..tyUInt64})
   of tyFloat:    result = handleFloatRange(f, a)
   of tyFloat32:  result = handleFloatRange(f, a)
   of tyFloat64:  result = handleFloatRange(f, a)
diff --git a/tests/misc/tunsignedcomp.nim b/tests/misc/tunsignedcomp.nim
new file mode 100644
index 000000000..03c841b2f
--- /dev/null
+++ b/tests/misc/tunsignedcomp.nim
@@ -0,0 +1,134 @@
+discard """
+  output: ''''''
+"""
+
+# All operations involving uint64 are commented out
+# as they're not yet supported.
+# All other operations are handled by implicit conversions from uints to ints
+# uint64 could be supported but would need special implementation of the operators
+
+# unsigned < signed
+assert 10'u8 < 20'i8
+assert 10'u8 < 20'i16
+assert 10'u8 < 20'i32
+assert 10'u8 < 20'i64
+
+assert 10'u16 < 20'i8
+assert 10'u16 < 20'i16
+assert 10'u16 < 20'i32
+assert 10'u16 < 20'i64
+
+assert 10'u32 < 20'i8
+assert 10'u32 < 20'i16
+assert 10'u32 < 20'i32
+assert 10'u32 < 20'i64
+
+# assert 10'u64 < 20'i8
+# assert 10'u64 < 20'i16
+# assert 10'u64 < 20'i32
+# assert 10'u64 < 20'i64
+
+# signed < unsigned
+assert 10'i8 < 20'u8
+assert 10'i8 < 20'u16
+assert 10'i8 < 20'u32
+# assert 10'i8 < 20'u64
+
+assert 10'i16 < 20'u8
+assert 10'i16 < 20'u16
+assert 10'i16 < 20'u32
+# assert 10'i16 < 20'u64
+
+assert 10'i32 < 20'u8
+assert 10'i32 < 20'u16
+assert 10'i32 < 20'u32
+# assert 10'i32 < 20'u64
+
+assert 10'i64 < 20'u8
+assert 10'i64 < 20'u16
+assert 10'i64 < 20'u32
+# assert 10'i64 < 20'u64
+
+# unsigned <= signed
+assert 10'u8 <= 20'i8
+assert 10'u8 <= 20'i16
+assert 10'u8 <= 20'i32
+assert 10'u8 <= 20'i64
+
+assert 10'u16 <= 20'i8
+assert 10'u16 <= 20'i16
+assert 10'u16 <= 20'i32
+assert 10'u16 <= 20'i64
+
+assert 10'u32 <= 20'i8
+assert 10'u32 <= 20'i16
+assert 10'u32 <= 20'i32
+assert 10'u32 <= 20'i64
+
+# assert 10'u64 <= 20'i8
+# assert 10'u64 <= 20'i16
+# assert 10'u64 <= 20'i32
+# assert 10'u64 <= 20'i64
+
+# signed <= unsigned
+assert 10'i8 <= 20'u8
+assert 10'i8 <= 20'u16
+assert 10'i8 <= 20'u32
+# assert 10'i8 <= 20'u64
+
+assert 10'i16 <= 20'u8
+assert 10'i16 <= 20'u16
+assert 10'i16 <= 20'u32
+# assert 10'i16 <= 20'u64
+
+assert 10'i32 <= 20'u8
+assert 10'i32 <= 20'u16
+assert 10'i32 <= 20'u32
+# assert 10'i32 <= 20'u64
+
+assert 10'i64 <= 20'u8
+assert 10'i64 <= 20'u16
+assert 10'i64 <= 20'u32
+# assert 10'i64 <= 20'u64
+
+# signed == unsigned
+assert 10'i8 == 10'u8
+assert 10'i8 == 10'u16
+assert 10'i8 == 10'u32
+# assert 10'i8 == 10'u64
+
+assert 10'i16 == 10'u8
+assert 10'i16 == 10'u16
+assert 10'i16 == 10'u32
+# assert 10'i16 == 10'u64
+
+assert 10'i32 == 10'u8
+assert 10'i32 == 10'u16
+assert 10'i32 == 10'u32
+# assert 10'i32 == 10'u64
+
+assert 10'i64 == 10'u8
+assert 10'i64 == 10'u16
+assert 10'i64 == 10'u32
+# assert 10'i64 == 10'u64
+
+# unsigned == signed
+assert 10'u8 == 10'i8
+assert 10'u8 == 10'i16
+assert 10'u8 == 10'i32
+# assert 10'u8 == 10'i64
+
+assert 10'u16 == 10'i8
+assert 10'u16 == 10'i16
+assert 10'u16 == 10'i32
+# assert 10'u16 == 10'i64
+
+assert 10'u32 == 10'i8
+assert 10'u32 == 10'i16
+assert 10'u32 == 10'i32
+# assert 10'u32 == 10'i64
+
+# assert 10'u64 == 10'i8
+# assert 10'u64 == 10'i16
+# assert 10'u64 == 10'i32
+# assert 10'u64 == 10'i64
diff --git a/tests/misc/tunsignedconv.nim b/tests/misc/tunsignedconv.nim
new file mode 100644
index 000000000..547bc92ca
--- /dev/null
+++ b/tests/misc/tunsignedconv.nim
@@ -0,0 +1,52 @@
+discard """
+  output: ''''''
+"""
+
+import unsigned
+
+# Tests unsigned literals and implicit conversion between uints and ints
+# Passes if it compiles
+
+var h8:uint8 = 128
+var h16:uint16 = 32768
+var h32:uint32 = 2147483648'u32
+var h64:uint64 = 9223372036854775808'u64
+var foobar:uint64 = 9223372036854775813'u64 # Issue 728
+
+var v8:uint8 = 10
+var v16:uint16 = 10
+var v32:uint32 = 10
+var v64:uint64 = 10
+
+var a8:int = v8 + 10
+var a16:int = v16 + 10
+# var a32:int = v32 + 10
+# var a64:int = v64 + 10
+
+var d8  = v8 + 10'i8
+var d16 = v8 + 10'i16
+var d32 = v8 + 10'i32
+# var d64 = v8 + 10'i64
+
+var f8  = v16 + 10'i8
+var f16 = v16 + 10'i16
+var f32 = v16 + 10'i32
+# var f64 = v16 + 10'i64
+
+var g8  = v32 + 10'i8
+var g16 = v32 + 10'i16
+var g32 = v32 + 10'i32
+# var g64 = v32 + 10'i64
+
+# var n8  = v64 + 10'i8
+# var n16 = v64 + 10'i16
+# var n32 = v64 + 10'i32
+# var n64 = v64 + 10'i64
+
+var ar: array[0..20, int]
+var n8 = ar[v8]
+var n16 = ar[v16]
+var n32 = ar[v32]
+var n64 = ar[v64]
+
+