Commit ce14c543d1

Sahnvour <sahnvour@pm.me>
2019-09-03 22:29:04
error message and test for alignment of variables of zero-bit types
1 parent a4ce10d
Changed files (3)
src/analyze.cpp
@@ -2671,6 +2671,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
         }
     }
 
+    if (!type_has_bits(struct_type)) {
+        assert(struct_type->abi_align == 0);
+    }
+
     struct_type->data.structure.resolve_loop_flag_other = false;
 
     if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
src/ir.cpp
@@ -14839,6 +14839,12 @@ static IrInstruction *ir_analyze_alloca(IrAnalyze *ira, IrInstruction *source_in
     if (align != 0) {
         if ((err = type_resolve(ira->codegen, var_type, ResolveStatusAlignmentKnown)))
             return ira->codegen->invalid_instruction;
+        if (!type_has_bits(var_type)) {
+                ir_add_error(ira, source_inst,
+                    buf_sprintf("variable '%s' of zero-bit type '%s' has no in-memory representation, it cannot be aligned",
+                        name_hint, buf_ptr(&var_type->name)));
+            return ira->codegen->invalid_instruction;
+        }
     }
     assert(result->base.value.data.x_ptr.special != ConstPtrSpecialInvalid);
 
test/compile_errors.zig
@@ -6462,4 +6462,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
         "tmp.zig:5:30: error: expression value is ignored",
         "tmp.zig:9:30: error: expression value is ignored",
     );
+
+    cases.add(
+        "aligned variable of zero-bit type",
+        \\export fn f() void {
+        \\    var s: struct {} align(4) = undefined;
+        \\}
+    ,
+        "tmp.zig:2:5: error: variable 's' of zero-bit type 'struct:2:12' has no in-memory representation, it cannot be aligned",
+    );
 }