From 64034a3d1aeede8e0a5c8bd44d672a1d7902d981 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 5 Sep 2016 23:19:13 -0400 Subject: [PATCH] maxValue and minValue builtins return number literal closes #170 --- src/analyze.cpp | 11 +++++++---- test/cases/max_value_type.zig | 10 ++++++++++ test/self_hosted.zig | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 test/cases/max_value_type.zig diff --git a/src/analyze.cpp b/src/analyze.cpp index 3064ad359d..6a24c42bc7 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4326,10 +4326,13 @@ static TypeTableEntry *analyze_min_max_value(CodeGen *g, ImportTableEntry *impor if (type_entry->id == TypeTableEntryIdInvalid) { return g->builtin_types.entry_invalid; - } else if (type_entry->id == TypeTableEntryIdInt || - type_entry->id == TypeTableEntryIdFloat || - type_entry->id == TypeTableEntryIdBool) - { + } else if (type_entry->id == TypeTableEntryIdInt) { + eval_min_max_value(g, type_entry, &get_resolved_expr(node)->const_val, is_max); + return g->builtin_types.entry_num_lit_int; + } else if (type_entry->id == TypeTableEntryIdFloat) { + eval_min_max_value(g, type_entry, &get_resolved_expr(node)->const_val, is_max); + return g->builtin_types.entry_num_lit_float; + } else if (type_entry->id == TypeTableEntryIdBool) { eval_min_max_value(g, type_entry, &get_resolved_expr(node)->const_val, is_max); return type_entry; } else { diff --git a/test/cases/max_value_type.zig b/test/cases/max_value_type.zig new file mode 100644 index 0000000000..62b9508318 --- /dev/null +++ b/test/cases/max_value_type.zig @@ -0,0 +1,10 @@ +const assert = @import("std").debug.assert; + +#attribute("test") +fn maxValueType() { + // If the type of @maxValue(i32) was i32 then this implicit cast to + // u32 would not work. But since the value is a number literal, + // it works fine. + const x: u32 = @maxValue(i32); + assert(x == 2147483647); +} diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 930a6a8fa9..cfbca50505 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -7,6 +7,7 @@ const test_return_type_type = @import("cases/return_type_type.zig"); const test_zeroes = @import("cases/zeroes.zig"); const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig"); const test_maybe_return = @import("cases/maybe_return.zig"); +const test_max_value_type = @import("cases/max_value_type.zig"); // normal comment /// this is a documentation comment