Commit 9e92dbdd08

Andrew Kelley <superjoe30@gmail.com>
2016-09-09 14:58:39
std: use parameter type inference on min and max functions
1 parent 100990b
src/analyze.cpp
@@ -988,6 +988,7 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
         case TypeTableEntryIdNullLit:
         case TypeTableEntryIdNamespace:
         case TypeTableEntryIdGenericFn:
+        case TypeTableEntryIdVar:
             fn_proto->skip = true;
             add_node_error(g, fn_proto->return_type,
                 buf_sprintf("return type '%s' not allowed", buf_ptr(&fn_type_id.return_type->name)));
@@ -1009,8 +1010,6 @@ static TypeTableEntry *analyze_fn_proto_type(CodeGen *g, ImportTableEntry *impor
         case TypeTableEntryIdFn:
         case TypeTableEntryIdTypeDecl:
             break;
-        case TypeTableEntryIdVar:
-            zig_panic("TODO var return type");
     }
 
 
std/hash_map.zig
@@ -180,8 +180,8 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
                 if (entry.distance_from_start_index < distance_from_start_index) {
                     // robin hood to the rescue
                     const tmp = *entry;
-                    hm.max_distance_from_start_index = math.max(usize,
-                        hm.max_distance_from_start_index, distance_from_start_index);
+                    hm.max_distance_from_start_index = math.max(hm.max_distance_from_start_index,
+                        distance_from_start_index);
                     *entry = Entry {
                         .used = true,
                         .distance_from_start_index = distance_from_start_index,
@@ -201,8 +201,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
                 hm.size += 1;
             }
 
-            hm.max_distance_from_start_index = math.max(usize, distance_from_start_index,
-                hm.max_distance_from_start_index);
+            hm.max_distance_from_start_index = math.max(distance_from_start_index, hm.max_distance_from_start_index);
             *entry = Entry {
                 .used = true,
                 .distance_from_start_index = distance_from_start_index,
std/io.zig
@@ -79,7 +79,7 @@ pub struct OutStream {
         const dest_space_left = os.buffer.len - os.index;
 
         while (src_bytes_left > 0) {
-            const copy_amt = math.min(usize, dest_space_left, src_bytes_left);
+            const copy_amt = math.min(dest_space_left, src_bytes_left);
             @memcpy(&os.buffer[os.index], &bytes[src_index], copy_amt);
             os.index += copy_amt;
             if (os.index == os.buffer.len) {
std/math.zig
@@ -4,11 +4,11 @@ pub enum Cmp {
     Less,
 }
 
-pub fn min(inline T: type, x: T, y: T) -> T {
+pub fn min(x: var, y: var) -> @typeOf(x + y) {
     if (x < y) x else y
 }
 
-pub fn max(inline T: type, x: T, y: T) -> T {
+pub fn max(x: var, y: var) -> @typeOf(x + y) {
     if (x > y) x else y
 }
 
@@ -25,3 +25,7 @@ pub fn subOverflow(inline T: type, a: T, b: T) -> %T {
     var answer: T = undefined;
     if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer
 }
+pub fn shlOverflow(inline T: type, a: T, b: T) -> %T {
+    var answer: T = undefined;
+    if (@shlWithOverflow(T, a, b, &answer)) error.Overflow else answer
+}
std/mem.zig
@@ -50,7 +50,7 @@ pub fn copy(inline T: type, dest: []T, source: []const T) {
 /// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,
 /// memory b, respectively.
 pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {
-    const n = math.min(usize, a.len, b.len);
+    const n = math.min(a.len, b.len);
     var i: usize = 0;
     while (i < n; i += 1) {
         if (a[i] == b[i]) continue;