aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/cli.zig10
-rw-r--r--test/compile_errors.zig46
-rw-r--r--test/run_translated_c.zig128
-rw-r--r--test/runtime_safety.zig2
-rw-r--r--test/stage1/behavior/bugs/1322.zig4
-rw-r--r--test/stage1/behavior/enum.zig15
-rw-r--r--test/stage1/behavior/fn.zig2
-rw-r--r--test/stage1/behavior/type_info.zig2
-rw-r--r--test/stage1/behavior/union.zig45
-rw-r--r--test/stage2/cbe.zig48
-rw-r--r--test/stage2/test.zig47
-rw-r--r--test/stage2/wasm.zig92
-rw-r--r--test/standalone/cat/main.zig2
-rw-r--r--test/tests.zig2
-rw-r--r--test/translate_c.zig76
15 files changed, 383 insertions, 138 deletions
diff --git a/test/cli.zig b/test/cli.zig
index 33dbc2d62b..c0702fa54c 100644
--- a/test/cli.zig
+++ b/test/cli.zig
@@ -51,9 +51,9 @@ fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 {
}
fn printCmd(cwd: []const u8, argv: []const []const u8) void {
- std.debug.warn("cd {} && ", .{cwd});
+ std.debug.warn("cd {s} && ", .{cwd});
for (argv) |arg| {
- std.debug.warn("{} ", .{arg});
+ std.debug.warn("{s} ", .{arg});
}
std.debug.warn("\n", .{});
}
@@ -75,14 +75,14 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
if ((code != 0) == expect_0) {
std.debug.warn("The following command exited with error code {}:\n", .{code});
printCmd(cwd, argv);
- std.debug.warn("stderr:\n{}\n", .{result.stderr});
+ std.debug.warn("stderr:\n{s}\n", .{result.stderr});
return error.CommandFailed;
}
},
else => {
std.debug.warn("The following command terminated unexpectedly:\n", .{});
printCmd(cwd, argv);
- std.debug.warn("stderr:\n{}\n", .{result.stderr});
+ std.debug.warn("stderr:\n{s}\n", .{result.stderr});
return error.CommandFailed;
},
}
@@ -113,7 +113,7 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
\\ return num * num;
\\}
\\extern fn zig_panic() noreturn;
- \\pub inline fn panic(msg: []const u8, error_return_trace: ?*@import("builtin").StackTrace) noreturn {
+ \\pub fn panic(msg: []const u8, error_return_trace: ?*@import("builtin").StackTrace) noreturn {
\\ zig_panic();
\\}
);
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index f936eb49a7..2fb4c36ed4 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -323,7 +323,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ e: E,
\\};
\\export fn entry() void {
- \\ if (@TagType(E) != u8) @compileError("did not infer u8 tag type");
+ \\ if (@typeInfo(E).Enum.tag_type != u8) @compileError("did not infer u8 tag type");
\\ const s: S = undefined;
\\}
, &[_][]const u8{
@@ -1648,7 +1648,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ @call(.{ .modifier = .compile_time }, baz, .{});
\\}
\\fn foo() void {}
- \\inline fn bar() void {}
+ \\fn bar() callconv(.Inline) void {}
\\fn baz1() void {}
\\fn baz2() void {}
, &[_][]const u8{
@@ -2728,7 +2728,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\const InvalidToken = struct {};
\\const ExpectedVarDeclOrFn = struct {};
, &[_][]const u8{
- "tmp.zig:4:9: error: expected type '@TagType(Error)', found 'type'",
+ "tmp.zig:4:9: error: expected type '@typeInfo(Error).Union.tag_type.?', found 'type'",
});
cases.addTest("binary OR operator on error sets",
@@ -3944,7 +3944,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn entry() void {
\\ var a = b;
\\}
- \\inline fn b() void { }
+ \\fn b() callconv(.Inline) void { }
, &[_][]const u8{
"tmp.zig:2:5: error: functions marked inline must be stored in const or comptime var",
"tmp.zig:4:1: note: declared here",
@@ -6782,11 +6782,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
// \\export fn foo() void {
// \\ bar();
// \\}
- // \\inline fn bar() void {
+ // \\fn bar() callconv(.Inline) void {
// \\ baz();
// \\ quux();
// \\}
- // \\inline fn baz() void {
+ // \\fn baz() callconv(.Inline) void {
// \\ bar();
// \\ quux();
// \\}
@@ -6799,7 +6799,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
// \\export fn foo() void {
// \\ quux(@ptrToInt(bar));
// \\}
- // \\inline fn bar() void { }
+ // \\fn bar() callconv(.Inline) void { }
// \\extern fn quux(usize) void;
//, &[_][]const u8{
// "tmp.zig:4:1: error: unable to inline function",
@@ -7207,7 +7207,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\export fn entry() void {
\\ foo();
\\}
- \\inline fn foo() void {
+ \\fn foo() callconv(.Inline) void {
\\ @setAlignStack(16);
\\}
, &[_][]const u8{
@@ -7462,24 +7462,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:4:5: note: declared here",
});
- cases.add("@TagType when union has no attached enum",
- \\const Foo = union {
- \\ A: i32,
- \\};
- \\export fn entry() void {
- \\ const x = @TagType(Foo);
- \\}
- , &[_][]const u8{
- "tmp.zig:5:24: error: union 'Foo' has no tag",
- "tmp.zig:1:13: note: consider 'union(enum)' here",
- });
-
cases.add("non-integer tag type to automatic union enum",
\\const Foo = union(enum(f32)) {
\\ A: i32,
\\};
\\export fn entry() void {
- \\ const x = @TagType(Foo);
+ \\ const x = @typeInfo(Foo).Union.tag_type.?;
\\}
, &[_][]const u8{
"tmp.zig:1:24: error: expected integer tag type, found 'f32'",
@@ -7490,7 +7478,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ A: i32,
\\};
\\export fn entry() void {
- \\ const x = @TagType(Foo);
+ \\ const x = @typeInfo(Foo).Union.tag_type.?;
\\}
, &[_][]const u8{
"tmp.zig:1:19: error: expected enum tag type, found 'u32'",
@@ -7981,6 +7969,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:7:37: note: referenced here",
});
+ // issue #7810
+ cases.add("comptime slice-len increment beyond bounds",
+ \\export fn foo_slice_len_increment_beyond_bounds() void {
+ \\ comptime {
+ \\ var buf_storage: [8]u8 = undefined;
+ \\ var buf: []const u8 = buf_storage[0..];
+ \\ buf.len += 1;
+ \\ buf[8] = 42;
+ \\ }
+ \\}
+ , &[_][]const u8{
+ ":6:12: error: out of bounds slice",
+ });
+
cases.add("comptime slice-sentinel is out of bounds (unterminated)",
\\export fn foo_array() void {
\\ comptime {
diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig
index e3f9d6b132..b8af201e36 100644
--- a/test/run_translated_c.zig
+++ b/test/run_translated_c.zig
@@ -794,4 +794,132 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0;
\\}
, "");
+
+ cases.add("Wide, UTF-16, and UTF-32 string literals",
+ \\#include <stdlib.h>
+ \\#include <stdint.h>
+ \\#include <wchar.h>
+ \\int main(void) {
+ \\ const wchar_t *wide_str = L"wide";
+ \\ const wchar_t wide_hello[] = L"hello";
+ \\ if (wcslen(wide_str) != 4) abort();
+ \\ if (wcslen(L"literal") != 7) abort();
+ \\ if (wcscmp(wide_hello, L"hello") != 0) abort();
+ \\
+ \\ const uint16_t *u16_str = u"wide";
+ \\ const uint16_t u16_hello[] = u"hello";
+ \\ if (u16_str[3] != u'e' || u16_str[4] != 0) abort();
+ \\ if (u16_hello[4] != u'o' || u16_hello[5] != 0) abort();
+ \\
+ \\ const uint32_t *u32_str = U"wide";
+ \\ const uint32_t u32_hello[] = U"hello";
+ \\ if (u32_str[3] != U'e' || u32_str[4] != 0) abort();
+ \\ if (u32_hello[4] != U'o' || u32_hello[5] != 0) abort();
+ \\ return 0;
+ \\}
+ , "");
+
+ cases.add("Address of function is no-op",
+ \\#include <stdlib.h>
+ \\#include <stdbool.h>
+ \\typedef int (*myfunc)(int);
+ \\int a(int arg) { return arg + 1;}
+ \\int b(int arg) { return arg + 2;}
+ \\int caller(myfunc fn, int arg) {
+ \\ return fn(arg);
+ \\}
+ \\int main() {
+ \\ myfunc arr[3] = {&a, &b, a};
+ \\ myfunc foo = a;
+ \\ myfunc bar = &(a);
+ \\ if (foo != bar) abort();
+ \\ if (arr[0] == arr[1]) abort();
+ \\ if (arr[0] != arr[2]) abort();
+ \\ if (caller(b, 40) != 42) abort();
+ \\ if (caller(&b, 40) != 42) abort();
+ \\ return 0;
+ \\}
+ , "");
+
+ cases.add("Obscure ways of calling functions; issue #4124",
+ \\#include <stdlib.h>
+ \\static int add(int a, int b) {
+ \\ return a + b;
+ \\}
+ \\typedef int (*adder)(int, int);
+ \\typedef void (*funcptr)(void);
+ \\int main() {
+ \\ if ((add)(1, 2) != 3) abort();
+ \\ if ((&add)(1, 2) != 3) abort();
+ \\ if (add(3, 1) != 4) abort();
+ \\ if ((*add)(2, 3) != 5) abort();
+ \\ if ((**add)(7, -1) != 6) abort();
+ \\ if ((***add)(-2, 9) != 7) abort();
+ \\
+ \\ int (*ptr)(int a, int b);
+ \\ ptr = add;
+ \\
+ \\ if (ptr(1, 2) != 3) abort();
+ \\ if ((*ptr)(3, 1) != 4) abort();
+ \\ if ((**ptr)(2, 3) != 5) abort();
+ \\ if ((***ptr)(7, -1) != 6) abort();
+ \\ if ((****ptr)(-2, 9) != 7) abort();
+ \\
+ \\ funcptr addr1 = (funcptr)(add);
+ \\ funcptr addr2 = (funcptr)(&add);
+ \\
+ \\ if (addr1 != addr2) abort();
+ \\ if (((int(*)(int, int))addr1)(1, 2) != 3) abort();
+ \\ if (((adder)addr2)(1, 2) != 3) abort();
+ \\ return 0;
+ \\}
+ , "");
+
+ cases.add("Return boolean expression as int; issue #6215",
+ \\#include <stdlib.h>
+ \\#include <stdbool.h>
+ \\bool actual_bool(void) { return 4 - 1 < 4;}
+ \\char char_bool_ret(void) { return 0 || 1; }
+ \\short short_bool_ret(void) { return 0 < 1; }
+ \\int int_bool_ret(void) { return 1 && 1; }
+ \\long long_bool_ret(void) { return !(0 > 1); }
+ \\static int GLOBAL = 1;
+ \\int nested_scopes(int a, int b) {
+ \\ if (a == 1) {
+ \\ int target = 1;
+ \\ return b == target;
+ \\ } else {
+ \\ int target = 2;
+ \\ if (b == target) {
+ \\ return GLOBAL == 1;
+ \\ }
+ \\ return target == 2;
+ \\ }
+ \\}
+ \\int main(void) {
+ \\ if (!actual_bool()) abort();
+ \\ if (!char_bool_ret()) abort();
+ \\ if (!short_bool_ret()) abort();
+ \\ if (!int_bool_ret()) abort();
+ \\ if (!long_bool_ret()) abort();
+ \\ if (!nested_scopes(1, 1)) abort();
+ \\ if (nested_scopes(1, 2)) abort();
+ \\ if (!nested_scopes(0, 2)) abort();
+ \\ if (!nested_scopes(0, 3)) abort();
+ \\ return 1 != 1;
+ \\}
+ , "");
+
+ cases.add("Comma operator should create new scope; issue #7989",
+ \\#include <stdlib.h>
+ \\#include <stdio.h>
+ \\int main(void) {
+ \\ if (1 || (abort(), 1)) {}
+ \\ if (0 && (1, printf("do not print\n"))) {}
+ \\ int x = 0;
+ \\ x = (x = 3, 4, x + 1);
+ \\ if (x != 4) abort();
+ \\ return 0;
+ \\}
+ , "");
}
diff --git a/test/runtime_safety.zig b/test/runtime_safety.zig
index 2ab728b580..eb49b2dbc1 100644
--- a/test/runtime_safety.zig
+++ b/test/runtime_safety.zig
@@ -74,7 +74,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\pub fn main() void {
\\ var u: U = undefined;
\\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
- \\ var t: @TagType(U) = u;
+ \\ var t: @typeInfo(U).Union.tag_type.? = u;
\\ var n = @tagName(t);
\\}
);
diff --git a/test/stage1/behavior/bugs/1322.zig b/test/stage1/behavior/bugs/1322.zig
index 3231a985e7..02ead6afff 100644
--- a/test/stage1/behavior/bugs/1322.zig
+++ b/test/stage1/behavior/bugs/1322.zig
@@ -13,7 +13,7 @@ const C = struct {};
test "tagged union with all void fields but a meaningful tag" {
var a: A = A{ .b = B{ .c = C{} } };
- std.testing.expect(@as(@TagType(B), a.b) == @TagType(B).c);
+ std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).c);
a = A{ .b = B.None };
- std.testing.expect(@as(@TagType(B), a.b) == @TagType(B).None);
+ std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).None);
}
diff --git a/test/stage1/behavior/enum.zig b/test/stage1/behavior/enum.zig
index 1d424d6e39..ecb95be8f5 100644
--- a/test/stage1/behavior/enum.zig
+++ b/test/stage1/behavior/enum.zig
@@ -1,5 +1,6 @@
const expect = @import("std").testing.expect;
const mem = @import("std").mem;
+const Tag = @import("std").meta.Tag;
test "extern enum" {
const S = struct {
@@ -827,12 +828,12 @@ test "set enum tag type" {
{
var x = Small.One;
x = Small.Two;
- comptime expect(@TagType(Small) == u2);
+ comptime expect(Tag(Small) == u2);
}
{
var x = Small2.One;
x = Small2.Two;
- comptime expect(@TagType(Small2) == u2);
+ comptime expect(Tag(Small2) == u2);
}
}
@@ -905,11 +906,11 @@ fn getC(data: *const BitFieldOfEnums) C {
}
test "casting enum to its tag type" {
- testCastEnumToTagType(Small2.Two);
- comptime testCastEnumToTagType(Small2.Two);
+ testCastEnumTag(Small2.Two);
+ comptime testCastEnumTag(Small2.Two);
}
-fn testCastEnumToTagType(value: Small2) void {
+fn testCastEnumTag(value: Small2) void {
expect(@enumToInt(value) == 1);
}
@@ -1163,14 +1164,14 @@ test "enum with comptime_int tag type" {
Two = 2,
Three = 1,
};
- comptime expect(@TagType(Enum) == comptime_int);
+ comptime expect(Tag(Enum) == comptime_int);
}
test "enum with one member default to u0 tag type" {
const E0 = enum {
X,
};
- comptime expect(@TagType(E0) == u0);
+ comptime expect(Tag(E0) == u0);
}
test "tagName on enum literals" {
diff --git a/test/stage1/behavior/fn.zig b/test/stage1/behavior/fn.zig
index dd69d00c60..a1e726c565 100644
--- a/test/stage1/behavior/fn.zig
+++ b/test/stage1/behavior/fn.zig
@@ -113,7 +113,7 @@ test "assign inline fn to const variable" {
a();
}
-inline fn inlineFn() void {}
+fn inlineFn() callconv(.Inline) void {}
test "pass by non-copying value" {
expect(addPointCoords(Point{ .x = 1, .y = 2 }) == 3);
diff --git a/test/stage1/behavior/type_info.zig b/test/stage1/behavior/type_info.zig
index aa5ac89c94..6dec7ca4d2 100644
--- a/test/stage1/behavior/type_info.zig
+++ b/test/stage1/behavior/type_info.zig
@@ -14,7 +14,7 @@ test "type info: tag type, void info" {
}
fn testBasic() void {
- expect(@TagType(TypeInfo) == TypeId);
+ expect(@typeInfo(TypeInfo).Union.tag_type == TypeId);
const void_info = @typeInfo(void);
expect(void_info == TypeId.Void);
expect(void_info.Void == {});
diff --git a/test/stage1/behavior/union.zig b/test/stage1/behavior/union.zig
index 63f36e755a..e46b6bb6b9 100644
--- a/test/stage1/behavior/union.zig
+++ b/test/stage1/behavior/union.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
+const Tag = std.meta.Tag;
const Value = union(enum) {
Int: u64,
@@ -128,7 +129,7 @@ const MultipleChoice = union(enum(u32)) {
test "simple union(enum(u32))" {
var x = MultipleChoice.C;
expect(x == MultipleChoice.C);
- expect(@enumToInt(@as(@TagType(MultipleChoice), x)) == 60);
+ expect(@enumToInt(@as(Tag(MultipleChoice), x)) == 60);
}
const MultipleChoice2 = union(enum(u32)) {
@@ -144,13 +145,13 @@ const MultipleChoice2 = union(enum(u32)) {
};
test "union(enum(u32)) with specified and unspecified tag values" {
- comptime expect(@TagType(@TagType(MultipleChoice2)) == u32);
+ comptime expect(Tag(Tag(MultipleChoice2)) == u32);
testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
}
fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
- expect(@enumToInt(@as(@TagType(MultipleChoice2), x)) == 60);
+ expect(@enumToInt(@as(Tag(MultipleChoice2), x)) == 60);
expect(1123 == switch (x) {
MultipleChoice2.A => 1,
MultipleChoice2.B => 2,
@@ -204,11 +205,11 @@ test "union field access gives the enum values" {
}
test "cast union to tag type of union" {
- testCastUnionToTagType(TheUnion{ .B = 1234 });
- comptime testCastUnionToTagType(TheUnion{ .B = 1234 });
+ testCastUnionToTag(TheUnion{ .B = 1234 });
+ comptime testCastUnionToTag(TheUnion{ .B = 1234 });
}
-fn testCastUnionToTagType(x: TheUnion) void {
+fn testCastUnionToTag(x: TheUnion) void {
expect(@as(TheTag, x) == TheTag.B);
}
@@ -298,7 +299,7 @@ const TaggedUnionWithAVoid = union(enum) {
fn testTaggedUnionInit(x: anytype) bool {
const y = TaggedUnionWithAVoid{ .A = x };
- return @as(@TagType(TaggedUnionWithAVoid), y) == TaggedUnionWithAVoid.A;
+ return @as(Tag(TaggedUnionWithAVoid), y) == TaggedUnionWithAVoid.A;
}
pub const UnionEnumNoPayloads = union(enum) {
@@ -309,8 +310,8 @@ pub const UnionEnumNoPayloads = union(enum) {
test "tagged union with no payloads" {
const a = UnionEnumNoPayloads{ .B = {} };
switch (a) {
- @TagType(UnionEnumNoPayloads).A => @panic("wrong"),
- @TagType(UnionEnumNoPayloads).B => {},
+ Tag(UnionEnumNoPayloads).A => @panic("wrong"),
+ Tag(UnionEnumNoPayloads).B => {},
}
}
@@ -325,9 +326,9 @@ test "union with only 1 field casted to its enum type" {
};
var e = Expr{ .Literal = Literal{ .Bool = true } };
- const Tag = @TagType(Expr);
- comptime expect(@TagType(Tag) == u0);
- var t = @as(Tag, e);
+ const ExprTag = Tag(Expr);
+ comptime expect(Tag(ExprTag) == u0);
+ var t = @as(ExprTag, e);
expect(t == Expr.Literal);
}
@@ -337,17 +338,17 @@ test "union with only 1 field casted to its enum type which has enum value speci
Bool: bool,
};
- const Tag = enum(comptime_int) {
+ const ExprTag = enum(comptime_int) {
Literal = 33,
};
- const Expr = union(Tag) {
+ const Expr = union(ExprTag) {
Literal: Literal,
};
var e = Expr{ .Literal = Literal{ .Bool = true } };
- comptime expect(@TagType(Tag) == comptime_int);
- var t = @as(Tag, e);
+ comptime expect(Tag(ExprTag) == comptime_int);
+ var t = @as(ExprTag, e);
expect(t == Expr.Literal);
expect(@enumToInt(t) == 33);
comptime expect(@enumToInt(t) == 33);
@@ -501,7 +502,7 @@ test "union with one member defaults to u0 tag type" {
const U0 = union(enum) {
X: u32,
};
- comptime expect(@TagType(@TagType(U0)) == u0);
+ comptime expect(Tag(Tag(U0)) == u0);
}
test "union with comptime_int tag" {
@@ -510,7 +511,7 @@ test "union with comptime_int tag" {
Y: u16,
Z: u8,
};
- comptime expect(@TagType(@TagType(Union)) == comptime_int);
+ comptime expect(Tag(Tag(Union)) == comptime_int);
}
test "extern union doesn't trigger field check at comptime" {
@@ -591,7 +592,7 @@ test "function call result coerces from tagged union to the tag" {
Two: usize,
};
- const ArchTag = @TagType(Arch);
+ const ArchTag = Tag(Arch);
fn doTheTest() void {
var x: ArchTag = getArch1();
@@ -696,8 +697,8 @@ test "cast from pointer to anonymous struct to pointer to union" {
test "method call on an empty union" {
const S = struct {
- const MyUnion = union(Tag) {
- pub const Tag = enum { X1, X2 };
+ const MyUnion = union(MyUnionTag) {
+ pub const MyUnionTag = enum { X1, X2 };
X1: [0]u8,
X2: [0]u8,
@@ -797,7 +798,7 @@ test "union enum type gets a separate scope" {
};
fn doTheTest() void {
- expect(!@hasDecl(@TagType(U), "foo"));
+ expect(!@hasDecl(Tag(U), "foo"));
}
};
diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig
index 6d4e2062bf..35ae1dbf12 100644
--- a/test/stage2/cbe.zig
+++ b/test/stage2/cbe.zig
@@ -179,12 +179,58 @@ pub fn addCases(ctx: *TestContext) !void {
\\ return y - 1;
\\}
\\
- \\inline fn rec(n: usize) usize {
+ \\fn rec(n: usize) callconv(.Inline) usize {
\\ if (n <= 1) return n;
\\ return rec(n - 1);
\\}
, "");
}
+ {
+ var case = ctx.exeFromCompiledC("control flow", .{});
+
+ // Simple while loop
+ case.addCompareOutput(
+ \\export fn main() c_int {
+ \\ var a: c_int = 0;
+ \\ while (a < 5) : (a+=1) {}
+ \\ return a - 5;
+ \\}
+ , "");
+ case.addCompareOutput(
+ \\export fn main() c_int {
+ \\ var a = true;
+ \\ while (!a) {}
+ \\ return 0;
+ \\}
+ , "");
+
+ // If expression
+ case.addCompareOutput(
+ \\export fn main() c_int {
+ \\ var cond: c_int = 0;
+ \\ var a: c_int = @as(c_int, if (cond == 0)
+ \\ 2
+ \\ else
+ \\ 3) + 9;
+ \\ return a - 11;
+ \\}
+ , "");
+
+ // Switch expression
+ case.addCompareOutput(
+ \\export fn main() c_int {
+ \\ var cond: c_int = 0;
+ \\ var a: c_int = switch (cond) {
+ \\ 1 => 1,
+ \\ 2 => 2,
+ \\ 99...300, 12 => 3,
+ \\ 0 => 4,
+ \\ else => 5,
+ \\ };
+ \\ return a - 4;
+ \\}
+ , "");
+ }
ctx.c("empty start function", linux_x64,
\\export fn _start() noreturn {
\\ unreachable;
diff --git a/test/stage2/test.zig b/test/stage2/test.zig
index 0d5a52980b..486edeb864 100644
--- a/test/stage2/test.zig
+++ b/test/stage2/test.zig
@@ -255,7 +255,7 @@ pub fn addCases(ctx: *TestContext) !void {
\\ exit(y - 6);
\\}
\\
- \\inline fn add(a: usize, b: usize, c: usize) usize {
+ \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
\\ return a + b + c;
\\}
\\
@@ -962,43 +962,6 @@ pub fn addCases(ctx: *TestContext) !void {
,
"hello\nhello\nhello\nhello\nhello\n",
);
-
- // comptime switch
-
- // Basic for loop
- case.addCompareOutput(
- \\pub export fn _start() noreturn {
- \\ assert(foo() == 1);
- \\ exit();
- \\}
- \\
- \\fn foo() u32 {
- \\ const a: comptime_int = 1;
- \\ var b: u32 = 0;
- \\ switch (a) {
- \\ 1 => b = 1,
- \\ 2 => b = 2,
- \\ else => unreachable,
- \\ }
- \\ return b;
- \\}
- \\
- \\pub fn assert(ok: bool) void {
- \\ if (!ok) unreachable; // assertion failure
- \\}
- \\
- \\fn exit() noreturn {
- \\ asm volatile ("syscall"
- \\ :
- \\ : [number] "{rax}" (231),
- \\ [arg1] "{rdi}" (0)
- \\ : "rcx", "r11", "memory"
- \\ );
- \\ unreachable;
- \\}
- ,
- "",
- );
}
{
@@ -1265,7 +1228,7 @@ pub fn addCases(ctx: *TestContext) !void {
\\ exit(y - 6);
\\}
\\
- \\inline fn add(a: usize, b: usize, c: usize) usize {
+ \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
\\ if (a == 10) @compileError("bad");
\\ return a + b + c;
\\}
@@ -1288,7 +1251,7 @@ pub fn addCases(ctx: *TestContext) !void {
\\ exit(y - 6);
\\}
\\
- \\inline fn add(a: usize, b: usize, c: usize) usize {
+ \\fn add(a: usize, b: usize, c: usize) callconv(.Inline) usize {
\\ if (a == 10) @compileError("bad");
\\ return a + b + c;
\\}
@@ -1314,7 +1277,7 @@ pub fn addCases(ctx: *TestContext) !void {
\\ exit(y - 21);
\\}
\\
- \\inline fn fibonacci(n: usize) usize {
+ \\fn fibonacci(n: usize) callconv(.Inline) usize {
\\ if (n <= 2) return n;
\\ return fibonacci(n - 2) + fibonacci(n - 1);
\\}
@@ -1337,7 +1300,7 @@ pub fn addCases(ctx: *TestContext) !void {
\\ exit(y - 21);
\\}
\\
- \\inline fn fibonacci(n: usize) usize {
+ \\fn fibonacci(n: usize) callconv(.Inline) usize {
\\ if (n <= 2) return n;
\\ return fibonacci(n - 2) + fibonacci(n - 1);
\\}
diff --git a/test/stage2/wasm.zig b/test/stage2/wasm.zig
index f522db8809..06ede2d735 100644
--- a/test/stage2/wasm.zig
+++ b/test/stage2/wasm.zig
@@ -122,4 +122,96 @@ pub fn addCases(ctx: *TestContext) !void {
\\}
, "35\n");
}
+
+ {
+ var case = ctx.exe("wasm conditions", wasi);
+
+ case.addCompareOutput(
+ \\export fn _start() u32 {
+ \\ var i: u32 = 5;
+ \\ if (i > @as(u32, 4)) {
+ \\ i += 10;
+ \\ }
+ \\ return i;
+ \\}
+ , "15\n");
+
+ case.addCompareOutput(
+ \\export fn _start() u32 {
+ \\ var i: u32 = 5;
+ \\ if (i < @as(u32, 4)) {
+ \\ i += 10;
+ \\ } else {
+ \\ i = 2;
+ \\ }
+ \\ return i;
+ \\}
+ , "2\n");
+
+ case.addCompareOutput(
+ \\export fn _start() u32 {
+ \\ var i: u32 = 5;
+ \\ if (i < @as(u32, 4)) {
+ \\ i += 10;
+ \\ } else if(i == @as(u32, 5)) {
+ \\ i = 20;
+ \\ }
+ \\ return i;
+ \\}
+ , "20\n");
+
+ case.addCompareOutput(
+ \\export fn _start() u32 {
+ \\ var i: u32 = 11;
+ \\ if (i < @as(u32, 4)) {
+ \\ i += 10;
+ \\ } else {
+ \\ if (i > @as(u32, 10)) {
+ \\ i += 20;
+ \\ } else {
+ \\ i = 20;
+ \\ }
+ \\ }
+ \\ return i;
+ \\}
+ , "31\n");
+ }
+
+ {
+ var case = ctx.exe("wasm while loops", wasi);
+
+ case.addCompareOutput(
+ \\export fn _start() u32 {
+ \\ var i: u32 = 0;
+ \\ while(i < @as(u32, 5)){
+ \\ i += 1;
+ \\ }
+ \\
+ \\ return i;
+ \\}
+ , "5\n");
+
+ case.addCompareOutput(
+ \\export fn _start() u32 {
+ \\ var i: u32 = 0;
+ \\ while(i < @as(u32, 10)){
+ \\ var x: u32 = 1;
+ \\ i += x;
+ \\ }
+ \\ return i;
+ \\}
+ , "10\n");
+
+ case.addCompareOutput(
+ \\export fn _start() u32 {
+ \\ var i: u32 = 0;
+ \\ while(i < @as(u32, 10)){
+ \\ var x: u32 = 1;
+ \\ i += x;
+ \\ if (i == @as(u32, 5)) break;
+ \\ }
+ \\ return i;
+ \\}
+ , "5\n");
+ }
}
diff --git a/test/standalone/cat/main.zig b/test/standalone/cat/main.zig
index 89e5fde3cd..80ec97877a 100644
--- a/test/standalone/cat/main.zig
+++ b/test/standalone/cat/main.zig
@@ -41,6 +41,6 @@ pub fn main() !void {
}
fn usage(exe: []const u8) !void {
- warn("Usage: {} [FILE]...\n", .{exe});
+ warn("Usage: {s} [FILE]...\n", .{exe});
return error.Invalid;
}
diff --git a/test/tests.zig b/test/tests.zig
index ec6d9e1df8..a0a50d29a5 100644
--- a/test/tests.zig
+++ b/test/tests.zig
@@ -499,7 +499,7 @@ pub fn addPkgTests(
if (skip_single_threaded and test_target.single_threaded)
continue;
- const ArchTag = @TagType(builtin.Arch);
+ const ArchTag = std.meta.Tag(builtin.Arch);
if (test_target.disable_native and
test_target.target.getOsTag() == std.Target.current.os.tag and
test_target.target.getCpuArch() == std.Target.current.cpu.arch)
diff --git a/test/translate_c.zig b/test/translate_c.zig
index 10ac76a2c5..95969a2f72 100644
--- a/test/translate_c.zig
+++ b/test/translate_c.zig
@@ -43,7 +43,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
,
\\pub const VALUE = ((((1 + (2 * 3)) + (4 * 5)) + 6) << 7) | @boolToInt(8 == 9);
,
- \\pub inline fn _AL_READ3BYTES(p: anytype) @TypeOf(((@import("std").meta.cast([*c]u8, p)).* | (((@import("std").meta.cast([*c]u8, p)) + 1).* << 8)) | (((@import("std").meta.cast([*c]u8, p)) + 2).* << 16)) {
+ \\pub fn _AL_READ3BYTES(p: anytype) callconv(.Inline) @TypeOf(((@import("std").meta.cast([*c]u8, p)).* | (((@import("std").meta.cast([*c]u8, p)) + 1).* << 8)) | (((@import("std").meta.cast([*c]u8, p)) + 2).* << 16)) {
\\ return ((@import("std").meta.cast([*c]u8, p)).* | (((@import("std").meta.cast([*c]u8, p)) + 1).* << 8)) | (((@import("std").meta.cast([*c]u8, p)) + 2).* << 16);
\\}
});
@@ -116,7 +116,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\};
\\pub const Color = struct_Color;
,
- \\pub inline fn CLITERAL(type_1: anytype) @TypeOf(type_1) {
+ \\pub fn CLITERAL(type_1: anytype) callconv(.Inline) @TypeOf(type_1) {
\\ return type_1;
\\}
,
@@ -148,7 +148,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("correct semicolon after infixop",
\\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
, &[_][]const u8{
- \\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf(((_fp.*._flags) & _IO_ERR_SEEN) != 0) {
+ \\pub fn __ferror_unlocked_body(_fp: anytype) callconv(.Inline) @TypeOf(((_fp.*._flags) & _IO_ERR_SEEN) != 0) {
\\ return ((_fp.*._flags) & _IO_ERR_SEEN) != 0;
\\}
});
@@ -157,7 +157,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\#define FOO(x) ((x >= 0) + (x >= 0))
\\#define BAR 1 && 2 > 4
, &[_][]const u8{
- \\pub inline fn FOO(x: anytype) @TypeOf(@boolToInt(x >= 0) + @boolToInt(x >= 0)) {
+ \\pub fn FOO(x: anytype) callconv(.Inline) @TypeOf(@boolToInt(x >= 0) + @boolToInt(x >= 0)) {
\\ return @boolToInt(x >= 0) + @boolToInt(x >= 0);
\\}
,
@@ -208,7 +208,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ break :blk bar;
\\};
,
- \\pub inline fn bar(x: anytype) @TypeOf(baz(1, 2)) {
+ \\pub fn bar(x: anytype) callconv(.Inline) @TypeOf(baz(1, 2)) {
\\ return blk: {
\\ _ = &x;
\\ _ = 3;
@@ -1305,10 +1305,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ var a: c_int = undefined;
\\ var b: f32 = undefined;
\\ var c: ?*c_void = undefined;
- \\ return !(a == @as(c_int, 0));
- \\ return !(a != 0);
- \\ return !(b != 0);
- \\ return !(c != null);
+ \\ return @boolToInt(!(a == @as(c_int, 0)));
+ \\ return @boolToInt(!(a != 0));
+ \\ return @boolToInt(!(b != 0));
+ \\ return @boolToInt(!(c != null));
\\}
});
@@ -1590,13 +1590,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, &[_][]const u8{
\\pub extern var fn_ptr: ?fn () callconv(.C) void;
,
- \\pub inline fn foo() void {
+ \\pub fn foo() callconv(.Inline) void {
\\ return fn_ptr.?();
\\}
,
\\pub extern var fn_ptr2: ?fn (c_int, f32) callconv(.C) u8;
,
- \\pub inline fn bar(arg_1: c_int, arg_2: f32) u8 {
+ \\pub fn bar(arg_1: c_int, arg_2: f32) callconv(.Inline) u8 {
\\ return fn_ptr2.?(arg_1, arg_2);
\\}
});
@@ -1629,7 +1629,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
,
\\pub const glClearPFN = PFNGLCLEARPROC;
,
- \\pub inline fn glClearUnion(arg_2: GLbitfield) void {
+ \\pub fn glClearUnion(arg_2: GLbitfield) callconv(.Inline) void {
\\ return glProcs.gl.Clear.?(arg_2);
\\}
,
@@ -1650,15 +1650,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
, &[_][]const u8{
\\pub extern var c: c_int;
,
- \\pub inline fn BASIC(c_1: anytype) @TypeOf(c_1 * 2) {
+ \\pub fn BASIC(c_1: anytype) callconv(.Inline) @TypeOf(c_1 * 2) {
\\ return c_1 * 2;
\\}
,
- \\pub inline fn FOO(L: anytype, b: anytype) @TypeOf(L + b) {
+ \\pub fn FOO(L: anytype, b: anytype) callconv(.Inline) @TypeOf(L + b) {
\\ return L + b;
\\}
,
- \\pub inline fn BAR() @TypeOf(c * c) {
+ \\pub fn BAR() callconv(.Inline) @TypeOf(c * c) {
\\ return c * c;
\\}
});
@@ -1723,11 +1723,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
, &[_][]const u8{
\\pub export fn foo() c_int {
- \\ _ = @as(c_int, 2);
- \\ _ = @as(c_int, 4);
- \\ _ = @as(c_int, 2);
- \\ _ = @as(c_int, 4);
- \\ return @as(c_int, 6);
+ \\ _ = (blk: {
+ \\ _ = @as(c_int, 2);
+ \\ break :blk @as(c_int, 4);
+ \\ });
+ \\ return (blk: {
+ \\ _ = (blk_1: {
+ \\ _ = @as(c_int, 2);
+ \\ break :blk_1 @as(c_int, 4);
+ \\ });
+ \\ break :blk @as(c_int, 6);
+ \\ });
\\}
});
@@ -1774,8 +1780,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ while (true) {
\\ var a_1: c_int = 4;
\\ a_1 = 9;
- \\ _ = @as(c_int, 6);
- \\ return a_1;
+ \\ return (blk: {
+ \\ _ = @as(c_int, 6);
+ \\ break :blk a_1;
+ \\ });
\\ }
\\ while (true) {
\\ var a_1: c_int = 2;
@@ -1805,9 +1813,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ var b: c_int = 4;
\\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
\\ var a: c_int = 2;
- \\ a = 6;
- \\ _ = @as(c_int, 5);
- \\ _ = @as(c_int, 7);
+ \\ _ = (blk: {
+ \\ _ = (blk_1: {
+ \\ a = 6;
+ \\ break :blk_1 @as(c_int, 5);
+ \\ });
+ \\ break :blk @as(c_int, 7);
+ \\ });
\\ }
\\ }
\\ var i: u8 = @bitCast(u8, @truncate(i8, @as(c_int, 2)));
@@ -2298,7 +2310,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("macro call",
\\#define CALL(arg) bar(arg)
, &[_][]const u8{
- \\pub inline fn CALL(arg: anytype) @TypeOf(bar(arg)) {
+ \\pub fn CALL(arg: anytype) callconv(.Inline) @TypeOf(bar(arg)) {
\\ return bar(arg);
\\}
});
@@ -2802,8 +2814,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\ fn_f64(3);
\\ fn_bool(@as(c_int, 123) != 0);
\\ fn_bool(@as(c_int, 0) != 0);
- \\ fn_bool(@ptrToInt(&fn_int) != 0);
- \\ fn_int(@intCast(c_int, @ptrToInt(&fn_int)));
+ \\ fn_bool(@ptrToInt(fn_int) != 0);
+ \\ fn_int(@intCast(c_int, @ptrToInt(fn_int)));
\\ fn_ptr(@intToPtr(?*c_void, @as(c_int, 42)));
\\}
});
@@ -2860,7 +2872,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\#define BAR (void*) a
\\#define BAZ (uint32_t)(2)
, &[_][]const u8{
- \\pub inline fn FOO(bar: anytype) @TypeOf(baz((@import("std").meta.cast(?*c_void, baz)))) {
+ \\pub fn FOO(bar: anytype) callconv(.Inline) @TypeOf(baz((@import("std").meta.cast(?*c_void, baz)))) {
\\ return baz((@import("std").meta.cast(?*c_void, baz)));
\\}
,
@@ -2902,11 +2914,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\#define MIN(a, b) ((b) < (a) ? (b) : (a))
\\#define MAX(a, b) ((b) > (a) ? (b) : (a))
, &[_][]const u8{
- \\pub inline fn MIN(a: anytype, b: anytype) @TypeOf(if (b < a) b else a) {
+ \\pub fn MIN(a: anytype, b: anytype) callconv(.Inline) @TypeOf(if (b < a) b else a) {
\\ return if (b < a) b else a;
\\}
,
- \\pub inline fn MAX(a: anytype, b: anytype) @TypeOf(if (b > a) b else a) {
+ \\pub fn MAX(a: anytype, b: anytype) callconv(.Inline) @TypeOf(if (b > a) b else a) {
\\ return if (b > a) b else a;
\\}
});
@@ -3094,7 +3106,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)
\\
, &[_][]const u8{
- \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf((@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen) {
+ \\pub fn DefaultScreen(dpy: anytype) callconv(.Inline) @TypeOf((@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen) {
\\ return (@import("std").meta.cast(_XPrivDisplay, dpy)).*.default_screen;
\\}
});