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...@@ -7858,6 +7858,8 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
7858 result_type = ira->codegen->builtin_types.entry_invalid;7858 result_type = ira->codegen->builtin_types.entry_invalid;
7859 }7859 }
78607860
7861 bool is_comptime_var = ir_get_var_is_comptime(var);
7862
7861 switch (result_type->id) {7863 switch (result_type->id) {
7862 case TypeTableEntryIdTypeDecl:7864 case TypeTableEntryIdTypeDecl:
7863 zig_unreachable();7865 zig_unreachable();
...@@ -7865,7 +7867,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -7865,7 +7867,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
7865 break; // handled above7867 break; // handled above
7866 case TypeTableEntryIdNumLitFloat:7868 case TypeTableEntryIdNumLitFloat:
7867 case TypeTableEntryIdNumLitInt:7869 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)) {
7869 ir_add_error_node(ira, source_node, buf_sprintf("unable to infer variable type"));7871 ir_add_error_node(ira, source_node, buf_sprintf("unable to infer variable type"));
7870 result_type = ira->codegen->builtin_types.entry_invalid;7872 result_type = ira->codegen->builtin_types.entry_invalid;
7871 }7873 }
std/hash_map.zig+2-1
...@@ -7,7 +7,8 @@ const Allocator = mem.Allocator;...@@ -7,7 +7,8 @@ const Allocator = mem.Allocator;
7const want_modification_safety = !@compileVar("is_release");7const want_modification_safety = !@compileVar("is_release");
8const debug_u32 = if (want_modification_safety) u32 else void;8const 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,
11 comptime eql: fn(a: K, b: K)->bool) -> type12 comptime eql: fn(a: K, b: K)->bool) -> type
12{13{
13 struct {14 struct {
test/run_tests.cpp+7
...@@ -1697,6 +1697,13 @@ fn foo() {...@@ -1697,6 +1697,13 @@ fn foo() {
1697}1697}
1698fn bar() -> i32 { 0 }1698fn bar() -> i32 { 0 }
1699 )SOURCE", 1, ".tmp_source.zig:3:8: error: return value ignored");1699 )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");
1700}1707}
17011708
1702//////////////////////////////////////////////////////////////////////////////1709//////////////////////////////////////////////////////////////////////////////