Commit 0a26586724

Andrew Kelley <superjoe30@gmail.com>
2016-01-27 22:33:31
fix comparing incompatible number literals crash
closes #94
1 parent 707154d
Changed files (2)
src/analyze.cpp
@@ -1234,14 +1234,19 @@ static bool num_lit_fits_in_other_type(CodeGen *g, AstNode *literal_node, TypeTa
         {
             return true;
         }
-    } else if (other_type->id == TypeTableEntryIdNumLitFloat ||
-               other_type->id == TypeTableEntryIdNumLitInt)
+    } else if ((other_type->id == TypeTableEntryIdNumLitFloat &&
+                const_val->data.x_bignum.kind == BigNumKindFloat) ||
+               (other_type->id == TypeTableEntryIdNumLitInt &&
+                const_val->data.x_bignum.kind == BigNumKindInt))
     {
         return true;
     }
 
+    const char *num_lit_str = (const_val->data.x_bignum.kind == BigNumKindFloat) ? "float" : "integer";
+
     add_node_error(g, literal_node,
-        buf_sprintf("value %s cannot be represented in type '%s'",
+        buf_sprintf("%s value %s cannot be implicitly casted to type '%s'",
+            num_lit_str,
             buf_ptr(bignum_to_buf(&const_val->data.x_bignum)),
             buf_ptr(&other_type->name)));
     return false;
@@ -1351,14 +1356,6 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa
             prev_type = cur_type;
             prev_node = cur_node;
             continue;
-        } else if (prev_type->id == TypeTableEntryIdNumLitFloat &&
-                   cur_type->id == TypeTableEntryIdNumLitFloat)
-        {
-            continue;
-        } else if (prev_type->id == TypeTableEntryIdNumLitInt &&
-                   cur_type->id == TypeTableEntryIdNumLitInt)
-        {
-            continue;
         } else if (prev_type->id == TypeTableEntryIdNumLitInt ||
                     prev_type->id == TypeTableEntryIdNumLitFloat)
         {
test/run_tests.cpp
@@ -1445,7 +1445,7 @@ fn f() -> i32 {
 fn f() {
     if (0) {}
 }
-    )SOURCE", 1, ".tmp_source.zig:3:9: error: value 0 cannot be represented in type 'bool'");
+    )SOURCE", 1, ".tmp_source.zig:3:9: error: integer value 0 cannot be implicitly casted to type 'bool'");
 
     add_compile_fail_case("assign unreachable", R"SOURCE(
 fn f() {
@@ -1721,6 +1721,14 @@ struct Foo {
 }
     )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeof");
 
+    add_compile_fail_case("integer overflow error", R"SOURCE(
+const x : u8 = 300;
+    )SOURCE", 1, ".tmp_source.zig:2:16: error: integer value 300 cannot be implicitly casted to type 'u8'");
+
+    add_compile_fail_case("incompatible number literals", R"SOURCE(
+const x = 2 == 2.0;
+    )SOURCE", 1, ".tmp_source.zig:2:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'");
+
 }
 
 static void print_compiler_invocation(TestCase *test_case) {