From cb5d2b691aadde5665cefc54542e3e0651ebc2fa Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 4 Jun 2022 13:12:55 +0300 Subject: Sema: validate equality on store to comptime field --- test/behavior/struct.zig | 22 ++++++++++++++++++++++ .../invalid_store_to_comptime_field.zig | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/cases/compile_errors/invalid_store_to_comptime_field.zig (limited to 'test') diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index 5cbb8e973e..624f1609d4 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1336,3 +1336,25 @@ test "packed struct field access via pointer" { try S.doTheTest(); comptime try S.doTheTest(); } + +test "store to comptime field" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + + { + const S = struct { + comptime a: [2]u32 = [2]u32{ 1, 2 }, + }; + var s: S = .{}; + s.a = [2]u32{ 1, 2 }; + s.a[0] = 1; + } + { + const T = struct { a: u32, b: u32 }; + const S = struct { + comptime a: T = T{ .a = 1, .b = 2 }, + }; + var s: S = .{}; + s.a = T{ .a = 1, .b = 2 }; + s.a.a = 1; + } +} diff --git a/test/cases/compile_errors/invalid_store_to_comptime_field.zig b/test/cases/compile_errors/invalid_store_to_comptime_field.zig new file mode 100644 index 0000000000..3bbd9bbaa8 --- /dev/null +++ b/test/cases/compile_errors/invalid_store_to_comptime_field.zig @@ -0,0 +1,20 @@ +pub export fn entry() void { + const S = struct { + comptime a: [2]u32 = [2]u32{ 1, 2 }, + }; + var s: S = .{}; + s.a = [2]u32{ 2, 2 }; +} +pub export fn entry1() void { + const T = struct { a: u32, b: u32 }; + const S = struct { + comptime a: T = T{ .a = 1, .b = 2 }, + }; + var s: S = .{}; + s.a = T{ .a = 2, .b = 2 }; +} +// error +// backend=stage2,llvm +// +// :6:19: error: value stored in comptime field does not match the default value of the field +// :14:19: error: value stored in comptime field does not match the default value of the field -- cgit v1.2.3