From cc7060d0d934135d797bd2bc24288ecab095051a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 14 Feb 2019 19:53:46 -0500 Subject: [PATCH] compile error for C pointer with align attribute See #1059 --- src/ir.cpp | 5 +++++ test/compile_errors.zig | 12 ++++++++---- test/stage1/behavior/type_info.zig | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 7c46b2171..6e190adf6 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5057,6 +5057,11 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode IrInstruction *align_value; if (align_expr != nullptr) { + if (ptr_len == PtrLenC) { + exec_add_error_node(irb->codegen, irb->exec, node, + buf_sprintf("[*c] pointers may not have align attribute")); + return irb->codegen->invalid_instruction; + } align_value = ir_gen_node(irb, align_expr, scope); if (align_value == irb->codegen->invalid_instruction) return align_value; diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 5e9b69164..ab9eda3f1 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -118,13 +118,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ); cases.addTest( - "C pointer pointing to non C ABI compatible type", + "C pointer pointing to non C ABI compatible type or has align attr", \\const Foo = struct {}; - \\export fn entry() [*c]Foo { - \\ return undefined; + \\export fn a() void { + \\ const T = [*c]Foo; + \\} + \\export fn b() void { + \\ const T = [*c]align(4) u8; \\} , - ".tmp_source.zig:2:19: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'", + ".tmp_source.zig:3:15: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'", + ".tmp_source.zig:6:15: error: [*c] pointers may not have align attribute", ); cases.addTest( diff --git a/test/stage1/behavior/type_info.zig b/test/stage1/behavior/type_info.zig index dc185cc96..52e03c2d7 100644 --- a/test/stage1/behavior/type_info.zig +++ b/test/stage1/behavior/type_info.zig @@ -67,12 +67,12 @@ test "type info: C pointer type info" { } fn testCPtr() void { - const ptr_info = @typeInfo([*c]align(4) const i8); + const ptr_info = @typeInfo([*c]const i8); expect(TypeId(ptr_info) == TypeId.Pointer); expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.C); expect(ptr_info.Pointer.is_const); expect(!ptr_info.Pointer.is_volatile); - expect(ptr_info.Pointer.alignment == 4); + expect(ptr_info.Pointer.alignment == 1); expect(ptr_info.Pointer.child == i8); }