authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-27 14:33:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-27 14:33:31-07:00
log0a265867242757accd9a8dafee2f6cce7c738f29
treecf0b31ca6c5e898f898a343a995db5d5aad57094
parent707154da36ee10c7893cacfafdd3034385d69103

fix comparing incompatible number literals crash

closes #94

2 files changed, 17 insertions(+), 12 deletions(-)

src/analyze.cpp+8-11
......@@ -1234,14 +1234,19 @@ static bool num_lit_fits_in_other_type(CodeGen *g, AstNode *literal_node, TypeTa
12341234 {
12351235 return true;
12361236 }
1237 } else if (other_type->id == TypeTableEntryIdNumLitFloat ||
1238 other_type->id == TypeTableEntryIdNumLitInt)
1237 } else if ((other_type->id == TypeTableEntryIdNumLitFloat &&
1238 const_val->data.x_bignum.kind == BigNumKindFloat) ||
1239 (other_type->id == TypeTableEntryIdNumLitInt &&
1240 const_val->data.x_bignum.kind == BigNumKindInt))
12391241 {
12401242 return true;
12411243 }
12421244
1245 const char *num_lit_str = (const_val->data.x_bignum.kind == BigNumKindFloat) ? "float" : "integer";
1246
12431247 add_node_error(g, literal_node,
1244 buf_sprintf("value %s cannot be represented in type '%s'",
1248 buf_sprintf("%s value %s cannot be implicitly casted to type '%s'",
1249 num_lit_str,
12451250 buf_ptr(bignum_to_buf(&const_val->data.x_bignum)),
12461251 buf_ptr(&other_type->name)));
12471252 return false;
......@@ -1351,14 +1356,6 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa
13511356 prev_type = cur_type;
13521357 prev_node = cur_node;
13531358 continue;
1354 } else if (prev_type->id == TypeTableEntryIdNumLitFloat &&
1355 cur_type->id == TypeTableEntryIdNumLitFloat)
1356 {
1357 continue;
1358 } else if (prev_type->id == TypeTableEntryIdNumLitInt &&
1359 cur_type->id == TypeTableEntryIdNumLitInt)
1360 {
1361 continue;
13621359 } else if (prev_type->id == TypeTableEntryIdNumLitInt ||
13631360 prev_type->id == TypeTableEntryIdNumLitFloat)
13641361 {
test/run_tests.cpp+9-1
......@@ -1445,7 +1445,7 @@ fn f() -> i32 {
14451445fn f() {
14461446 if (0) {}
14471447}
1448 )SOURCE", 1, ".tmp_source.zig:3:9: error: value 0 cannot be represented in type 'bool'");
1448 )SOURCE", 1, ".tmp_source.zig:3:9: error: integer value 0 cannot be implicitly casted to type 'bool'");
14491449
14501450 add_compile_fail_case("assign unreachable", R"SOURCE(
14511451fn f() {
......@@ -1721,6 +1721,14 @@ struct Foo {
17211721}
17221722 )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeof");
17231723
1724 add_compile_fail_case("integer overflow error", R"SOURCE(
1725const x : u8 = 300;
1726 )SOURCE", 1, ".tmp_source.zig:2:16: error: integer value 300 cannot be implicitly casted to type 'u8'");
1727
1728 add_compile_fail_case("incompatible number literals", R"SOURCE(
1729const x = 2 == 2.0;
1730 )SOURCE", 1, ".tmp_source.zig:2:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'");
1731
17241732}
17251733
17261734static void print_compiler_invocation(TestCase *test_case) {