authorgravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-09-03 22:29:04+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-03 21:14:40-04:00
logce14c543d165efbd926ea6bd654d999c625b366f
tree8d4f08b3ff22dee7a98fd09b157f008ba114c035
parenta4ce10df8087e7051340e14e4acd018092f935f0

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


3 files changed, 19 insertions(+), 0 deletions(-)

src/analyze.cpp+4
......@@ -2671,6 +2671,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
26712671 }
26722672 }
26732673
2674 if (!type_has_bits(struct_type)) {
2675 assert(struct_type->abi_align == 0);
2676 }
2677
26742678 struct_type->data.structure.resolve_loop_flag_other = false;
26752679
26762680 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
src/ir.cpp+6
......@@ -14839,6 +14839,12 @@ static IrInstruction *ir_analyze_alloca(IrAnalyze *ira, IrInstruction *source_in
1483914839 if (align != 0) {
1484014840 if ((err = type_resolve(ira->codegen, var_type, ResolveStatusAlignmentKnown)))
1484114841 return ira->codegen->invalid_instruction;
14842 if (!type_has_bits(var_type)) {
14843 ir_add_error(ira, source_inst,
14844 buf_sprintf("variable '%s' of zero-bit type '%s' has no in-memory representation, it cannot be aligned",
14845 name_hint, buf_ptr(&var_type->name)));
14846 return ira->codegen->invalid_instruction;
14847 }
1484214848 }
1484314849 assert(result->base.value.data.x_ptr.special != ConstPtrSpecialInvalid);
1484414850
test/compile_errors.zig+9
......@@ -6462,4 +6462,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
64626462 "tmp.zig:5:30: error: expression value is ignored",
64636463 "tmp.zig:9:30: error: expression value is ignored",
64646464 );
6465
6466 cases.add(
6467 "aligned variable of zero-bit type",
6468 \\export fn f() void {
6469 \\ var s: struct {} align(4) = undefined;
6470 \\}
6471 ,
6472 "tmp.zig:2:5: error: variable 's' of zero-bit type 'struct:2:12' has no in-memory representation, it cannot be aligned",
6473 );
64656474}