authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-02 18:51:19-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-02 18:51:19-05:00
log6d0afc2bd233121d6b82f66cc09f74310a6eea4a
treebf2518175eb0b005a5c44c20c98ccf30b76d5bb0
parent03b6d9f547417e1f56f5dfb5079f7aa2dee832a6

add compile error for assigning number literal to non-comptime var


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

src/ir.cpp+3-1
......@@ -7858,6 +7858,8 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
78587858 result_type = ira->codegen->builtin_types.entry_invalid;
78597859 }
78607860
7861 bool is_comptime_var = ir_get_var_is_comptime(var);
7862
78617863 switch (result_type->id) {
78627864 case TypeTableEntryIdTypeDecl:
78637865 zig_unreachable();
......@@ -7865,7 +7867,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
78657867 break; // handled above
78667868 case TypeTableEntryIdNumLitFloat:
78677869 case TypeTableEntryIdNumLitInt:
7868 if (is_export || is_extern || casted_init_value->value.special == ConstValSpecialRuntime) {
7870 if (is_export || is_extern || (!var->src_is_const && !is_comptime_var)) {
78697871 ir_add_error_node(ira, source_node, buf_sprintf("unable to infer variable type"));
78707872 result_type = ira->codegen->builtin_types.entry_invalid;
78717873 }
std/hash_map.zig+2-1
......@@ -7,7 +7,8 @@ const Allocator = mem.Allocator;
77const want_modification_safety = !@compileVar("is_release");
88const debug_u32 = if (want_modification_safety) u32 else void;
99
10pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn(key: K)->u32,
10pub fn HashMap(comptime K: type, comptime V: type,
11 comptime hash: fn(key: K)->u32,
1112 comptime eql: fn(a: K, b: K)->bool) -> type
1213{
1314 struct {
test/run_tests.cpp+7
......@@ -1697,6 +1697,13 @@ fn foo() {
16971697}
16981698fn bar() -> i32 { 0 }
16991699 )SOURCE", 1, ".tmp_source.zig:3:8: error: return value ignored");
1700
1701 add_compile_fail_case("integer literal on a non-comptime var", R"SOURCE(
1702fn foo() {
1703 var i = 0;
1704 while (i < 10; i += 1) { }
1705}
1706 )SOURCE", 1, ".tmp_source.zig:3:5: error: unable to infer variable type");
17001707}
17011708
17021709//////////////////////////////////////////////////////////////////////////////