diff --git a/src/all_types.hpp b/src/all_types.hpp index d5d708fd09..92faad1e03 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -2621,7 +2621,7 @@ struct IrInstructionTypeOf { struct IrInstructionToPtrType { IrInstruction base; - IrInstruction *value; + IrInstruction *ptr; }; struct IrInstructionPtrTypeChild { diff --git a/src/ir.cpp b/src/ir.cpp index b602df29ad..c572e3c885 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,17 @@ 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 +16326,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, diff --git a/src/ir_print.cpp b/src/ir_print.cpp index ab7685d758..dba0e4ee00 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -356,7 +356,7 @@ static void ir_print_typeof(IrPrint *irp, IrInstructionTypeOf *instruction) { static void ir_print_to_ptr_type(IrPrint *irp, IrInstructionToPtrType *instruction) { fprintf(irp->f, "@toPtrType("); - ir_print_other_instruction(irp, instruction->value); + ir_print_other_instruction(irp, instruction->ptr); fprintf(irp->f, ")"); } 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;