Skip to content

Commit cc7060d

Browse files
committed
compile error for C pointer with align attribute
See #1059
1 parent 973a93d commit cc7060d

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/ir.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -5057,6 +5057,11 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
50575057

50585058
IrInstruction *align_value;
50595059
if (align_expr != nullptr) {
5060+
if (ptr_len == PtrLenC) {
5061+
exec_add_error_node(irb->codegen, irb->exec, node,
5062+
buf_sprintf("[*c] pointers may not have align attribute"));
5063+
return irb->codegen->invalid_instruction;
5064+
}
50605065
align_value = ir_gen_node(irb, align_expr, scope);
50615066
if (align_value == irb->codegen->invalid_instruction)
50625067
return align_value;

test/compile_errors.zig

+8-4
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
118118
);
119119

120120
cases.addTest(
121-
"C pointer pointing to non C ABI compatible type",
121+
"C pointer pointing to non C ABI compatible type or has align attr",
122122
\\const Foo = struct {};
123-
\\export fn entry() [*c]Foo {
124-
\\ return undefined;
123+
\\export fn a() void {
124+
\\ const T = [*c]Foo;
125+
\\}
126+
\\export fn b() void {
127+
\\ const T = [*c]align(4) u8;
125128
\\}
126129
,
127-
".tmp_source.zig:2:19: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
130+
".tmp_source.zig:3:15: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
131+
".tmp_source.zig:6:15: error: [*c] pointers may not have align attribute",
128132
);
129133

130134
cases.addTest(

test/stage1/behavior/type_info.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ test "type info: C pointer type info" {
6767
}
6868

6969
fn testCPtr() void {
70-
const ptr_info = @typeInfo([*c]align(4) const i8);
70+
const ptr_info = @typeInfo([*c]const i8);
7171
expect(TypeId(ptr_info) == TypeId.Pointer);
7272
expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.C);
7373
expect(ptr_info.Pointer.is_const);
7474
expect(!ptr_info.Pointer.is_volatile);
75-
expect(ptr_info.Pointer.alignment == 4);
75+
expect(ptr_info.Pointer.alignment == 1);
7676
expect(ptr_info.Pointer.child == i8);
7777
}
7878

0 commit comments

Comments
 (0)