mirror of
https://github.com/ziglang/zig.git
synced 2025-12-25 23:53:15 +00:00
This commit updates stage2 to enforce the property that the syntax `fn()void` is a function *body* not a *pointer*. To get a pointer, the syntax `*const fn()void` is required. ZIR puts function alignment into the func instruction rather than the decl because this way it makes it into function types. LLVM backend respects function alignments. Struct and Union have methods `fieldSrcLoc` to help look up source locations of their fields. These trigger full loading, tokenization, and parsing of source files, so should only be called once it is confirmed that an error message needs to be printed. There are some nice new error hints for explaining why a type is required to be comptime, particularly for structs that contain function body types. `Type.requiresComptime` is now moved into Sema because it can fail and might need to trigger field type resolution. Comptime pointer loading takes into account types that do not have a well-defined memory layout and does not try to compute a byte offset for them. `fn()void` syntax no longer secretly makes a pointer. You get a function body type, which requires comptime. However a pointer to a function body can be runtime known (obviously). Compile errors that report "expected pointer, found ..." are factored out into convenience functions `checkPtrOperand` and `checkPtrType` and have a note about function pointers. Implemented `Value.hash` for functions, enum literals, and undefined values. stage1 is not updated to this (yet?), so some workarounds and disabled tests are needed to keep everything working. Should we update stage1 to these new type semantics? Yes probably because I don't want to add too much conditional compilation logic in the std lib for the different backends.
135 lines
2.9 KiB
Zig
135 lines
2.9 KiB
Zig
const builtin = @import("builtin");
|
|
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
const expectError = std.testing.expectError;
|
|
const expectEqual = std.testing.expectEqual;
|
|
const mem = std.mem;
|
|
|
|
test "error values" {
|
|
const a = @errorToInt(error.err1);
|
|
const b = @errorToInt(error.err2);
|
|
try expect(a != b);
|
|
}
|
|
|
|
test "redefinition of error values allowed" {
|
|
shouldBeNotEqual(error.AnError, error.SecondError);
|
|
}
|
|
fn shouldBeNotEqual(a: anyerror, b: anyerror) void {
|
|
if (a == b) unreachable;
|
|
}
|
|
|
|
test "error binary operator" {
|
|
const a = errBinaryOperatorG(true) catch 3;
|
|
const b = errBinaryOperatorG(false) catch 3;
|
|
try expect(a == 3);
|
|
try expect(b == 10);
|
|
}
|
|
fn errBinaryOperatorG(x: bool) anyerror!isize {
|
|
return if (x) error.ItBroke else @as(isize, 10);
|
|
}
|
|
|
|
test "empty error union" {
|
|
const x = error{} || error{};
|
|
_ = x;
|
|
}
|
|
|
|
pub fn foo() anyerror!i32 {
|
|
const x = try bar();
|
|
return x + 1;
|
|
}
|
|
|
|
pub fn bar() anyerror!i32 {
|
|
return 13;
|
|
}
|
|
|
|
pub fn baz() anyerror!i32 {
|
|
const y = foo() catch 1234;
|
|
return y + 1;
|
|
}
|
|
|
|
test "error wrapping" {
|
|
try expect((baz() catch unreachable) == 15);
|
|
}
|
|
|
|
test "unwrap simple value from error" {
|
|
const i = unwrapSimpleValueFromErrorDo() catch unreachable;
|
|
try expect(i == 13);
|
|
}
|
|
fn unwrapSimpleValueFromErrorDo() anyerror!isize {
|
|
return 13;
|
|
}
|
|
|
|
test "error return in assignment" {
|
|
doErrReturnInAssignment() catch unreachable;
|
|
}
|
|
|
|
fn doErrReturnInAssignment() anyerror!void {
|
|
var x: i32 = undefined;
|
|
x = try makeANonErr();
|
|
}
|
|
|
|
fn makeANonErr() anyerror!i32 {
|
|
return 1;
|
|
}
|
|
|
|
test "syntax: optional operator in front of error union operator" {
|
|
comptime {
|
|
try expect(?(anyerror!i32) == ?(anyerror!i32));
|
|
}
|
|
}
|
|
|
|
test "widen cast integer payload of error union function call" {
|
|
const S = struct {
|
|
fn errorable() !u64 {
|
|
var x = @as(u64, try number());
|
|
return x;
|
|
}
|
|
|
|
fn number() anyerror!u32 {
|
|
return 1234;
|
|
}
|
|
};
|
|
try expect((try S.errorable()) == 1234);
|
|
}
|
|
|
|
test "debug info for optional error set" {
|
|
const SomeError = error{Hello};
|
|
var a_local_variable: ?SomeError = null;
|
|
_ = a_local_variable;
|
|
}
|
|
|
|
test "implicit cast to optional to error union to return result loc" {
|
|
const S = struct {
|
|
fn entry() !void {
|
|
var x: Foo = undefined;
|
|
if (func(&x)) |opt| {
|
|
try expect(opt != null);
|
|
} else |_| @panic("expected non error");
|
|
}
|
|
fn func(f: *Foo) anyerror!?*Foo {
|
|
return f;
|
|
}
|
|
const Foo = struct {
|
|
field: i32,
|
|
};
|
|
};
|
|
try S.entry();
|
|
//comptime S.entry(); TODO
|
|
}
|
|
|
|
test "error: fn returning empty error set can be passed as fn returning any error" {
|
|
entry();
|
|
comptime entry();
|
|
}
|
|
|
|
fn entry() void {
|
|
foo2(bar2);
|
|
}
|
|
|
|
fn foo2(f: fn () anyerror!void) void {
|
|
const x = f();
|
|
x catch {};
|
|
}
|
|
|
|
fn bar2() (error{}!void) {}
|