6dba1f1c8e
* `@truncate` builtin allows casting to the same size integer. It also performs two's complement casting between signed and unsigned integers. * The idiomatic way to convert between bytes and numbers is now `mem.readInt` and `mem.writeInt` instead of an unsafe cast. It works at compile time, is safer, and looks cleaner. * Implicitly casting an array to a slice is allowed only if the slice is const. * Constant pointer values know if their memory is from a compile- time constant value or a compile-time variable. * Cast from [N]u8 to []T no longer allowed, but [N]u8 to []const T still allowed. * Fix inability to pass a mutable pointer to comptime variable at compile-time to a function and have the function modify the memory pointed to by the pointer. * Add the `comptime T: type` parameter back to mem.eql. Prevents accidentally creating instantiations for arrays.
103 lines
1.7 KiB
Zig
103 lines
1.7 KiB
Zig
const assert = @import("std").debug.assert;
|
|
const mem = @import("std").mem;
|
|
|
|
pub fn foo() -> %i32 {
|
|
const x = %return bar();
|
|
return x + 1
|
|
}
|
|
|
|
pub fn bar() -> %i32 {
|
|
return 13;
|
|
}
|
|
|
|
pub fn baz() -> %i32 {
|
|
const y = foo() %% 1234;
|
|
return y + 1;
|
|
}
|
|
|
|
fn errorWrapping() {
|
|
@setFnTest(this);
|
|
|
|
assert(%%baz() == 15);
|
|
}
|
|
|
|
error ItBroke;
|
|
fn gimmeItBroke() -> []const u8 {
|
|
@errorName(error.ItBroke)
|
|
}
|
|
|
|
fn errorName() {
|
|
@setFnTest(this);
|
|
assert(mem.eql(u8, @errorName(error.AnError), "AnError"));
|
|
assert(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
|
|
}
|
|
error AnError;
|
|
error ALongerErrorName;
|
|
|
|
|
|
fn errorValues() {
|
|
@setFnTest(this);
|
|
|
|
const a = i32(error.err1);
|
|
const b = i32(error.err2);
|
|
assert(a != b);
|
|
}
|
|
error err1;
|
|
error err2;
|
|
|
|
|
|
fn redefinitionOfErrorValuesAllowed() {
|
|
@setFnTest(this);
|
|
|
|
shouldBeNotEqual(error.AnError, error.SecondError);
|
|
}
|
|
error AnError;
|
|
error AnError;
|
|
error SecondError;
|
|
fn shouldBeNotEqual(a: error, b: error) {
|
|
if (a == b) @unreachable()
|
|
}
|
|
|
|
|
|
fn errBinaryOperator() {
|
|
@setFnTest(this);
|
|
|
|
const a = errBinaryOperatorG(true) %% 3;
|
|
const b = errBinaryOperatorG(false) %% 3;
|
|
assert(a == 3);
|
|
assert(b == 10);
|
|
}
|
|
error ItBroke;
|
|
fn errBinaryOperatorG(x: bool) -> %isize {
|
|
if (x) {
|
|
error.ItBroke
|
|
} else {
|
|
isize(10)
|
|
}
|
|
}
|
|
|
|
|
|
fn unwrapSimpleValueFromError() {
|
|
@setFnTest(this);
|
|
|
|
const i = %%unwrapSimpleValueFromErrorDo();
|
|
assert(i == 13);
|
|
}
|
|
fn unwrapSimpleValueFromErrorDo() -> %isize { 13 }
|
|
|
|
|
|
fn errReturnInAssignment() {
|
|
@setFnTest(this);
|
|
|
|
%%doErrReturnInAssignment();
|
|
}
|
|
|
|
fn doErrReturnInAssignment() -> %void {
|
|
var x : i32 = undefined;
|
|
x = %return makeANonErr();
|
|
}
|
|
|
|
fn makeANonErr() -> %i32 {
|
|
return 1;
|
|
}
|