aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-06-06 16:12:31 -0400
committerGitHub <noreply@github.com>2022-06-06 16:12:31 -0400
commit367e2b2fe43a2de09767ad8d5657866088b44678 (patch)
tree686b57ecfcfd472d70493c46c60a562b96d5ad31 /test
parent41bf81dc3231eb763c93eb95b152e7ab8d3c5af8 (diff)
parent14685e59b26c8dc002ce6c25c6916cbad54e79d0 (diff)
downloadzig-367e2b2fe43a2de09767ad8d5657866088b44678.tar.gz
zig-367e2b2fe43a2de09767ad8d5657866088b44678.zip
Merge pull request #11800 from Vexu/stage2
`zig2 build test-std` progress
Diffstat (limited to 'test')
-rw-r--r--test/behavior/basic.zig33
-rw-r--r--test/behavior/struct.zig22
-rw-r--r--test/cases/compile_errors/invalid_store_to_comptime_field.zig20
3 files changed, 75 insertions, 0 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index 3129091091..a69df862c1 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -1,5 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
+const assert = std.debug.assert;
const mem = std.mem;
const expect = std.testing.expect;
const expectEqualStrings = std.testing.expectEqualStrings;
@@ -1053,3 +1054,35 @@ test "const alloc with comptime known initializer is made comptime known" {
if (u.a == 0) @compileError("bad");
}
}
+
+comptime {
+ // coerce result ptr outside a function
+ const S = struct { a: comptime_int };
+ var s: S = undefined;
+ s = S{ .a = 1 };
+ assert(s.a == 1);
+}
+
+test "switch inside @as gets correct type" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+
+ var a: u32 = 0;
+ var b: [2]u32 = undefined;
+ b[0] = @as(u32, switch (a) {
+ 1 => 1,
+ else => 0,
+ });
+}
+
+test "inline call of function with a switch inside the return statement" {
+ const S = struct {
+ inline fn foo(x: anytype) @TypeOf(x) {
+ return switch (x) {
+ 1 => 1,
+ else => unreachable,
+ };
+ }
+ };
+ try expect(S.foo(1) == 1);
+}
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