authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-03 18:47:30-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-03 18:47:30-04:00
log2680f9ab48e041108a29271f55a69d9f869461b9
treec5f7c0ae8f71c4423373b686fa354751104b6d58
parentc5f1925bc801c34133437d0d78de6b1d520f9f12
parent9bd8b01650f9cf21e601117951711b21aa5fd216

Merge remote-tracking branch 'origin/master' into async-fs


9 files changed, 137 insertions(+), 6 deletions(-)

.gitignore+11
......@@ -1,3 +1,14 @@
1# This file is for zig-specific build artifacts.
2# If you have OS-specific or editor-specific files to ignore,
3# such as *.swp or .DS_Store, put those in your global
4# ~/.gitignore and put this in your ~/.gitconfig:
5#
6# [core]
7# excludesfile = ~/.gitignore
8#
9# Cheers!
10# -andrewrk
11
112zig-cache/
213build/
314build-*/
src/ir.cpp+15-1
......@@ -3332,7 +3332,15 @@ static VariableTableEntry *create_local_var(CodeGen *codegen, AstNode *node, Sco
33323332static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *scope, Buf *name,
33333333 bool src_is_const, bool gen_is_const, bool is_shadowable, IrInstruction *is_comptime)
33343334{
3335 VariableTableEntry *var = create_local_var(irb->codegen, node, scope, name, src_is_const, gen_is_const, is_shadowable, is_comptime);
3335 bool is_underscored = name ? buf_eql_str(name, "_") : false;
3336 VariableTableEntry *var = create_local_var( irb->codegen
3337 , node
3338 , scope
3339 , (is_underscored ? nullptr : name)
3340 , src_is_const
3341 , gen_is_const
3342 , (is_underscored ? true : is_shadowable)
3343 , is_comptime );
33363344 if (is_comptime != nullptr || gen_is_const) {
33373345 var->mem_slot_index = exec_next_mem_slot(irb->exec);
33383346 var->owner_exec = irb->exec;
......@@ -5186,6 +5194,11 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
51865194
51875195 AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;
51885196
5197 if (buf_eql_str(variable_declaration->symbol, "_")) {
5198 add_node_error(irb->codegen, node, buf_sprintf("`_` is not a declarable symbol"));
5199 return irb->codegen->invalid_instruction;
5200 }
5201
51895202 IrInstruction *type_instruction;
51905203 if (variable_declaration->type != nullptr) {
51915204 type_instruction = ir_gen_node(irb, variable_declaration->type, scope);
......@@ -5198,6 +5211,7 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, Scope *scope, AstNode *nod
51985211 bool is_shadowable = false;
51995212 bool is_const = variable_declaration->is_const;
52005213 bool is_extern = variable_declaration->is_extern;
5214
52015215 IrInstruction *is_comptime = ir_build_const_bool(irb, scope, node,
52025216 ir_should_inline(irb->exec, scope) || variable_declaration->is_comptime);
52035217 VariableTableEntry *var = ir_create_var(irb, node, scope, variable_declaration->symbol,
std/os/linux/index.zig+1-1
......@@ -999,7 +999,7 @@ pub fn setgroups(size: usize, list: *const u32) usize {
999999}
10001000
10011001pub fn getpid() i32 {
1002 return @bitCast(i32, u32(syscall0(SYS_getpid)));
1002 return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid)));
10031003}
10041004
10051005pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
std/os/linux/test.zig+4
......@@ -3,6 +3,10 @@ const builtin = @import("builtin");
33const linux = std.os.linux;
44const assert = std.debug.assert;
55
6test "getpid" {
7 assert(linux.getpid() != 0);
8}
9
610test "timer" {
711 const epoll_fd = linux.epoll_create();
812 var err = linux.getErrno(epoll_fd);
std/rand/index.zig+5-4
......@@ -30,7 +30,7 @@ pub const DefaultCsprng = Isaac64;
3030pub const Random = struct {
3131 fillFn: fn (r: *Random, buf: []u8) void,
3232
33 /// Read random bytes into the specified buffer until fill.
33 /// Read random bytes into the specified buffer until full.
3434 pub fn bytes(r: *Random, buf: []u8) void {
3535 r.fillFn(r, buf);
3636 }
......@@ -48,10 +48,10 @@ pub const Random = struct {
4848 }
4949 }
5050
51 /// Get a random unsigned integer with even distribution between `start`
52 /// inclusive and `end` exclusive.
51 /// Return a random integer with even distribution between `start`
52 /// inclusive and `end` exclusive. `start` must be less than `end`.
5353 pub fn range(r: *Random, comptime T: type, start: T, end: T) T {
54 assert(start <= end);
54 assert(start < end);
5555 if (T.is_signed) {
5656 const uint = @IntType(false, T.bit_count);
5757 if (start >= 0 and end >= 0) {
......@@ -664,6 +664,7 @@ test "Random range" {
664664 testRange(&prng.random, -4, 3);
665665 testRange(&prng.random, -4, -1);
666666 testRange(&prng.random, 10, 14);
667 // TODO: test that prng.random.range(1, 1) causes an assertion error
667668}
668669
669670fn testRange(r: *Random, start: i32, end: i32) void {
test/behavior.zig+1
......@@ -60,6 +60,7 @@ comptime {
6060 _ = @import("cases/try.zig");
6161 _ = @import("cases/type_info.zig");
6262 _ = @import("cases/undefined.zig");
63 _ = @import("cases/underscore.zig");
6364 _ = @import("cases/union.zig");
6465 _ = @import("cases/var_args.zig");
6566 _ = @import("cases/void.zig");
test/cases/underscore.zig created+28
......@@ -0,0 +1,28 @@
1const std = @import("std");
2const assert = std.debug.assert;
3
4test "ignore lval with underscore" {
5 _ = false;
6}
7
8test "ignore lval with underscore (for loop)" {
9 for ([]void{}) |_, i| {
10 for ([]void{}) |_, j| {
11 break;
12 }
13 break;
14 }
15}
16
17test "ignore lval with underscore (while loop)" {
18 while (optionalReturnError()) |_| {
19 while (optionalReturnError()) |_| {
20 break;
21 } else |_| { }
22 break;
23 } else |_| { }
24}
25
26fn optionalReturnError() !?u32 {
27 return error.optionalReturnError;
28}
test/cases/union.zig+14
......@@ -297,3 +297,17 @@ test "access a member of tagged union with conflicting enum tag name" {
297297
298298 comptime assert(Bar.A == u8);
299299}
300
301test "tagged union initialization with runtime void" {
302 assert(testTaggedUnionInit({}));
303}
304
305const TaggedUnionWithAVoid = union(enum) {
306 A,
307 B: i32,
308};
309
310fn testTaggedUnionInit(x: var) bool {
311 const y = TaggedUnionWithAVoid{ .A = x };
312 return @TagType(TaggedUnionWithAVoid)(y) == TaggedUnionWithAVoid.A;
313}
test/compile_errors.zig+58
......@@ -22,6 +22,64 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
2222 ".tmp_source.zig:3:28: error: @handle() in non-async function",
2323 );
2424
25 cases.add(
26 "`_` is not a declarable symbol",
27 \\export fn f1() usize {
28 \\ var _: usize = 2;
29 \\ return _;
30 \\}
31 ,
32 ".tmp_source.zig:2:5: error: `_` is not a declarable symbol",
33 ".tmp_source.zig:3:12: error: use of undeclared identifier '_'",
34 );
35
36 cases.add(
37 "`_` should not be usable inside for",
38 \\export fn returns() void {
39 \\ for ([]void{}) |_, i| {
40 \\ for ([]void{}) |_, j| {
41 \\ return _;
42 \\ }
43 \\ }
44 \\}
45 ,
46 ".tmp_source.zig:4:20: error: use of undeclared identifier '_'",
47 );
48
49 cases.add(
50 "`_` should not be usable inside while",
51 \\export fn returns() void {
52 \\ while (optionalReturn()) |_| {
53 \\ while (optionalReturn()) |_| {
54 \\ return _;
55 \\ }
56 \\ }
57 \\}
58 \\fn optionalReturn() ?u32 {
59 \\ return 1;
60 \\}
61 ,
62 ".tmp_source.zig:4:20: error: use of undeclared identifier '_'",
63 );
64
65 cases.add(
66 "`_` should not be usable inside while else",
67 \\export fn returns() void {
68 \\ while (optionalReturnError()) |_| {
69 \\ while (optionalReturnError()) |_| {
70 \\ return;
71 \\ } else |_| {
72 \\ if (_ == error.optionalReturnError) return;
73 \\ }
74 \\ }
75 \\}
76 \\fn optionalReturnError() !?u32 {
77 \\ return error.optionalReturnError;
78 \\}
79 ,
80 ".tmp_source.zig:6:17: error: use of undeclared identifier '_'",
81 );
82
2583 cases.add(
2684 "while loop body expression ignored",
2785 \\fn returns() usize {