error message and test for alignment of variables of zero-bit types

This commit is contained in:
Sahnvour 2019-09-03 22:29:04 +02:00 committed by Andrew Kelley
parent a4ce10df80
commit ce14c543d1
3 changed files with 19 additions and 0 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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",
);
}