authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-19 15:35:00-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-19 15:35:00-05:00
log067968c57f2c7ed4f0606913aa608f6c2be418d2
tree72872a0203c6cf61bf4a62dcb6950d393f73aa92
parent400006bbe790f2173fd6e40d80608691a95b437e
parentc8ce351ec982608d3ea0c60a34c7b18d894dee01
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'matthew-mcallister-slice-deref-failure'


3 files changed, 56 insertions(+), 33 deletions(-)

src/all_types.hpp+6-5
......@@ -2105,6 +2105,11 @@ struct IrBasicBlock {
21052105 IrInstruction *must_be_comptime_source_instr;
21062106};
21072107
2108enum LVal {
2109 LValNone,
2110 LValPtr,
2111};
2112
21082113// These instructions are in transition to having "pass 1" instructions
21092114// and "pass 2" instructions. The pass 1 instructions are suffixed with Src
21102115// and pass 2 are suffixed with Gen.
......@@ -2368,6 +2373,7 @@ struct IrInstructionUnOp {
23682373
23692374 IrUnOp op_id;
23702375 IrInstruction *value;
2376 LVal lval;
23712377};
23722378
23732379enum IrBinOp {
......@@ -3104,11 +3110,6 @@ struct IrInstructionTypeName {
31043110 IrInstruction *type_value;
31053111};
31063112
3107enum LVal {
3108 LValNone,
3109 LValPtr,
3110};
3111
31123113struct IrInstructionDeclRef {
31133114 IrInstruction base;
31143115
src/ir.cpp+29-25
......@@ -1303,14 +1303,23 @@ static IrInstruction *ir_build_ptr_type(IrBuilder *irb, Scope *scope, AstNode *s
13031303 return &ptr_type_of_instruction->base;
13041304}
13051305
1306static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *source_node, IrUnOp op_id, IrInstruction *value) {
1307 IrInstructionUnOp *br_instruction = ir_build_instruction<IrInstructionUnOp>(irb, scope, source_node);
1308 br_instruction->op_id = op_id;
1309 br_instruction->value = value;
1306static IrInstruction *ir_build_un_op_lval(IrBuilder *irb, Scope *scope, AstNode *source_node, IrUnOp op_id,
1307 IrInstruction *value, LVal lval)
1308{
1309 IrInstructionUnOp *instruction = ir_build_instruction<IrInstructionUnOp>(irb, scope, source_node);
1310 instruction->op_id = op_id;
1311 instruction->value = value;
1312 instruction->lval = lval;
13101313
13111314 ir_ref_instruction(value, irb->current_basic_block);
13121315
1313 return &br_instruction->base;
1316 return &instruction->base;
1317}
1318
1319static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *source_node, IrUnOp op_id,
1320 IrInstruction *value)
1321{
1322 return ir_build_un_op_lval(irb, scope, source_node, op_id, value, LValNone);
13141323}
13151324
13161325static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node,
......@@ -7223,7 +7232,10 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
72237232 if (value == irb->codegen->invalid_instruction)
72247233 return value;
72257234
7226 return ir_build_un_op(irb, scope, node, IrUnOpDereference, value);
7235 // We essentially just converted any lvalue from &(x.*) to (&x).*;
7236 // this inhibits checking that x is a pointer later, so we directly
7237 // record whether the pointer check is needed
7238 return ir_build_un_op_lval(irb, scope, node, IrUnOpDereference, value, lval);
72277239 }
72287240 case NodeTypeUnwrapOptional: {
72297241 AstNode *expr_node = node->data.unwrap_optional.expr;
......@@ -11463,7 +11475,7 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
1146311475 return load_ptr_instruction;
1146411476 } else {
1146511477 ir_add_error_node(ira, source_instruction->source_node,
11466 buf_sprintf("attempt to dereference non pointer type '%s'",
11478 buf_sprintf("attempt to dereference non-pointer type '%s'",
1146711479 buf_ptr(&type_entry->name)));
1146811480 return ira->codegen->invalid_instruction;
1146911481 }
......@@ -13678,11 +13690,7 @@ no_mem_slot:
1367813690static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source_instr,
1367913691 IrInstruction *ptr, IrInstruction *uncasted_value)
1368013692{
13681 if (ptr->value.type->id != ZigTypeIdPointer) {
13682 ir_add_error(ira, ptr,
13683 buf_sprintf("attempt to dereference non pointer type '%s'", buf_ptr(&ptr->value.type->name)));
13684 return ira->codegen->invalid_instruction;
13685 }
13693 assert(ptr->value.type->id == ZigTypeIdPointer);
1368613694
1368713695 if (ptr->value.data.x_ptr.special == ConstPtrSpecialDiscard) {
1368813696 return ir_const_void(ira, source_instr);
......@@ -14612,11 +14620,18 @@ static IrInstruction *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstruction
1461214620 buf_ptr(&ptr_type->name)));
1461314621 return ira->codegen->invalid_instruction;
1461414622 }
14615 // this dereference is always an rvalue because in the IR gen we identify lvalue and emit
14616 // one of the ptr instructions
14623
1461714624 IrInstruction *result = ir_get_deref(ira, &instruction->base, ptr);
1461814625 if (result == ira->codegen->invalid_instruction)
1461914626 return ira->codegen->invalid_instruction;
14627
14628 // If the result needs to be an lvalue, type check it
14629 if (instruction->lval == LValPtr && result->value.type->id != ZigTypeIdPointer) {
14630 ir_add_error(ira, &instruction->base,
14631 buf_sprintf("attempt to dereference non-pointer type '%s'", buf_ptr(&result->value.type->name)));
14632 return ira->codegen->invalid_instruction;
14633 }
14634
1462014635 return result;
1462114636 }
1462214637 case IrUnOpOptional:
......@@ -15442,12 +15457,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1544215457 if (type_is_invalid(container_ptr->value.type))
1544315458 return ira->codegen->invalid_instruction;
1544415459
15445 if (container_ptr->value.type->id != ZigTypeIdPointer) {
15446 ir_add_error_node(ira, field_ptr_instruction->base.source_node,
15447 buf_sprintf("attempt to dereference non-pointer type '%s'",
15448 buf_ptr(&container_ptr->value.type->name)));
15449 return ira->codegen->invalid_instruction;
15450 }
1545115460 ZigType *container_type = container_ptr->value.type->data.pointer.child_type;
1545215461
1545315462 Buf *field_name = field_ptr_instruction->field_name_buffer;
......@@ -16658,11 +16667,6 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1665816667 return ir_const_type(ira, &switch_target_instruction->base, ptr_type->data.pointer.child_type);
1665916668 }
1666016669
16661 if (target_value_ptr->value.type->id != ZigTypeIdPointer) {
16662 ir_add_error(ira, target_value_ptr, buf_sprintf("invalid deref on switch target"));
16663 return ira->codegen->invalid_instruction;
16664 }
16665
1666616670 ZigType *target_type = target_value_ptr->value.type->data.pointer.child_type;
1666716671 ConstExprValue *pointee_val = nullptr;
1666816672 if (instr_is_comptime(target_value_ptr)) {
test/compile_errors.zig+21-3
......@@ -164,6 +164,24 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
164164 break :x tc;
165165 });
166166
167 cases.addTest(
168 "assign to invalid dereference",
169 \\export fn entry() void {
170 \\ 'a'.* = 1;
171 \\}
172 ,
173 ".tmp_source.zig:2:8: error: attempt to dereference non-pointer type 'comptime_int'",
174 );
175
176 cases.addTest(
177 "take slice of invalid dereference",
178 \\export fn entry() void {
179 \\ const x = 'a'.*[0..];
180 \\}
181 ,
182 ".tmp_source.zig:2:18: error: attempt to dereference non-pointer type 'comptime_int'",
183 );
184
167185 cases.addTest(
168186 "@truncate undefined value",
169187 \\export fn entry() void {
......@@ -474,7 +492,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
474492 \\ _ = a.*.len;
475493 \\}
476494 ,
477 ".tmp_source.zig:3:12: error: attempt to dereference non-pointer type '[]u8'",
495 ".tmp_source.zig:3:10: error: attempt to dereference non-pointer type '[]u8'",
478496 );
479497
480498 cases.add(
......@@ -1185,7 +1203,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
11851203 \\ Filled,
11861204 \\};
11871205 ,
1188 ".tmp_source.zig:3:17: error: invalid deref on switch target",
1206 ".tmp_source.zig:3:17: error: attempt to dereference non-pointer type 'Tile'",
11891207 );
11901208
11911209 cases.add(
......@@ -4027,7 +4045,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
40274045 \\
40284046 \\export fn entry() usize { return @sizeOf(@typeOf(pass)); }
40294047 ,
4030 ".tmp_source.zig:4:10: error: attempt to dereference non pointer type '[10]u8'",
4048 ".tmp_source.zig:4:10: error: attempt to dereference non-pointer type '[10]u8'",
40314049 );
40324050
40334051 cases.add(