Skip to content

Commit

Permalink
fix test failure, organize code, add new compile error
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Feb 5, 2018
1 parent ec59f76 commit 44d8d65
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 272 deletions.
9 changes: 4 additions & 5 deletions CMakeLists.txt
Expand Up @@ -439,11 +439,10 @@ set(ZIG_STD_FILES
"os/darwin_errno.zig"
"os/get_user_id.zig"
"os/index.zig"
"os/linux.zig"
"os/linux_errno.zig"
"os/linux_random.zig"
"os/linux_i386.zig"
"os/linux_x86_64.zig"
"os/linux/index.zig"
"os/linux/errno.zig"
"os/linux/i386.zig"
"os/linux/x86_64.zig"
"os/path.zig"
"os/windows/error.zig"
"os/windows/index.zig"
Expand Down
18 changes: 18 additions & 0 deletions src/ir.cpp
Expand Up @@ -6193,6 +6193,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
if (other_type->id == TypeTableEntryIdFloat) {
return true;
} else if (other_type->id == TypeTableEntryIdInt && const_val_is_int) {
if (!other_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) {
Buf *val_buf = buf_alloc();
bigint_append_buf(val_buf, &const_val->data.x_bigint, 10);
ir_add_error(ira, instruction,
buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'",
buf_ptr(val_buf),
buf_ptr(&other_type->name)));
return false;
}
if (bigint_fits_in_bits(&const_val->data.x_bigint, other_type->data.integral.bit_count,
other_type->data.integral.is_signed))
{
Expand All @@ -6205,6 +6214,15 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
if (const_val_fits_in_num_lit(const_val, child_type)) {
return true;
} else if (child_type->id == TypeTableEntryIdInt && const_val_is_int) {
if (!child_type->data.integral.is_signed && const_val->data.x_bigint.is_negative) {
Buf *val_buf = buf_alloc();
bigint_append_buf(val_buf, &const_val->data.x_bigint, 10);
ir_add_error(ira, instruction,
buf_sprintf("cannot cast negative value %s to unsigned integer type '%s'",
buf_ptr(val_buf),
buf_ptr(&child_type->name)));
return false;
}
if (bigint_fits_in_bits(&const_val->data.x_bigint,
child_type->data.integral.bit_count,
child_type->data.integral.is_signed))
Expand Down
2 changes: 1 addition & 1 deletion std/c/linux.zig
@@ -1,4 +1,4 @@
pub use @import("../os/linux_errno.zig");
pub use @import("../os/linux/errno.zig");

pub extern "c" fn getrandom(buf_ptr: &u8, buf_len: usize, flags: c_uint) c_int;
extern "c" fn __errno_location() &c_int;
Expand Down
2 changes: 1 addition & 1 deletion std/io.zig
Expand Up @@ -2,7 +2,7 @@ const std = @import("index.zig");
const builtin = @import("builtin");
const Os = builtin.Os;
const system = switch(builtin.os) {
Os.linux => @import("os/linux.zig"),
Os.linux => @import("os/linux/index.zig"),
Os.macosx, Os.ios => @import("os/darwin.zig"),
Os.windows => @import("os/windows/index.zig"),
else => @compileError("Unsupported OS"),
Expand Down
37 changes: 28 additions & 9 deletions std/os/index.zig
Expand Up @@ -6,7 +6,7 @@ const os = this;

pub const windows = @import("windows/index.zig");
pub const darwin = @import("darwin.zig");
pub const linux = @import("linux.zig");
pub const linux = @import("linux/index.zig");
pub const zen = @import("zen.zig");
pub const posix = switch(builtin.os) {
Os.linux => linux,
Expand Down Expand Up @@ -78,13 +78,28 @@ error WouldBlock;
pub fn getRandomBytes(buf: []u8) %void {
switch (builtin.os) {
Os.linux => while (true) {
const err = posix.getErrno(posix.getRandomBytes(buf));
if (err > 0) return unexpectedErrorPosix(err);
// TODO check libc version and potentially call c.getrandom.
// See #397
const err = posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0));
if (err > 0) {
switch (err) {
posix.EINVAL => unreachable,
posix.EFAULT => unreachable,
posix.EINTR => continue,
posix.ENOSYS => {
const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0);
defer close(fd);

try posixRead(fd, buf);
return;
},
else => return unexpectedErrorPosix(err),
}
}
return;
},
Os.macosx, Os.ios => {
const fd = try posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC,
0, null);
const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0);
defer close(fd);

try posixRead(fd, buf);
Expand Down Expand Up @@ -253,8 +268,12 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al
mem.copy(u8, path0, file_path);
path0[file_path.len] = 0;

return posixOpenC(path0.ptr, flags, perm);
}

pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) %i32 {
while (true) {
const result = posix.open(path0.ptr, flags, perm);
const result = posix.open(file_path, flags, perm);
const err = posix.getErrno(result);
if (err > 0) {
return switch (err) {
Expand Down Expand Up @@ -1486,10 +1505,10 @@ test "std.os" {
_ = @import("darwin_errno.zig");
_ = @import("darwin.zig");
_ = @import("get_user_id.zig");
_ = @import("linux_errno.zig");
_ = @import("linux/errno.zig");
//_ = @import("linux_i386.zig");
_ = @import("linux_x86_64.zig");
_ = @import("linux.zig");
_ = @import("linux/x86_64.zig");
_ = @import("linux/index.zig");
_ = @import("path.zig");
_ = @import("windows/index.zig");
}
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion std/os/linux_i386.zig → std/os/linux/i386.zig
@@ -1,4 +1,5 @@
const linux = @import("linux.zig");
const std = @import("../../index.zig");
const linux = std.os.linux;
const socklen_t = linux.socklen_t;
const iovec = linux.iovec;

Expand Down
13 changes: 6 additions & 7 deletions std/os/linux.zig → std/os/linux/index.zig
@@ -1,13 +1,12 @@
const std = @import("../index.zig");
const std = @import("../../index.zig");
const assert = std.debug.assert;
const builtin = @import("builtin");
const arch = switch (builtin.arch) {
builtin.Arch.x86_64 => @import("linux_x86_64.zig"),
builtin.Arch.i386 => @import("linux_i386.zig"),
builtin.Arch.x86_64 => @import("x86_64.zig"),
builtin.Arch.i386 => @import("i386.zig"),
else => @compileError("unsupported arch"),
};
pub use @import("linux_errno.zig");
pub use @import("linux_random.zig");
pub use @import("errno.zig");

pub const PATH_MAX = 4096;

Expand Down Expand Up @@ -788,10 +787,10 @@ pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_va
return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value));
}

test "import linux_test" {
test "import linux test" {
// TODO lazy analysis should prevent this test from being compiled on windows, but
// it is still compiled on windows
if (builtin.os == builtin.Os.linux) {
_ = @import("linux_test.zig");
_ = @import("test.zig");
}
}
2 changes: 1 addition & 1 deletion std/os/linux_test.zig → std/os/linux/test.zig
@@ -1,4 +1,4 @@
const std = @import("std");
const std = @import("../../index.zig");
const linux = std.os.linux;
const assert = std.debug.assert;

Expand Down
3 changes: 2 additions & 1 deletion std/os/linux_x86_64.zig → std/os/linux/x86_64.zig
@@ -1,4 +1,5 @@
const linux = @import("linux.zig");
const std = @import("../../index.zig");
const linux = std.os.linux;
const socklen_t = linux.socklen_t;
const iovec = linux.iovec;

Expand Down

0 comments on commit 44d8d65

Please sign in to comment.