Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write Zig code to avoid some of bugs #30

Closed
master-q opened this issue Oct 14, 2020 · 8 comments
Closed

Write Zig code to avoid some of bugs #30

master-q opened this issue Oct 14, 2020 · 8 comments

Comments

@master-q
Copy link
Member

No description provided.

@master-q
Copy link
Member Author

@master-q
Copy link
Member Author

https://news.ycombinator.com/item?id=17184729

Zig is memory-safe if you keep runtime checks enabled (e.g. with debug or release-safe optimization levels) but it does not have the compile-time guarantees of Rust.

Is it also true today?

@master-q
Copy link
Member Author

const std = @import("std");

pub fn main() !void {
    var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    const ptr = &array[20];
    ptr.* += 1;
}
$ zig build-exe test.zig
./test.zig:5:23: error: index 20 outside array of size 10
    const ptr = &array[20];
                      ^

@master-q
Copy link
Member Author

const std = @import("std");

pub fn main() !void {
    var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    const slice = array[2..20];
    slice[1] += 1;
}
$ zig build-exe test.zig
$ ./test
index out of bounds
/home/kiwamu/tmp/zig/test.zig:5:24: 0x22a934 in main (test)
    const slice = array[2..20];
                       ^

It causes a run-time error.

@master-q
Copy link
Member Author

master-q commented Nov 2, 2020

https://ziglang.org/documentation/master/#Integer-Overflow

If we capture compile time error, we need comptime keyword.

@master-q
Copy link
Member Author

master-q commented Nov 2, 2020

https://ziglang.org/documentation/master/#Division-by-Zero

Or use const variable to causes compile error.

@master-q
Copy link
Member Author

master-q commented Nov 2, 2020

https://ziglang.org/documentation/master/#Lifetime-and-Ownership

Above means the Zig doesn't have lifetime such as Rust's.

@master-q
Copy link
Member Author

master-q commented Nov 2, 2020

Umm... Finally we think that the Zig language is not suitable for this postmortem.

@master-q master-q closed this as completed Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant