mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
OOM -> catch unreachable
This commit is contained in:
parent
5d7e981f95
commit
c88edbc46f
@ -66,26 +66,26 @@ pub const TestContext = struct {
|
|||||||
|
|
||||||
/// Adds a subcase in which the module is updated with new ZIR, and the
|
/// Adds a subcase in which the module is updated with new ZIR, and the
|
||||||
/// resulting ZIR is validated.
|
/// resulting ZIR is validated.
|
||||||
pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) !void {
|
pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) void {
|
||||||
try self.updates.append(.{
|
self.updates.append(.{
|
||||||
.src = src,
|
.src = src,
|
||||||
.case = .{ .Transformation = result },
|
.case = .{ .Transformation = result },
|
||||||
});
|
}) catch unreachable;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) !void {
|
pub fn addCompareOutput(self: *Case, src: [:0]const u8, result: []const u8) void {
|
||||||
try self.updates.append(.{
|
self.updates.append(.{
|
||||||
.src = src,
|
.src = src,
|
||||||
.case = .{ .Execution = result },
|
.case = .{ .Execution = result },
|
||||||
});
|
}) catch unreachable;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a subcase in which the module is updated with invalid ZIR, and
|
/// Adds a subcase in which the module is updated with invalid ZIR, and
|
||||||
/// ensures that compilation fails for the expected reasons.
|
/// ensures that compilation fails for the expected reasons.
|
||||||
///
|
///
|
||||||
/// Errors must be specified in sequential order.
|
/// Errors must be specified in sequential order.
|
||||||
pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) !void {
|
pub fn addError(self: *Case, src: [:0]const u8, errors: []const []const u8) void {
|
||||||
var array = try self.updates.allocator.alloc(ErrorMsg, errors.len);
|
var array = self.updates.allocator.alloc(ErrorMsg, errors.len) catch unreachable;
|
||||||
for (errors) |e, i| {
|
for (errors) |e, i| {
|
||||||
if (e[0] != ':') {
|
if (e[0] != ':') {
|
||||||
@panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n");
|
@panic("Invalid test: error must be specified as follows:\n:line:column: error: message\n=========\n");
|
||||||
@ -118,7 +118,7 @@ pub const TestContext = struct {
|
|||||||
.column = column - 1,
|
.column = column - 1,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
try self.updates.append(.{ .src = src, .case = .{ .Error = array } });
|
self.updates.append(.{ .src = src, .case = .{ .Error = array } }) catch unreachable;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -127,15 +127,14 @@ pub const TestContext = struct {
|
|||||||
name: []const u8,
|
name: []const u8,
|
||||||
target: std.zig.CrossTarget,
|
target: std.zig.CrossTarget,
|
||||||
T: TestType,
|
T: TestType,
|
||||||
) !*Case {
|
) *Case {
|
||||||
const case = Case{
|
ctx.cases.append(Case{
|
||||||
.name = name,
|
.name = name,
|
||||||
.target = target,
|
.target = target,
|
||||||
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
|
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
|
||||||
.output_mode = .Exe,
|
.output_mode = .Exe,
|
||||||
.@"type" = T,
|
.@"type" = T,
|
||||||
};
|
}) catch unreachable;
|
||||||
try ctx.cases.append(case);
|
|
||||||
return &ctx.cases.items[ctx.cases.items.len - 1];
|
return &ctx.cases.items[ctx.cases.items.len - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,14 +143,14 @@ pub const TestContext = struct {
|
|||||||
name: []const u8,
|
name: []const u8,
|
||||||
target: std.zig.CrossTarget,
|
target: std.zig.CrossTarget,
|
||||||
T: TestType,
|
T: TestType,
|
||||||
) !*Case {
|
) *Case {
|
||||||
try ctx.cases.append(Case{
|
ctx.cases.append(Case{
|
||||||
.name = name,
|
.name = name,
|
||||||
.target = target,
|
.target = target,
|
||||||
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
|
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
|
||||||
.output_mode = .Obj,
|
.output_mode = .Obj,
|
||||||
.@"type" = T,
|
.@"type" = T,
|
||||||
});
|
}) catch unreachable;
|
||||||
return &ctx.cases.items[ctx.cases.items.len - 1];
|
return &ctx.cases.items[ctx.cases.items.len - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,9 +160,8 @@ pub const TestContext = struct {
|
|||||||
T: TestType,
|
T: TestType,
|
||||||
src: [:0]const u8,
|
src: [:0]const u8,
|
||||||
expected_stdout: []const u8,
|
expected_stdout: []const u8,
|
||||||
) !void {
|
) void {
|
||||||
var c = try ctx.addExe(name, .{}, T);
|
ctx.addExe(name, .{}, T).addCompareOutput(src, expected_stdout);
|
||||||
try c.addCompareOutput(src, expected_stdout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn addTransform(
|
pub fn addTransform(
|
||||||
@ -173,9 +171,8 @@ pub const TestContext = struct {
|
|||||||
T: TestType,
|
T: TestType,
|
||||||
src: [:0]const u8,
|
src: [:0]const u8,
|
||||||
result: [:0]const u8,
|
result: [:0]const u8,
|
||||||
) !void {
|
) void {
|
||||||
var c = try ctx.addObj(name, target, T);
|
ctx.addObj(name, target, T).addTransform(src, result);
|
||||||
try c.addTransform(src, result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn addError(
|
pub fn addError(
|
||||||
@ -185,16 +182,13 @@ pub const TestContext = struct {
|
|||||||
T: TestType,
|
T: TestType,
|
||||||
src: [:0]const u8,
|
src: [:0]const u8,
|
||||||
expected_errors: []const []const u8,
|
expected_errors: []const []const u8,
|
||||||
) !void {
|
) void {
|
||||||
var c = try ctx.addObj(name, target, T);
|
ctx.addObj(name, target, T).addError(src, expected_errors);
|
||||||
try c.addError(src, expected_errors);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init() TestContext {
|
fn init() TestContext {
|
||||||
const allocator = std.heap.page_allocator;
|
const allocator = std.heap.page_allocator;
|
||||||
return .{
|
return .{ .cases = std.ArrayList(Case).init(allocator) };
|
||||||
.cases = std.ArrayList(Case).init(allocator),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinit(self: *TestContext) void {
|
fn deinit(self: *TestContext) void {
|
||||||
|
|||||||
@ -17,9 +17,9 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
var case = try ctx.addExe("hello world with updates", linux_x64, .Zig);
|
var case = ctx.addExe("hello world with updates", linux_x64, .Zig);
|
||||||
// Regular old hello world
|
// Regular old hello world
|
||||||
try case.addCompareOutput(
|
case.addCompareOutput(
|
||||||
\\export fn _start() noreturn {
|
\\export fn _start() noreturn {
|
||||||
\\ print();
|
\\ print();
|
||||||
\\
|
\\
|
||||||
@ -51,7 +51,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
"Hello, World!\n",
|
"Hello, World!\n",
|
||||||
);
|
);
|
||||||
// Now change the message only
|
// Now change the message only
|
||||||
try case.addCompareOutput(
|
case.addCompareOutput(
|
||||||
\\export fn _start() noreturn {
|
\\export fn _start() noreturn {
|
||||||
\\ print();
|
\\ print();
|
||||||
\\
|
\\
|
||||||
@ -83,7 +83,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
"What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
|
"What is up? This is a longer message that will force the data to be relocated in virtual address space.\n",
|
||||||
);
|
);
|
||||||
// Now we print it twice.
|
// Now we print it twice.
|
||||||
try case.addCompareOutput(
|
case.addCompareOutput(
|
||||||
\\export fn _start() noreturn {
|
\\export fn _start() noreturn {
|
||||||
\\ print();
|
\\ print();
|
||||||
\\ print();
|
\\ print();
|
||||||
|
|||||||
@ -9,7 +9,7 @@ const linux_x64 = std.zig.CrossTarget{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub fn addCases(ctx: *TestContext) !void {
|
pub fn addCases(ctx: *TestContext) !void {
|
||||||
try ctx.addError("call undefined local", linux_x64, .ZIR,
|
ctx.addError("call undefined local", linux_x64, .ZIR,
|
||||||
\\@noreturn = primitive(noreturn)
|
\\@noreturn = primitive(noreturn)
|
||||||
\\
|
\\
|
||||||
\\@start_fnty = fntype([], @noreturn, cc=Naked)
|
\\@start_fnty = fntype([], @noreturn, cc=Naked)
|
||||||
@ -19,7 +19,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
// TODO: address inconsistency in this message and the one in the next test
|
// TODO: address inconsistency in this message and the one in the next test
|
||||||
, &[_][]const u8{":5:13: error: unrecognized identifier: %test"});
|
, &[_][]const u8{":5:13: error: unrecognized identifier: %test"});
|
||||||
|
|
||||||
try ctx.addError("call with non-existent target", linux_x64, .ZIR,
|
ctx.addError("call with non-existent target", linux_x64, .ZIR,
|
||||||
\\@noreturn = primitive(noreturn)
|
\\@noreturn = primitive(noreturn)
|
||||||
\\
|
\\
|
||||||
\\@start_fnty = fntype([], @noreturn, cc=Naked)
|
\\@start_fnty = fntype([], @noreturn, cc=Naked)
|
||||||
@ -31,7 +31,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
, &[_][]const u8{":5:13: error: decl 'notafunc' not found"});
|
, &[_][]const u8{":5:13: error: decl 'notafunc' not found"});
|
||||||
|
|
||||||
// TODO: this error should occur at the call site, not the fntype decl
|
// TODO: this error should occur at the call site, not the fntype decl
|
||||||
try ctx.addError("call naked function", linux_x64, .ZIR,
|
ctx.addError("call naked function", linux_x64, .ZIR,
|
||||||
\\@noreturn = primitive(noreturn)
|
\\@noreturn = primitive(noreturn)
|
||||||
\\
|
\\
|
||||||
\\@start_fnty = fntype([], @noreturn, cc=Naked)
|
\\@start_fnty = fntype([], @noreturn, cc=Naked)
|
||||||
@ -46,51 +46,51 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
// TODO: re-enable these tests.
|
// TODO: re-enable these tests.
|
||||||
// https://github.com/ziglang/zig/issues/1364
|
// https://github.com/ziglang/zig/issues/1364
|
||||||
|
|
||||||
// try ctx.addError("Export same symbol twice", linux_x64, .Zig,
|
// ctx.addError("Export same symbol twice", linux_x64, .Zig,
|
||||||
// \\export fn entry() void {}
|
// \\export fn entry() void {}
|
||||||
// \\export fn entry() void {}
|
// \\export fn entry() void {}
|
||||||
// , &[_][]const u8{":2:1: error: exported symbol collision"});
|
// , &[_][]const u8{":2:1: error: exported symbol collision"});
|
||||||
|
|
||||||
// try ctx.addError("Missing function name", linux_x64, .Zig,
|
// ctx.addError("Missing function name", linux_x64, .Zig,
|
||||||
// \\fn() void {}
|
// \\fn() void {}
|
||||||
// , &[_][]const u8{":1:3: error: missing function name"});
|
// , &[_][]const u8{":1:3: error: missing function name"});
|
||||||
//try ctx.testCompileError(
|
//ctx.testCompileError(
|
||||||
// \\comptime {
|
// \\comptime {
|
||||||
// \\ return;
|
// \\ return;
|
||||||
// \\}
|
// \\}
|
||||||
//, "1.zig", 2, 5, "return expression outside function definition");
|
//, "1.zig", 2, 5, "return expression outside function definition");
|
||||||
|
|
||||||
//try ctx.testCompileError(
|
//ctx.testCompileError(
|
||||||
// \\export fn entry() void {
|
// \\export fn entry() void {
|
||||||
// \\ defer return;
|
// \\ defer return;
|
||||||
// \\}
|
// \\}
|
||||||
//, "1.zig", 2, 11, "cannot return from defer expression");
|
//, "1.zig", 2, 11, "cannot return from defer expression");
|
||||||
|
|
||||||
//try ctx.testCompileError(
|
//ctx.testCompileError(
|
||||||
// \\export fn entry() c_int {
|
// \\export fn entry() c_int {
|
||||||
// \\ return 36893488147419103232;
|
// \\ return 36893488147419103232;
|
||||||
// \\}
|
// \\}
|
||||||
//, "1.zig", 2, 12, "integer value '36893488147419103232' cannot be stored in type 'c_int'");
|
//, "1.zig", 2, 12, "integer value '36893488147419103232' cannot be stored in type 'c_int'");
|
||||||
|
|
||||||
//try ctx.testCompileError(
|
//ctx.testCompileError(
|
||||||
// \\comptime {
|
// \\comptime {
|
||||||
// \\ var a: *align(4) align(4) i32 = 0;
|
// \\ var a: *align(4) align(4) i32 = 0;
|
||||||
// \\}
|
// \\}
|
||||||
//, "1.zig", 2, 22, "Extra align qualifier");
|
//, "1.zig", 2, 22, "Extra align qualifier");
|
||||||
|
|
||||||
//try ctx.testCompileError(
|
//ctx.testCompileError(
|
||||||
// \\comptime {
|
// \\comptime {
|
||||||
// \\ var b: *const const i32 = 0;
|
// \\ var b: *const const i32 = 0;
|
||||||
// \\}
|
// \\}
|
||||||
//, "1.zig", 2, 19, "Extra align qualifier");
|
//, "1.zig", 2, 19, "Extra align qualifier");
|
||||||
|
|
||||||
//try ctx.testCompileError(
|
//ctx.testCompileError(
|
||||||
// \\comptime {
|
// \\comptime {
|
||||||
// \\ var c: *volatile volatile i32 = 0;
|
// \\ var c: *volatile volatile i32 = 0;
|
||||||
// \\}
|
// \\}
|
||||||
//, "1.zig", 2, 22, "Extra align qualifier");
|
//, "1.zig", 2, 22, "Extra align qualifier");
|
||||||
|
|
||||||
//try ctx.testCompileError(
|
//ctx.testCompileError(
|
||||||
// \\comptime {
|
// \\comptime {
|
||||||
// \\ var d: *allowzero allowzero i32 = 0;
|
// \\ var d: *allowzero allowzero i32 = 0;
|
||||||
// \\}
|
// \\}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ const linux_x64 = std.zig.CrossTarget{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub fn addCases(ctx: *TestContext) !void {
|
pub fn addCases(ctx: *TestContext) !void {
|
||||||
try ctx.addTransform("referencing decls which appear later in the file", linux_x64, .ZIR,
|
ctx.addTransform("referencing decls which appear later in the file", linux_x64, .ZIR,
|
||||||
\\@void = primitive(void)
|
\\@void = primitive(void)
|
||||||
\\@fnty = fntype([], @void, cc=C)
|
\\@fnty = fntype([], @void, cc=C)
|
||||||
\\
|
\\
|
||||||
@ -32,7 +32,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
\\})
|
\\})
|
||||||
\\
|
\\
|
||||||
);
|
);
|
||||||
try ctx.addTransform("elemptr, add, cmp, condbr, return, breakpoint", linux_x64, .ZIR,
|
ctx.addTransform("elemptr, add, cmp, condbr, return, breakpoint", linux_x64, .ZIR,
|
||||||
\\@void = primitive(void)
|
\\@void = primitive(void)
|
||||||
\\@usize = primitive(usize)
|
\\@usize = primitive(usize)
|
||||||
\\@fnty = fntype([], @void, cc=C)
|
\\@fnty = fntype([], @void, cc=C)
|
||||||
@ -86,8 +86,8 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
);
|
);
|
||||||
|
|
||||||
{
|
{
|
||||||
var case = try ctx.addObj("reference cycle with compile error in the cycle", linux_x64, .ZIR);
|
var case = ctx.addObj("reference cycle with compile error in the cycle", linux_x64, .ZIR);
|
||||||
try case.addTransform(
|
case.addTransform(
|
||||||
\\@void = primitive(void)
|
\\@void = primitive(void)
|
||||||
\\@fnty = fntype([], @void, cc=C)
|
\\@fnty = fntype([], @void, cc=C)
|
||||||
\\
|
\\
|
||||||
@ -133,7 +133,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
\\
|
\\
|
||||||
);
|
);
|
||||||
// Now we introduce a compile error
|
// Now we introduce a compile error
|
||||||
try case.addError(
|
case.addError(
|
||||||
\\@void = primitive(void)
|
\\@void = primitive(void)
|
||||||
\\@fnty = fntype([], @void, cc=C)
|
\\@fnty = fntype([], @void, cc=C)
|
||||||
\\
|
\\
|
||||||
@ -163,7 +163,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
// Now we remove the call to `a`. `a` and `b` form a cycle, but no entry points are
|
// Now we remove the call to `a`. `a` and `b` form a cycle, but no entry points are
|
||||||
// referencing either of them. This tests that the cycle is detected, and the error
|
// referencing either of them. This tests that the cycle is detected, and the error
|
||||||
// goes away.
|
// goes away.
|
||||||
try case.addTransform(
|
case.addTransform(
|
||||||
\\@void = primitive(void)
|
\\@void = primitive(void)
|
||||||
\\@fnty = fntype([], @void, cc=C)
|
\\@fnty = fntype([], @void, cc=C)
|
||||||
\\
|
\\
|
||||||
@ -207,7 +207,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try ctx.addCompareOutput("hello world ZIR", .ZIR,
|
ctx.addCompareOutput("hello world ZIR", .ZIR,
|
||||||
\\@noreturn = primitive(noreturn)
|
\\@noreturn = primitive(noreturn)
|
||||||
\\@void = primitive(void)
|
\\@void = primitive(void)
|
||||||
\\@usize = primitive(usize)
|
\\@usize = primitive(usize)
|
||||||
@ -265,7 +265,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
|||||||
\\
|
\\
|
||||||
);
|
);
|
||||||
|
|
||||||
try ctx.addCompareOutput("function call with no args no return value", .ZIR,
|
ctx.addCompareOutput("function call with no args no return value", .ZIR,
|
||||||
\\@noreturn = primitive(noreturn)
|
\\@noreturn = primitive(noreturn)
|
||||||
\\@void = primitive(void)
|
\\@void = primitive(void)
|
||||||
\\@usize = primitive(usize)
|
\\@usize = primitive(usize)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user