From a859f94644cb362d84d3f9d72bc02b00a75fc32a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 14 Mar 2022 16:21:11 -0700 Subject: stage2: LLVM codegen of arrays should use type length, not value length It is possible for the value length to be longer than the type because we allow in-memory coercing of types such as `[5:0]u8` to `[5]u8`. In such a case, the value length is 6 but the type length if 5. The `.repeated` value type already got this right, so this is extending similar logic out to `.aggregate` and `.bytes`. Both scenarios are tested in behavior tests. Fixes #11165 --- test/behavior.zig | 1 + test/behavior/bugs/11165.zig | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 test/behavior/bugs/11165.zig (limited to 'test') diff --git a/test/behavior.zig b/test/behavior.zig index 7efe687c75..03d15645fa 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -62,6 +62,7 @@ test { _ = @import("behavior/bugs/11100.zig"); _ = @import("behavior/bugs/10970.zig"); _ = @import("behavior/bugs/11046.zig"); + _ = @import("behavior/bugs/11165.zig"); _ = @import("behavior/call.zig"); _ = @import("behavior/cast.zig"); _ = @import("behavior/comptime_memory.zig"); diff --git a/test/behavior/bugs/11165.zig b/test/behavior/bugs/11165.zig new file mode 100644 index 0000000000..d253c17b67 --- /dev/null +++ b/test/behavior/bugs/11165.zig @@ -0,0 +1,42 @@ +const builtin = @import("builtin"); + +test "bytes" { + const S = struct { + a: u32, + c: [5]u8, + }; + + const U = union { + s: S, + }; + + const s_1 = S{ + .a = undefined, + .c = "12345".*, // this caused problems + }; + _ = s_1; + + const u_2 = U{ .s = s_1 }; + _ = u_2; +} + +test "aggregate" { + const S = struct { + a: u32, + c: [5]u8, + }; + + const U = union { + s: S, + }; + + const c = [5:0]u8{ 1, 2, 3, 4, 5 }; + const s_1 = S{ + .a = undefined, + .c = c, // this caused problems + }; + _ = s_1; + + const u_2 = U{ .s = s_1 }; + _ = u_2; +} -- cgit v1.2.3