From 3109c898505820fb8cc89f87759292a9aebe3db1 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Sat, 6 Apr 2019 10:56:07 +0200 Subject: fixed 1726 --- src/ir.cpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index b602df29ad..102ccc6f4a 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1551,11 +1551,11 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou return &instruction->base; } -static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) { +static IrInstruction *ir_build_to_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *ptr) { IrInstructionToPtrType *instruction = ir_build_instruction(irb, scope, source_node); - instruction->value = value; + instruction->ptr = ptr; - ir_ref_instruction(value, irb->current_basic_block); + ir_ref_instruction(ptr, irb->current_basic_block); return &instruction->base; } @@ -5689,9 +5689,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo if (array_val_ptr == irb->codegen->invalid_instruction) return array_val_ptr; - IrInstruction *array_val = ir_build_load_ptr(irb, parent_scope, array_node, array_val_ptr); - - IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_val); + IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_val_ptr); IrInstruction *elem_var_type; if (node->data.for_expr.elem_is_ptr) { elem_var_type = pointer_type; @@ -16301,18 +16299,20 @@ static IrInstruction *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira, IrInstructionToPtrType *to_ptr_type_instruction) { Error err; - - IrInstruction *value = to_ptr_type_instruction->value->child; - ZigType *type_entry = value->value.type; - if (type_is_invalid(type_entry)) + IrInstruction *ptr_ptr = to_ptr_type_instruction->ptr->child; + if (type_is_invalid(ptr_ptr->value.type)) return ira->codegen->invalid_instruction; + ZigType *ptr_ptr_type = ptr_ptr->value.type; + assert(ptr_ptr_type->id == ZigTypeIdPointer); + ZigType *type_entry = ptr_ptr_type->data.pointer.child_type; + ZigType *ptr_type; if (type_entry->id == ZigTypeIdArray) { // TODO: Allow capturing pointer to const array. // const a = "123"; for (a) |*c| continue; // error: expected type '*u8', found '*const u8' - ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.array.child_type, false); + ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.array.child_type, ptr_ptr_type->data.pointer.is_const); } else if (is_array_ref(type_entry)) { ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.pointer.child_type->data.array.child_type, type_entry->data.pointer.is_const); @@ -16329,9 +16329,6 @@ static IrInstruction *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira, ptr_type = adjust_ptr_align(ira->codegen, ptr_type, reduced_align); } } else if (type_entry->id == ZigTypeIdArgTuple) { - ConstExprValue *arg_tuple_val = ir_resolve_const(ira, value, UndefBad); - if (!arg_tuple_val) - return ira->codegen->invalid_instruction; zig_panic("TODO for loop on var args"); } else { ir_add_error_node(ira, to_ptr_type_instruction->base.source_node, -- cgit v1.2.3 From 65cf5a8e44f299bb24e139cc25df717f1ddba455 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Sat, 6 Apr 2019 11:04:55 +0200 Subject: removed todo comment and added test --- src/ir.cpp | 3 --- test/stage1/behavior/for.zig | 6 ++++++ 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 102ccc6f4a..c572e3c885 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -16309,9 +16309,6 @@ static IrInstruction *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira, ZigType *ptr_type; if (type_entry->id == ZigTypeIdArray) { - // TODO: Allow capturing pointer to const array. - // const a = "123"; for (a) |*c| continue; - // error: expected type '*u8', found '*const u8' ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.array.child_type, ptr_ptr_type->data.pointer.is_const); } else if (is_array_ref(type_entry)) { ptr_type = get_pointer_to_type(ira->codegen, diff --git a/test/stage1/behavior/for.zig b/test/stage1/behavior/for.zig index b10dd14fa4..45a5334d1a 100644 --- a/test/stage1/behavior/for.zig +++ b/test/stage1/behavior/for.zig @@ -27,7 +27,13 @@ test "for loop with pointer elem var" { mem.copy(u8, target[0..], source); mangleString(target[0..]); expect(mem.eql(u8, target, "bcdefgh")); + + for (source) |*c, i| + expect(@typeOf(c) == *const u8); + for (target) |*c, i| + expect(@typeOf(c) == *u8); } + fn mangleString(s: []u8) void { for (s) |*c| { c.* += 1; -- cgit v1.2.3