authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-26 14:01:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-26 14:01:59-04:00
logd316f704501ff7fc809fdfb082226031ef875046
tree0f436bc3f8af84e90c13b42c3d5c45fa3d3f056d
parent73a7747a9cfb180a92fa0d98f9387d5ad1f47fd2
signaturelock-open Commit is signed but in an unrecognized format.

fix regression on struct field with undefined type


5 files changed, 40 insertions(+), 52 deletions(-)

src/analyze.cpp+25-34
......@@ -961,18 +961,14 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
961961 return entry;
962962}
963963
964ConstExprValue *analyze_const_value_allow_lazy(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
965 Buf *type_name, bool allow_lazy)
964ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
965 Buf *type_name, UndefAllowed undef)
966966{
967967 size_t backward_branch_count = 0;
968968 size_t backward_branch_quota = default_backward_branch_quota;
969969 return ir_eval_const_value(g, scope, node, type_entry,
970970 &backward_branch_count, &backward_branch_quota,
971 nullptr, nullptr, node, type_name, nullptr, nullptr, allow_lazy);
972}
973
974ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, Buf *type_name) {
975 return analyze_const_value_allow_lazy(g, scope, node, type_entry, type_name, false);
971 nullptr, nullptr, node, type_name, nullptr, nullptr, undef);
976972}
977973
978974static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, ZigType *parent_type,
......@@ -1162,22 +1158,12 @@ static OnePossibleValue type_val_resolve_has_one_possible_value(CodeGen *g, Cons
11621158}
11631159
11641160ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
1165 ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);
1161 ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type,
1162 nullptr, UndefBad);
11661163 if (type_is_invalid(result->type))
11671164 return g->builtin_types.entry_invalid;
1168
1169 assert(result->special != ConstValSpecialRuntime);
1170 // Reject undefined as valid `type` type even though the specification
1171 // allows it to be casted to anything.
1172 // See also ir_resolve_type()
1173 if (result->special == ConstValSpecialUndef) {
1174 add_node_error(g, node,
1175 buf_sprintf("expected type 'type', found '%s'",
1176 buf_ptr(&g->builtin_types.entry_undef->name)));
1177 return g->builtin_types.entry_invalid;
1178 }
1179
1180 assert(result->data.x_type != nullptr);
1165 src_assert(result->special == ConstValSpecialStatic, node);
1166 src_assert(result->data.x_type != nullptr, node);
11811167 return result->data.x_type;
11821168}
11831169
......@@ -1225,7 +1211,8 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_cou
12251211}
12261212
12271213static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_t *result) {
1228 ConstExprValue *align_result = analyze_const_value(g, scope, node, get_align_amt_type(g), nullptr);
1214 ConstExprValue *align_result = analyze_const_value(g, scope, node, get_align_amt_type(g),
1215 nullptr, UndefBad);
12291216 if (type_is_invalid(align_result->type))
12301217 return false;
12311218
......@@ -1247,7 +1234,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
12471234 ZigType *ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
12481235 PtrLenUnknown, 0, 0, 0, false);
12491236 ZigType *str_type = get_slice_type(g, ptr_type);
1250 ConstExprValue *result_val = analyze_const_value(g, scope, node, str_type, nullptr);
1237 ConstExprValue *result_val = analyze_const_value(g, scope, node, str_type, nullptr, UndefBad);
12511238 if (type_is_invalid(result_val->type))
12521239 return false;
12531240
......@@ -2261,7 +2248,8 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
22612248
22622249 if (tag_value != nullptr) {
22632250 // A user-specified value is available
2264 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
2251 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type,
2252 nullptr, UndefBad);
22652253 if (type_is_invalid(result->type)) {
22662254 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
22672255 continue;
......@@ -2377,8 +2365,8 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
23772365 return ErrorSemanticAnalyzeFail;
23782366 }
23792367
2380 ConstExprValue *field_type_val = analyze_const_value_allow_lazy(g, scope,
2381 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, true);
2368 ConstExprValue *field_type_val = analyze_const_value(g, scope,
2369 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, LazyOkNoUndef);
23822370 if (type_is_invalid(field_type_val->type)) {
23832371 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
23842372 return ErrorSemanticAnalyzeFail;
......@@ -2660,8 +2648,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
26602648 return ErrorSemanticAnalyzeFail;
26612649 }
26622650 } else {
2663 ConstExprValue *field_type_val = analyze_const_value_allow_lazy(g, scope,
2664 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, true);
2651 ConstExprValue *field_type_val = analyze_const_value(g, scope,
2652 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, LazyOkNoUndef);
26652653 if (type_is_invalid(field_type_val->type)) {
26662654 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
26672655 return ErrorSemanticAnalyzeFail;
......@@ -2726,7 +2714,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
27262714 // In a second pass we will fill in the unspecified ones.
27272715 if (tag_value != nullptr) {
27282716 ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type;
2729 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
2717 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type,
2718 nullptr, UndefBad);
27302719 if (type_is_invalid(result->type)) {
27312720 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
27322721 return ErrorSemanticAnalyzeFail;
......@@ -2929,7 +2918,7 @@ void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn) {
29292918 fake_decl->data.symbol_expr.symbol = tld_fn->base.name;
29302919
29312920 // call this for the side effects of casting to panic_fn_type
2932 analyze_const_value(g, tld_fn->base.parent_scope, fake_decl, panic_fn_type, nullptr);
2921 analyze_const_value(g, tld_fn->base.parent_scope, fake_decl, panic_fn_type, nullptr, UndefBad);
29332922}
29342923
29352924ZigType *get_test_fn_type(CodeGen *g) {
......@@ -3075,7 +3064,8 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
30753064static void resolve_decl_comptime(CodeGen *g, TldCompTime *tld_comptime) {
30763065 assert(tld_comptime->base.source_node->type == NodeTypeCompTime);
30773066 AstNode *expr_node = tld_comptime->base.source_node->data.comptime_expr.expr;
3078 analyze_const_value(g, tld_comptime->base.parent_scope, expr_node, g->builtin_types.entry_void, nullptr);
3067 analyze_const_value(g, tld_comptime->base.parent_scope, expr_node, g->builtin_types.entry_void,
3068 nullptr, UndefBad);
30793069}
30803070
30813071static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
......@@ -3443,8 +3433,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
34433433 if (explicit_type && explicit_type->id == ZigTypeIdInvalid) {
34443434 implicit_type = explicit_type;
34453435 } else if (var_decl->expr) {
3446 init_value = analyze_const_value_allow_lazy(g, tld_var->base.parent_scope, var_decl->expr, explicit_type,
3447 var_decl->symbol, allow_lazy);
3436 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type,
3437 var_decl->symbol, allow_lazy ? LazyOk : UndefOk);
34483438 assert(init_value);
34493439 implicit_type = init_value->type;
34503440
......@@ -3599,7 +3589,8 @@ static void preview_use_decl(CodeGen *g, TldUsingNamespace *using_namespace, Sco
35993589 using_namespace->base.resolution = TldResolutionResolving;
36003590 assert(using_namespace->base.source_node->type == NodeTypeUsingNamespace);
36013591 ConstExprValue *result = analyze_const_value(g, &dest_decls_scope->base,
3602 using_namespace->base.source_node->data.using_namespace.expr, g->builtin_types.entry_type, nullptr);
3592 using_namespace->base.source_node->data.using_namespace.expr, g->builtin_types.entry_type,
3593 nullptr, UndefBad);
36033594 using_namespace->using_namespace_value = result;
36043595
36053596 if (type_is_invalid(result->type)) {
src/analyze.hpp+2-3
......@@ -240,9 +240,8 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
240240
241241void src_assert(bool ok, AstNode *source_node);
242242bool is_container(ZigType *type_entry);
243ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, Buf *type_name);
244ConstExprValue *analyze_const_value_allow_lazy(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
245 Buf *type_name, bool allow_lazy);
243ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
244 Buf *type_name, UndefAllowed undef);
246245
247246void resolve_llvm_types_fn(CodeGen *g, ZigFn *fn);
248247bool fn_is_async(ZigFn *fn);
src/ir.cpp+11-13
......@@ -398,7 +398,7 @@ static void ir_ref_var(ZigVar *var) {
398398ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {
399399 ConstExprValue *result = ir_eval_const_value(ira->codegen, scope, node, ira->codegen->builtin_types.entry_type,
400400 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr, nullptr,
401 node, nullptr, ira->new_irb.exec, nullptr, false);
401 node, nullptr, ira->new_irb.exec, nullptr, UndefBad);
402402
403403 if (type_is_invalid(result->type))
404404 return ira->codegen->builtin_types.entry_invalid;
......@@ -10734,14 +10734,14 @@ static Error ir_resolve_const_val(CodeGen *codegen, IrExecutable *exec, AstNode
1073410734 buf_sprintf("unable to evaluate constant expression"));
1073510735 return ErrorSemanticAnalyzeFail;
1073610736 case ConstValSpecialUndef:
10737 if (undef_allowed == UndefOk)
10737 if (undef_allowed == UndefOk || undef_allowed == LazyOk)
1073810738 return ErrorNone;
1073910739
1074010740 exec_add_error_node(codegen, exec, source_node,
1074110741 buf_sprintf("use of undefined value here causes undefined behavior"));
1074210742 return ErrorSemanticAnalyzeFail;
1074310743 case ConstValSpecialLazy:
10744 if (undef_allowed == LazyOk)
10744 if (undef_allowed == LazyOk || undef_allowed == LazyOkNoUndef)
1074510745 return ErrorNone;
1074610746
1074710747 if ((err = ir_resolve_lazy(codegen, source_node, val)))
......@@ -10765,7 +10765,7 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un
1076510765ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1076610766 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,
1076710767 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
10768 IrExecutable *parent_exec, AstNode *expected_type_source_node, bool allow_lazy)
10768 IrExecutable *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef_allowed)
1076910769{
1077010770 Error err;
1077110771
......@@ -10819,11 +10819,8 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
1081910819
1082010820 ConstExprValue *result = ir_exec_const_result(codegen, analyzed_executable);
1082110821
10822 if (!allow_lazy) {
10823 if ((err = ir_resolve_lazy(codegen, node, result))) {
10824 return &codegen->invalid_instruction->value;
10825 }
10826 }
10822 if ((err = ir_resolve_const_val(codegen, analyzed_executable, node, result, undef_allowed)))
10823 return &codegen->invalid_instruction->value;
1082710824
1082810825 return result;
1082910826}
......@@ -15578,7 +15575,8 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1557815575 AstNode *body_node = fn_entry->body_node;
1557915576 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
1558015577 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
15581 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node, false);
15578 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node,
15579 UndefOk);
1558215580
1558315581 if (inferred_err_set_type != nullptr) {
1558415582 inferred_err_set_type->data.error_set.infer_fn = nullptr;
......@@ -15776,7 +15774,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1577615774 fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen),
1577715775 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,
1577815776 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec,
15779 nullptr, false);
15777 nullptr, UndefBad);
1578015778 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
1578115779 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
1578215780 copy_const_val(&const_instruction->base.value, align_result, true);
......@@ -19129,7 +19127,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1912919127 Scope *analyze_scope = &get_container_scope(container_type)->base;
1913019128 // memoize it
1913119129 field->init_val = analyze_const_value(ira->codegen, analyze_scope, init_node,
19132 field->type_entry, nullptr);
19130 field->type_entry, nullptr, UndefOk);
1913319131 }
1913419132 if (type_is_invalid(field->init_val->type))
1913519133 return ira->codegen->invalid_instruction;
......@@ -20640,7 +20638,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
2064020638 ZigType *void_type = ira->codegen->builtin_types.entry_void;
2064120639 ConstExprValue *cimport_result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,
2064220640 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,
20643 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr, false);
20641 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr, UndefBad);
2064420642 if (type_is_invalid(cimport_result->type))
2064520643 return ira->codegen->invalid_instruction;
2064620644
src/ir.hpp+1-1
......@@ -16,7 +16,7 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
1616ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1717 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,
1818 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
19 IrExecutable *parent_exec, AstNode *expected_type_source_node, bool allow_lazy);
19 IrExecutable *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef);
2020
2121Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val);
2222
test/compile_errors.zig+1-1
......@@ -404,7 +404,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
404404 \\ const foo: Foo = undefined;
405405 \\}
406406 ,
407 "tmp.zig:2:8: error: expected type 'type', found '(undefined)'",
407 "tmp.zig:2:8: error: use of undefined value here causes undefined behavior",
408408 );
409409
410410 cases.add(