Merge branch 'master' of github.com:ziglang/zig

This commit is contained in:
Jimmi Holst Christensen 2019-04-07 03:19:08 +02:00
commit 7c38651a65
4 changed files with 19 additions and 19 deletions

View File

@ -2621,7 +2621,7 @@ struct IrInstructionTypeOf {
struct IrInstructionToPtrType { struct IrInstructionToPtrType {
IrInstruction base; IrInstruction base;
IrInstruction *value; IrInstruction *ptr;
}; };
struct IrInstructionPtrTypeChild { struct IrInstructionPtrTypeChild {

View File

@ -1551,11 +1551,11 @@ static IrInstruction *ir_build_typeof(IrBuilder *irb, Scope *scope, AstNode *sou
return &instruction->base; 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<IrInstructionToPtrType>(irb, scope, source_node); IrInstructionToPtrType *instruction = ir_build_instruction<IrInstructionToPtrType>(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; 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) if (array_val_ptr == irb->codegen->invalid_instruction)
return array_val_ptr; 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_ptr);
IrInstruction *pointer_type = ir_build_to_ptr_type(irb, parent_scope, array_node, array_val);
IrInstruction *elem_var_type; IrInstruction *elem_var_type;
if (node->data.for_expr.elem_is_ptr) { if (node->data.for_expr.elem_is_ptr) {
elem_var_type = pointer_type; elem_var_type = pointer_type;
@ -16301,18 +16299,17 @@ static IrInstruction *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira,
IrInstructionToPtrType *to_ptr_type_instruction) IrInstructionToPtrType *to_ptr_type_instruction)
{ {
Error err; Error err;
IrInstruction *ptr_ptr = to_ptr_type_instruction->ptr->child;
IrInstruction *value = to_ptr_type_instruction->value->child; if (type_is_invalid(ptr_ptr->value.type))
ZigType *type_entry = value->value.type;
if (type_is_invalid(type_entry))
return ira->codegen->invalid_instruction; 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; ZigType *ptr_type;
if (type_entry->id == ZigTypeIdArray) { if (type_entry->id == ZigTypeIdArray) {
// TODO: Allow capturing pointer to const array. ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.array.child_type, ptr_ptr_type->data.pointer.is_const);
// 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);
} else if (is_array_ref(type_entry)) { } else if (is_array_ref(type_entry)) {
ptr_type = get_pointer_to_type(ira->codegen, ptr_type = get_pointer_to_type(ira->codegen,
type_entry->data.pointer.child_type->data.array.child_type, type_entry->data.pointer.is_const); 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); ptr_type = adjust_ptr_align(ira->codegen, ptr_type, reduced_align);
} }
} else if (type_entry->id == ZigTypeIdArgTuple) { } 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"); zig_panic("TODO for loop on var args");
} else { } else {
ir_add_error_node(ira, to_ptr_type_instruction->base.source_node, ir_add_error_node(ira, to_ptr_type_instruction->base.source_node,

View File

@ -356,7 +356,7 @@ static void ir_print_typeof(IrPrint *irp, IrInstructionTypeOf *instruction) {
static void ir_print_to_ptr_type(IrPrint *irp, IrInstructionToPtrType *instruction) { static void ir_print_to_ptr_type(IrPrint *irp, IrInstructionToPtrType *instruction) {
fprintf(irp->f, "@toPtrType("); fprintf(irp->f, "@toPtrType(");
ir_print_other_instruction(irp, instruction->value); ir_print_other_instruction(irp, instruction->ptr);
fprintf(irp->f, ")"); fprintf(irp->f, ")");
} }

View File

@ -27,7 +27,13 @@ test "for loop with pointer elem var" {
mem.copy(u8, target[0..], source); mem.copy(u8, target[0..], source);
mangleString(target[0..]); mangleString(target[0..]);
expect(mem.eql(u8, target, "bcdefgh")); 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 { fn mangleString(s: []u8) void {
for (s) |*c| { for (s) |*c| {
c.* += 1; c.* += 1;