diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index fd82a30ce0..5f86321e9d 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -7232,13 +7232,13 @@ bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b) { } return true; case ZigTypeIdFnFrame: - zig_panic("TODO"); + zig_panic("TODO: const_values_equal ZigTypeIdFnFrame"); case ZigTypeIdAnyFrame: - zig_panic("TODO"); + zig_panic("TODO: const_values_equal ZigTypeIdAnyFrame"); case ZigTypeIdUndefined: - zig_panic("TODO"); + zig_panic("TODO: const_values_equal ZigTypeIdUndefined"); case ZigTypeIdNull: - zig_panic("TODO"); + zig_panic("TODO: const_values_equal ZigTypeIdNull"); case ZigTypeIdOptional: if (get_src_ptr_type(a->type) != nullptr) return const_values_equal_ptr(a, b); @@ -7247,8 +7247,16 @@ bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b) { } else { return const_values_equal(g, a->data.x_optional, b->data.x_optional); } - case ZigTypeIdErrorUnion: - zig_panic("TODO"); + case ZigTypeIdErrorUnion: { + bool a_is_err = a->data.x_err_union.error_set->data.x_err_set != nullptr; + bool b_is_err = b->data.x_err_union.error_set->data.x_err_set != nullptr; + if (a_is_err != b_is_err) return false; + if (a_is_err) { + return const_values_equal(g, a->data.x_err_union.error_set, b->data.x_err_union.error_set); + } else { + return const_values_equal(g, a->data.x_err_union.payload, b->data.x_err_union.payload); + } + } case ZigTypeIdBoundFn: case ZigTypeIdInvalid: case ZigTypeIdUnreachable: diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 738c4b7b0a..386bc43086 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -7855,9 +7855,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ZigValue *const_val, const char *n case ZigTypeIdOpaque: zig_unreachable(); case ZigTypeIdFnFrame: - zig_panic("TODO"); + zig_panic("TODO: gen_const_val ZigTypeIdFnFrame"); case ZigTypeIdAnyFrame: - zig_panic("TODO"); + zig_panic("TODO: gen_const_val ZigTypeIdAnyFrame"); } zig_unreachable(); } diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index e868811def..eca589a710 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -10843,7 +10843,7 @@ static void float_init_bigfloat(ZigValue *dest_val, BigFloat *bigfloat) { dest_val->data.x_f64 = bigfloat_to_f64(bigfloat); break; case 80: - zig_panic("TODO"); + zig_panic("TODO: float_init_bigfloat c_longdouble"); case 128: dest_val->data.x_f128 = bigfloat_to_f128(bigfloat); break; @@ -11483,7 +11483,7 @@ static void value_to_bigfloat(BigFloat *out, ZigValue *val) { bigfloat_init_64(out, val->data.x_f64); return; case 80: - zig_panic("TODO"); + zig_panic("TODO: value_to_bigfloat c_longdouble"); case 128: bigfloat_init_128(out, val->data.x_f128); return; @@ -11573,7 +11573,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstGen *instruction break; } case 80: - zig_panic("TODO"); + zig_panic("TODO: ir_num_lit_fits_in_other_type c_longdouble"); case 128: { float128_t tmp = bigfloat_to_f128(&tmp_bf); bigfloat_init_128(&orig_bf, tmp); @@ -11618,7 +11618,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstGen *instruction break; } case 80: - zig_panic("TODO"); + zig_panic("TODO: ir_num_lit_fits_in_other_type c_longdouble"); case 128: { float16_t tmp = f128M_to_f16(&const_val->data.x_f128); float128_t orig; @@ -11643,7 +11643,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstGen *instruction break; } case 80: - zig_panic("TODO"); + zig_panic("TODO: ir_num_lit_fits_in_other_type c_longdouble"); case 128: { float32_t tmp = f128M_to_f32(&const_val->data.x_f128); float128_t orig; @@ -11660,7 +11660,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstGen *instruction case 64: switch (const_val->type->data.floating.bit_count) { case 80: - zig_panic("TODO"); + zig_panic("TODO: ir_num_lit_fits_in_other_type c_longdouble"); case 128: { float64_t tmp = f128M_to_f64(&const_val->data.x_f128); float128_t orig; @@ -11676,7 +11676,7 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstGen *instruction break; case 80: assert(const_val->type->data.floating.bit_count == 128); - zig_panic("TODO"); + zig_panic("TODO: ir_num_lit_fits_in_other_type c_longdouble"); case 128: return true; default: @@ -13068,7 +13068,7 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr, zig_unreachable(); case CastOpErrSet: case CastOpBitCast: - zig_panic("TODO"); + zig_panic("TODO: eval_const_expr_implicit_cast CastOpErrSet, CastOpBitCast"); case CastOpNoop: { copy_const_val(ira->codegen, const_val, other_val); const_val->type = new_type; @@ -13088,7 +13088,7 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr, const_val->data.x_f64 = bigfloat_to_f64(&other_val->data.x_bigfloat); break; case 80: - zig_panic("TODO"); + zig_panic("TODO: eval_const_expr_implicit_cast c_longdouble"); case 128: const_val->data.x_f128 = bigfloat_to_f128(&other_val->data.x_bigfloat); break; @@ -13117,7 +13117,7 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr, const_val->data.x_f64 = bigfloat_to_f64(&bigfloat); break; case 80: - zig_panic("TODO"); + zig_panic("TODO: eval_const_expr_implicit_cast c_longdouble"); case 128: const_val->data.x_f128 = bigfloat_to_f128(&bigfloat); break; @@ -21433,7 +21433,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source ZigValue *array_val = ptr_val->data.x_ptr.data.base_array.array_val; assert(array_val->type->id == ZigTypeIdArray); if (array_val->data.x_array.special != ConstArraySpecialNone) - zig_panic("TODO"); + zig_panic("TODO: ir_read_const_ptr ConstPtrSpecialSubArray !ConstArraySpecialNone"); if (dst_size > src_size) { size_t elem_index = ptr_val->data.x_ptr.data.base_array.elem_index; opt_ir_add_error_node(ira, codegen, source_node, @@ -21458,7 +21458,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source ZigValue *array_val = ptr_val->data.x_ptr.data.base_array.array_val; assert(array_val->type->id == ZigTypeIdArray); if (array_val->data.x_array.special != ConstArraySpecialNone) - zig_panic("TODO"); + zig_panic("TODO: ir_read_const_ptr ConstPtrSpecialBaseArray !ConstArraySpecialNone"); size_t elem_size = src_size; size_t elem_index = ptr_val->data.x_ptr.data.base_array.elem_index; src_size = elem_size * (array_val->type->data.array.len - elem_index); @@ -21487,7 +21487,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source case ConstPtrSpecialDiscard: case ConstPtrSpecialHardCodedAddr: case ConstPtrSpecialFunction: - zig_panic("TODO"); + zig_panic("TODO: ir_read_const_ptr"); } zig_unreachable(); } @@ -28815,13 +28815,13 @@ done_with_return_type: case ConstPtrSpecialBaseArray: zig_unreachable(); case ConstPtrSpecialBaseStruct: - zig_panic("TODO"); + zig_panic("TODO: ir_analyze_instruction_slice ConstPtrSpecialBaseStruct"); case ConstPtrSpecialBaseErrorUnionCode: - zig_panic("TODO"); + zig_panic("TODO: ir_analyze_instruction_slice ConstPtrSpecialBaseErrorUnionCode"); case ConstPtrSpecialBaseErrorUnionPayload: - zig_panic("TODO"); + zig_panic("TODO: ir_analyze_instruction_slice ConstPtrSpecialBaseErrorUnionPayload"); case ConstPtrSpecialBaseOptionalPayload: - zig_panic("TODO"); + zig_panic("TODO: ir_analyze_instruction_slice ConstPtrSpecialBaseOptionalPayload"); case ConstPtrSpecialHardCodedAddr: init_const_ptr_hard_coded_addr(ira->codegen, ptr_val, parent_ptr->type->data.pointer.child_type, @@ -28829,9 +28829,9 @@ done_with_return_type: return_type_is_const); break; case ConstPtrSpecialFunction: - zig_panic("TODO"); + zig_panic("TODO: ir_analyze_instruction_slice ConstPtrSpecialFunction"); case ConstPtrSpecialNull: - zig_panic("TODO"); + zig_panic("TODO: ir_analyze_instruction_slice ConstPtrSpecialNull"); } // In the case of pointer-to-array, we must restore this because above it overwrites ptr_val->type diff --git a/src/translate_c.zig b/src/translate_c.zig index 5a4fe76615..1b2aa4b219 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -550,7 +550,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void { const fn_decl_loc = fn_decl.getLocation(); const has_body = fn_decl.hasBody(); const storage_class = fn_decl.getStorageClass(); - const decl_ctx = FnDeclContext{ + var decl_ctx = FnDeclContext{ .fn_name = fn_name, .has_body = has_body, .storage_class = storage_class, @@ -584,6 +584,12 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void { const proto_node = switch (fn_type.getTypeClass()) { .FunctionProto => blk: { const fn_proto_type = @ptrCast(*const clang.FunctionProtoType, fn_type); + if (has_body and fn_proto_type.isVariadic()) { + decl_ctx.has_body = false; + decl_ctx.storage_class = .Extern; + decl_ctx.is_export = false; + try emitWarning(c, fn_decl_loc, "TODO unable to translate variadic function, demoted to declaration", .{}); + } break :blk transFnProto(rp, fn_decl, fn_proto_type, fn_decl_loc, decl_ctx, true) catch |err| switch (err) { error.UnsupportedType => { return failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function", .{}); diff --git a/test/stage1/behavior/error.zig b/test/stage1/behavior/error.zig index 9635f2870c..529b89d1e2 100644 --- a/test/stage1/behavior/error.zig +++ b/test/stage1/behavior/error.zig @@ -441,3 +441,12 @@ test "error payload type is correctly resolved" { expectEqual(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create()); } + +test "error union comptime caching" { + const S = struct { + fn foo(comptime arg: anytype) void {} + }; + + S.foo(@as(anyerror!void, {})); + S.foo(@as(anyerror!void, {})); +} \ No newline at end of file diff --git a/test/translate_c.zig b/test/translate_c.zig index f90af8b7be..60f3d0e2c5 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -3,6 +3,15 @@ const std = @import("std"); const CrossTarget = std.zig.CrossTarget; pub fn addCases(cases: *tests.TranslateCContext) void { + cases.add("variadic function demoted to prototype", + \\int foo(int bar, ...) { + \\ return 1; + \\} + , &[_][]const u8{ + \\warning: TODO unable to translate variadic function, demoted to declaration + \\pub extern fn foo(bar: c_int, ...) c_int; + }); + cases.add("pointer to opaque demoted struct", \\typedef struct { \\ _Atomic int foo;