authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-23 14:32:13-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-23 14:32:13-04:00
log5a919dd82d798c489219ea60a59f1c0ea3fc3956
tree4fd8ca25bfa73d842b803353a8aa80a510072829
parent10d2f08d376a302bd79e87d17c06922d3145a939
parent99153ac0aa390f01091308073b39947c45851ae6

Merge remote-tracking branch 'origin/master' into self-hosted-libc-hello-world


15 files changed, 683 insertions(+), 387 deletions(-)

CMakeLists.txt+1-1
......@@ -556,7 +556,7 @@ set(ZIG_STD_FILES
556556 "net.zig"
557557 "os/child_process.zig"
558558 "os/darwin.zig"
559 "os/darwin_errno.zig"
559 "os/darwin/errno.zig"
560560 "os/epoch.zig"
561561 "os/file.zig"
562562 "os/get_app_data_dir.zig"
README.md+1-1
......@@ -70,7 +70,7 @@ that counts as "freestanding" for the purposes of this table.
7070
7171## Community
7272
73 * IRC: `#zig` on Freenode.
73 * IRC: `#zig` on Freenode ([Channel Logs](https://irclog.whitequark.org/zig/)).
7474 * Reddit: [/r/zig](https://www.reddit.com/r/zig)
7575 * Email list: [ziglang@googlegroups.com](https://groups.google.com/forum/#!forum/ziglang)
7676
src-self-hosted/ir.zig+1-2
......@@ -2172,8 +2172,7 @@ const Analyze = struct {
21722172 break :fits true;
21732173 }
21742174 if (dest_type.cast(Type.Int)) |int| {
2175 break :fits (from_int.positive or from_int.eqZero() or int.key.is_signed) and
2176 int.key.bit_count >= from_int.bitcount();
2175 break :fits from_int.fitsInTwosComp(int.key.is_signed, int.key.bit_count);
21772176 }
21782177 break :cast;
21792178 };
src/codegen.cpp+31
......@@ -60,6 +60,33 @@ PackageTableEntry *new_anonymous_package(void) {
6060 return new_package("", "");
6161}
6262
63static const char *symbols_that_llvm_depends_on[] = {
64 "memcpy",
65 "memset",
66 "sqrt",
67 "powi",
68 "sin",
69 "cos",
70 "pow",
71 "exp",
72 "exp2",
73 "log",
74 "log10",
75 "log2",
76 "fma",
77 "fabs",
78 "minnum",
79 "maxnum",
80 "copysign",
81 "floor",
82 "ceil",
83 "trunc",
84 "rint",
85 "nearbyint",
86 "round",
87 // TODO probably all of compiler-rt needs to go here
88};
89
6390CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
6491 Buf *zig_lib_dir)
6592{
......@@ -94,6 +121,10 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
94121 g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib);
95122 buf_resize(&g->global_asm, 0);
96123
124 for (size_t i = 0; i < array_length(symbols_that_llvm_depends_on); i += 1) {
125 g->external_prototypes.put(buf_create_from_str(symbols_that_llvm_depends_on[i]), nullptr);
126 }
127
97128 if (root_src_path) {
98129 Buf *src_basename = buf_alloc();
99130 Buf *src_dir = buf_alloc();
src/ir.cpp+59-25
......@@ -2961,16 +2961,34 @@ static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_sco
29612961 results[ReturnKindUnconditional] = 0;
29622962 results[ReturnKindError] = 0;
29632963
2964 while (inner_scope != outer_scope) {
2965 assert(inner_scope);
2966 if (inner_scope->id == ScopeIdDefer) {
2967 AstNode *defer_node = inner_scope->source_node;
2968 assert(defer_node->type == NodeTypeDefer);
2969 ReturnKind defer_kind = defer_node->data.defer.kind;
2970 results[defer_kind] += 1;
2964 Scope *scope = inner_scope;
29712965
2966 while (scope != outer_scope) {
2967 assert(scope);
2968 switch (scope->id) {
2969 case ScopeIdDefer: {
2970 AstNode *defer_node = scope->source_node;
2971 assert(defer_node->type == NodeTypeDefer);
2972 ReturnKind defer_kind = defer_node->data.defer.kind;
2973 results[defer_kind] += 1;
2974 scope = scope->parent;
2975 continue;
2976 }
2977 case ScopeIdDecls:
2978 case ScopeIdFnDef:
2979 return;
2980 case ScopeIdBlock:
2981 case ScopeIdVarDecl:
2982 case ScopeIdLoop:
2983 case ScopeIdSuspend:
2984 case ScopeIdCompTime:
2985 scope = scope->parent;
2986 continue;
2987 case ScopeIdDeferExpr:
2988 case ScopeIdCImport:
2989 case ScopeIdCoroPrelude:
2990 zig_unreachable();
29722991 }
2973 inner_scope = inner_scope->parent;
29742992 }
29752993}
29762994
......@@ -2986,27 +3004,43 @@ static bool ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *o
29863004 if (!scope)
29873005 return is_noreturn;
29883006
2989 if (scope->id == ScopeIdDefer) {
2990 AstNode *defer_node = scope->source_node;
2991 assert(defer_node->type == NodeTypeDefer);
2992 ReturnKind defer_kind = defer_node->data.defer.kind;
2993 if (defer_kind == ReturnKindUnconditional ||
2994 (gen_error_defers && defer_kind == ReturnKindError))
2995 {
2996 AstNode *defer_expr_node = defer_node->data.defer.expr;
2997 Scope *defer_expr_scope = defer_node->data.defer.expr_scope;
2998 IrInstruction *defer_expr_value = ir_gen_node(irb, defer_expr_node, defer_expr_scope);
2999 if (defer_expr_value != irb->codegen->invalid_instruction) {
3000 if (defer_expr_value->value.type != nullptr && defer_expr_value->value.type->id == TypeTableEntryIdUnreachable) {
3001 is_noreturn = true;
3002 } else {
3003 ir_mark_gen(ir_build_check_statement_is_void(irb, defer_expr_scope, defer_expr_node, defer_expr_value));
3007 switch (scope->id) {
3008 case ScopeIdDefer: {
3009 AstNode *defer_node = scope->source_node;
3010 assert(defer_node->type == NodeTypeDefer);
3011 ReturnKind defer_kind = defer_node->data.defer.kind;
3012 if (defer_kind == ReturnKindUnconditional ||
3013 (gen_error_defers && defer_kind == ReturnKindError))
3014 {
3015 AstNode *defer_expr_node = defer_node->data.defer.expr;
3016 Scope *defer_expr_scope = defer_node->data.defer.expr_scope;
3017 IrInstruction *defer_expr_value = ir_gen_node(irb, defer_expr_node, defer_expr_scope);
3018 if (defer_expr_value != irb->codegen->invalid_instruction) {
3019 if (defer_expr_value->value.type != nullptr && defer_expr_value->value.type->id == TypeTableEntryIdUnreachable) {
3020 is_noreturn = true;
3021 } else {
3022 ir_mark_gen(ir_build_check_statement_is_void(irb, defer_expr_scope, defer_expr_node, defer_expr_value));
3023 }
30043024 }
30053025 }
3026 scope = scope->parent;
3027 continue;
30063028 }
3007
3029 case ScopeIdDecls:
3030 case ScopeIdFnDef:
3031 return is_noreturn;
3032 case ScopeIdBlock:
3033 case ScopeIdVarDecl:
3034 case ScopeIdLoop:
3035 case ScopeIdSuspend:
3036 case ScopeIdCompTime:
3037 scope = scope->parent;
3038 continue;
3039 case ScopeIdDeferExpr:
3040 case ScopeIdCImport:
3041 case ScopeIdCoroPrelude:
3042 zig_unreachable();
30083043 }
3009 scope = scope->parent;
30103044 }
30113045 return is_noreturn;
30123046}
std/c/darwin.zig+1-1
......@@ -30,7 +30,7 @@ pub extern "c" fn sysctl(name: [*]c_int, namelen: c_uint, oldp: ?*c_void, oldlen
3030pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
3131pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
3232
33pub use @import("../os/darwin_errno.zig");
33pub use @import("../os/darwin/errno.zig");
3434
3535pub const _errno = __error;
3636
std/event/tcp.zig+2-1
......@@ -125,8 +125,9 @@ pub async fn connect(loop: *Loop, _address: *const std.net.Address) !std.os.File
125125test "listen on a port, send bytes, receive bytes" {
126126 if (builtin.os != builtin.Os.linux) {
127127 // TODO build abstractions for other operating systems
128 return;
128 return error.SkipZigTest;
129129 }
130
130131 const MyServer = struct {
131132 tcp_server: Server,
132133
std/math/big/int.zig+134-12
......@@ -116,13 +116,63 @@ pub const Int = struct {
116116 return !r.isOdd();
117117 }
118118
119 fn bitcount(self: Int) usize {
120 const u_bit_count = (self.len - 1) * Limb.bit_count + (Limb.bit_count - @clz(self.limbs[self.len - 1]));
121 return @boolToInt(!self.positive) + u_bit_count;
119 // Returns the number of bits required to represent the absolute value of self.
120 fn bitCountAbs(self: Int) usize {
121 return (self.len - 1) * Limb.bit_count + (Limb.bit_count - @clz(self.limbs[self.len - 1]));
122122 }
123123
124 // Returns the number of bits required to represent the integer in twos-complement form.
125 //
126 // If the integer is negative the value returned is the number of bits needed by a signed
127 // integer to represent the value. If positive the value is the number of bits for an
128 // unsigned integer. Any unsigned integer will fit in the signed integer with bitcount
129 // one greater than the returned value.
130 //
131 // e.g. -127 returns 8 as it will fit in an i8. 127 returns 7 since it fits in a u7.
132 fn bitCountTwosComp(self: Int) usize {
133 var bits = self.bitCountAbs();
134
135 // If the entire value has only one bit set (e.g. 0b100000000) then the negation in twos
136 // complement requires one less bit.
137 if (!self.positive) block: {
138 bits += 1;
139
140 if (@popCount(self.limbs[self.len - 1]) == 1) {
141 for (self.limbs[0 .. self.len - 1]) |limb| {
142 if (@popCount(limb) != 0) {
143 break :block;
144 }
145 }
146
147 bits -= 1;
148 }
149 }
150
151 return bits;
152 }
153
154 pub fn fitsInTwosComp(self: Int, is_signed: bool, bit_count: usize) bool {
155 if (self.eqZero()) {
156 return true;
157 }
158 if (!is_signed and !self.positive) {
159 return false;
160 }
161
162 const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and is_signed);
163 return bit_count >= req_bits;
164 }
165
166 pub fn fits(self: Int, comptime T: type) bool {
167 return self.fitsInTwosComp(T.is_signed, T.bit_count);
168 }
169
170 // Returns the approximate size of the integer in the given base. Negative values accomodate for
171 // the minus sign. This is used for determining the number of characters needed to print the
172 // value. It is inexact and will exceed the given value by 1-2 digits.
124173 pub fn sizeInBase(self: Int, base: usize) usize {
125 return (self.bitcount() / math.log2(base)) + 1;
174 const bit_count = usize(@boolToInt(!self.positive)) + self.bitCountAbs();
175 return (bit_count / math.log2(base)) + 1;
126176 }
127177
128178 pub fn set(self: *Int, value: var) Allocator.Error!void {
......@@ -190,9 +240,9 @@ pub const Int = struct {
190240 pub fn to(self: Int, comptime T: type) ConvertError!T {
191241 switch (@typeId(T)) {
192242 TypeId.Int => {
193 const UT = if (T.is_signed) @IntType(false, T.bit_count - 1) else T;
243 const UT = @IntType(false, T.bit_count);
194244
195 if (self.bitcount() > 8 * @sizeOf(UT)) {
245 if (self.bitCountTwosComp() > T.bit_count) {
196246 return error.TargetTooSmall;
197247 }
198248
......@@ -209,9 +259,17 @@ pub const Int = struct {
209259 }
210260
211261 if (!T.is_signed) {
212 return if (self.positive) r else error.NegativeIntoUnsigned;
262 return if (self.positive) @intCast(T, r) else error.NegativeIntoUnsigned;
213263 } else {
214 return if (self.positive) @intCast(T, r) else -@intCast(T, r);
264 if (self.positive) {
265 return @intCast(T, r);
266 } else {
267 if (math.cast(T, r)) |ok| {
268 return -ok;
269 } else |_| {
270 return @minValue(T);
271 }
272 }
215273 }
216274 },
217275 else => {
......@@ -1137,24 +1195,88 @@ test "big.int bitcount + sizeInBase" {
11371195 var a = try Int.init(al);
11381196
11391197 try a.set(0b100);
1140 debug.assert(a.bitcount() == 3);
1198 debug.assert(a.bitCountAbs() == 3);
11411199 debug.assert(a.sizeInBase(2) >= 3);
11421200 debug.assert(a.sizeInBase(10) >= 1);
11431201
1202 a.negate();
1203 debug.assert(a.bitCountAbs() == 3);
1204 debug.assert(a.sizeInBase(2) >= 4);
1205 debug.assert(a.sizeInBase(10) >= 2);
1206
11441207 try a.set(0xffffffff);
1145 debug.assert(a.bitcount() == 32);
1208 debug.assert(a.bitCountAbs() == 32);
11461209 debug.assert(a.sizeInBase(2) >= 32);
11471210 debug.assert(a.sizeInBase(10) >= 10);
11481211
11491212 try a.shiftLeft(a, 5000);
1150 debug.assert(a.bitcount() == 5032);
1213 debug.assert(a.bitCountAbs() == 5032);
11511214 debug.assert(a.sizeInBase(2) >= 5032);
11521215 a.positive = false;
11531216
1154 debug.assert(a.bitcount() == 5033);
1217 debug.assert(a.bitCountAbs() == 5032);
11551218 debug.assert(a.sizeInBase(2) >= 5033);
11561219}
11571220
1221test "big.int bitcount/to" {
1222 var a = try Int.init(al);
1223
1224 try a.set(0);
1225 debug.assert(a.bitCountTwosComp() == 0);
1226
1227 // TODO: stack smashing
1228 // debug.assert((try a.to(u0)) == 0);
1229 // TODO: sigsegv
1230 // debug.assert((try a.to(i0)) == 0);
1231
1232 try a.set(-1);
1233 debug.assert(a.bitCountTwosComp() == 1);
1234 debug.assert((try a.to(i1)) == -1);
1235
1236 try a.set(-8);
1237 debug.assert(a.bitCountTwosComp() == 4);
1238 debug.assert((try a.to(i4)) == -8);
1239
1240 try a.set(127);
1241 debug.assert(a.bitCountTwosComp() == 7);
1242 debug.assert((try a.to(u7)) == 127);
1243
1244 try a.set(-128);
1245 debug.assert(a.bitCountTwosComp() == 8);
1246 debug.assert((try a.to(i8)) == -128);
1247
1248 try a.set(-129);
1249 debug.assert(a.bitCountTwosComp() == 9);
1250 debug.assert((try a.to(i9)) == -129);
1251}
1252
1253test "big.int fits" {
1254 var a = try Int.init(al);
1255
1256 try a.set(0);
1257 debug.assert(a.fits(u0));
1258 debug.assert(a.fits(i0));
1259
1260 try a.set(255);
1261 debug.assert(!a.fits(u0));
1262 debug.assert(!a.fits(u1));
1263 debug.assert(!a.fits(i8));
1264 debug.assert(a.fits(u8));
1265 debug.assert(a.fits(u9));
1266 debug.assert(a.fits(i9));
1267
1268 try a.set(-128);
1269 debug.assert(!a.fits(i7));
1270 debug.assert(a.fits(i8));
1271 debug.assert(a.fits(i9));
1272 debug.assert(!a.fits(u9));
1273
1274 try a.set(0x1ffffffffeeeeeeee);
1275 debug.assert(!a.fits(u32));
1276 debug.assert(!a.fits(u64));
1277 debug.assert(a.fits(u65));
1278}
1279
11581280test "big.int string set" {
11591281 var a = try Int.init(al);
11601282 try a.setString(10, "120317241209124781241290847124");
std/os/darwin.zig+87-1
......@@ -2,7 +2,7 @@ const std = @import("../index.zig");
22const c = std.c;
33const assert = std.debug.assert;
44
5pub use @import("darwin_errno.zig");
5pub use @import("darwin/errno.zig");
66
77pub const PATH_MAX = 1024;
88
......@@ -482,6 +482,92 @@ pub const NOTE_MACH_CONTINUOUS_TIME = 0x00000080;
482482/// data is mach absolute time units
483483pub const NOTE_MACHTIME = 0x00000100;
484484
485pub const AF_UNSPEC: c_int = 0;
486pub const AF_LOCAL: c_int = 1;
487pub const AF_UNIX: c_int = AF_LOCAL;
488pub const AF_INET: c_int = 2;
489pub const AF_SYS_CONTROL: c_int = 2;
490pub const AF_IMPLINK: c_int = 3;
491pub const AF_PUP: c_int = 4;
492pub const AF_CHAOS: c_int = 5;
493pub const AF_NS: c_int = 6;
494pub const AF_ISO: c_int = 7;
495pub const AF_OSI: c_int = AF_ISO;
496pub const AF_ECMA: c_int = 8;
497pub const AF_DATAKIT: c_int = 9;
498pub const AF_CCITT: c_int = 10;
499pub const AF_SNA: c_int = 11;
500pub const AF_DECnet: c_int = 12;
501pub const AF_DLI: c_int = 13;
502pub const AF_LAT: c_int = 14;
503pub const AF_HYLINK: c_int = 15;
504pub const AF_APPLETALK: c_int = 16;
505pub const AF_ROUTE: c_int = 17;
506pub const AF_LINK: c_int = 18;
507pub const AF_XTP: c_int = 19;
508pub const AF_COIP: c_int = 20;
509pub const AF_CNT: c_int = 21;
510pub const AF_RTIP: c_int = 22;
511pub const AF_IPX: c_int = 23;
512pub const AF_SIP: c_int = 24;
513pub const AF_PIP: c_int = 25;
514pub const AF_ISDN: c_int = 28;
515pub const AF_E164: c_int = AF_ISDN;
516pub const AF_KEY: c_int = 29;
517pub const AF_INET6: c_int = 30;
518pub const AF_NATM: c_int = 31;
519pub const AF_SYSTEM: c_int = 32;
520pub const AF_NETBIOS: c_int = 33;
521pub const AF_PPP: c_int = 34;
522pub const AF_MAX: c_int = 40;
523
524pub const PF_UNSPEC: c_int = AF_UNSPEC;
525pub const PF_LOCAL: c_int = AF_LOCAL;
526pub const PF_UNIX: c_int = PF_LOCAL;
527pub const PF_INET: c_int = AF_INET;
528pub const PF_IMPLINK: c_int = AF_IMPLINK;
529pub const PF_PUP: c_int = AF_PUP;
530pub const PF_CHAOS: c_int = AF_CHAOS;
531pub const PF_NS: c_int = AF_NS;
532pub const PF_ISO: c_int = AF_ISO;
533pub const PF_OSI: c_int = AF_ISO;
534pub const PF_ECMA: c_int = AF_ECMA;
535pub const PF_DATAKIT: c_int = AF_DATAKIT;
536pub const PF_CCITT: c_int = AF_CCITT;
537pub const PF_SNA: c_int = AF_SNA;
538pub const PF_DECnet: c_int = AF_DECnet;
539pub const PF_DLI: c_int = AF_DLI;
540pub const PF_LAT: c_int = AF_LAT;
541pub const PF_HYLINK: c_int = AF_HYLINK;
542pub const PF_APPLETALK: c_int = AF_APPLETALK;
543pub const PF_ROUTE: c_int = AF_ROUTE;
544pub const PF_LINK: c_int = AF_LINK;
545pub const PF_XTP: c_int = AF_XTP;
546pub const PF_COIP: c_int = AF_COIP;
547pub const PF_CNT: c_int = AF_CNT;
548pub const PF_SIP: c_int = AF_SIP;
549pub const PF_IPX: c_int = AF_IPX;
550pub const PF_RTIP: c_int = AF_RTIP;
551pub const PF_PIP: c_int = AF_PIP;
552pub const PF_ISDN: c_int = AF_ISDN;
553pub const PF_KEY: c_int = AF_KEY;
554pub const PF_INET6: c_int = AF_INET6;
555pub const PF_NATM: c_int = AF_NATM;
556pub const PF_SYSTEM: c_int = AF_SYSTEM;
557pub const PF_NETBIOS: c_int = AF_NETBIOS;
558pub const PF_PPP: c_int = AF_PPP;
559pub const PF_MAX: c_int = AF_MAX;
560
561pub const SYSPROTO_EVENT: c_int = 1;
562pub const SYSPROTO_CONTROL: c_int = 2;
563
564pub const SOCK_STREAM: c_int = 1;
565pub const SOCK_DGRAM: c_int = 2;
566pub const SOCK_RAW: c_int = 3;
567pub const SOCK_RDM: c_int = 4;
568pub const SOCK_SEQPACKET: c_int = 5;
569pub const SOCK_MAXADDRLEN: c_int = 255;
570
485571fn wstatus(x: i32) i32 {
486572 return x & 0o177;
487573}
std/os/darwin/errno.zig created+328
......@@ -0,0 +1,328 @@
1/// Operation not permitted
2pub const EPERM = 1;
3
4/// No such file or directory
5pub const ENOENT = 2;
6
7/// No such process
8pub const ESRCH = 3;
9
10/// Interrupted system call
11pub const EINTR = 4;
12
13/// Input/output error
14pub const EIO = 5;
15
16/// Device not configured
17pub const ENXIO = 6;
18
19/// Argument list too long
20pub const E2BIG = 7;
21
22/// Exec format error
23pub const ENOEXEC = 8;
24
25/// Bad file descriptor
26pub const EBADF = 9;
27
28/// No child processes
29pub const ECHILD = 10;
30
31/// Resource deadlock avoided
32pub const EDEADLK = 11;
33
34/// Cannot allocate memory
35pub const ENOMEM = 12;
36
37/// Permission denied
38pub const EACCES = 13;
39
40/// Bad address
41pub const EFAULT = 14;
42
43/// Block device required
44pub const ENOTBLK = 15;
45
46/// Device / Resource busy
47pub const EBUSY = 16;
48
49/// File exists
50pub const EEXIST = 17;
51
52/// Cross-device link
53pub const EXDEV = 18;
54
55/// Operation not supported by device
56pub const ENODEV = 19;
57
58/// Not a directory
59pub const ENOTDIR = 20;
60
61/// Is a directory
62pub const EISDIR = 21;
63
64/// Invalid argument
65pub const EINVAL = 22;
66
67/// Too many open files in system
68pub const ENFILE = 23;
69
70/// Too many open files
71pub const EMFILE = 24;
72
73/// Inappropriate ioctl for device
74pub const ENOTTY = 25;
75
76/// Text file busy
77pub const ETXTBSY = 26;
78
79/// File too large
80pub const EFBIG = 27;
81
82/// No space left on device
83pub const ENOSPC = 28;
84
85/// Illegal seek
86pub const ESPIPE = 29;
87
88/// Read-only file system
89pub const EROFS = 30;
90
91/// Too many links
92pub const EMLINK = 31;
93/// Broken pipe
94
95// math software
96pub const EPIPE = 32;
97
98/// Numerical argument out of domain
99pub const EDOM = 33;
100/// Result too large
101
102// non-blocking and interrupt i/o
103pub const ERANGE = 34;
104
105/// Resource temporarily unavailable
106pub const EAGAIN = 35;
107
108/// Operation would block
109pub const EWOULDBLOCK = EAGAIN;
110
111/// Operation now in progress
112pub const EINPROGRESS = 36;
113/// Operation already in progress
114
115// ipc/network software -- argument errors
116pub const EALREADY = 37;
117
118/// Socket operation on non-socket
119pub const ENOTSOCK = 38;
120
121/// Destination address required
122pub const EDESTADDRREQ = 39;
123
124/// Message too long
125pub const EMSGSIZE = 40;
126
127/// Protocol wrong type for socket
128pub const EPROTOTYPE = 41;
129
130/// Protocol not available
131pub const ENOPROTOOPT = 42;
132
133/// Protocol not supported
134pub const EPROTONOSUPPORT = 43;
135
136/// Socket type not supported
137pub const ESOCKTNOSUPPORT = 44;
138
139/// Operation not supported
140pub const ENOTSUP = 45;
141
142/// Protocol family not supported
143pub const EPFNOSUPPORT = 46;
144
145/// Address family not supported by protocol family
146pub const EAFNOSUPPORT = 47;
147
148/// Address already in use
149pub const EADDRINUSE = 48;
150/// Can't assign requested address
151
152// ipc/network software -- operational errors
153pub const EADDRNOTAVAIL = 49;
154
155/// Network is down
156pub const ENETDOWN = 50;
157
158/// Network is unreachable
159pub const ENETUNREACH = 51;
160
161/// Network dropped connection on reset
162pub const ENETRESET = 52;
163
164/// Software caused connection abort
165pub const ECONNABORTED = 53;
166
167/// Connection reset by peer
168pub const ECONNRESET = 54;
169
170/// No buffer space available
171pub const ENOBUFS = 55;
172
173/// Socket is already connected
174pub const EISCONN = 56;
175
176/// Socket is not connected
177pub const ENOTCONN = 57;
178
179/// Can't send after socket shutdown
180pub const ESHUTDOWN = 58;
181
182/// Too many references: can't splice
183pub const ETOOMANYREFS = 59;
184
185/// Operation timed out
186pub const ETIMEDOUT = 60;
187
188/// Connection refused
189pub const ECONNREFUSED = 61;
190
191/// Too many levels of symbolic links
192pub const ELOOP = 62;
193
194/// File name too long
195pub const ENAMETOOLONG = 63;
196
197/// Host is down
198pub const EHOSTDOWN = 64;
199
200/// No route to host
201pub const EHOSTUNREACH = 65;
202/// Directory not empty
203
204// quotas & mush
205pub const ENOTEMPTY = 66;
206
207/// Too many processes
208pub const EPROCLIM = 67;
209
210/// Too many users
211pub const EUSERS = 68;
212/// Disc quota exceeded
213
214// Network File System
215pub const EDQUOT = 69;
216
217/// Stale NFS file handle
218pub const ESTALE = 70;
219
220/// Too many levels of remote in path
221pub const EREMOTE = 71;
222
223/// RPC struct is bad
224pub const EBADRPC = 72;
225
226/// RPC version wrong
227pub const ERPCMISMATCH = 73;
228
229/// RPC prog. not avail
230pub const EPROGUNAVAIL = 74;
231
232/// Program version wrong
233pub const EPROGMISMATCH = 75;
234
235/// Bad procedure for program
236pub const EPROCUNAVAIL = 76;
237
238/// No locks available
239pub const ENOLCK = 77;
240
241/// Function not implemented
242pub const ENOSYS = 78;
243
244/// Inappropriate file type or format
245pub const EFTYPE = 79;
246
247/// Authentication error
248pub const EAUTH = 80;
249/// Need authenticator
250
251// Intelligent device errors
252pub const ENEEDAUTH = 81;
253
254/// Device power is off
255pub const EPWROFF = 82;
256
257/// Device error, e.g. paper out
258pub const EDEVERR = 83;
259/// Value too large to be stored in data type
260
261// Program loading errors
262pub const EOVERFLOW = 84;
263
264/// Bad executable
265pub const EBADEXEC = 85;
266
267/// Bad CPU type in executable
268pub const EBADARCH = 86;
269
270/// Shared library version mismatch
271pub const ESHLIBVERS = 87;
272
273/// Malformed Macho file
274pub const EBADMACHO = 88;
275
276/// Operation canceled
277pub const ECANCELED = 89;
278
279/// Identifier removed
280pub const EIDRM = 90;
281
282/// No message of desired type
283pub const ENOMSG = 91;
284
285/// Illegal byte sequence
286pub const EILSEQ = 92;
287
288/// Attribute not found
289pub const ENOATTR = 93;
290
291/// Bad message
292pub const EBADMSG = 94;
293
294/// Reserved
295pub const EMULTIHOP = 95;
296
297/// No message available on STREAM
298pub const ENODATA = 96;
299
300/// Reserved
301pub const ENOLINK = 97;
302
303/// No STREAM resources
304pub const ENOSR = 98;
305
306/// Not a STREAM
307pub const ENOSTR = 99;
308
309/// Protocol error
310pub const EPROTO = 100;
311
312/// STREAM ioctl timeout
313pub const ETIME = 101;
314
315/// No such policy registered
316pub const ENOPOLICY = 103;
317
318/// State not recoverable
319pub const ENOTRECOVERABLE = 104;
320
321/// Previous owner died
322pub const EOWNERDEAD = 105;
323
324/// Interface output queue is full
325pub const EQFULL = 106;
326
327/// Must be equal largest errno
328pub const ELAST = 106;
std/os/darwin_errno.zig deleted-328
......@@ -1,328 +0,0 @@
1/// Operation not permitted
2pub const EPERM = 1;
3
4/// No such file or directory
5pub const ENOENT = 2;
6
7/// No such process
8pub const ESRCH = 3;
9
10/// Interrupted system call
11pub const EINTR = 4;
12
13/// Input/output error
14pub const EIO = 5;
15
16/// Device not configured
17pub const ENXIO = 6;
18
19/// Argument list too long
20pub const E2BIG = 7;
21
22/// Exec format error
23pub const ENOEXEC = 8;
24
25/// Bad file descriptor
26pub const EBADF = 9;
27
28/// No child processes
29pub const ECHILD = 10;
30
31/// Resource deadlock avoided
32pub const EDEADLK = 11;
33
34/// Cannot allocate memory
35pub const ENOMEM = 12;
36
37/// Permission denied
38pub const EACCES = 13;
39
40/// Bad address
41pub const EFAULT = 14;
42
43/// Block device required
44pub const ENOTBLK = 15;
45
46/// Device / Resource busy
47pub const EBUSY = 16;
48
49/// File exists
50pub const EEXIST = 17;
51
52/// Cross-device link
53pub const EXDEV = 18;
54
55/// Operation not supported by device
56pub const ENODEV = 19;
57
58/// Not a directory
59pub const ENOTDIR = 20;
60
61/// Is a directory
62pub const EISDIR = 21;
63
64/// Invalid argument
65pub const EINVAL = 22;
66
67/// Too many open files in system
68pub const ENFILE = 23;
69
70/// Too many open files
71pub const EMFILE = 24;
72
73/// Inappropriate ioctl for device
74pub const ENOTTY = 25;
75
76/// Text file busy
77pub const ETXTBSY = 26;
78
79/// File too large
80pub const EFBIG = 27;
81
82/// No space left on device
83pub const ENOSPC = 28;
84
85/// Illegal seek
86pub const ESPIPE = 29;
87
88/// Read-only file system
89pub const EROFS = 30;
90
91/// Too many links
92pub const EMLINK = 31;
93/// Broken pipe
94
95// math software
96pub const EPIPE = 32;
97
98/// Numerical argument out of domain
99pub const EDOM = 33;
100/// Result too large
101
102// non-blocking and interrupt i/o
103pub const ERANGE = 34;
104
105/// Resource temporarily unavailable
106pub const EAGAIN = 35;
107
108/// Operation would block
109pub const EWOULDBLOCK = EAGAIN;
110
111/// Operation now in progress
112pub const EINPROGRESS = 36;
113/// Operation already in progress
114
115// ipc/network software -- argument errors
116pub const EALREADY = 37;
117
118/// Socket operation on non-socket
119pub const ENOTSOCK = 38;
120
121/// Destination address required
122pub const EDESTADDRREQ = 39;
123
124/// Message too long
125pub const EMSGSIZE = 40;
126
127/// Protocol wrong type for socket
128pub const EPROTOTYPE = 41;
129
130/// Protocol not available
131pub const ENOPROTOOPT = 42;
132
133/// Protocol not supported
134pub const EPROTONOSUPPORT = 43;
135
136/// Socket type not supported
137pub const ESOCKTNOSUPPORT = 44;
138
139/// Operation not supported
140pub const ENOTSUP = 45;
141
142/// Protocol family not supported
143pub const EPFNOSUPPORT = 46;
144
145/// Address family not supported by protocol family
146pub const EAFNOSUPPORT = 47;
147
148/// Address already in use
149pub const EADDRINUSE = 48;
150/// Can't assign requested address
151
152// ipc/network software -- operational errors
153pub const EADDRNOTAVAIL = 49;
154
155/// Network is down
156pub const ENETDOWN = 50;
157
158/// Network is unreachable
159pub const ENETUNREACH = 51;
160
161/// Network dropped connection on reset
162pub const ENETRESET = 52;
163
164/// Software caused connection abort
165pub const ECONNABORTED = 53;
166
167/// Connection reset by peer
168pub const ECONNRESET = 54;
169
170/// No buffer space available
171pub const ENOBUFS = 55;
172
173/// Socket is already connected
174pub const EISCONN = 56;
175
176/// Socket is not connected
177pub const ENOTCONN = 57;
178
179/// Can't send after socket shutdown
180pub const ESHUTDOWN = 58;
181
182/// Too many references: can't splice
183pub const ETOOMANYREFS = 59;
184
185/// Operation timed out
186pub const ETIMEDOUT = 60;
187
188/// Connection refused
189pub const ECONNREFUSED = 61;
190
191/// Too many levels of symbolic links
192pub const ELOOP = 62;
193
194/// File name too long
195pub const ENAMETOOLONG = 63;
196
197/// Host is down
198pub const EHOSTDOWN = 64;
199
200/// No route to host
201pub const EHOSTUNREACH = 65;
202/// Directory not empty
203
204// quotas & mush
205pub const ENOTEMPTY = 66;
206
207/// Too many processes
208pub const EPROCLIM = 67;
209
210/// Too many users
211pub const EUSERS = 68;
212/// Disc quota exceeded
213
214// Network File System
215pub const EDQUOT = 69;
216
217/// Stale NFS file handle
218pub const ESTALE = 70;
219
220/// Too many levels of remote in path
221pub const EREMOTE = 71;
222
223/// RPC struct is bad
224pub const EBADRPC = 72;
225
226/// RPC version wrong
227pub const ERPCMISMATCH = 73;
228
229/// RPC prog. not avail
230pub const EPROGUNAVAIL = 74;
231
232/// Program version wrong
233pub const EPROGMISMATCH = 75;
234
235/// Bad procedure for program
236pub const EPROCUNAVAIL = 76;
237
238/// No locks available
239pub const ENOLCK = 77;
240
241/// Function not implemented
242pub const ENOSYS = 78;
243
244/// Inappropriate file type or format
245pub const EFTYPE = 79;
246
247/// Authentication error
248pub const EAUTH = 80;
249/// Need authenticator
250
251// Intelligent device errors
252pub const ENEEDAUTH = 81;
253
254/// Device power is off
255pub const EPWROFF = 82;
256
257/// Device error, e.g. paper out
258pub const EDEVERR = 83;
259/// Value too large to be stored in data type
260
261// Program loading errors
262pub const EOVERFLOW = 84;
263
264/// Bad executable
265pub const EBADEXEC = 85;
266
267/// Bad CPU type in executable
268pub const EBADARCH = 86;
269
270/// Shared library version mismatch
271pub const ESHLIBVERS = 87;
272
273/// Malformed Macho file
274pub const EBADMACHO = 88;
275
276/// Operation canceled
277pub const ECANCELED = 89;
278
279/// Identifier removed
280pub const EIDRM = 90;
281
282/// No message of desired type
283pub const ENOMSG = 91;
284
285/// Illegal byte sequence
286pub const EILSEQ = 92;
287
288/// Attribute not found
289pub const ENOATTR = 93;
290
291/// Bad message
292pub const EBADMSG = 94;
293
294/// Reserved
295pub const EMULTIHOP = 95;
296
297/// No message available on STREAM
298pub const ENODATA = 96;
299
300/// Reserved
301pub const ENOLINK = 97;
302
303/// No STREAM resources
304pub const ENOSR = 98;
305
306/// Not a STREAM
307pub const ENOSTR = 99;
308
309/// Protocol error
310pub const EPROTO = 100;
311
312/// STREAM ioctl timeout
313pub const ETIME = 101;
314
315/// No such policy registered
316pub const ENOPOLICY = 103;
317
318/// State not recoverable
319pub const ENOTRECOVERABLE = 104;
320
321/// Previous owner died
322pub const EOWNERDEAD = 105;
323
324/// Interface output queue is full
325pub const EQFULL = 106;
326
327/// Must be equal largest errno
328pub const ELAST = 106;
std/os/file.zig+5-11
......@@ -15,7 +15,7 @@ pub const File = struct {
1515 /// The OS-specific file descriptor or file handle.
1616 handle: os.FileHandle,
1717
18 const OpenError = os.WindowsOpenError || os.PosixOpenError;
18 pub const OpenError = os.WindowsOpenError || os.PosixOpenError;
1919
2020 /// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
2121 /// Call close to clean up.
......@@ -239,7 +239,7 @@ pub const File = struct {
239239 },
240240 Os.windows => {
241241 var pos: windows.LARGE_INTEGER = undefined;
242 if (windows.SetFilePointerEx(self.handle, 0, *pos, windows.FILE_CURRENT) == 0) {
242 if (windows.SetFilePointerEx(self.handle, 0, &pos, windows.FILE_CURRENT) == 0) {
243243 const err = windows.GetLastError();
244244 return switch (err) {
245245 windows.ERROR.INVALID_PARAMETER => error.BadFd,
......@@ -248,13 +248,7 @@ pub const File = struct {
248248 }
249249
250250 assert(pos >= 0);
251 if (@sizeOf(@typeOf(pos)) > @sizeOf(usize)) {
252 if (pos > @maxValue(usize)) {
253 return error.FilePosLargerThanPointerRange;
254 }
255 }
256
257 return usize(pos);
251 return math.cast(usize, pos) catch error.FilePosLargerThanPointerRange;
258252 },
259253 else => @compileError("unsupported OS"),
260254 }
......@@ -286,7 +280,7 @@ pub const File = struct {
286280 Unexpected,
287281 };
288282
289 fn mode(self: *File) ModeError!os.FileMode {
283 pub fn mode(self: *File) ModeError!os.FileMode {
290284 if (is_posix) {
291285 var stat: posix.Stat = undefined;
292286 const err = posix.getErrno(posix.fstat(self.handle, &stat));
......@@ -361,7 +355,7 @@ pub const File = struct {
361355
362356 pub const WriteError = os.WindowsWriteError || os.PosixWriteError;
363357
364 fn write(self: *File, bytes: []const u8) WriteError!void {
358 pub fn write(self: *File, bytes: []const u8) WriteError!void {
365359 if (is_posix) {
366360 try os.posixWrite(self.handle, bytes);
367361 } else if (is_windows) {
std/os/index.zig+1-1
......@@ -11,7 +11,7 @@ const os = this;
1111test "std.os" {
1212 _ = @import("child_process.zig");
1313 _ = @import("darwin.zig");
14 _ = @import("darwin_errno.zig");
14 _ = @import("darwin/errno.zig");
1515 _ = @import("get_user_id.zig");
1616 _ = @import("linux/index.zig");
1717 _ = @import("path.zig");
std/special/test_runner.zig+17-3
......@@ -5,11 +5,25 @@ const test_fn_list = builtin.__zig_test_fn_slice;
55const warn = std.debug.warn;
66
77pub fn main() !void {
8 var ok_count: usize = 0;
9 var skip_count: usize = 0;
810 for (test_fn_list) |test_fn, i| {
911 warn("Test {}/{} {}...", i + 1, test_fn_list.len, test_fn.name);
1012
11 try test_fn.func();
12
13 warn("OK\n");
13 if (test_fn.func()) |_| {
14 ok_count += 1;
15 warn("OK\n");
16 } else |err| switch (err) {
17 error.SkipZigTest => {
18 skip_count += 1;
19 warn("SKIP\n");
20 },
21 else => return err,
22 }
23 }
24 if (ok_count == test_fn_list.len) {
25 warn("All tests passed.\n");
26 } else {
27 warn("{} passed; {} skipped.\n", ok_count, skip_count);
1428 }
1529}
test/cases/defer.zig+15
......@@ -61,3 +61,18 @@ test "defer and labeled break" {
6161
6262 assert(i == 1);
6363}
64
65test "errdefer does not apply to fn inside fn" {
66 if (testNestedFnErrDefer()) |_| @panic("expected error") else |e| assert(e == error.Bad);
67}
68
69fn testNestedFnErrDefer() error!void {
70 var a: i32 = 0;
71 errdefer a += 1;
72 const S = struct {
73 fn baz() error {
74 return error.Bad;
75 }
76 };
77 return S.baz();
78}