aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/behavior/basic.zig2
-rw-r--r--test/behavior/cast.zig122
-rw-r--r--test/cases/compile_errors/assign_local_bad_coercion.zig22
-rw-r--r--test/cases/compile_errors/break_void_result_location.zig2
-rw-r--r--test/cases/compile_errors/cast_without_result_type.zig28
-rw-r--r--test/cases/compile_errors/cast_without_result_type_due_to_generic_parameter.zig31
-rw-r--r--test/cases/compile_errors/nested_error_set_mismatch.zig6
-rw-r--r--test/cases/compile_errors/nested_ptr_cast_bad_operand.zig22
-rw-r--r--test/cases/compile_errors/redundant_ptr_cast.zig19
-rw-r--r--test/cases/compile_errors/union_init_with_none_or_multiple_fields.zig2
-rw-r--r--test/cbe.zig9
-rw-r--r--test/compare_output.zig2
-rw-r--r--test/run_translated_c.zig2
13 files changed, 257 insertions, 12 deletions
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index 87cbb3e242..57a579cfdb 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -647,7 +647,7 @@ test "multiline string literal is null terminated" {
\\three
;
const s2 = "one\ntwo)\nthree";
- try expect(std.cstr.cmp(s1, s2) == 0);
+ try expect(std.mem.orderZ(u8, s1, s2) == .eq);
}
test "string escapes" {
diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig
index e6aa53bd41..731d74909c 100644
--- a/test/behavior/cast.zig
+++ b/test/behavior/cast.zig
@@ -2219,3 +2219,125 @@ test "peer type resolution: pointer attributes are combined correctly" {
try expectEqualSlices(u8, std.mem.span(@volatileCast(r2)), "bar");
try expectEqualSlices(u8, std.mem.span(@volatileCast(r3)), "baz");
}
+
+test "cast builtins can wrap result in optional" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
+
+ const S = struct {
+ const MyEnum = enum(u32) { _ };
+ fn a() ?MyEnum {
+ return @enumFromInt(123);
+ }
+ fn b() ?u32 {
+ return @intFromFloat(42.50);
+ }
+ fn c() ?*const f32 {
+ const x: u32 = 1;
+ return @ptrCast(&x);
+ }
+
+ fn doTheTest() !void {
+ const ra = a() orelse return error.ImpossibleError;
+ const rb = b() orelse return error.ImpossibleError;
+ const rc = c() orelse return error.ImpossibleError;
+
+ comptime assert(@TypeOf(ra) == MyEnum);
+ comptime assert(@TypeOf(rb) == u32);
+ comptime assert(@TypeOf(rc) == *const f32);
+
+ try expect(@intFromEnum(ra) == 123);
+ try expect(rb == 42);
+ try expect(@as(*const u32, @ptrCast(rc)).* == 1);
+ }
+ };
+
+ try S.doTheTest();
+ try comptime S.doTheTest();
+}
+
+test "cast builtins can wrap result in error union" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
+
+ const S = struct {
+ const MyEnum = enum(u32) { _ };
+ const E = error{ImpossibleError};
+ fn a() E!MyEnum {
+ return @enumFromInt(123);
+ }
+ fn b() E!u32 {
+ return @intFromFloat(42.50);
+ }
+ fn c() E!*const f32 {
+ const x: u32 = 1;
+ return @ptrCast(&x);
+ }
+
+ fn doTheTest() !void {
+ const ra = try a();
+ const rb = try b();
+ const rc = try c();
+
+ comptime assert(@TypeOf(ra) == MyEnum);
+ comptime assert(@TypeOf(rb) == u32);
+ comptime assert(@TypeOf(rc) == *const f32);
+
+ try expect(@intFromEnum(ra) == 123);
+ try expect(rb == 42);
+ try expect(@as(*const u32, @ptrCast(rc)).* == 1);
+ }
+ };
+
+ try S.doTheTest();
+ try comptime S.doTheTest();
+}
+
+test "cast builtins can wrap result in error union and optional" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
+
+ const S = struct {
+ const MyEnum = enum(u32) { _ };
+ const E = error{ImpossibleError};
+ fn a() E!?MyEnum {
+ return @enumFromInt(123);
+ }
+ fn b() E!?u32 {
+ return @intFromFloat(42.50);
+ }
+ fn c() E!?*const f32 {
+ const x: u32 = 1;
+ return @ptrCast(&x);
+ }
+
+ fn doTheTest() !void {
+ const ra = try a() orelse return error.ImpossibleError;
+ const rb = try b() orelse return error.ImpossibleError;
+ const rc = try c() orelse return error.ImpossibleError;
+
+ comptime assert(@TypeOf(ra) == MyEnum);
+ comptime assert(@TypeOf(rb) == u32);
+ comptime assert(@TypeOf(rc) == *const f32);
+
+ try expect(@intFromEnum(ra) == 123);
+ try expect(rb == 42);
+ try expect(@as(*const u32, @ptrCast(rc)).* == 1);
+ }
+ };
+
+ try S.doTheTest();
+ try comptime S.doTheTest();
+}
diff --git a/test/cases/compile_errors/assign_local_bad_coercion.zig b/test/cases/compile_errors/assign_local_bad_coercion.zig
new file mode 100644
index 0000000000..0aa93f48bd
--- /dev/null
+++ b/test/cases/compile_errors/assign_local_bad_coercion.zig
@@ -0,0 +1,22 @@
+fn g() u64 {
+ return 0;
+}
+
+export fn constEntry() u32 {
+ const x: u32 = g();
+ return x;
+}
+
+export fn varEntry() u32 {
+ var x: u32 = g();
+ return x;
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :6:21: error: expected type 'u32', found 'u64'
+// :6:21: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
+// :11:19: error: expected type 'u32', found 'u64'
+// :11:19: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
diff --git a/test/cases/compile_errors/break_void_result_location.zig b/test/cases/compile_errors/break_void_result_location.zig
index 696ea39667..bd15aa6a77 100644
--- a/test/cases/compile_errors/break_void_result_location.zig
+++ b/test/cases/compile_errors/break_void_result_location.zig
@@ -29,4 +29,4 @@ export fn f4() void {
// :2:22: error: expected type 'usize', found 'void'
// :7:9: error: expected type 'usize', found 'void'
// :14:9: error: expected type 'usize', found 'void'
-// :18:1: error: expected type 'usize', found 'void'
+// :19:27: error: expected type 'usize', found 'void'
diff --git a/test/cases/compile_errors/cast_without_result_type.zig b/test/cases/compile_errors/cast_without_result_type.zig
new file mode 100644
index 0000000000..3d8a3d5412
--- /dev/null
+++ b/test/cases/compile_errors/cast_without_result_type.zig
@@ -0,0 +1,28 @@
+export fn a() void {
+ _ = @ptrFromInt(123);
+}
+export fn b() void {
+ const x = @ptrCast(@alignCast(@as(*u8, undefined)));
+ _ = x;
+}
+export fn c() void {
+ _ = &@intCast(@as(u64, 123));
+ _ = S;
+}
+export fn d() void {
+ var x: f32 = 0;
+ _ = x + @floatFromInt(123);
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :2:9: error: @ptrFromInt must have a known result type
+// :2:9: note: use @as to provide explicit result type
+// :5:15: error: @ptrCast must have a known result type
+// :5:15: note: use @as to provide explicit result type
+// :9:10: error: @intCast must have a known result type
+// :9:10: note: use @as to provide explicit result type
+// :14:13: error: @floatFromInt must have a known result type
+// :14:13: note: use @as to provide explicit result type
diff --git a/test/cases/compile_errors/cast_without_result_type_due_to_generic_parameter.zig b/test/cases/compile_errors/cast_without_result_type_due_to_generic_parameter.zig
new file mode 100644
index 0000000000..a9006c5352
--- /dev/null
+++ b/test/cases/compile_errors/cast_without_result_type_due_to_generic_parameter.zig
@@ -0,0 +1,31 @@
+export fn a() void {
+ bar(@ptrFromInt(123));
+}
+export fn b() void {
+ bar(@ptrCast(@alignCast(@as(*u8, undefined))));
+}
+export fn c() void {
+ bar(@intCast(@as(u64, 123)));
+}
+export fn d() void {
+ bar(@floatFromInt(123));
+}
+
+fn bar(_: anytype) void {}
+
+// error
+// backend=stage2
+// target=native
+//
+// :2:9: error: @ptrFromInt must have a known result type
+// :2:9: note: result type is unknown due to anytype parameter
+// :2:9: note: use @as to provide explicit result type
+// :5:9: error: @ptrCast must have a known result type
+// :5:9: note: result type is unknown due to anytype parameter
+// :5:9: note: use @as to provide explicit result type
+// :8:9: error: @intCast must have a known result type
+// :8:9: note: result type is unknown due to anytype parameter
+// :8:9: note: use @as to provide explicit result type
+// :11:9: error: @floatFromInt must have a known result type
+// :11:9: note: result type is unknown due to anytype parameter
+// :11:9: note: use @as to provide explicit result type
diff --git a/test/cases/compile_errors/nested_error_set_mismatch.zig b/test/cases/compile_errors/nested_error_set_mismatch.zig
index c0d68a5e53..3addd5ee43 100644
--- a/test/cases/compile_errors/nested_error_set_mismatch.zig
+++ b/test/cases/compile_errors/nested_error_set_mismatch.zig
@@ -14,6 +14,6 @@ fn foo() ?OtherError!i32 {
// backend=llvm
// target=native
//
-// :4:1: error: expected type '?error{NextError}!i32', found '?error{OutOfMemory}!i32'
-// :4:1: note: optional type child 'error{OutOfMemory}!i32' cannot cast into optional type child 'error{NextError}!i32'
-// :4:1: note: 'error.OutOfMemory' not a member of destination error set
+// :5:34: error: expected type '?error{NextError}!i32', found '?error{OutOfMemory}!i32'
+// :5:34: note: optional type child 'error{OutOfMemory}!i32' cannot cast into optional type child 'error{NextError}!i32'
+// :5:34: note: 'error.OutOfMemory' not a member of destination error set
diff --git a/test/cases/compile_errors/nested_ptr_cast_bad_operand.zig b/test/cases/compile_errors/nested_ptr_cast_bad_operand.zig
new file mode 100644
index 0000000000..ec7ee3075c
--- /dev/null
+++ b/test/cases/compile_errors/nested_ptr_cast_bad_operand.zig
@@ -0,0 +1,22 @@
+const p: ?*const u8 = null;
+export fn a() void {
+ _ = @as(*const u32, @ptrCast(@alignCast(p)));
+}
+export fn b() void {
+ _ = @constCast(@volatileCast(123));
+}
+export fn c() void {
+ const x: ?*f32 = @constCast(@ptrCast(@addrSpaceCast(@volatileCast(p))));
+ _ = x;
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :3:45: error: null pointer casted to type '*const u32'
+// :6:34: error: expected pointer type, found 'comptime_int'
+// :9:22: error: cast increases pointer alignment
+// :9:71: note: '?*const u8' has alignment '1'
+// :9:22: note: '?*f32' has alignment '4'
+// :9:22: note: use @alignCast to assert pointer alignment
diff --git a/test/cases/compile_errors/redundant_ptr_cast.zig b/test/cases/compile_errors/redundant_ptr_cast.zig
new file mode 100644
index 0000000000..933eeb2719
--- /dev/null
+++ b/test/cases/compile_errors/redundant_ptr_cast.zig
@@ -0,0 +1,19 @@
+const p: *anyopaque = undefined;
+export fn a() void {
+ _ = @ptrCast(@ptrCast(p));
+}
+export fn b() void {
+ const ptr1: *u32 = @alignCast(@ptrCast(@alignCast(p)));
+ _ = ptr1;
+}
+export fn c() void {
+ _ = @constCast(@alignCast(@ptrCast(@constCast(@volatileCast(p)))));
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :3:18: error: redundant @ptrCast
+// :6:44: error: redundant @alignCast
+// :10:40: error: redundant @constCast
diff --git a/test/cases/compile_errors/union_init_with_none_or_multiple_fields.zig b/test/cases/compile_errors/union_init_with_none_or_multiple_fields.zig
index f808ec8227..be99c394d3 100644
--- a/test/cases/compile_errors/union_init_with_none_or_multiple_fields.zig
+++ b/test/cases/compile_errors/union_init_with_none_or_multiple_fields.zig
@@ -27,7 +27,7 @@ export fn u2m() void {
// backend=stage2
// target=native
//
-// :9:1: error: union initializer must initialize one field
+// :10:20: error: union initializer must initialize one field
// :1:12: note: union declared here
// :14:20: error: cannot initialize multiple union fields at once; unions can only have one active field
// :14:31: note: additional initializer here
diff --git a/test/cbe.zig b/test/cbe.zig
index b56202c7e5..7eb212b2fe 100644
--- a/test/cbe.zig
+++ b/test/cbe.zig
@@ -1,5 +1,6 @@
const std = @import("std");
const Cases = @import("src/Cases.zig");
+const nl = if (@import("builtin").os.tag == .windows) "\r\n" else "\n";
// These tests should work with all platforms, but we're using linux_x64 for
// now for consistency. Will be expanded eventually.
@@ -19,7 +20,7 @@ pub fn addCases(ctx: *Cases) !void {
\\ _ = puts("hello world!");
\\ return 0;
\\}
- , "hello world!" ++ std.cstr.line_sep);
+ , "hello world!" ++ nl);
// Now change the message only
case.addCompareOutput(
@@ -28,7 +29,7 @@ pub fn addCases(ctx: *Cases) !void {
\\ _ = puts("yo");
\\ return 0;
\\}
- , "yo" ++ std.cstr.line_sep);
+ , "yo" ++ nl);
// Add an unused Decl
case.addCompareOutput(
@@ -38,7 +39,7 @@ pub fn addCases(ctx: *Cases) !void {
\\ return 0;
\\}
\\fn unused() void {}
- , "yo!" ++ std.cstr.line_sep);
+ , "yo!" ++ nl);
// Comptime return type and calling convention expected.
case.addError(
@@ -67,7 +68,7 @@ pub fn addCases(ctx: *Cases) !void {
\\ _ = printf("Hello, %s!\n", "world");
\\ return 0;
\\}
- , "Hello, world!" ++ std.cstr.line_sep);
+ , "Hello, world!" ++ nl);
}
{
diff --git a/test/compare_output.zig b/test/compare_output.zig
index 92dfd76b58..bcdc642a1e 100644
--- a/test/compare_output.zig
+++ b/test/compare_output.zig
@@ -15,7 +15,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ _ = c.puts("Hello, world!");
\\ return 0;
\\}
- , "Hello, world!" ++ std.cstr.line_sep);
+ , "Hello, world!" ++ if (@import("builtin").os.tag == .windows) "\r\n" else "\n");
cases.add("hello world without libc",
\\const io = @import("std").io;
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig
index a64a3eb7a4..fe32d376d2 100644
--- a/test/run_translated_c.zig
+++ b/test/run_translated_c.zig
@@ -1,6 +1,6 @@
const std = @import("std");
const tests = @import("tests.zig");
-const nl = std.cstr.line_sep;
+const nl = if (@import("builtin").os.tag == .windows) "\r\n" else "\n";
pub fn addCases(cases: *tests.RunTranslatedCContext) void {
cases.add("dereference address of",