authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-28 11:39:36-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-28 11:39:36-05:00
log287d3c37e1f53b6f0a0b29753b68254001194e62
tree3d9a4db2edfc3d2ec1df5c9856b5d1ddf2d562ac
parent8710fdbf39c793027f332bd00efe70edaa86d91c
signaturelock-open Commit is signed but in an unrecognized format.

fix 0-bit child type coerced to optional return ptr result location

by un-special-casing 0 bit types in result locations

3 files changed, 55 insertions(+), 32 deletions(-)

src/analyze.cpp+19-1
......@@ -5700,6 +5700,9 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {
57005700 assert(field_type != nullptr);
57015701 result->data.x_struct.fields[i] = get_the_one_possible_value(g, field_type);
57025702 }
5703 } else if (result->type->id == ZigTypeIdPointer) {
5704 result->data.x_ptr.special = ConstPtrSpecialRef;
5705 result->data.x_ptr.data.ref.pointee = get_the_one_possible_value(g, result->type->data.pointer.child_type);
57035706 }
57045707 g->one_possible_values.put(type_entry, result);
57055708 return result;
......@@ -9471,7 +9474,11 @@ static void dump_value_indent(ZigValue *val, int indent) {
94719474 fprintf(stderr, " ");
94729475 }
94739476 fprintf(stderr, "%s: ", buf_ptr(val->type->data.structure.fields[i]->name));
9474 dump_value_indent(val->data.x_struct.fields[i], 1);
9477 if (val->data.x_struct.fields == nullptr) {
9478 fprintf(stderr, "<null>\n");
9479 } else {
9480 dump_value_indent(val->data.x_struct.fields[i], 1);
9481 }
94759482 }
94769483 for (int i = 0; i < indent; i += 1) {
94779484 fprintf(stderr, " ");
......@@ -9505,6 +9512,9 @@ static void dump_value_indent(ZigValue *val, int indent) {
95059512
95069513 case ZigTypeIdPointer:
95079514 switch (val->data.x_ptr.special) {
9515 case ConstPtrSpecialInvalid:
9516 fprintf(stderr, "<!invalid ptr!>\n");
9517 return;
95089518 case ConstPtrSpecialRef:
95099519 fprintf(stderr, "<ref\n");
95109520 dump_value_indent(val->data.x_ptr.data.ref.pointee, indent + 1);
......@@ -9526,6 +9536,14 @@ static void dump_value_indent(ZigValue *val, int indent) {
95269536 }
95279537 break;
95289538 }
9539 case ConstPtrSpecialBaseOptionalPayload: {
9540 ZigValue *optional_val = val->data.x_ptr.data.base_optional_payload.optional_val;
9541 fprintf(stderr, "<optional %p payload\n", optional_val);
9542 if (optional_val != nullptr) {
9543 dump_value_indent(optional_val, indent + 1);
9544 }
9545 break;
9546 }
95299547 default:
95309548 fprintf(stderr, "TODO dump more pointer things\n");
95319549 }
src/ir.cpp+14-31
......@@ -18576,7 +18576,6 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
1857618576 ResultLoc *result_loc_pass1, ZigType *value_type, IrInstGen *value, bool force_runtime,
1857718577 bool allow_discard)
1857818578{
18579 Error err;
1858018579 if (!allow_discard && result_loc_is_discard(result_loc_pass1)) {
1858118580 result_loc_pass1 = no_result_loc();
1858218581 }
......@@ -18672,32 +18671,22 @@ static IrInstGen *ir_resolve_result(IrAnalyze *ira, IrInst *suspend_source_instr
1867218671 {
1867318672 bool same_comptime_repr = types_have_same_zig_comptime_repr(ira->codegen, actual_elem_type, value_type);
1867418673 if (!same_comptime_repr) {
18675 bool has_bits;
18676 if ((err = type_has_bits2(ira->codegen, value_type, &has_bits)))
18677 return ira->codegen->invalid_inst_gen;
18678 if (has_bits) {
18679 result_loc_pass1->written = false;
18680 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);
18681 }
18674 result_loc_pass1->written = false;
18675 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);
1868218676 }
1868318677 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion) {
18684 bool has_bits;
18685 if ((err = type_has_bits2(ira->codegen, value_type, &has_bits)))
18686 return ira->codegen->invalid_inst_gen;
18687 if (has_bits) {
18688 if (value_type->id == ZigTypeIdErrorSet) {
18689 return ir_analyze_unwrap_err_code(ira, suspend_source_instr, result_loc, true);
18678 if (value_type->id == ZigTypeIdErrorSet) {
18679 return ir_analyze_unwrap_err_code(ira, suspend_source_instr, result_loc, true);
18680 } else {
18681 IrInstGen *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr,
18682 result_loc, false, true);
18683 ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type;
18684 if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
18685 value_type->id != ZigTypeIdNull)
18686 {
18687 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true);
1869018688 } else {
18691 IrInstGen *unwrapped_err_ptr = ir_analyze_unwrap_error_payload(ira, suspend_source_instr,
18692 result_loc, false, true);
18693 ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type;
18694 if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
18695 value_type->id != ZigTypeIdNull)
18696 {
18697 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true);
18698 } else {
18699 return unwrapped_err_ptr;
18700 }
18689 return unwrapped_err_ptr;
1870118690 }
1870218691 }
1870318692 }
......@@ -22215,14 +22204,8 @@ static IrInstGen *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInst* sou
2221522204 }
2221622205 break;
2221722206 case OnePossibleValueYes: {
22218 ZigValue *pointee = create_const_vals(1);
22219 pointee->special = ConstValSpecialStatic;
22220 pointee->type = child_type;
22221 pointee->parent.id = ConstParentIdOptionalPayload;
22222 pointee->parent.data.p_optional_payload.optional_val = optional_val;
22223
2222422207 optional_val->special = ConstValSpecialStatic;
22225 optional_val->data.x_optional = pointee;
22208 optional_val->data.x_optional = get_the_one_possible_value(ira->codegen, child_type);
2222622209 break;
2222722210 }
2222822211 }
test/stage1/behavior/optional.zig+22
......@@ -153,3 +153,25 @@ test "optional with void type" {
153153 var x = Foo{ .x = null };
154154 expect(x.x == null);
155155}
156
157test "0-bit child type coerced to optional return ptr result location" {
158 const S = struct {
159 fn doTheTest() void {
160 var y = Foo{};
161 var z = y.thing();
162 expect(z != null);
163 }
164
165 const Foo = struct {
166 pub const Bar = struct {
167 field: *Foo,
168 };
169
170 pub fn thing(self: *Foo) ?Bar {
171 return Bar{ .field = self };
172 }
173 };
174 };
175 S.doTheTest();
176 comptime S.doTheTest();
177}