| author | |
| committer | |
| log | 6bba7c702b4482c33c1de7414fb145d0dfc40fcc |
| tree | 847637921287c6b4c5f6e1f23bb971381bc74142 |
| parent | 35391f1709bb413e277ede1178d4714473561565 |
2 files changed, 39 insertions(+), 0 deletions(-)
src/ir.cpp+13| ... | @@ -29098,6 +29098,19 @@ static IrInstGen *ir_align_cast(IrAnalyze *ira, IrInstGen *target, uint32_t alig | ... | @@ -29098,6 +29098,19 @@ static IrInstGen *ir_align_cast(IrAnalyze *ira, IrInstGen *target, uint32_t alig |
| 29098 | ZigType *result_type; | 29098 | ZigType *result_type; |
| 29099 | uint32_t old_align_bytes; | 29099 | uint32_t old_align_bytes; |
| 29100 | 29100 | ||
| 29101 | ZigType *actual_ptr = target_type; | ||
| 29102 | if (actual_ptr->id == ZigTypeIdOptional) { | ||
| 29103 | actual_ptr = actual_ptr->data.maybe.child_type; | ||
| 29104 | } else if (is_slice(actual_ptr)) { | ||
| 29105 | actual_ptr = actual_ptr->data.structure.fields[slice_ptr_index]->type_entry; | ||
| 29106 | } | ||
| 29107 | |||
| 29108 | if (safety_check_on && !type_has_bits(ira->codegen, actual_ptr)) { | ||
| 29109 | ir_add_error(ira, &target->base, | ||
| 29110 | buf_sprintf("cannot adjust alignment of zero sized type '%s'", buf_ptr(&target_type->name))); | ||
| 29111 | return ira->codegen->invalid_inst_gen; | ||
| 29112 | } | ||
| 29113 | |||
| 29101 | if (target_type->id == ZigTypeIdPointer) { | 29114 | if (target_type->id == ZigTypeIdPointer) { |
| 29102 | result_type = adjust_ptr_align(ira->codegen, target_type, align_bytes); | 29115 | result_type = adjust_ptr_align(ira->codegen, target_type, align_bytes); |
| 29103 | if ((err = resolve_ptr_align(ira, target_type, &old_align_bytes))) | 29116 | if ((err = resolve_ptr_align(ira, target_type, &old_align_bytes))) |
test/compile_errors.zig+26| ... | @@ -2,6 +2,32 @@ const tests = @import("tests.zig"); | ... | @@ -2,6 +2,32 @@ const tests = @import("tests.zig"); |
| 2 | const std = @import("std"); | 2 | const std = @import("std"); |
| 3 | 3 | ||
| 4 | pub fn addCases(cases: *tests.CompileErrorContext) void { | 4 | pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 5 | cases.addTest("@alignCast of zero sized types", | ||
| 6 | \\export fn foo() void { | ||
| 7 | \\ const a: *void = undefined; | ||
| 8 | \\ _ = @alignCast(2, a); | ||
| 9 | \\} | ||
| 10 | \\export fn bar() void { | ||
| 11 | \\ const a: ?*void = undefined; | ||
| 12 | \\ _ = @alignCast(2, a); | ||
| 13 | \\} | ||
| 14 | \\export fn baz() void { | ||
| 15 | \\ const a: []void = undefined; | ||
| 16 | \\ _ = @alignCast(2, a); | ||
| 17 | \\} | ||
| 18 | \\export fn qux() void { | ||
| 19 | \\ const a = struct { | ||
| 20 | \\ fn a(comptime b: u32) void {} | ||
| 21 | \\ }.a; | ||
| 22 | \\ _ = @alignCast(2, a); | ||
| 23 | \\} | ||
| 24 | , &[_][]const u8{ | ||
| 25 | "tmp.zig:3:23: error: cannot adjust alignment of zero sized type '*void'", | ||
| 26 | "tmp.zig:7:23: error: cannot adjust alignment of zero sized type '?*void'", | ||
| 27 | "tmp.zig:11:23: error: cannot adjust alignment of zero sized type '[]void'", | ||
| 28 | "tmp.zig:17:23: error: cannot adjust alignment of zero sized type 'fn(u32) anytype'", | ||
| 29 | }); | ||
| 30 | |||
| 5 | cases.addTest("invalid pointer with @Type", | 31 | cases.addTest("invalid pointer with @Type", |
| 6 | \\export fn entry() void { | 32 | \\export fn entry() void { |
| 7 | \\ _ = @Type(.{ .Pointer = .{ | 33 | \\ _ = @Type(.{ .Pointer = .{ |