Commit d0b055d69e

Andrew Kelley <andrew@ziglang.org>
2020-01-11 22:41:45
fix implicit cast regression
1 parent 96d64a4
Changed files (4)
src/all_types.hpp
@@ -473,6 +473,9 @@ struct ZigValue {
     // uncomment these to find bugs. can't leave them uncommented because of a gcc-9 warning
     //ZigValue(const ZigValue &other) = delete; // plz zero initialize with {}
     //ZigValue& operator= (const ZigValue &other) = delete; // use copy_const_val
+
+    // for use in debuggers
+    void dump();
 };
 
 enum ReturnKnowledge {
src/analyze.cpp
@@ -9304,11 +9304,12 @@ bool type_has_optional_repr(ZigType *ty) {
 
 void copy_const_val(ZigValue *dest, ZigValue *src) {
     uint32_t prev_align = dest->llvm_align;
+    ConstParent prev_parent = dest->parent;
     memcpy(dest, src, sizeof(ZigValue));
     dest->llvm_align = prev_align;
     if (src->special != ConstValSpecialStatic)
         return;
-    dest->parent.id = ConstParentIdNone;
+    dest->parent = prev_parent;
     if (dest->type->id == ZigTypeIdStruct) {
         dest->data.x_struct.fields = alloc_const_vals_ptrs(dest->type->data.structure.src_field_count);
         for (size_t i = 0; i < dest->type->data.structure.src_field_count; i += 1) {
@@ -9362,3 +9363,128 @@ bool type_is_numeric(ZigType *ty) {
     }
     zig_unreachable();
 }
+
+static void dump_value_indent(ZigValue *val, int indent) {
+    for (int i = 0; i < indent; i += 1) {
+        fprintf(stderr, " ");
+    }
+    fprintf(stderr, "Value@%p(", val);
+    if (val->type != nullptr) {
+        fprintf(stderr, "%s)", buf_ptr(&val->type->name));
+    } else {
+        fprintf(stderr, "type=nullptr)");
+    }
+    switch (val->special) {
+        case ConstValSpecialUndef:
+            fprintf(stderr, "[undefined]\n");
+            return;
+        case ConstValSpecialLazy:
+            fprintf(stderr, "[lazy]\n");
+            return;
+        case ConstValSpecialRuntime:
+            fprintf(stderr, "[runtime]\n");
+            return;
+        case ConstValSpecialStatic:
+            break;
+    }
+    if (val->type == nullptr)
+        return;
+    switch (val->type->id) {
+        case ZigTypeIdInvalid:
+            fprintf(stderr, "<invalid>\n");
+            return;
+        case ZigTypeIdUnreachable:
+            fprintf(stderr, "<unreachable>\n");
+            return;
+        case ZigTypeIdVoid:
+            fprintf(stderr, "<{}>\n");
+            return;
+        case ZigTypeIdMetaType:
+            fprintf(stderr, "<%s>\n", buf_ptr(&val->data.x_type->name));
+            return;
+        case ZigTypeIdBool:
+            fprintf(stderr, "<%s>\n", val->data.x_bool ? "true" : "false");
+            return;
+        case ZigTypeIdComptimeFloat:
+        case ZigTypeIdComptimeInt:
+        case ZigTypeIdInt:
+        case ZigTypeIdFloat:
+        case ZigTypeIdUndefined:
+            fprintf(stderr, "<TODO dump number>\n");
+            return;
+
+        case ZigTypeIdStruct:
+            fprintf(stderr, "<struct\n");
+            for (size_t i = 0; i < val->type->data.structure.src_field_count; i += 1) {
+                for (int j = 0; j < indent; j += 1) {
+                    fprintf(stderr, " ");
+                }
+                fprintf(stderr, "%s: ", buf_ptr(val->type->data.structure.fields[i]->name));
+                dump_value_indent(val->data.x_struct.fields[i], 1);
+            }
+            for (int i = 0; i < indent; i += 1) {
+                fprintf(stderr, " ");
+            }
+            fprintf(stderr, ">\n");
+            return;
+
+        case ZigTypeIdOptional:
+            fprintf(stderr, "<\n");
+            dump_value_indent(val->data.x_optional, indent + 1);
+
+            for (int i = 0; i < indent; i += 1) {
+                fprintf(stderr, " ");
+            }
+            fprintf(stderr, ">\n");
+            return;
+
+        case ZigTypeIdErrorUnion:
+            if (val->data.x_err_union.payload != nullptr) {
+                fprintf(stderr, "<\n");
+                dump_value_indent(val->data.x_err_union.payload, indent + 1);
+            } else {
+                fprintf(stderr, "<\n");
+                dump_value_indent(val->data.x_err_union.error_set, 0);
+            }
+            for (int i = 0; i < indent; i += 1) {
+                fprintf(stderr, " ");
+            }
+            fprintf(stderr, ">\n");
+            return;
+
+        case ZigTypeIdPointer:
+            switch (val->data.x_ptr.special) {
+                case ConstPtrSpecialRef:
+                    fprintf(stderr, "<ref\n");
+                    dump_value_indent(val->data.x_ptr.data.ref.pointee, indent + 1);
+                    break;
+                default:
+                    fprintf(stderr, "TODO dump more pointer things\n");
+            }
+            for (int i = 0; i < indent; i += 1) {
+                fprintf(stderr, " ");
+            }
+            fprintf(stderr, ">\n");
+            return;
+
+        case ZigTypeIdVector:
+        case ZigTypeIdArray:
+        case ZigTypeIdNull:
+        case ZigTypeIdErrorSet:
+        case ZigTypeIdEnum:
+        case ZigTypeIdUnion:
+        case ZigTypeIdFn:
+        case ZigTypeIdBoundFn:
+        case ZigTypeIdOpaque:
+        case ZigTypeIdFnFrame:
+        case ZigTypeIdAnyFrame:
+        case ZigTypeIdEnumLiteral:
+            fprintf(stderr, "<TODO dump value>\n");
+            return;
+    }
+    zig_unreachable();
+}
+
+void ZigValue::dump() {
+    dump_value_indent(this, 0);
+}
src/ir.cpp
@@ -4190,12 +4190,6 @@ static ScopeDeferExpr *get_scope_defer_expr(Scope *scope) {
 static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {
     assert(node->type == NodeTypeReturnExpr);
 
-    ZigFn *fn_entry = exec_fn_entry(irb->exec);
-    if (!fn_entry) {
-        add_node_error(irb->codegen, node, buf_sprintf("return expression outside function definition"));
-        return irb->codegen->invalid_instruction;
-    }
-
     ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(scope);
     if (scope_defer_expr) {
         if (!scope_defer_expr->reported_err) {
@@ -17079,9 +17073,11 @@ static IrInstruction *ir_analyze_alloca(IrAnalyze *ira, IrInstruction *source_in
     result->base.value->type = get_pointer_to_type_extra(ira->codegen, var_type, false, false,
             PtrLenSingle, align, 0, 0, false);
 
-    ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
-    if (fn_entry != nullptr) {
-        fn_entry->alloca_gen_list.append(result);
+    if (!force_comptime) {
+        ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
+        if (fn_entry != nullptr) {
+            fn_entry->alloca_gen_list.append(result);
+        }
     }
     result->base.is_gen = true;
     return &result->base;
@@ -17619,7 +17615,8 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
                         result_loc, false, true);
                 ZigType *actual_payload_type = actual_elem_type->data.error_union.payload_type;
                 if (actual_payload_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
-                    value_type->id != ZigTypeIdNull) {
+                    value_type->id != ZigTypeIdNull)
+                {
                     return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, unwrapped_err_ptr, false, true);
                 } else {
                     return unwrapped_err_ptr;
@@ -21176,7 +21173,7 @@ static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstr
 
     if (instr_is_comptime(base_ptr)) {
         ZigValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
-        if (!ptr_val)
+        if (ptr_val == nullptr)
             return ira->codegen->invalid_instruction;
         if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
             ZigValue *optional_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
@@ -28366,11 +28363,6 @@ static IrInstruction *ir_analyze_instruction_implicit_cast(IrAnalyze *ira, IrIns
     if (type_is_invalid(operand->value->type))
         return operand;
 
-    IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base,
-            &instruction->result_loc_cast->base, operand->value->type, operand, false, true);
-    if (result_loc != nullptr && (type_is_invalid(result_loc->value->type) || instr_is_unreachable(result_loc)))
-        return result_loc;
-
     ZigType *dest_type = ir_resolve_type(ira, instruction->result_loc_cast->base.source_instruction->child);
     if (type_is_invalid(dest_type))
         return ira->codegen->invalid_instruction;
test/stage1/behavior.zig
@@ -54,7 +54,7 @@ comptime {
     _ = @import("behavior/byteswap.zig");
     _ = @import("behavior/byval_arg_var.zig");
     _ = @import("behavior/call.zig");
-    //_ = @import("behavior/cast.zig");
+    _ = @import("behavior/cast.zig");
     _ = @import("behavior/const_slice_child.zig");
     _ = @import("behavior/defer.zig");
     _ = @import("behavior/enum.zig");
@@ -81,9 +81,9 @@ comptime {
     _ = @import("behavior/muladd.zig");
     _ = @import("behavior/namespace_depends_on_compile_var.zig");
     _ = @import("behavior/new_stack_call.zig");
-    //_ = @import("behavior/null.zig");
+    _ = @import("behavior/null.zig");
     //_ = @import("behavior/optional.zig");
-    //_ = @import("behavior/pointers.zig");
+    _ = @import("behavior/pointers.zig");
     _ = @import("behavior/popcount.zig");
     _ = @import("behavior/ptrcast.zig");
     _ = @import("behavior/pub_enum.zig");