aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/std/crypto/chacha20.zig203
-rw-r--r--src/analyze.cpp5
-rw-r--r--src/codegen.cpp19
-rw-r--r--src/ir.cpp70
-rw-r--r--test/compile_errors.zig10
-rw-r--r--test/stage1/behavior/error.zig15
6 files changed, 284 insertions, 38 deletions
diff --git a/lib/std/crypto/chacha20.zig b/lib/std/crypto/chacha20.zig
index 10d2130659..8a0f677660 100644
--- a/lib/std/crypto/chacha20.zig
+++ b/lib/std/crypto/chacha20.zig
@@ -7,6 +7,7 @@ const assert = std.debug.assert;
const testing = std.testing;
const builtin = @import("builtin");
const maxInt = std.math.maxInt;
+const Poly1305 = std.crypto.Poly1305;
const QuarterRound = struct {
a: usize,
@@ -434,3 +435,205 @@ test "crypto.chacha20 test vector 5" {
chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
testing.expectEqualSlices(u8, &expected_result, &result);
}
+
+pub const chacha20poly1305_tag_size = 16;
+
+pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) void {
+ assert(dst.len >= plaintext.len + chacha20poly1305_tag_size);
+
+ // derive poly1305 key
+ var polyKey = [_]u8{0} ** 32;
+ chaCha20IETF(polyKey[0..], polyKey[0..], 0, key, nonce);
+
+ // encrypt plaintext
+ chaCha20IETF(dst[0..plaintext.len], plaintext, 1, key, nonce);
+
+ // construct mac
+ var mac = Poly1305.init(polyKey[0..]);
+ mac.update(data);
+ if (data.len % 16 != 0) {
+ const zeros = [_]u8{0} ** 16;
+ const padding = 16 - (data.len % 16);
+ mac.update(zeros[0..padding]);
+ }
+ mac.update(dst[0..plaintext.len]);
+ if (plaintext.len % 16 != 0) {
+ const zeros = [_]u8{0} ** 16;
+ const padding = 16 - (plaintext.len % 16);
+ mac.update(zeros[0..padding]);
+ }
+ var lens: [16]u8 = undefined;
+ mem.writeIntSliceLittle(u64, lens[0..8], data.len);
+ mem.writeIntSliceLittle(u64, lens[8..16], plaintext.len);
+ mac.update(lens[0..]);
+ mac.final(dst[plaintext.len..]);
+}
+
+/// Verifies and decrypts an authenticated message produced by chacha20poly1305Seal.
+pub fn chacha20poly1305Open(dst: []u8, msgAndTag: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) !void {
+ if (msgAndTag.len < chacha20poly1305_tag_size) {
+ return error.InvalidMessage;
+ }
+
+ // split ciphertext and tag
+ assert(dst.len >= msgAndTag.len - chacha20poly1305_tag_size);
+ var ciphertext = msgAndTag[0 .. msgAndTag.len - chacha20poly1305_tag_size];
+ var polyTag = msgAndTag[ciphertext.len..];
+
+ // derive poly1305 key
+ var polyKey = [_]u8{0} ** 32;
+ chaCha20IETF(polyKey[0..], polyKey[0..], 0, key, nonce);
+
+ // construct mac
+ var mac = Poly1305.init(polyKey[0..]);
+
+ mac.update(data);
+ if (data.len % 16 != 0) {
+ const zeros = [_]u8{0} ** 16;
+ const padding = 16 - (data.len % 16);
+ mac.update(zeros[0..padding]);
+ }
+ mac.update(ciphertext);
+ if (ciphertext.len % 16 != 0) {
+ const zeros = [_]u8{0} ** 16;
+ const padding = 16 - (ciphertext.len % 16);
+ mac.update(zeros[0..padding]);
+ }
+ var lens: [16]u8 = undefined;
+ mem.writeIntSliceLittle(u64, lens[0..8], data.len);
+ mem.writeIntSliceLittle(u64, lens[8..16], ciphertext.len);
+ mac.update(lens[0..]);
+ var computedTag: [16]u8 = undefined;
+ mac.final(computedTag[0..]);
+
+ // verify mac in constant time
+ // TODO: we can't currently guarantee that this will run in constant time.
+ // See https://github.com/ziglang/zig/issues/1776
+ var acc: u8 = 0;
+ for (computedTag) |_, i| {
+ acc |= (computedTag[i] ^ polyTag[i]);
+ }
+ if (acc != 0) {
+ return error.AuthenticationFailed;
+ }
+
+ // decrypt ciphertext
+ chaCha20IETF(dst[0..ciphertext.len], ciphertext, 1, key, nonce);
+}
+
+test "seal" {
+ {
+ const plaintext = "";
+ const data = "";
+ const key = [_]u8{
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ };
+ const nonce = [_]u8{ 0x7, 0x0, 0x0, 0x0, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 };
+ const exp_out = [_]u8{ 0xa0, 0x78, 0x4d, 0x7a, 0x47, 0x16, 0xf3, 0xfe, 0xb4, 0xf6, 0x4e, 0x7f, 0x4b, 0x39, 0xbf, 0x4 };
+
+ var out: [exp_out.len]u8 = undefined;
+ chacha20poly1305Seal(out[0..], plaintext, data, key, nonce);
+ testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
+ }
+ {
+ const plaintext = [_]u8{
+ 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
+ 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
+ 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
+ 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
+ 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
+ 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
+ 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
+ 0x74, 0x2e,
+ };
+ const data = [_]u8{ 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };
+ const key = [_]u8{
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ };
+ const nonce = [_]u8{ 0x7, 0x0, 0x0, 0x0, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 };
+ const exp_out = [_]u8{
+ 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb, 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
+ 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x8, 0xfe, 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
+ 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12, 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
+ 0x1a, 0x71, 0xde, 0xa, 0x9e, 0x6, 0xb, 0x29, 0x5, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
+ 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c, 0x98, 0x3, 0xae, 0xe3, 0x28, 0x9, 0x1b, 0x58,
+ 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94, 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
+ 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d, 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
+ 0x61, 0x16, 0x1a, 0xe1, 0xb, 0x59, 0x4f, 0x9, 0xe2, 0x6a, 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60,
+ 0x6, 0x91,
+ };
+
+ var out: [exp_out.len]u8 = undefined;
+ chacha20poly1305Seal(out[0..], plaintext[0..], data[0..], key, nonce);
+ testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
+ }
+}
+
+test "open" {
+ {
+ const ciphertext = [_]u8{ 0xa0, 0x78, 0x4d, 0x7a, 0x47, 0x16, 0xf3, 0xfe, 0xb4, 0xf6, 0x4e, 0x7f, 0x4b, 0x39, 0xbf, 0x4 };
+ const data = "";
+ const key = [_]u8{
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ };
+ const nonce = [_]u8{ 0x7, 0x0, 0x0, 0x0, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 };
+ const exp_out = "";
+
+ var out: [exp_out.len]u8 = undefined;
+ try chacha20poly1305Open(out[0..], ciphertext[0..], data, key, nonce);
+ testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
+ }
+ {
+ const ciphertext = [_]u8{
+ 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb, 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
+ 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x8, 0xfe, 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
+ 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12, 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
+ 0x1a, 0x71, 0xde, 0xa, 0x9e, 0x6, 0xb, 0x29, 0x5, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
+ 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c, 0x98, 0x3, 0xae, 0xe3, 0x28, 0x9, 0x1b, 0x58,
+ 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94, 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
+ 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d, 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
+ 0x61, 0x16, 0x1a, 0xe1, 0xb, 0x59, 0x4f, 0x9, 0xe2, 0x6a, 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60,
+ 0x6, 0x91,
+ };
+ const data = [_]u8{ 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };
+ const key = [_]u8{
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ };
+ const nonce = [_]u8{ 0x7, 0x0, 0x0, 0x0, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 };
+ const exp_out = [_]u8{
+ 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
+ 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
+ 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
+ 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
+ 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
+ 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
+ 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
+ 0x74, 0x2e,
+ };
+
+ var out: [exp_out.len]u8 = undefined;
+ try chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], key, nonce);
+ testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
+
+ // corrupting the ciphertext, data, key, or nonce should cause a failure
+ var bad_ciphertext = ciphertext;
+ bad_ciphertext[0] ^= 1;
+ testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], bad_ciphertext[0..], data[0..], key, nonce));
+ var bad_data = data;
+ bad_data[0] ^= 1;
+ testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], bad_data[0..], key, nonce));
+ var bad_key = key;
+ bad_key[0] ^= 1;
+ testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], bad_key, nonce));
+ var bad_nonce = nonce;
+ bad_nonce[0] ^= 1;
+ testing.expectError(error.AuthenticationFailed, chacha20poly1305Open(out[0..], ciphertext[0..], data[0..], key, bad_nonce));
+
+ // a short ciphertext should result in a different error
+ testing.expectError(error.InvalidMessage, chacha20poly1305Open(out[0..], "", data[0..], key, bad_nonce));
+ }
+}
diff --git a/src/analyze.cpp b/src/analyze.cpp
index b01a9769e8..dacb1d2b70 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -6372,10 +6372,11 @@ static Error resolve_pointer_zero_bits(CodeGen *g, ZigType *ty) {
ZigType *elem_type = ty->data.pointer.child_type;
- if ((err = type_resolve(g, elem_type, ResolveStatusZeroBitsKnown)))
+ bool has_bits;
+ if ((err = type_has_bits2(g, elem_type, &has_bits)))
return err;
- if (type_has_bits(elem_type)) {
+ if (has_bits) {
ty->abi_size = g->builtin_types.entry_usize->abi_size;
ty->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
ty->abi_align = g->builtin_types.entry_usize->abi_align;
diff --git a/src/codegen.cpp b/src/codegen.cpp
index 1455b4b743..01cda22cd4 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -1725,11 +1725,14 @@ static void gen_var_debug_decl(CodeGen *g, ZigVar *var) {
static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
Error err;
- if ((err = type_resolve(g, instruction->value->type, ResolveStatusZeroBitsKnown))) {
+
+ bool value_has_bits;
+ if ((err = type_has_bits2(g, instruction->value->type, &value_has_bits)))
codegen_report_errors_and_exit(g);
- }
- if (!type_has_bits(instruction->value->type))
+
+ if (!value_has_bits)
return nullptr;
+
if (!instruction->llvm_value) {
if (instruction->id == IrInstructionIdAwaitGen) {
IrInstructionAwaitGen *await = reinterpret_cast<IrInstructionAwaitGen*>(instruction);
@@ -5560,13 +5563,21 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab
static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *executable,
IrInstructionUnwrapErrPayload *instruction)
{
+ Error err;
+
if (instruction->base.value->special != ConstValSpecialRuntime)
return nullptr;
bool want_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base) &&
g->errors_by_index.length > 1;
- if (!want_safety && !type_has_bits(instruction->base.value->type))
+
+ bool value_has_bits;
+ if ((err = type_has_bits2(g, instruction->base.value->type, &value_has_bits)))
+ codegen_report_errors_and_exit(g);
+
+ if (!want_safety && !value_has_bits)
return nullptr;
+
ZigType *ptr_type = instruction->value->value->type;
assert(ptr_type->id == ZigTypeIdPointer);
ZigType *err_union_type = ptr_type->data.pointer.child_type;
diff --git a/src/ir.cpp b/src/ir.cpp
index a862ff1068..149c41dfec 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -12687,9 +12687,10 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
ZigType *field_type = resolve_union_field_type(ira->codegen, union_field);
if (field_type == nullptr)
return ira->codegen->invalid_instruction;
- if ((err = type_resolve(ira->codegen, field_type, ResolveStatusZeroBitsKnown)))
+ bool has_bits;
+ if ((err = type_has_bits2(ira->codegen, field_type, &has_bits)))
return ira->codegen->invalid_instruction;
- if (type_has_bits(field_type)) {
+ if (has_bits) {
AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(i);
add_error_note(ira->codegen, msg, field_node,
buf_sprintf("field '%s' has type '%s'",
@@ -13892,10 +13893,10 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,
actual_type, source_node, !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
{
- if ((err = type_resolve(ira->codegen, actual_type, ResolveStatusZeroBitsKnown))) {
+ bool has_bits;
+ if ((err = type_has_bits2(ira->codegen, actual_type, &has_bits)))
return ira->codegen->invalid_instruction;
- }
- if (!type_has_bits(actual_type)) {
+ if (!has_bits) {
return ir_get_ref(ira, source_instr, value, false, false);
}
}
@@ -17163,10 +17164,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
if (is_comptime)
return nullptr;
}
- if ((err = type_resolve(ira->codegen, ira->explicit_return_type, ResolveStatusZeroBitsKnown))) {
+ bool has_bits;
+ if ((err = type_has_bits2(ira->codegen, ira->explicit_return_type, &has_bits)))
return ira->codegen->invalid_instruction;
- }
- if (!type_has_bits(ira->explicit_return_type) || !handle_is_ptr(ira->explicit_return_type)) {
+ if (!has_bits || !handle_is_ptr(ira->explicit_return_type)) {
ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
if (fn_entry == nullptr || fn_entry->inferred_async_node == nullptr) {
return nullptr;
@@ -26082,6 +26083,7 @@ static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruct
ZigType *result_type = get_pointer_to_type_extra(ira->codegen, payload_type,
ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
PtrLenSingle, 0, 0, 0, false);
+
if (instr_is_comptime(base_ptr)) {
ZigValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
if (!ptr_val)
@@ -26599,6 +26601,23 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
if ((err = resolve_ptr_align(ira, dest_type, &dest_align_bytes)))
return ira->codegen->invalid_instruction;
+ if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
+ return ira->codegen->invalid_instruction;
+
+ if ((err = type_resolve(ira->codegen, src_type, ResolveStatusZeroBitsKnown)))
+ return ira->codegen->invalid_instruction;
+
+ if (type_has_bits(dest_type) && !type_has_bits(src_type)) {
+ ErrorMsg *msg = ir_add_error(ira, source_instr,
+ buf_sprintf("'%s' and '%s' do not have the same in-memory representation",
+ buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));
+ add_error_note(ira->codegen, msg, ptr->source_node,
+ buf_sprintf("'%s' has no in-memory bits", buf_ptr(&src_type->name)));
+ add_error_note(ira->codegen, msg, dest_type_src->source_node,
+ buf_sprintf("'%s' has in-memory bits", buf_ptr(&dest_type->name)));
+ return ira->codegen->invalid_instruction;
+ }
+
if (instr_is_comptime(ptr)) {
bool dest_allows_addr_zero = ptr_allows_addr_zero(dest_type);
UndefAllowed is_undef_allowed = dest_allows_addr_zero ? UndefOk : UndefBad;
@@ -26649,23 +26668,6 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_
IrInstruction *casted_ptr = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on);
- if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
- return ira->codegen->invalid_instruction;
-
- if ((err = type_resolve(ira->codegen, src_type, ResolveStatusZeroBitsKnown)))
- return ira->codegen->invalid_instruction;
-
- if (type_has_bits(dest_type) && !type_has_bits(src_type)) {
- ErrorMsg *msg = ir_add_error(ira, source_instr,
- buf_sprintf("'%s' and '%s' do not have the same in-memory representation",
- buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));
- add_error_note(ira->codegen, msg, ptr->source_node,
- buf_sprintf("'%s' has no in-memory bits", buf_ptr(&src_type->name)));
- add_error_note(ira->codegen, msg, dest_type_src->source_node,
- buf_sprintf("'%s' has in-memory bits", buf_ptr(&dest_type->name)));
- return ira->codegen->invalid_instruction;
- }
-
// Keep the bigger alignment, it can only help-
// unless the target is zero bits.
IrInstruction *result;
@@ -27136,15 +27138,16 @@ static IrInstruction *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstru
return ira->codegen->invalid_instruction;
}
- if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
+ bool has_bits;
+ if ((err = type_has_bits2(ira->codegen, dest_type, &has_bits)))
return ira->codegen->invalid_instruction;
- if (!type_has_bits(dest_type)) {
+
+ if (!has_bits) {
ir_add_error(ira, dest_type_value,
buf_sprintf("type '%s' has 0 bits and cannot store information", buf_ptr(&dest_type->name)));
return ira->codegen->invalid_instruction;
}
-
IrInstruction *target = instruction->target->child;
if (type_is_invalid(target->value->type))
return ira->codegen->invalid_instruction;
@@ -27182,9 +27185,11 @@ static IrInstruction *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstru
return ira->codegen->invalid_instruction;
}
- if ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusZeroBitsKnown)))
+ bool has_bits;
+ if ((err = type_has_bits2(ira->codegen, target->value->type, &has_bits)))
return ira->codegen->invalid_instruction;
- if (!type_has_bits(target->value->type)) {
+
+ if (!has_bits) {
ir_add_error(ira, target,
buf_sprintf("pointer to size 0 type has no address"));
return ira->codegen->invalid_instruction;
@@ -29067,9 +29072,10 @@ static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, La
break;
}
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
- if ((err = type_resolve(ira->codegen, param_type, ResolveStatusZeroBitsKnown)))
+ bool has_bits;
+ if ((err = type_has_bits2(ira->codegen, param_type, &has_bits)))
return nullptr;
- if (!type_has_bits(param_type)) {
+ if (!has_bits) {
ir_add_error(ira, param_type_inst,
buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 450f91b9be..a653e1662e 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -2,6 +2,16 @@ const tests = @import("tests.zig");
const builtin = @import("builtin");
pub fn addCases(cases: *tests.CompileErrorContext) void {
+ cases.add("comptime ptrcast of zero-sized type",
+ \\fn foo() void {
+ \\ const node: struct {} = undefined;
+ \\ const vla_ptr = @ptrCast([*]const u8, &node);
+ \\}
+ \\comptime { foo(); }
+ , &[_][]const u8{
+ "tmp.zig:3:21: error: '*const struct:2:17' and '[*]const u8' do not have the same in-memory representation",
+ });
+
cases.add("slice sentinel mismatch",
\\fn foo() [:0]u8 {
\\ var x: []u8 = undefined;
diff --git a/test/stage1/behavior/error.zig b/test/stage1/behavior/error.zig
index 7c1e093748..d4f64b7cff 100644
--- a/test/stage1/behavior/error.zig
+++ b/test/stage1/behavior/error.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const expect = std.testing.expect;
const expectError = std.testing.expectError;
+const expectEqual = std.testing.expectEqual;
const mem = std.mem;
const builtin = @import("builtin");
@@ -427,3 +428,17 @@ test "return result loc as peer result loc in inferred error set function" {
S.doTheTest();
comptime S.doTheTest();
}
+
+test "error payload type is correctly resolved" {
+ const MyIntWrapper = struct {
+ const Self = @This();
+
+ x: i32,
+
+ pub fn create() anyerror!Self {
+ return Self{ .x = 42 };
+ }
+ };
+
+ expectEqual(MyIntWrapper{ .x = 42 }, try MyIntWrapper.create());
+}