remove error to/from int casting syntax; add @errorToInt/@intToError
See #1061
This commit is contained in:
@@ -140,8 +140,8 @@ test "explicit cast from integer to error type" {
|
||||
comptime testCastIntToErr(error.ItBroke);
|
||||
}
|
||||
fn testCastIntToErr(err: error) void {
|
||||
const x = usize(err);
|
||||
const y = error(x);
|
||||
const x = @errorToInt(err);
|
||||
const y = @intToError(x);
|
||||
assert(error.ItBroke == y);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ test "@errorName" {
|
||||
}
|
||||
|
||||
test "error values" {
|
||||
const a = i32(error.err1);
|
||||
const b = i32(error.err2);
|
||||
const a = @errorToInt(error.err1);
|
||||
const b = @errorToInt(error.err2);
|
||||
assert(a != b);
|
||||
}
|
||||
|
||||
@@ -147,14 +147,14 @@ test "syntax: optional operator in front of error union operator" {
|
||||
}
|
||||
|
||||
test "comptime err to int of error set with only 1 possible value" {
|
||||
testErrToIntWithOnePossibleValue(error.A, u32(error.A));
|
||||
comptime testErrToIntWithOnePossibleValue(error.A, u32(error.A));
|
||||
testErrToIntWithOnePossibleValue(error.A, @errorToInt(error.A));
|
||||
comptime testErrToIntWithOnePossibleValue(error.A, @errorToInt(error.A));
|
||||
}
|
||||
fn testErrToIntWithOnePossibleValue(
|
||||
x: error{A},
|
||||
comptime value: u32,
|
||||
) void {
|
||||
if (u32(x) != value) {
|
||||
if (@errorToInt(x) != value) {
|
||||
@compileError("bad");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ fn testErrorSet() void {
|
||||
assert(TypeId(error_set_info) == TypeId.ErrorSet);
|
||||
assert(error_set_info.ErrorSet.errors.len == 3);
|
||||
assert(mem.eql(u8, error_set_info.ErrorSet.errors[0].name, "First"));
|
||||
assert(error_set_info.ErrorSet.errors[2].value == usize(TestErrorSet.Third));
|
||||
assert(error_set_info.ErrorSet.errors[2].value == @errorToInt(TestErrorSet.Third));
|
||||
|
||||
const error_union_info = @typeInfo(TestErrorSet!usize);
|
||||
assert(TypeId(error_union_info) == TypeId.ErrorUnion);
|
||||
|
||||
@@ -467,25 +467,34 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
|
||||
cases.add(
|
||||
"int to err global invalid number",
|
||||
\\const Set1 = error{A, B};
|
||||
\\const Set1 = error{
|
||||
\\ A,
|
||||
\\ B,
|
||||
\\};
|
||||
\\comptime {
|
||||
\\ var x: usize = 3;
|
||||
\\ var y = error(x);
|
||||
\\ var x: u16 = 3;
|
||||
\\ var y = @intToError(x);
|
||||
\\}
|
||||
,
|
||||
".tmp_source.zig:4:18: error: integer value 3 represents no error",
|
||||
".tmp_source.zig:7:13: error: integer value 3 represents no error",
|
||||
);
|
||||
|
||||
cases.add(
|
||||
"int to err non global invalid number",
|
||||
\\const Set1 = error{A, B};
|
||||
\\const Set2 = error{A, C};
|
||||
\\const Set1 = error{
|
||||
\\ A,
|
||||
\\ B,
|
||||
\\};
|
||||
\\const Set2 = error{
|
||||
\\ A,
|
||||
\\ C,
|
||||
\\};
|
||||
\\comptime {
|
||||
\\ var x = usize(Set1.B);
|
||||
\\ var y = Set2(x);
|
||||
\\ var x = @errorToInt(Set1.B);
|
||||
\\ var y = @errSetCast(Set2, @intToError(x));
|
||||
\\}
|
||||
,
|
||||
".tmp_source.zig:5:17: error: integer value 2 represents no error in 'Set2'",
|
||||
".tmp_source.zig:11:13: error: error.B not a member of error set 'Set2'",
|
||||
);
|
||||
|
||||
cases.add(
|
||||
@@ -2612,17 +2621,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||
".tmp_source.zig:2:21: error: expected pointer, found 'usize'",
|
||||
);
|
||||
|
||||
cases.add(
|
||||
"too many error values to cast to small integer",
|
||||
\\const Error = error { A, B, C, D, E, F, G, H };
|
||||
\\fn foo(e: Error) u2 {
|
||||
\\ return u2(e);
|
||||
\\}
|
||||
\\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
|
||||
,
|
||||
".tmp_source.zig:3:14: error: too many error values to fit in 'u2'",
|
||||
);
|
||||
|
||||
cases.add(
|
||||
"asm at compile time",
|
||||
\\comptime {
|
||||
|
||||
@@ -175,7 +175,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
|
||||
\\ if (x.len == 0) return error.Whatever;
|
||||
\\}
|
||||
\\fn widenSlice(slice: []align(1) const u8) []align(1) const i32 {
|
||||
\\ return ([]align(1) const i32)(slice);
|
||||
\\ return @bytesToSlice(i32, slice);
|
||||
\\}
|
||||
);
|
||||
|
||||
@@ -227,12 +227,12 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
|
||||
\\pub fn main() void {
|
||||
\\ _ = bar(9999);
|
||||
\\}
|
||||
\\fn bar(x: u32) error {
|
||||
\\ return error(x);
|
||||
\\fn bar(x: u16) error {
|
||||
\\ return @intToError(x);
|
||||
\\}
|
||||
);
|
||||
|
||||
cases.addRuntimeSafety("cast integer to non-global error set and no match",
|
||||
cases.addRuntimeSafety("@errSetCast error not present in destination",
|
||||
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
|
||||
\\ @import("std").os.exit(126);
|
||||
\\}
|
||||
@@ -242,7 +242,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
|
||||
\\ _ = foo(Set1.B);
|
||||
\\}
|
||||
\\fn foo(set1: Set1) Set2 {
|
||||
\\ return Set2(set1);
|
||||
\\ return @errSetCast(Set2, set1);
|
||||
\\}
|
||||
);
|
||||
|
||||
@@ -252,12 +252,12 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
|
||||
\\}
|
||||
\\pub fn main() !void {
|
||||
\\ var array align(4) = []u32{0x11111111, 0x11111111};
|
||||
\\ const bytes = ([]u8)(array[0..]);
|
||||
\\ const bytes = @sliceToBytes(array[0..]);
|
||||
\\ if (foo(bytes) != 0x11111111) return error.Wrong;
|
||||
\\}
|
||||
\\fn foo(bytes: []u8) u32 {
|
||||
\\ const slice4 = bytes[1..5];
|
||||
\\ const int_slice = ([]u32)(@alignCast(4, slice4));
|
||||
\\ const int_slice = @bytesToSlice(u32, @alignCast(4, slice4));
|
||||
\\ return int_slice[0];
|
||||
\\}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user