commit ce14c543d165efbd926ea6bd654d999c625b366f (tree)
parent a4ce10df8087e7051340e14e4acd018092f935f0
Author: Sahnvour <sahnvour@pm.me>
Date: Tue, 3 Sep 2019 22:29:04 +0200
error message and test for alignment of variables of zero-bit types
Diffstat:
3 files changed, 19 insertions(+), 0 deletions(-)
diff --git a/src/analyze.cpp b/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) {
diff --git a/src/ir.cpp b/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);
diff --git a/test/compile_errors.zig b/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",
+ );
}