aboutsummaryrefslogtreecommitdiff
path: root/src/ir.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-02-19 15:27:10 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-02-19 15:27:10 -0500
commit1034af40f96ef6c926f8a4e41bee19c59b311ed7 (patch)
tree45fbc3a0db27f3c8cf990e58e770d19e0c30f3c4 /src/ir.cpp
parent400006bbe790f2173fd6e40d80608691a95b437e (diff)
parent91989e70ba68e3543acffef079d97c9416b5259c (diff)
downloadzig-1034af40f96ef6c926f8a4e41bee19c59b311ed7.tar.gz
zig-1034af40f96ef6c926f8a4e41bee19c59b311ed7.zip
Merge branch 'slice-deref-failure' of https://github.com/matthew-mcallister/zig into matthew-mcallister-slice-deref-failure
Diffstat (limited to 'src/ir.cpp')
-rw-r--r--src/ir.cpp39
1 files changed, 18 insertions, 21 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index 5c5893cdd5..e5f09dd93a 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -1307,6 +1307,7 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour
IrInstructionUnOp *br_instruction = ir_build_instruction<IrInstructionUnOp>(irb, scope, source_node);
br_instruction->op_id = op_id;
br_instruction->value = value;
+ br_instruction->lval = LValNone;
ir_ref_instruction(value, irb->current_basic_block);
@@ -7223,7 +7224,13 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
if (value == irb->codegen->invalid_instruction)
return value;
- return ir_build_un_op(irb, scope, node, IrUnOpDereference, value);
+ // We essentially just converted any lvalue from &(x.*) to (&x).*;
+ // this inhibits checking that x is a pointer later, so we directly
+ // record whether the pointer check is needed
+ IrInstructionUnOp *result = (IrInstructionUnOp*)ir_build_un_op(irb, scope, node, IrUnOpDereference, value);
+ result->lval = lval;
+
+ return &result->base;
}
case NodeTypeUnwrapOptional: {
AstNode *expr_node = node->data.unwrap_optional.expr;
@@ -11463,7 +11470,7 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
return load_ptr_instruction;
} else {
ir_add_error_node(ira, source_instruction->source_node,
- buf_sprintf("attempt to dereference non pointer type '%s'",
+ buf_sprintf("attempt to dereference non-pointer type '%s'",
buf_ptr(&type_entry->name)));
return ira->codegen->invalid_instruction;
}
@@ -13678,12 +13685,6 @@ no_mem_slot:
static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source_instr,
IrInstruction *ptr, IrInstruction *uncasted_value)
{
- if (ptr->value.type->id != ZigTypeIdPointer) {
- ir_add_error(ira, ptr,
- buf_sprintf("attempt to dereference non pointer type '%s'", buf_ptr(&ptr->value.type->name)));
- return ira->codegen->invalid_instruction;
- }
-
if (ptr->value.data.x_ptr.special == ConstPtrSpecialDiscard) {
return ir_const_void(ira, source_instr);
}
@@ -14612,11 +14613,18 @@ static IrInstruction *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstruction
buf_ptr(&ptr_type->name)));
return ira->codegen->invalid_instruction;
}
- // this dereference is always an rvalue because in the IR gen we identify lvalue and emit
- // one of the ptr instructions
+
IrInstruction *result = ir_get_deref(ira, &instruction->base, ptr);
if (result == ira->codegen->invalid_instruction)
return ira->codegen->invalid_instruction;
+
+ // If the result needs to be an lvalue, type check it
+ if (instruction->lval == LValPtr && result->value.type->id != ZigTypeIdPointer) {
+ ir_add_error(ira, &instruction->base,
+ buf_sprintf("attempt to dereference non-pointer type '%s'", buf_ptr(&result->value.type->name)));
+ return ira->codegen->invalid_instruction;
+ }
+
return result;
}
case IrUnOpOptional:
@@ -15442,12 +15450,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
if (type_is_invalid(container_ptr->value.type))
return ira->codegen->invalid_instruction;
- if (container_ptr->value.type->id != ZigTypeIdPointer) {
- ir_add_error_node(ira, field_ptr_instruction->base.source_node,
- buf_sprintf("attempt to dereference non-pointer type '%s'",
- buf_ptr(&container_ptr->value.type->name)));
- return ira->codegen->invalid_instruction;
- }
ZigType *container_type = container_ptr->value.type->data.pointer.child_type;
Buf *field_name = field_ptr_instruction->field_name_buffer;
@@ -16658,11 +16660,6 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
return ir_const_type(ira, &switch_target_instruction->base, ptr_type->data.pointer.child_type);
}
- if (target_value_ptr->value.type->id != ZigTypeIdPointer) {
- ir_add_error(ira, target_value_ptr, buf_sprintf("invalid deref on switch target"));
- return ira->codegen->invalid_instruction;
- }
-
ZigType *target_type = target_value_ptr->value.type->data.pointer.child_type;
ConstExprValue *pointee_val = nullptr;
if (instr_is_comptime(target_value_ptr)) {