authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-05 18:50:36-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-05 18:50:36-05:00
loge621ad014e919521268d3e2c94afadbdadadb59f
treea038e0051b6d8c9b220cb2faa2918eb3f42fa909
parent97e105489085fcf4530b3944499531d76848bbd1

pass cannot assign to constant test


8 files changed, 70 insertions(+), 36 deletions(-)

example/cat/main.zig+4-4
......@@ -10,7 +10,7 @@ pub fn main(args: [][]u8) -> %void {
1010 for (args[1...]) |arg| {
1111 if (str.eql(arg, "-")) {
1212 catted_anything = true;
13 cat_stream(io.stdin) %% |err| return err;
13 cat_stream(&io.stdin) %% |err| return err;
1414 } else if (arg[0] == '-') {
1515 return usage(exe);
1616 } else {
......@@ -24,11 +24,11 @@ pub fn main(args: [][]u8) -> %void {
2424 defer %%is.close();
2525
2626 catted_anything = true;
27 cat_stream(is) %% |err| return err;
27 cat_stream(&is) %% |err| return err;
2828 }
2929 }
3030 if (!catted_anything) {
31 cat_stream(io.stdin) %% |err| return err;
31 cat_stream(&io.stdin) %% |err| return err;
3232 }
3333 io.stdout.flush() %% |err| return err;
3434}
......@@ -40,7 +40,7 @@ fn usage(exe: []u8) -> %void {
4040 return error.Invalid;
4141}
4242
43fn cat_stream(is: io.InStream) -> %void {
43fn cat_stream(is: &io.InStream) -> %void {
4444 var buf: [1024 * 4]u8 = undefined;
4545
4646 while (true) {
src/ir.cpp+48-21
......@@ -721,17 +721,23 @@ static IrInstruction *ir_build_bin_op_from(IrBuilder *irb, IrInstruction *old_in
721721 return new_instruction;
722722}
723723
724static IrInstruction *ir_build_var_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, VariableTableEntry *var) {
724static IrInstruction *ir_build_var_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
725 VariableTableEntry *var, bool is_const)
726{
725727 IrInstructionVarPtr *instruction = ir_build_instruction<IrInstructionVarPtr>(irb, scope, source_node);
726728 instruction->var = var;
729 instruction->is_const = is_const;
727730
728731 ir_ref_var(var);
729732
730733 return &instruction->base;
731734}
732735
733static IrInstruction *ir_build_var_ptr_from(IrBuilder *irb, IrInstruction *old_instruction, VariableTableEntry *var) {
734 IrInstruction *new_instruction = ir_build_var_ptr(irb, old_instruction->scope, old_instruction->source_node, var);
736static IrInstruction *ir_build_var_ptr_from(IrBuilder *irb, IrInstruction *old_instruction,
737 VariableTableEntry *var, bool is_const)
738{
739 IrInstruction *new_instruction = ir_build_var_ptr(irb, old_instruction->scope,
740 old_instruction->source_node, var, is_const);
735741 ir_link_new_instruction(new_instruction, old_instruction);
736742 return new_instruction;
737743
......@@ -3401,7 +3407,7 @@ static IrInstruction *ir_gen_decl_ref(IrBuilder *irb, AstNode *source_node, Tld
34013407 {
34023408 TldVar *tld_var = (TldVar *)tld;
34033409 VariableTableEntry *var = tld_var->var;
3404 IrInstruction *var_ptr = ir_build_var_ptr(irb, scope, source_node, var);
3410 IrInstruction *var_ptr = ir_build_var_ptr(irb, scope, source_node, var, false);
34053411 if (lval != LValPurposeNone)
34063412 return var_ptr;
34073413 else
......@@ -3449,7 +3455,7 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
34493455
34503456 VariableTableEntry *var = find_variable(irb->codegen, scope, variable_name);
34513457 if (var) {
3452 IrInstruction *var_ptr = ir_build_var_ptr(irb, scope, node, var);
3458 IrInstruction *var_ptr = ir_build_var_ptr(irb, scope, node, var, lval == LValPurposeAddressOfConst);
34533459 if (lval != LValPurposeNone)
34543460 return var_ptr;
34553461 else
......@@ -4309,7 +4315,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
43094315
43104316 IrInstruction *undefined_value = ir_build_const_undefined(irb, child_scope, elem_node);
43114317 ir_build_var_decl(irb, child_scope, elem_node, elem_var, elem_var_type, undefined_value);
4312 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, child_scope, node, elem_var);
4318 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, child_scope, node, elem_var, false);
43134319
43144320 AstNode *index_var_source_node;
43154321 VariableTableEntry *index_var;
......@@ -4327,7 +4333,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
43274333 IrInstruction *zero = ir_build_const_usize(irb, child_scope, node, 0);
43284334 IrInstruction *one = ir_build_const_usize(irb, child_scope, node, 1);
43294335 ir_build_var_decl(irb, child_scope, index_var_source_node, index_var, usize, zero);
4330 IrInstruction *index_ptr = ir_build_var_ptr(irb, child_scope, node, index_var);
4336 IrInstruction *index_ptr = ir_build_var_ptr(irb, child_scope, node, index_var, false);
43314337
43324338
43334339 IrBasicBlock *cond_block = ir_build_basic_block(irb, child_scope, "ForCond");
......@@ -4351,7 +4357,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
43514357 } else {
43524358 elem_val = ir_build_load_ptr(irb, child_scope, node, elem_ptr);
43534359 }
4354 ir_build_store_ptr(irb, child_scope, node, elem_var_ptr, elem_val);
4360 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, elem_var_ptr, elem_val));
43554361
43564362 LoopStackItem *loop_stack_item = irb->loop_stack.add_one();
43574363 loop_stack_item->break_block = end_block;
......@@ -4365,7 +4371,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
43654371
43664372 ir_set_cursor_at_end(irb, continue_block);
43674373 IrInstruction *new_index_val = ir_build_bin_op(irb, child_scope, node, IrBinOpAdd, index_val, one, false);
4368 ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val);
4374 ir_mark_gen(ir_build_store_ptr(irb, child_scope, node, index_ptr, new_index_val));
43694375 ir_build_br(irb, child_scope, node, cond_block, is_comptime);
43704376
43714377 ir_set_cursor_at_end(irb, end_block);
......@@ -7545,9 +7551,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
75457551 size_t next_proto_i = 0;
75467552
75477553 if (first_arg_ptr) {
7548 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
7549 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
7550 return ira->codegen->builtin_types.entry_invalid;
7554 IrInstruction *first_arg;
7555 assert(first_arg_ptr->value.type->id == TypeTableEntryIdPointer);
7556 if (handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
7557 first_arg = first_arg_ptr;
7558 } else {
7559 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
7560 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
7561 return ira->codegen->builtin_types.entry_invalid;
7562 }
75517563
75527564 if (!ir_analyze_fn_call_generic_arg(ira, fn_proto_node, first_arg, &impl_fn->child_scope,
75537565 &next_proto_i, generic_id, &fn_type_id, casted_args, impl_fn))
......@@ -7612,9 +7624,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
76127624 IrInstruction **casted_args = allocate<IrInstruction *>(call_param_count);
76137625 size_t next_arg_index = 0;
76147626 if (first_arg_ptr) {
7615 IrInstruction *first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
7616 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
7617 return ira->codegen->builtin_types.entry_invalid;
7627 IrInstruction *first_arg;
7628 assert(first_arg_ptr->value.type->id == TypeTableEntryIdPointer);
7629 if (handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
7630 first_arg = first_arg_ptr;
7631 } else {
7632 first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
7633 if (first_arg->value.type->id == TypeTableEntryIdInvalid)
7634 return ira->codegen->builtin_types.entry_invalid;
7635 }
76187636
76197637 TypeTableEntry *param_type = fn_type_id->param_info[next_arg_index].type;
76207638 if (param_type->id == TypeTableEntryIdInvalid)
......@@ -8090,7 +8108,9 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
80908108 return resolved_type;
80918109}
80928110
8093static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruction, VariableTableEntry *var) {
8111static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
8112 VariableTableEntry *var, bool is_const_ptr)
8113{
80948114 assert(var->value.type);
80958115 if (var->value.type->id == TypeTableEntryIdInvalid)
80968116 return var->value.type;
......@@ -8110,9 +8130,10 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc
81108130
81118131 if (mem_slot && mem_slot->special != ConstValSpecialRuntime) {
81128132 ConstPtrSpecial ptr_special = is_comptime ? ConstPtrSpecialInline : ConstPtrSpecialNone;
8113 return ir_analyze_const_ptr(ira, instruction, mem_slot, var->value.type, false, ptr_special, var->src_is_const);
8133 bool is_const = (var->value.type->id == TypeTableEntryIdMetaType) ? is_const_ptr : var->src_is_const;
8134 return ir_analyze_const_ptr(ira, instruction, mem_slot, var->value.type, false, ptr_special, is_const);
81148135 } else {
8115 ir_build_var_ptr_from(&ira->new_irb, instruction, var);
8136 ir_build_var_ptr_from(&ira->new_irb, instruction, var, false);
81168137 type_ensure_zero_bits_known(ira->codegen, var->value.type);
81178138 return get_pointer_to_type(ira->codegen, var->value.type, var->src_is_const);
81188139 }
......@@ -8120,7 +8141,7 @@ static TypeTableEntry *ir_analyze_var_ptr(IrAnalyze *ira, IrInstruction *instruc
81208141
81218142static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstructionVarPtr *var_ptr_instruction) {
81228143 VariableTableEntry *var = var_ptr_instruction->var;
8123 return ir_analyze_var_ptr(ira, &var_ptr_instruction->base, var);
8144 return ir_analyze_var_ptr(ira, &var_ptr_instruction->base, var, var_ptr_instruction->is_const);
81248145}
81258146
81268147static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionElemPtr *elem_ptr_instruction) {
......@@ -8313,7 +8334,7 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
83138334 {
83148335 TldVar *tld_var = (TldVar *)tld;
83158336 VariableTableEntry *var = tld_var->var;
8316 return ir_analyze_var_ptr(ira, source_instruction, var);
8337 return ir_analyze_var_ptr(ira, source_instruction, var, false);
83178338 }
83188339 case TldIdFn:
83198340 {
......@@ -8549,6 +8570,12 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
85498570 if (value->value.type->id == TypeTableEntryIdInvalid)
85508571 return value->value.type;
85518572
8573 assert(ptr->value.type->id == TypeTableEntryIdPointer);
8574 if (ptr->value.type->data.pointer.is_const && !store_ptr_instruction->base.is_gen) {
8575 ir_add_error(ira, &store_ptr_instruction->base, buf_sprintf("cannot assign to constant"));
8576 return ira->codegen->builtin_types.entry_invalid;
8577 }
8578
85528579 TypeTableEntry *child_type = ptr->value.type->data.pointer.child_type;
85538580 IrInstruction *casted_value = ir_implicit_cast(ira, value, child_type);
85548581 if (casted_value == ira->codegen->invalid_instruction)
......@@ -8580,7 +8607,7 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru
85808607 IrInstructionVarPtr *var_ptr_inst = (IrInstructionVarPtr *)ptr;
85818608 VariableTableEntry *var = var_ptr_inst->var;
85828609 new_ptr_inst = ir_build_var_ptr(&ira->new_irb, store_ptr_instruction->base.scope,
8583 store_ptr_instruction->base.source_node, var);
8610 store_ptr_instruction->base.source_node, var, false);
85848611 assert(var->mem_slot_index != SIZE_MAX);
85858612 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
85868613 mem_slot->special = ConstValSpecialRuntime;
std/cstr.zig+2-2
......@@ -39,7 +39,7 @@ pub const CBuf = struct {
3939
4040 /// Must deinitialize with deinit.
4141 pub fn initEmpty(allocator: &Allocator) -> %CBuf {
42 const self = CBuf {
42 var self = CBuf {
4343 .list = List(u8).init(allocator),
4444 };
4545 %return self.resize(0);
......@@ -48,7 +48,7 @@ pub const CBuf = struct {
4848
4949 /// Must deinitialize with deinit.
5050 pub fn initFromMem(allocator: &Allocator, m: []const u8) -> %CBuf {
51 const self = CBuf {
51 var self = CBuf {
5252 .list = List(u8).init(allocator),
5353 };
5454 %return self.resize(m.len);
std/list.zig+1-1
......@@ -23,7 +23,7 @@ pub fn List(inline T: type) -> type{
2323 l.allocator.free(T, l.items);
2424 }
2525
26 pub fn toSlice(l: &Self) -> []T {
26 pub fn toSlice(l: &const Self) -> []const T {
2727 return l.items[0...l.len];
2828 }
2929
test/cases/enum_with_members.zig+1-1
......@@ -6,7 +6,7 @@ const ET = enum {
66 SINT: i32,
77 UINT: u32,
88
9 pub fn print(a: &ET, buf: []u8) -> %usize {
9 pub fn print(a: &const ET, buf: []u8) -> %usize {
1010 return switch (*a) {
1111 ET.SINT => |x| { io.bufPrintInt(i32, buf, x) },
1212 ET.UINT => |x| { io.bufPrintInt(u32, buf, x) },
test/cases/misc.zig+7
......@@ -490,3 +490,10 @@ const test_pointer_to_void_return_type_x = void{};
490490fn testPointerToVoidReturnType2() -> &const void {
491491 return &test_pointer_to_void_return_type_x;
492492}
493
494
495fn nonConstPtrToAliasedType() {
496 @setFnTest(this);
497 const int = i32;
498 assert(?&int == ?&i32);
499}
test/cases/struct.zig+2-2
......@@ -155,7 +155,7 @@ fn memberFunctions() {
155155}
156156const MemberFnRand = struct {
157157 seed: u32,
158 pub fn getSeed(r: MemberFnRand) -> u32 {
158 pub fn getSeed(r: &const MemberFnRand) -> u32 {
159159 r.seed
160160 }
161161};
......@@ -184,7 +184,7 @@ fn emptyStructMethodCall() {
184184 assert(es.method() == 1234);
185185}
186186const EmptyStruct = struct {
187 fn method(es: EmptyStruct) -> i32 {
187 fn method(es: &const EmptyStruct) -> i32 {
188188 1234
189189 }
190190};
test/run_tests.cpp+5-5
......@@ -548,7 +548,7 @@ const B = struct {
548548const C = struct {
549549 x: i32,
550550
551 fn d(c: C) {
551 fn d(c: &const C) {
552552 %%io.stdout.printf("OK\n");
553553 }
554554};
......@@ -577,13 +577,13 @@ const io = @import("std").io;
577577const Foo = struct {
578578 field1: Bar,
579579
580 fn method(a: &Foo) -> bool { true }
580 fn method(a: &const Foo) -> bool { true }
581581};
582582
583583const Bar = struct {
584584 field2: i32,
585585
586 fn method(b: &Bar) -> bool { true }
586 fn method(b: &const Bar) -> bool { true }
587587};
588588
589589pub fn main(args: [][]u8) -> %void {
......@@ -787,14 +787,14 @@ fn f(a : unreachable) {}
787787fn f() {
788788 3 = 3;
789789}
790 )SOURCE", 1, ".tmp_source.zig:3:5: error: invalid assignment target");
790 )SOURCE", 1, ".tmp_source.zig:3:7: error: cannot assign to constant");
791791
792792 add_compile_fail_case("assign to constant variable", R"SOURCE(
793793fn f() {
794794 const a = 3;
795795 a = 4;
796796}
797 )SOURCE", 1, ".tmp_source.zig:4:5: error: cannot assign to constant");
797 )SOURCE", 1, ".tmp_source.zig:4:7: error: cannot assign to constant");
798798
799799 add_compile_fail_case("use of undeclared identifier", R"SOURCE(
800800fn f() {