This commit is contained in:
2026-01-19 05:43:29 +09:00
parent 40f41a4fd0
commit 0f6fddd794
36 changed files with 1724 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
// Zig provides a level of safety, where problems may be found during execution.
// Safety can be left on, or turned off.
// Zig has many cases of so-called `detectable illegal behaviour`,
// meaning that illegal behaviour will be caught (causing a panic) with safety on,
// but will result in undefined behaviour with safety off.
// Users are strongly recommended to develop and test their software with safety on, despite its speed penalties.
// For example, runtime safety protects you from out of bounds indices.
test "out of bounds" {
const a = [3]u8{ 1, 2, 3 };
var index: u8 = 5;
const b = a[index];
_ = b;
index = index;
}
// test "out of bounds"...index out of bounds
// .\tests.zig:43:14: 0x7ff698cc1b82 in test "out of bounds" (test.obj)
// const b = a[index];
// ^
// The user may disable runtime safety for the current block using the built-in function `@setRuntimeSafety`.
test "out of bounds, no safety" {
@setRuntimeSafety(true);
// defer @setRuntimeSafety(false);
const a = [3]u8{ 1, 2, 3 };
var index: u8 = 5;
const b = a[index]; // no panic
_ = b;
index = index; // no panic
}
// Safety is off for some build modes (to be discussed later).