better handling of arrays in packed structs

* Separate LoadPtr IR instructions into pass1 and pass2 variants.
 * Define `type_size_bits` for extern structs to be the same as
   their `@sizeOf(T) * 8` and allow them in packed structs.
 * More helpful error messages when trying to use types in
   packed structs that are not allowed.
 * Support arrays in packed structs even when they are not
   byte-aligned.
 * Add compile error for using arrays in packed structs when the
   padding bits would be problematic. This is necessary since
   we do not have packed arrays.

closes #677
This commit is contained in:
Andrew Kelley
2019-02-21 14:44:14 -05:00
parent 2bb795dc45
commit 1066004b79
7 changed files with 248 additions and 52 deletions

View File

@@ -1,6 +1,60 @@
const tests = @import("tests.zig");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.addTest(
"packed struct with fields of not allowed types",
\\const A = packed struct {
\\ x: anyerror,
\\};
\\const B = packed struct {
\\ x: [2]u24,
\\};
\\const C = packed struct {
\\ x: [1]anyerror,
\\};
\\const D = packed struct {
\\ x: [1]S,
\\};
\\const E = packed struct {
\\ x: [1]U,
\\};
\\const F = packed struct {
\\ x: ?anyerror,
\\};
\\const G = packed struct {
\\ x: Enum,
\\};
\\export fn entry() void {
\\ var a: A = undefined;
\\ var b: B = undefined;
\\ var r: C = undefined;
\\ var d: D = undefined;
\\ var e: E = undefined;
\\ var f: F = undefined;
\\ var g: G = undefined;
\\}
\\const S = struct {
\\ x: i32,
\\};
\\const U = struct {
\\ A: i32,
\\ B: u32,
\\};
\\const Enum = enum {
\\ A,
\\ B,
\\};
,
".tmp_source.zig:2:5: error: type 'anyerror' not allowed in packed struct; no guaranteed in-memory representation",
".tmp_source.zig:5:5: error: array of 'u24' not allowed in packed struct due to padding bits",
".tmp_source.zig:8:5: error: type 'anyerror' not allowed in packed struct; no guaranteed in-memory representation",
".tmp_source.zig:11:5: error: non-packed, non-extern struct 'S' not allowed in packed struct; no guaranteed in-memory representation",
".tmp_source.zig:14:5: error: non-packed, non-extern struct 'U' not allowed in packed struct; no guaranteed in-memory representation",
".tmp_source.zig:17:5: error: type '?anyerror' not allowed in packed struct; no guaranteed in-memory representation",
".tmp_source.zig:20:5: error: type 'Enum' not allowed in packed struct; no guaranteed in-memory representation",
".tmp_source.zig:38:14: note: enum declaration does not specify an integer tag type",
);
cases.addCase(x: {
var tc = cases.create(
"deduplicate undeclared identifier",