translate-c: Add support for cast-to-union

Fixes #10955
This commit is contained in:
Evan Haas
2022-02-21 13:18:18 -08:00
committed by Veikka Tuominen
parent 4a0b037464
commit 9716a1c3ab
8 changed files with 95 additions and 6 deletions

View File

@@ -1829,4 +1829,26 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0;
\\}
, "");
cases.add("Cast-to-union. Issue #10955",
\\#include <stdlib.h>
\\struct S { int x; };
\\union U {
\\ long l;
\\ double d;
\\ struct S s;
\\};
\\union U bar(union U u) { return u; }
\\int main(void) {
\\ union U u = (union U) 42L;
\\ if (u.l != 42L) abort();
\\ u = (union U) 2.0;
\\ if (u.d != 2.0) abort();
\\ u = bar((union U)4.0);
\\ if (u.d != 4.0) abort();
\\ u = (union U)(struct S){ .x = 5 };
\\ if (u.s.x != 5) abort();
\\ return 0;
\\}
, "");
}