authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-05 23:19:13-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-05 23:19:13-04:00
log64034a3d1aeede8e0a5c8bd44d672a1d7902d981
tree3c7ea61fd5cf169d307a958cf3a5fe0fcf6e31d2
parentcfaced3f73da2d5fc82f063715d51e06084baee8

maxValue and minValue builtins return number literal

closes #170

3 files changed, 18 insertions(+), 4 deletions(-)

src/analyze.cpp+7-4
......@@ -4326,10 +4326,13 @@ static TypeTableEntry *analyze_min_max_value(CodeGen *g, ImportTableEntry *impor
43264326
43274327 if (type_entry->id == TypeTableEntryIdInvalid) {
43284328 return g->builtin_types.entry_invalid;
4329 } else if (type_entry->id == TypeTableEntryIdInt ||
4330 type_entry->id == TypeTableEntryIdFloat ||
4331 type_entry->id == TypeTableEntryIdBool)
4332 {
4329 } else if (type_entry->id == TypeTableEntryIdInt) {
4330 eval_min_max_value(g, type_entry, &get_resolved_expr(node)->const_val, is_max);
4331 return g->builtin_types.entry_num_lit_int;
4332 } else if (type_entry->id == TypeTableEntryIdFloat) {
4333 eval_min_max_value(g, type_entry, &get_resolved_expr(node)->const_val, is_max);
4334 return g->builtin_types.entry_num_lit_float;
4335 } else if (type_entry->id == TypeTableEntryIdBool) {
43334336 eval_min_max_value(g, type_entry, &get_resolved_expr(node)->const_val, is_max);
43344337 return type_entry;
43354338 } else {
test/cases/max_value_type.zig created+10
......@@ -0,0 +1,10 @@
1const assert = @import("std").debug.assert;
2
3#attribute("test")
4fn maxValueType() {
5 // If the type of @maxValue(i32) was i32 then this implicit cast to
6 // u32 would not work. But since the value is a number literal,
7 // it works fine.
8 const x: u32 = @maxValue(i32);
9 assert(x == 2147483647);
10}
test/self_hosted.zig+1
......@@ -7,6 +7,7 @@ const test_return_type_type = @import("cases/return_type_type.zig");
77const test_zeroes = @import("cases/zeroes.zig");
88const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig");
99const test_maybe_return = @import("cases/maybe_return.zig");
10const test_max_value_type = @import("cases/max_value_type.zig");
1011
1112// normal comment
1213/// this is a documentation comment