authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-06 23:33:10-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-06 23:33:10-07:00
log66e3aa09104781f460da1ea721b083d18eec351b
treefa6a39c1df96635168fa02e1427daf238e31e8bd
parent180f539f6722e72b661825e9d4971651da872b99

initialize mutable variables to zero


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

src/analyze.cpp+2-2
......@@ -454,8 +454,8 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
454454 buf_sprintf("variable initialization is unreachable"));
455455 }
456456
457 if (implicit_type == nullptr) {
458 add_node_error(g, node, buf_sprintf("initial values are required for variable declaration"));
457 if (implicit_type == nullptr && variable_declaration->is_const) {
458 add_node_error(g, node, buf_sprintf("variables must have initial values or be declared 'mut'."));
459459 }
460460
461461 TypeTableEntry *type = explicit_type != nullptr ? explicit_type : implicit_type;
src/codegen.cpp+5-5
......@@ -526,14 +526,14 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
526526 variable->value_ref = gen_expr(g, node->data.variable_declaration.expr);
527527 return nullptr;
528528 } else {
529 LLVMValueRef value;
529530 if (node->data.variable_declaration.expr) {
530 LLVMValueRef value = gen_expr(g, node->data.variable_declaration.expr);
531
532 add_debug_source_node(g, node);
533 return LLVMBuildStore(g->builder, value, variable->value_ref);
531 value = gen_expr(g, node->data.variable_declaration.expr);
534532 } else {
535
533 value = LLVMConstNull(variable->type->type_ref);
536534 }
535 add_debug_source_node(g, node);
536 return LLVMBuildStore(g->builder, value, variable->value_ref);
537537 }
538538 }
539539 case NodeTypeCastExpr:
test/run_tests.cpp+5-2
......@@ -332,7 +332,7 @@ fn void_fun(a : i32, b : void, c : i32) {
332332}
333333 )SOURCE", "OK\n");
334334
335 add_simple_case("void parameters", R"SOURCE(
335 add_simple_case("mutable local variables", R"SOURCE(
336336#link("c")
337337extern {
338338 fn puts(s: *const u8) -> i32;
......@@ -340,6 +340,9 @@ extern {
340340}
341341
342342export fn _start() -> unreachable {
343 let mut zero : i32;
344 if (zero == 0) { puts("zero"); }
345
343346 let mut i = 0;
344347loop_start:
345348 if i == 3 {
......@@ -351,7 +354,7 @@ loop_start:
351354done:
352355 exit(0);
353356}
354 )SOURCE", "loop\nloop\nloop\n");
357 )SOURCE", "zero\nloop\nloop\nloop\n");
355358}
356359
357360static void add_compile_failure_test_cases(void) {