authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-09-05 10:17:47+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-05 13:09:43-04:00
log0107b19124255179a48cd605f31ed57d5ade28e7
tree4384d9d8973a0c58e79a74c7950912493657213c
parent9a358d2d33b0ecdec38ba3698acf8b239c43b667

Resolve lazy values when checking for definedness

Fixes #3154

2 files changed, 23 insertions(+), 9 deletions(-)

src/codegen.cpp+14-9
......@@ -183,7 +183,7 @@ static void render_const_val(CodeGen *g, ConstExprValue *const_val, const char *
183183static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const char *name);
184184static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name);
185185static void generate_error_name_table(CodeGen *g);
186static bool value_is_all_undef(ConstExprValue *const_val);
186static bool value_is_all_undef(CodeGen *g, ConstExprValue *const_val);
187187static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_type, LLVMValueRef ptr);
188188static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *name, uint32_t alignment);
189189
......@@ -3377,7 +3377,7 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI
33773377 return LLVMBuildTrunc(g->builder, shifted_value, get_llvm_type(g, child_type), "");
33783378}
33793379
3380static bool value_is_all_undef_array(ConstExprValue *const_val, size_t len) {
3380static bool value_is_all_undef_array(CodeGen *g, ConstExprValue *const_val, size_t len) {
33813381 switch (const_val->data.x_array.special) {
33823382 case ConstArraySpecialUndef:
33833383 return true;
......@@ -3385,7 +3385,7 @@ static bool value_is_all_undef_array(ConstExprValue *const_val, size_t len) {
33853385 return false;
33863386 case ConstArraySpecialNone:
33873387 for (size_t i = 0; i < len; i += 1) {
3388 if (!value_is_all_undef(&const_val->data.x_array.data.s_none.elements[i]))
3388 if (!value_is_all_undef(g, &const_val->data.x_array.data.s_none.elements[i]))
33893389 return false;
33903390 }
33913391 return true;
......@@ -3393,7 +3393,12 @@ static bool value_is_all_undef_array(ConstExprValue *const_val, size_t len) {
33933393 zig_unreachable();
33943394}
33953395
3396static bool value_is_all_undef(ConstExprValue *const_val) {
3396static bool value_is_all_undef(CodeGen *g, ConstExprValue *const_val) {
3397 Error err;
3398 if (const_val->special == ConstValSpecialLazy &&
3399 (err = ir_resolve_lazy(g, nullptr, const_val)))
3400 report_errors_and_exit(g);
3401
33973402 switch (const_val->special) {
33983403 case ConstValSpecialLazy:
33993404 zig_unreachable();
......@@ -3404,14 +3409,14 @@ static bool value_is_all_undef(ConstExprValue *const_val) {
34043409 case ConstValSpecialStatic:
34053410 if (const_val->type->id == ZigTypeIdStruct) {
34063411 for (size_t i = 0; i < const_val->type->data.structure.src_field_count; i += 1) {
3407 if (!value_is_all_undef(&const_val->data.x_struct.fields[i]))
3412 if (!value_is_all_undef(g, &const_val->data.x_struct.fields[i]))
34083413 return false;
34093414 }
34103415 return true;
34113416 } else if (const_val->type->id == ZigTypeIdArray) {
3412 return value_is_all_undef_array(const_val, const_val->type->data.array.len);
3417 return value_is_all_undef_array(g, const_val, const_val->type->data.array.len);
34133418 } else if (const_val->type->id == ZigTypeIdVector) {
3414 return value_is_all_undef_array(const_val, const_val->type->data.vector.len);
3419 return value_is_all_undef_array(g, const_val, const_val->type->data.vector.len);
34153420 } else {
34163421 return false;
34173422 }
......@@ -3532,7 +3537,7 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir
35323537 return nullptr;
35333538 }
35343539
3535 bool have_init_expr = !value_is_all_undef(&instruction->value->value);
3540 bool have_init_expr = !value_is_all_undef(g, &instruction->value->value);
35363541 if (have_init_expr) {
35373542 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
35383543 LLVMValueRef value = ir_llvm_value(g, instruction->value);
......@@ -4887,7 +4892,7 @@ static LLVMValueRef ir_render_memset(CodeGen *g, IrExecutable *executable, IrIns
48874892 ZigType *ptr_type = instruction->dest_ptr->value.type;
48884893 assert(ptr_type->id == ZigTypeIdPointer);
48894894
4890 bool val_is_undef = value_is_all_undef(&instruction->byte->value);
4895 bool val_is_undef = value_is_all_undef(g, &instruction->byte->value);
48914896 LLVMValueRef fill_char;
48924897 if (val_is_undef) {
48934898 fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);
test/stage1/behavior/sizeof_and_typeof.zig+9
......@@ -115,3 +115,12 @@ test "branching logic inside @typeOf" {
115115 comptime expect(T == i32);
116116 expect(S.data == 0);
117117}
118
119fn fn1(alpha: bool) void {
120 const n: usize = 7;
121 const v = if (alpha) n else @sizeOf(usize);
122}
123
124test "lazy @sizeOf result is checked for definedness" {
125 const f = fn1;
126}