authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-25 16:28:23-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-25 16:28:23-05:00
log7b8c5578c6c174893c5dd08e89cf3b3492ae065f
treea609d434b88129b347e016b407382a2456138ea7
parent33cbb29def181ee758b1f8bb93d8d81a4e377ce1
signaturelock-open Commit is signed but in an unrecognized format.

fix infinite recursion in type_has_one_possible_value

closes #2006

5 files changed, 36 insertions(+), 10 deletions(-)

src/all_types.hpp+11-3
......@@ -1240,6 +1240,12 @@ enum ZigTypeId {
12401240 ZigTypeIdVector,
12411241};
12421242
1243enum OnePossibleValue {
1244 OnePossibleValueInvalid,
1245 OnePossibleValueNo,
1246 OnePossibleValueYes,
1247};
1248
12431249struct ZigType {
12441250 ZigTypeId id;
12451251 Buf name;
......@@ -1247,9 +1253,6 @@ struct ZigType {
12471253 LLVMTypeRef type_ref;
12481254 ZigLLVMDIType *di_type;
12491255
1250 bool zero_bits; // this is denormalized data
1251 bool gen_h_loop_flag;
1252
12531256 union {
12541257 ZigTypePointer pointer;
12551258 ZigTypeInt integral;
......@@ -1275,6 +1278,11 @@ struct ZigType {
12751278 // If we generate a constant name value for this type, we memoize it here.
12761279 // The type of this is array
12771280 ConstExprValue *cached_const_name_val;
1281
1282 OnePossibleValue one_possible_value;
1283
1284 bool zero_bits; // this is denormalized data
1285 bool gen_h_loop_flag;
12781286};
12791287
12801288struct PackageTableEntry {
src/analyze.cpp+12-2
......@@ -5129,6 +5129,10 @@ bool type_has_bits(ZigType *type_entry) {
51295129// Whether you can infer the value based solely on the type.
51305130OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
51315131 assert(type_entry != nullptr);
5132
5133 if (type_entry->one_possible_value != OnePossibleValueInvalid)
5134 return type_entry->one_possible_value;
5135
51325136 Error err;
51335137 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
51345138 return OnePossibleValueInvalid;
......@@ -5176,8 +5180,14 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
51765180 case ZigTypeIdInt:
51775181 case ZigTypeIdVector:
51785182 return type_has_bits(type_entry) ? OnePossibleValueNo : OnePossibleValueYes;
5179 case ZigTypeIdPointer:
5180 return type_has_one_possible_value(g, type_entry->data.pointer.child_type);
5183 case ZigTypeIdPointer: {
5184 ZigType *elem_type = type_entry->data.pointer.child_type;
5185 // If the recursive function call asks, then we are not one possible value.
5186 type_entry->one_possible_value = OnePossibleValueNo;
5187 // Now update it to be the value of the recursive call.
5188 type_entry->one_possible_value = type_has_one_possible_value(g, elem_type);
5189 return type_entry->one_possible_value;
5190 }
51815191 case ZigTypeIdUnion:
51825192 if (type_entry->data.unionation.src_field_count > 1)
51835193 return OnePossibleValueNo;
src/analyze.hpp-5
......@@ -226,11 +226,6 @@ enum ReqCompTime {
226226};
227227ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry);
228228
229enum OnePossibleValue {
230 OnePossibleValueInvalid,
231 OnePossibleValueNo,
232 OnePossibleValueYes,
233};
234229OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry);
235230
236231Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
test/stage1/behavior.zig+1
......@@ -19,6 +19,7 @@ comptime {
1919 _ = @import("behavior/bugs/1442.zig");
2020 _ = @import("behavior/bugs/1486.zig");
2121 _ = @import("behavior/bugs/1851.zig");
22 _ = @import("behavior/bugs/2006.zig");
2223 _ = @import("behavior/bugs/394.zig");
2324 _ = @import("behavior/bugs/421.zig");
2425 _ = @import("behavior/bugs/655.zig");
test/stage1/behavior/bugs/2006.zig created+12
......@@ -0,0 +1,12 @@
1const std = @import("std");
2const expect = std.testing.expect;
3
4const S = struct {
5 p: *S,
6};
7test "bug 2006" {
8 var a: S = undefined;
9 a = S{ .p = undefined };
10 expect(@sizeOf(S) != 0);
11 expect(@sizeOf(*void) == 0);
12}