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...@@ -1234,14 +1234,19 @@ static bool num_lit_fits_in_other_type(CodeGen *g, AstNode *literal_node, TypeTa
1234 {1234 {
1235 return true;1235 return true;
1236 }1236 }
1237 } else if (other_type->id == TypeTableEntryIdNumLitFloat ||1237 } else if ((other_type->id == TypeTableEntryIdNumLitFloat &&
1238 other_type->id == TypeTableEntryIdNumLitInt)1238 const_val->data.x_bignum.kind == BigNumKindFloat) ||
1239 (other_type->id == TypeTableEntryIdNumLitInt &&
1240 const_val->data.x_bignum.kind == BigNumKindInt))
1239 {1241 {
1240 return true;1242 return true;
1241 }1243 }
12421244
1245 const char *num_lit_str = (const_val->data.x_bignum.kind == BigNumKindFloat) ? "float" : "integer";
1246
1243 add_node_error(g, literal_node,1247 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,
1245 buf_ptr(bignum_to_buf(&const_val->data.x_bignum)),1250 buf_ptr(bignum_to_buf(&const_val->data.x_bignum)),
1246 buf_ptr(&other_type->name)));1251 buf_ptr(&other_type->name)));
1247 return false;1252 return false;
...@@ -1351,14 +1356,6 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa...@@ -1351,14 +1356,6 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa
1351 prev_type = cur_type;1356 prev_type = cur_type;
1352 prev_node = cur_node;1357 prev_node = cur_node;
1353 continue;1358 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;
1362 } else if (prev_type->id == TypeTableEntryIdNumLitInt ||1359 } else if (prev_type->id == TypeTableEntryIdNumLitInt ||
1363 prev_type->id == TypeTableEntryIdNumLitFloat)1360 prev_type->id == TypeTableEntryIdNumLitFloat)
1364 {1361 {
test/run_tests.cpp+9-1
...@@ -1445,7 +1445,7 @@ fn f() -> i32 {...@@ -1445,7 +1445,7 @@ fn f() -> i32 {
1445fn f() {1445fn f() {
1446 if (0) {}1446 if (0) {}
1447}1447}
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
1450 add_compile_fail_case("assign unreachable", R"SOURCE(1450 add_compile_fail_case("assign unreachable", R"SOURCE(
1451fn f() {1451fn f() {
...@@ -1721,6 +1721,14 @@ struct Foo {...@@ -1721,6 +1721,14 @@ struct Foo {
1721}1721}
1722 )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeof");1722 )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
1724}1732}
17251733
1726static void print_compiler_invocation(TestCase *test_case) {1734static void print_compiler_invocation(TestCase *test_case) {