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 {...@@ -1240,6 +1240,12 @@ enum ZigTypeId {
1240 ZigTypeIdVector,1240 ZigTypeIdVector,
1241};1241};
12421242
1243enum OnePossibleValue {
1244 OnePossibleValueInvalid,
1245 OnePossibleValueNo,
1246 OnePossibleValueYes,
1247};
1248
1243struct ZigType {1249struct ZigType {
1244 ZigTypeId id;1250 ZigTypeId id;
1245 Buf name;1251 Buf name;
...@@ -1247,9 +1253,6 @@ struct ZigType {...@@ -1247,9 +1253,6 @@ struct ZigType {
1247 LLVMTypeRef type_ref;1253 LLVMTypeRef type_ref;
1248 ZigLLVMDIType *di_type;1254 ZigLLVMDIType *di_type;
12491255
1250 bool zero_bits; // this is denormalized data
1251 bool gen_h_loop_flag;
1252
1253 union {1256 union {
1254 ZigTypePointer pointer;1257 ZigTypePointer pointer;
1255 ZigTypeInt integral;1258 ZigTypeInt integral;
...@@ -1275,6 +1278,11 @@ struct ZigType {...@@ -1275,6 +1278,11 @@ struct ZigType {
1275 // If we generate a constant name value for this type, we memoize it here.1278 // If we generate a constant name value for this type, we memoize it here.
1276 // The type of this is array1279 // The type of this is array
1277 ConstExprValue *cached_const_name_val;1280 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;
1278};1286};
12791287
1280struct PackageTableEntry {1288struct PackageTableEntry {
src/analyze.cpp+12-2
...@@ -5129,6 +5129,10 @@ bool type_has_bits(ZigType *type_entry) {...@@ -5129,6 +5129,10 @@ bool type_has_bits(ZigType *type_entry) {
5129// Whether you can infer the value based solely on the type.5129// Whether you can infer the value based solely on the type.
5130OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {5130OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
5131 assert(type_entry != nullptr);5131 assert(type_entry != nullptr);
5132
5133 if (type_entry->one_possible_value != OnePossibleValueInvalid)
5134 return type_entry->one_possible_value;
5135
5132 Error err;5136 Error err;
5133 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))5137 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
5134 return OnePossibleValueInvalid;5138 return OnePossibleValueInvalid;
...@@ -5176,8 +5180,14 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {...@@ -5176,8 +5180,14 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) {
5176 case ZigTypeIdInt:5180 case ZigTypeIdInt:
5177 case ZigTypeIdVector:5181 case ZigTypeIdVector:
5178 return type_has_bits(type_entry) ? OnePossibleValueNo : OnePossibleValueYes;5182 return type_has_bits(type_entry) ? OnePossibleValueNo : OnePossibleValueYes;
5179 case ZigTypeIdPointer:5183 case ZigTypeIdPointer: {
5180 return type_has_one_possible_value(g, type_entry->data.pointer.child_type);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 }
5181 case ZigTypeIdUnion:5191 case ZigTypeIdUnion:
5182 if (type_entry->data.unionation.src_field_count > 1)5192 if (type_entry->data.unionation.src_field_count > 1)
5183 return OnePossibleValueNo;5193 return OnePossibleValueNo;
src/analyze.hpp-5
...@@ -226,11 +226,6 @@ enum ReqCompTime {...@@ -226,11 +226,6 @@ enum ReqCompTime {
226};226};
227ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry);227ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry);
228228
229enum OnePossibleValue {
230 OnePossibleValueInvalid,
231 OnePossibleValueNo,
232 OnePossibleValueYes,
233};
234OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry);229OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry);
235230
236Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,231Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
test/stage1/behavior.zig+1
...@@ -19,6 +19,7 @@ comptime {...@@ -19,6 +19,7 @@ comptime {
19 _ = @import("behavior/bugs/1442.zig");19 _ = @import("behavior/bugs/1442.zig");
20 _ = @import("behavior/bugs/1486.zig");20 _ = @import("behavior/bugs/1486.zig");
21 _ = @import("behavior/bugs/1851.zig");21 _ = @import("behavior/bugs/1851.zig");
22 _ = @import("behavior/bugs/2006.zig");
22 _ = @import("behavior/bugs/394.zig");23 _ = @import("behavior/bugs/394.zig");
23 _ = @import("behavior/bugs/421.zig");24 _ = @import("behavior/bugs/421.zig");
24 _ = @import("behavior/bugs/655.zig");25 _ = @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}