Skip to content

"union that needs padding bytes inside an array" behavior test trips LLVM assertion: "Initializer for struct element doesn't match" #11971

@andrewrk

Description

@andrewrk

Zig Version: 0.10.0-dev.2803+c248af3bd

const B = union(enum) {
    D: u8,
    E: u16,
};

const A = union(enum) {
    B: B,
    C: u8,
};

test "union that needs padding bytes inside an array" {
    var as = [_]A{
        A{ .B = B{ .D = 1 } },
        A{ .B = B{ .D = 1 } },
    };

    const a = as[0].B;
    try std.testing.expect(a.D == 1);
}
$ cmake ..  -DCMAKE_PREFIX_PATH=$HOME/local/llvm13-debug  -DZIG_SKIP_INSTALL_LIB_FILES=ON
$ make -j10 && gdb ./zig -ex 'run test ../test/behavior./zig -I../test -fno-stage1'
zig: /home/andy/Downloads/llvm-project-13/llvm/lib/IR/Constants.cpp:1253: llvm::ConstantAggregate::ConstantAggregate(llvm::Type*, llvm::Value::ValueTy, llvm::ArrayRef<llvm::Constant*>): Assertion `V[I]->getType() == ST->getTypeAtIndex(I) && "Initializer for struct element doesn't match!"' failed.
#10 0x0000000008447ef5 in LLVMConstNamedStruct (StructTy=0x10f0cb10, ConstantVals=0x7ffffffed350, Count=3) at /home/andy/Downloads/llvm-project-13/llvm/lib/IR/Core.cpp:1472
1472	  return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
(gdb) p Ty->dump()
%behavior.bugs.1381.A = type { %behavior.bugs.1381.B, i1, [1 x i8] }
(gdb) p Elements[0]->dump()
{ <{ i8, [1 x i8] }>, i1, [1 x i8] } { <{ i8, [1 x i8] }> <{ i8 1, [1 x i8] undef }>, i1 false, [1 x i8] undef }
(gdb) p Count
$7 = 3
#11 0x00000000010445aa in codegen.llvm.DeclGen.lowerValue (dg=0x7fffffff0480, tv=...) at /home/andy/Downloads/zig/src/codegen/llvm.zig:3454
3454	                    return llvm_union_ty.constNamedStruct(&fields, fields_len);
%behavior.bugs.1381.B = type { i16, i1, [1 x i8] }

Metadata

Metadata

Assignees

Labels

backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions