From 9e92dbdd0889a68e32cde85c1a07767200a9ad7e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 9 Sep 2016 08:58:39 -0400 Subject: [PATCH] std: use parameter type inference on min and max functions --- src/analyze.cpp | 3 +-- std/hash_map.zig | 7 +++---- std/io.zig | 2 +- std/math.zig | 8 ++++++-- std/mem.zig | 2 +- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index ad0dd7431e..298842c345 100644 --- a/src/analyze.cpp +++ b/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"); } diff --git a/std/hash_map.zig b/std/hash_map.zig index 16c0b65a47..e9bb533677 100644 --- a/std/hash_map.zig +++ b/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, diff --git a/std/io.zig b/std/io.zig index a0c868f816..e060251b0a 100644 --- a/std/io.zig +++ b/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) { diff --git a/std/math.zig b/std/math.zig index 5ee8fedf70..b44a4d5def 100644 --- a/std/math.zig +++ b/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 +} diff --git a/std/mem.zig b/std/mem.zig index aadbbbcb49..882588834c 100644 --- a/std/mem.zig +++ b/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;