authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-08-16 22:42:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-08-16 22:42:50-07:00
log37d167f6e0e26e8dc57950cb0fa1bfa630036521
tree8b947d065ef12b189cb7a4c880c0009e7a6cd2b8
parent0ae9023832584e256aa3b6df0f0829026141d21a

std: conform to style guidelines


26 files changed, 707 insertions(+), 686 deletions(-)

doc/style.md+59-3
......@@ -5,12 +5,68 @@ this documentation along with the compiler in order to provide a point of
55reference, should anyone wish to point to an authority on agreed upon Zig
66coding style.
77
8## Whitespace
9
810 * 4 space indentation
9 * `camelCaseFunctionName`
10 * `TitleCaseTypeName`
11 * `snake_case_variable_name`
1211 * Open braces on same line, unless you need to wrap.
1312 * If a list of things is longer than 2, put each item on its own line and
1413 exercise the abilty to put an extra comma at the end.
14 * Line length: aim for 100; use common sense.
15
16## Names
17
18Roughly speaking: `camelCaseFunctionName`, `TitleCaseTypeName`,
19`snake_case_variable_name`. More precisely:
20
21 * If `x` is a `struct` (or an alias of a `struct`), then `x` should be `TitleCase`.
22 * If `x` otherwise identifies a type, `x` should have `snake_case`.
23 * If `x` is callable, and `x`'s return type is `type`, then `x` should be `TitleCase`.
24 * If `x` is otherwise callable, then `x` should be `camelCase`.
25 * Otherwise, `x` should be `snake_case`.
26
27Acronyms, initialisms, proper nouns, or any other word that has capitalization
28rules in written English are subject to naming conventions just like any other
29word. Even acronyms that are only 2 letters long are subject to these
30conventions.
31
32Examples:
33
34```zig
35const namespace_name = @import("dir_name/file_name.zig");
36var global_var: i32;
37const const_name = 42;
38const primitive_type_alias = f32;
39const string_alias = []u8;
40
41struct StructName {}
42const StructAlias = StructName;
43
44fn functionName(param_name: TypeName) {
45 var functionPointer = functionName;
46 functionPointer();
47 functionPointer = otherFunction;
48 functionPointer();
49}
50const functionAlias = functionName;
51
52fn ListTemplateFunction(ChildType: type, inline fixed_size: usize) -> type {
53 struct ShortList(T: type, n: usize) {
54 field_name: [n]T,
55 fn methodName() {}
56 }
57 return List(ChildType, fixed_size);
58}
59
60// The word XML loses its casing when used in Zig identifiers.
61const xml_document =
62 \\<?xml version="1.0" encoding="UTF-8"?>
63 \\<document>
64 \\</document>
65 ;
66struct XmlParser {}
67
68// The initials BE (Big Endian) are just another word in Zig identifier names.
69fn readU32Be() -> u32 {}
70```
1571
1672See Zig standard library for examples.
example/cat/main.zig+3-3
......@@ -16,7 +16,7 @@ pub fn main(args: [][]u8) -> %void {
1616 } else {
1717 var is = io.InStream.open(arg) %% |err| {
1818 %%io.stderr.printf("Unable to open file: ");
19 %%io.stderr.printf(@err_name(err));
19 %%io.stderr.printf(@errName(err));
2020 %%io.stderr.printf("\n");
2121 return err;
2222 };
......@@ -45,7 +45,7 @@ fn cat_stream(is: io.InStream) -> %void {
4545 while (true) {
4646 const bytes_read = is.read(buf) %% |err| {
4747 %%io.stderr.printf("Unable to read from stream: ");
48 %%io.stderr.printf(@err_name(err));
48 %%io.stderr.printf(@errName(err));
4949 %%io.stderr.printf("\n");
5050 return err;
5151 };
......@@ -56,7 +56,7 @@ fn cat_stream(is: io.InStream) -> %void {
5656
5757 io.stdout.write(buf[0...bytes_read]) %% |err| {
5858 %%io.stderr.printf("Unable to write to stdout: ");
59 %%io.stderr.printf(@err_name(err));
59 %%io.stderr.printf(@errName(err));
6060 %%io.stderr.printf("\n");
6161 return err;
6262 };
example/guess_number/main.zig+5-4
......@@ -6,11 +6,12 @@ const os = std.os;
66pub fn main(args: [][]u8) -> %void {
77 %%io.stdout.printf("Welcome to the Guess Number Game in Zig.\n");
88
9 var seed: [@sizeof(usize)]u8 = undefined;
9 var seed: [@sizeOf(usize)]u8 = undefined;
1010 %%os.get_random_bytes(seed);
11 var rand = Rand.init(([]usize)(seed)[0]);
11 var rand: Rand = undefined;
12 rand.init(([]usize)(seed)[0]);
1213
13 const answer = rand.range_unsigned(u8, 0, 100) + 1;
14 const answer = rand.rangeUnsigned(u8, 0, 100) + 1;
1415
1516 while (true) {
1617 %%io.stdout.printf("\nGuess a number between 1 and 100: ");
......@@ -21,7 +22,7 @@ pub fn main(args: [][]u8) -> %void {
2122 return err;
2223 };
2324
24 const guess = io.parse_unsigned(u8, line_buf[0...line_len - 1], 10) %% {
25 const guess = io.parseUnsigned(u8, line_buf[0...line_len - 1], 10) %% {
2526 %%io.stdout.printf("Invalid number.\n");
2627 continue;
2728 };
src/analyze.cpp+1-1
......@@ -5206,7 +5206,7 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
52065206 case TypeTableEntryIdNamespace:
52075207 case TypeTableEntryIdGenericFn:
52085208 add_node_error(g, expr_node,
5209 buf_sprintf("type '%s' not eligible for @typeof", buf_ptr(&type_entry->name)));
5209 buf_sprintf("type '%s' not eligible for @typeOf", buf_ptr(&type_entry->name)));
52105210 return g->builtin_types.entry_invalid;
52115211 case TypeTableEntryIdMetaType:
52125212 case TypeTableEntryIdVoid:
src/codegen.cpp+23-23
......@@ -4642,7 +4642,7 @@ static void define_builtin_fns(CodeGen *g) {
46424642 }
46434643 {
46444644 BuiltinFnEntry *builtin_fn = create_builtin_fn_with_arg_count(g, BuiltinFnIdReturnAddress,
4645 "return_address", 0);
4645 "returnAddress", 0);
46464646 builtin_fn->return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
46474647
46484648 LLVMTypeRef fn_type = LLVMFunctionType(builtin_fn->return_type->type_ref,
......@@ -4652,7 +4652,7 @@ static void define_builtin_fns(CodeGen *g) {
46524652 }
46534653 {
46544654 BuiltinFnEntry *builtin_fn = create_builtin_fn_with_arg_count(g, BuiltinFnIdFrameAddress,
4655 "frame_address", 0);
4655 "frameAddress", 0);
46564656 builtin_fn->return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
46574657
46584658 LLVMTypeRef fn_type = LLVMFunctionType(builtin_fn->return_type->type_ref,
......@@ -4708,33 +4708,33 @@ static void define_builtin_fns(CodeGen *g) {
47084708
47094709 g->memset_fn_val = builtin_fn->fn_val;
47104710 }
4711 create_builtin_fn_with_arg_count(g, BuiltinFnIdSizeof, "sizeof", 1);
4712 create_builtin_fn_with_arg_count(g, BuiltinFnIdAlignof, "alignof", 1);
4713 create_builtin_fn_with_arg_count(g, BuiltinFnIdMaxValue, "max_value", 1);
4714 create_builtin_fn_with_arg_count(g, BuiltinFnIdMinValue, "min_value", 1);
4715 create_builtin_fn_with_arg_count(g, BuiltinFnIdMemberCount, "member_count", 1);
4716 create_builtin_fn_with_arg_count(g, BuiltinFnIdTypeof, "typeof", 1);
4717 create_builtin_fn_with_arg_count(g, BuiltinFnIdAddWithOverflow, "add_with_overflow", 4);
4718 create_builtin_fn_with_arg_count(g, BuiltinFnIdSubWithOverflow, "sub_with_overflow", 4);
4719 create_builtin_fn_with_arg_count(g, BuiltinFnIdMulWithOverflow, "mul_with_overflow", 4);
4720 create_builtin_fn_with_arg_count(g, BuiltinFnIdShlWithOverflow, "shl_with_overflow", 4);
4721 create_builtin_fn_with_arg_count(g, BuiltinFnIdCInclude, "c_include", 1);
4722 create_builtin_fn_with_arg_count(g, BuiltinFnIdCDefine, "c_define", 2);
4723 create_builtin_fn_with_arg_count(g, BuiltinFnIdCUndef, "c_undef", 1);
4724 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileVar, "compile_var", 1);
4725 create_builtin_fn_with_arg_count(g, BuiltinFnIdConstEval, "const_eval", 1);
4711 create_builtin_fn_with_arg_count(g, BuiltinFnIdSizeof, "sizeOf", 1);
4712 create_builtin_fn_with_arg_count(g, BuiltinFnIdAlignof, "alignOf", 1);
4713 create_builtin_fn_with_arg_count(g, BuiltinFnIdMaxValue, "maxValue", 1);
4714 create_builtin_fn_with_arg_count(g, BuiltinFnIdMinValue, "minValue", 1);
4715 create_builtin_fn_with_arg_count(g, BuiltinFnIdMemberCount, "memberCount", 1);
4716 create_builtin_fn_with_arg_count(g, BuiltinFnIdTypeof, "typeOf", 1);
4717 create_builtin_fn_with_arg_count(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4);
4718 create_builtin_fn_with_arg_count(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4);
4719 create_builtin_fn_with_arg_count(g, BuiltinFnIdMulWithOverflow, "mulWithOverflow", 4);
4720 create_builtin_fn_with_arg_count(g, BuiltinFnIdShlWithOverflow, "shlWithOverflow", 4);
4721 create_builtin_fn_with_arg_count(g, BuiltinFnIdCInclude, "cInclude", 1);
4722 create_builtin_fn_with_arg_count(g, BuiltinFnIdCDefine, "cDefine", 2);
4723 create_builtin_fn_with_arg_count(g, BuiltinFnIdCUndef, "cUndef", 1);
4724 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileVar, "compileVar", 1);
4725 create_builtin_fn_with_arg_count(g, BuiltinFnIdConstEval, "constEval", 1);
47264726 create_builtin_fn_with_arg_count(g, BuiltinFnIdCtz, "ctz", 2);
47274727 create_builtin_fn_with_arg_count(g, BuiltinFnIdClz, "clz", 2);
47284728 create_builtin_fn_with_arg_count(g, BuiltinFnIdImport, "import", 1);
4729 create_builtin_fn_with_arg_count(g, BuiltinFnIdCImport, "c_import", 1);
4730 create_builtin_fn_with_arg_count(g, BuiltinFnIdErrName, "err_name", 1);
4731 create_builtin_fn_with_arg_count(g, BuiltinFnIdEmbedFile, "embed_file", 1);
4729 create_builtin_fn_with_arg_count(g, BuiltinFnIdCImport, "cImport", 1);
4730 create_builtin_fn_with_arg_count(g, BuiltinFnIdErrName, "errName", 1);
4731 create_builtin_fn_with_arg_count(g, BuiltinFnIdEmbedFile, "embedFile", 1);
47324732 create_builtin_fn_with_arg_count(g, BuiltinFnIdCmpExchange, "cmpxchg", 5);
47334733 create_builtin_fn_with_arg_count(g, BuiltinFnIdFence, "fence", 1);
4734 create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "div_exact", 2);
4734 create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "divExact", 2);
47354735 create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2);
4736 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compile_err", 1);
4737 create_builtin_fn_with_arg_count(g, BuiltinFnIdIntType, "int_type", 2);
4736 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compileErr", 1);
4737 create_builtin_fn_with_arg_count(g, BuiltinFnIdIntType, "intType", 2);
47384738}
47394739
47404740static void init(CodeGen *g, Buf *source_path) {
std/bootstrap.zig+8-8
......@@ -4,7 +4,7 @@ const root = @import("@root");
44const linux = @import("linux.zig");
55const cstr = @import("cstr.zig");
66
7const want_start_symbol = switch(@compile_var("os")) {
7const want_start_symbol = switch(@compileVar("os")) {
88 linux => true,
99 else => false,
1010};
......@@ -16,7 +16,7 @@ var argv: &&u8 = undefined;
1616#attribute("naked")
1717#condition(want_start_symbol)
1818export fn _start() -> unreachable {
19 switch (@compile_var("arch")) {
19 switch (@compileVar("arch")) {
2020 x86_64 => {
2121 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
2222 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
......@@ -25,12 +25,12 @@ export fn _start() -> unreachable {
2525 argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));
2626 argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
2727 },
28 else => @compile_err("unsupported arch"),
28 else => @compileErr("unsupported arch"),
2929 }
30 call_main_and_exit()
30 callMainAndExit()
3131}
3232
33fn call_main() -> %void {
33fn callMain() -> %void {
3434 var args: [argc][]u8 = undefined;
3535 for (args) |arg, i| {
3636 const ptr = argv[i];
......@@ -39,8 +39,8 @@ fn call_main() -> %void {
3939 return root.main(args);
4040}
4141
42fn call_main_and_exit() -> unreachable {
43 call_main() %% linux.exit(1);
42fn callMainAndExit() -> unreachable {
43 callMain() %% linux.exit(1);
4444 linux.exit(0);
4545}
4646
......@@ -48,6 +48,6 @@ fn call_main_and_exit() -> unreachable {
4848export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
4949 argc = usize(c_argc);
5050 argv = c_argv;
51 call_main() %% return 1;
51 callMain() %% return 1;
5252 return 0;
5353}
std/compiler_rt.zig+5-5
......@@ -5,7 +5,7 @@ const si_int = c_int;
55const su_int = c_uint;
66
77const udwords = [2]su_int;
8const low = if (@compile_var("is_big_endian")) 1 else 0;
8const low = if (@compileVar("is_big_endian")) 1 else 0;
99const high = 1 - low;
1010
1111#debug_safety(false)
......@@ -20,8 +20,8 @@ fn du_int_to_udwords(x: du_int) -> udwords {
2020
2121#debug_safety(false)
2222export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
23 const n_uword_bits = @sizeof(su_int) * CHAR_BIT;
24 const n_udword_bits = @sizeof(du_int) * CHAR_BIT;
23 const n_uword_bits = @sizeOf(su_int) * CHAR_BIT;
24 const n_udword_bits = @sizeOf(du_int) * CHAR_BIT;
2525 var n = du_int_to_udwords(a);
2626 var d = du_int_to_udwords(b);
2727 var q: udwords = undefined;
......@@ -79,7 +79,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
7979 r[high] = n[high] & (d[high] - 1);
8080 *rem = *(&du_int)(&r[0]);
8181 }
82 return n[high] >> @ctz(@typeof(d[high]), d[high]);
82 return n[high] >> @ctz(@typeOf(d[high]), d[high]);
8383 }
8484 // K K
8585 // ---
......@@ -114,7 +114,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
114114 if (d[low] == 1) {
115115 return *(&du_int)(&n[0]);
116116 }
117 sr = @ctz(@typeof(d[low]), d[low]);
117 sr = @ctz(@typeOf(d[low]), d[low]);
118118 q[high] = n[high] >> sr;
119119 q[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr);
120120 return *(&du_int)(&q[0]);
std/cstr.zig+34-34
......@@ -24,11 +24,11 @@ pub fn cmp(a: &const u8, b: &const u8) -> i32 {
2424 return a[index] - b[index];
2525}
2626
27pub fn to_slice_const(str: &const u8) -> []const u8 {
27pub fn toSliceConst(str: &const u8) -> []const u8 {
2828 return str[0...strlen(str)];
2929}
3030
31pub fn to_slice(str: &u8) -> []u8 {
31pub fn toSlice(str: &u8) -> []u8 {
3232 return str[0...strlen(str)];
3333}
3434
......@@ -46,25 +46,25 @@ pub struct CBuf {
4646 }
4747
4848 /// Must deinitialize with deinit.
49 pub fn init_from_mem(self: &CBuf, allocator: &Allocator, m: []const u8) -> %void {
49 pub fn initFromMem(self: &CBuf, allocator: &Allocator, m: []const u8) -> %void {
5050 self.init(allocator);
5151 %return self.resize(m.len);
5252 mem.copy(u8, self.list.items, m);
5353 }
5454
5555 /// Must deinitialize with deinit.
56 pub fn init_from_cstr(self: &CBuf, allocator: &Allocator, s: &const u8) -> %void {
57 self.init_from_mem(allocator, s[0...strlen(s)])
56 pub fn initFromCStr(self: &CBuf, allocator: &Allocator, s: &const u8) -> %void {
57 self.initFromMem(allocator, s[0...strlen(s)])
5858 }
5959
6060 /// Must deinitialize with deinit.
61 pub fn init_from_cbuf(self: &CBuf, cbuf: &const CBuf) -> %void {
62 self.init_from_mem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()])
61 pub fn initFromCBuf(self: &CBuf, cbuf: &const CBuf) -> %void {
62 self.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()])
6363 }
6464
6565 /// Must deinitialize with deinit.
66 pub fn init_from_slice(self: &CBuf, other: &const CBuf, start: usize, end: usize) -> %void {
67 self.init_from_mem(other.list.allocator, other.list.items[start...end])
66 pub fn initFromSlice(self: &CBuf, other: &const CBuf, start: usize, end: usize) -> %void {
67 self.initFromMem(other.list.allocator, other.list.items[start...end])
6868 }
6969
7070 pub fn deinit(self: &CBuf) {
......@@ -80,66 +80,66 @@ pub struct CBuf {
8080 return self.list.len - 1;
8181 }
8282
83 pub fn append_mem(self: &CBuf, m: []const u8) -> %void {
83 pub fn appendMem(self: &CBuf, m: []const u8) -> %void {
8484 const old_len = self.len();
8585 %return self.resize(old_len + m.len);
8686 mem.copy(u8, self.list.items[old_len...], m);
8787 }
8888
89 pub fn append_cstr(self: &CBuf, s: &const u8) -> %void {
90 self.append_mem(s[0...strlen(s)])
89 pub fn appendCStr(self: &CBuf, s: &const u8) -> %void {
90 self.appendMem(s[0...strlen(s)])
9191 }
9292
93 pub fn append_char(self: &CBuf, c: u8) -> %void {
93 pub fn appendChar(self: &CBuf, c: u8) -> %void {
9494 %return self.resize(self.len() + 1);
9595 self.list.items[self.len() - 1] = c;
9696 }
9797
98 pub fn eql_mem(self: &const CBuf, m: []const u8) -> bool {
98 pub fn eqlMem(self: &const CBuf, m: []const u8) -> bool {
9999 if (self.len() != m.len) return false;
100100 return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
101101 }
102102
103 pub fn eql_cstr(self: &const CBuf, s: &const u8) -> bool {
104 self.eql_mem(s[0...strlen(s)])
103 pub fn eqlCStr(self: &const CBuf, s: &const u8) -> bool {
104 self.eqlMem(s[0...strlen(s)])
105105 }
106106
107 pub fn eql_cbuf(self: &const CBuf, other: &const CBuf) -> bool {
108 self.eql_mem(other.list.items[0...other.len()])
107 pub fn eqlCBuf(self: &const CBuf, other: &const CBuf) -> bool {
108 self.eqlMem(other.list.items[0...other.len()])
109109 }
110110
111 pub fn starts_with_mem(self: &const CBuf, m: []const u8) -> bool {
111 pub fn startsWithMem(self: &const CBuf, m: []const u8) -> bool {
112112 if (self.len() < m.len) return false;
113113 return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal;
114114 }
115115
116 pub fn starts_with_cbuf(self: &const CBuf, other: &const CBuf) -> bool {
117 self.starts_with_mem(other.list.items[0...other.len()])
116 pub fn startsWithCBuf(self: &const CBuf, other: &const CBuf) -> bool {
117 self.startsWithMem(other.list.items[0...other.len()])
118118 }
119119
120 pub fn starts_with_cstr(self: &const CBuf, s: &const u8) -> bool {
121 self.starts_with_mem(s[0...strlen(s)])
120 pub fn startsWithCStr(self: &const CBuf, s: &const u8) -> bool {
121 self.startsWithMem(s[0...strlen(s)])
122122 }
123123}
124124
125125#attribute("test")
126fn test_simple_cbuf() {
126fn testSimpleCBuf() {
127127 var buf: CBuf = undefined;
128128 buf.init(&debug.global_allocator);
129129 assert(buf.len() == 0);
130 %%buf.append_cstr(c"hello");
131 %%buf.append_char(' ');
132 %%buf.append_mem("world");
133 assert(buf.eql_cstr(c"hello world"));
134 assert(buf.eql_mem("hello world"));
130 %%buf.appendCStr(c"hello");
131 %%buf.appendChar(' ');
132 %%buf.appendMem("world");
133 assert(buf.eqlCStr(c"hello world"));
134 assert(buf.eqlMem("hello world"));
135135
136136 var buf2: CBuf = undefined;
137 %%buf2.init_from_cbuf(&buf);
138 assert(buf.eql_cbuf(&buf2));
137 %%buf2.initFromCBuf(&buf);
138 assert(buf.eqlCBuf(&buf2));
139139
140 assert(buf.starts_with_mem("hell"));
141 assert(buf.starts_with_cstr(c"hell"));
140 assert(buf.startsWithMem("hell"));
141 assert(buf.startsWithCStr(c"hell"));
142142
143143 %%buf2.resize(4);
144 assert(buf.starts_with_cbuf(&buf2));
144 assert(buf.startsWithCBuf(&buf2));
145145}
std/debug.zig+5-5
......@@ -6,10 +6,10 @@ pub fn assert(b: bool) {
66}
77
88pub fn printStackTrace() {
9 var maybe_fp: ?&const u8 = @frame_address();
9 var maybe_fp: ?&const u8 = @frameAddress();
1010 while (true) {
1111 const fp = maybe_fp ?? break;
12 const return_address = *(&const usize)(usize(fp) + @sizeof(usize));
12 const return_address = *(&const usize)(usize(fp) + @sizeOf(usize));
1313 %%io.stderr.print_u64(return_address);
1414 %%io.stderr.printf("\n");
1515 maybe_fp = *(&const ?&const u8)(fp);
......@@ -17,9 +17,9 @@ pub fn printStackTrace() {
1717}
1818
1919pub var global_allocator = Allocator {
20 .alloc_fn = globalAlloc,
21 .realloc_fn = globalRealloc,
22 .free_fn = globalFree,
20 .allocFn = globalAlloc,
21 .reallocFn = globalRealloc,
22 .freeFn = globalFree,
2323 .context = null,
2424};
2525
std/hash_map.zig+22-22
......@@ -4,27 +4,27 @@ const math = @import("math.zig");
44const mem = @import("mem.zig");
55const Allocator = mem.Allocator;
66
7const want_modification_safety = !@compile_var("is_release");
7const want_modification_safety = !@compileVar("is_release");
88const debug_u32 = if (want_modification_safety) u32 else void;
99
1010pub fn HashMap(inline K: type, inline V: type, inline hash: fn(key: K)->u32,
1111 inline eql: fn(a: K, b: K)->bool) -> type
1212{
13 SmallHashMap(K, V, hash, eql, @sizeof(usize))
13 SmallHashMap(K, V, hash, eql, @sizeOf(usize))
1414}
1515
16pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b: K)->bool, STATIC_SIZE: usize) {
16pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b: K)->bool, static_size: usize) {
1717 entries: []Entry,
1818 size: usize,
1919 max_distance_from_start_index: usize,
2020 allocator: &Allocator,
2121 // if the hash map is small enough, we use linear search through these
2222 // entries instead of allocating memory
23 prealloc_entries: [STATIC_SIZE]Entry,
23 prealloc_entries: [static_size]Entry,
2424 // this is used to detect bugs where a hashtable is edited while an iterator is running.
2525 modification_count: debug_u32,
2626
27 const Self = SmallHashMap(K, V, hash, eql, STATIC_SIZE);
27 const Self = SmallHashMap(K, V, hash, eql, static_size);
2828
2929 pub struct Entry {
3030 used: bool,
......@@ -80,11 +80,11 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
8080 }
8181 hm.size = 0;
8282 hm.max_distance_from_start_index = 0;
83 hm.increment_modification_count();
83 hm.incrementModificationCount();
8484 }
8585
8686 pub fn put(hm: &Self, key: K, value: V) -> %void {
87 hm.increment_modification_count();
87 hm.incrementModificationCount();
8888
8989 const resize = if (hm.entries.ptr == &hm.prealloc_entries[0]) {
9090 // preallocated entries table is full
......@@ -95,11 +95,11 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
9595 };
9696 if (resize) {
9797 const old_entries = hm.entries;
98 %return hm.init_capacity(hm.entries.len * 2);
98 %return hm.initCapacity(hm.entries.len * 2);
9999 // dump all of the old elements into the new table
100100 for (old_entries) |*old_entry| {
101101 if (old_entry.used) {
102 hm.internal_put(old_entry.key, old_entry.value);
102 hm.internalPut(old_entry.key, old_entry.value);
103103 }
104104 }
105105 if (old_entries.ptr != &hm.prealloc_entries[0]) {
......@@ -107,16 +107,16 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
107107 }
108108 }
109109
110 hm.internal_put(key, value);
110 hm.internalPut(key, value);
111111 }
112112
113113 pub fn get(hm: &Self, key: K) -> ?&Entry {
114 return hm.internal_get(key);
114 return hm.internalGet(key);
115115 }
116116
117117 pub fn remove(hm: &Self, key: K) {
118 hm.increment_modification_count();
119 const start_index = hm.key_to_index(key);
118 hm.incrementModificationCount();
119 const start_index = hm.keyToIndex(key);
120120 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {
121121 const index = (start_index + roll_over) % hm.entries.len;
122122 var entry = &hm.entries[index];
......@@ -142,7 +142,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
142142 unreachable{} // key not found
143143 }
144144
145 pub fn entry_iterator(hm: &Self) -> Iterator {
145 pub fn entryIterator(hm: &Self) -> Iterator {
146146 return Iterator {
147147 .hm = hm,
148148 .count = 0,
......@@ -151,7 +151,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
151151 };
152152 }
153153
154 fn init_capacity(hm: &Self, capacity: usize) -> %void {
154 fn initCapacity(hm: &Self, capacity: usize) -> %void {
155155 hm.entries = %return hm.allocator.alloc(Entry, capacity);
156156 hm.size = 0;
157157 hm.max_distance_from_start_index = 0;
......@@ -160,16 +160,16 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
160160 }
161161 }
162162
163 fn increment_modification_count(hm: &Self) {
163 fn incrementModificationCount(hm: &Self) {
164164 if (want_modification_safety) {
165165 hm.modification_count +%= 1;
166166 }
167167 }
168168
169 fn internal_put(hm: &Self, orig_key: K, orig_value: V) {
169 fn internalPut(hm: &Self, orig_key: K, orig_value: V) {
170170 var key = orig_key;
171171 var value = orig_value;
172 const start_index = hm.key_to_index(key);
172 const start_index = hm.keyToIndex(key);
173173 var roll_over: usize = 0;
174174 var distance_from_start_index: usize = 0;
175175 while (roll_over < hm.entries.len; {roll_over += 1; distance_from_start_index += 1}) {
......@@ -214,8 +214,8 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
214214 unreachable{} // put into a full map
215215 }
216216
217 fn internal_get(hm: &Self, key: K) -> ?&Entry {
218 const start_index = hm.key_to_index(key);
217 fn internalGet(hm: &Self, key: K) -> ?&Entry {
218 const start_index = hm.keyToIndex(key);
219219 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {
220220 const index = (start_index + roll_over) % hm.entries.len;
221221 const entry = &hm.entries[index];
......@@ -226,13 +226,13 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
226226 return null;
227227 }
228228
229 fn key_to_index(hm: &Self, key: K) -> usize {
229 fn keyToIndex(hm: &Self, key: K) -> usize {
230230 return usize(hash(key)) % hm.entries.len;
231231 }
232232}
233233
234234#attribute("test")
235fn basic_hash_map_test() {
235fn basicHashMapTest() {
236236 var map: HashMap(i32, i32, hash_i32, eql_i32) = undefined;
237237 map.init(&debug.global_allocator);
238238 defer map.deinit();
std/index.zig+1-1
......@@ -9,7 +9,7 @@ pub const list = @import("list.zig");
99pub const hash_map = @import("hash_map.zig");
1010pub const mem = @import("mem.zig");
1111pub const debug = @import("debug.zig");
12pub const linux = switch(@compile_var("os")) {
12pub const linux = switch(@compileVar("os")) {
1313 linux => @import("linux.zig"),
1414 else => null_import,
1515};
std/io.zig+21-29
......@@ -63,7 +63,7 @@ pub struct OutStream {
6363 buffer: [buffer_size]u8,
6464 index: usize,
6565
66 pub fn write_byte(os: &OutStream, b: u8) -> %void {
66 pub fn writeByte(os: &OutStream, b: u8) -> %void {
6767 if (os.buffer.len == os.index) %return os.flush();
6868 os.buffer[os.index] = b;
6969 os.index += 1;
......@@ -71,7 +71,7 @@ pub struct OutStream {
7171
7272 pub fn write(os: &OutStream, bytes: []const u8) -> %usize {
7373 var src_bytes_left = bytes.len;
74 var src_index: @typeof(bytes.len) = 0;
74 var src_index: @typeOf(bytes.len) = 0;
7575 const dest_space_left = os.buffer.len - os.index;
7676
7777 while (src_bytes_left > 0) {
......@@ -98,7 +98,7 @@ pub struct OutStream {
9898 if (os.index + max_u64_base10_digits >= os.buffer.len) {
9999 %return os.flush();
100100 }
101 const amt_printed = buf_print_u64(os.buffer[os.index...], x);
101 const amt_printed = bufPrintUnsigned(u64, os.buffer[os.index...], x);
102102 os.index += amt_printed;
103103
104104 return amt_printed;
......@@ -108,7 +108,7 @@ pub struct OutStream {
108108 if (os.index + max_u64_base10_digits >= os.buffer.len) {
109109 %return os.flush();
110110 }
111 const amt_printed = buf_print_i64(os.buffer[os.index...], x);
111 const amt_printed = bufPrintSigned(i64, os.buffer[os.index...], x);
112112 os.index += amt_printed;
113113
114114 return amt_printed;
......@@ -116,7 +116,7 @@ pub struct OutStream {
116116
117117 pub fn flush(os: &OutStream) -> %void {
118118 const write_ret = linux.write(os.fd, &os.buffer[0], os.index);
119 const write_err = linux.get_errno(write_ret);
119 const write_err = linux.getErrno(write_ret);
120120 if (write_err > 0) {
121121 return switch (write_err) {
122122 errno.EINVAL => unreachable{},
......@@ -135,7 +135,7 @@ pub struct OutStream {
135135
136136 pub fn close(os: &OutStream) -> %void {
137137 const close_ret = linux.close(os.fd);
138 const close_err = linux.get_errno(close_ret);
138 const close_err = linux.getErrno(close_ret);
139139 if (close_err > 0) {
140140 return switch (close_err) {
141141 errno.EIO => error.Io,
......@@ -152,7 +152,7 @@ pub struct InStream {
152152
153153 pub fn open(path: []u8) -> %InStream {
154154 const fd = linux.open(path, linux.O_LARGEFILE|linux.O_RDONLY, 0);
155 const fd_err = linux.get_errno(fd);
155 const fd_err = linux.getErrno(fd);
156156 if (fd_err > 0) {
157157 return switch (fd_err) {
158158 errno.EFAULT => unreachable{},
......@@ -180,7 +180,7 @@ pub struct InStream {
180180
181181 pub fn read(is: &InStream, buf: []u8) -> %usize {
182182 const amt_read = linux.read(is.fd, &buf[0], buf.len);
183 const read_err = linux.get_errno(amt_read);
183 const read_err = linux.getErrno(amt_read);
184184 if (read_err > 0) {
185185 return switch (read_err) {
186186 errno.EINVAL => unreachable{},
......@@ -196,7 +196,7 @@ pub struct InStream {
196196
197197 pub fn close(is: &InStream) -> %void {
198198 const close_ret = linux.close(is.fd);
199 const close_err = linux.get_errno(close_ret);
199 const close_err = linux.getErrno(close_ret);
200200 if (close_err > 0) {
201201 return switch (close_err) {
202202 errno.EIO => error.Io,
......@@ -208,20 +208,20 @@ pub struct InStream {
208208 }
209209}
210210
211pub fn parse_unsigned(inline T: type, buf: []u8, radix: u8) -> %T {
211pub fn parseUnsigned(inline T: type, buf: []u8, radix: u8) -> %T {
212212 var x: T = 0;
213213
214214 for (buf) |c| {
215 const digit = %return char_to_digit(c, radix);
216 x = %return math.mul_overflow(T, x, radix);
217 x = %return math.add_overflow(T, x, digit);
215 const digit = %return charToDigit(c, radix);
216 x = %return math.mulOverflow(T, x, radix);
217 x = %return math.addOverflow(T, x, digit);
218218 }
219219
220220 return x;
221221}
222222
223223pub error InvalidChar;
224fn char_to_digit(c: u8, radix: u8) -> %u8 {
224fn charToDigit(c: u8, radix: u8) -> %u8 {
225225 const value = if ('0' <= c && c <= '9') {
226226 c - '0'
227227 } else if ('A' <= c && c <= 'Z') {
......@@ -234,21 +234,17 @@ fn char_to_digit(c: u8, radix: u8) -> %u8 {
234234 return if (value >= radix) error.InvalidChar else value;
235235}
236236
237pub fn buf_print_signed(inline T: type, out_buf: []u8, x: T) -> usize {
238 const uint = @int_type(false, T.bit_count);
237pub fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {
238 const uint = @intType(false, T.bit_count);
239239 if (x < 0) {
240240 out_buf[0] = '-';
241 return 1 + buf_print_unsigned(uint, out_buf[1...], uint(-(x + 1)) + 1);
241 return 1 + bufPrintUnsigned(uint, out_buf[1...], uint(-(x + 1)) + 1);
242242 } else {
243 return buf_print_unsigned(uint, out_buf, uint(x));
243 return bufPrintUnsigned(uint, out_buf, uint(x));
244244 }
245245}
246246
247pub fn buf_print_i64(out_buf: []u8, x: i64) -> usize {
248 buf_print_signed(i64, out_buf, x)
249}
250
251pub fn buf_print_unsigned(inline T: type, out_buf: []u8, x: T) -> usize {
247pub fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {
252248 var buf: [max_u64_base10_digits]u8 = undefined;
253249 var a = x;
254250 var index: usize = buf.len;
......@@ -269,13 +265,9 @@ pub fn buf_print_unsigned(inline T: type, out_buf: []u8, x: T) -> usize {
269265 return len;
270266}
271267
272pub fn buf_print_u64(out_buf: []u8, x: u64) -> usize {
273 buf_print_unsigned(u64, out_buf, x)
274}
275
276268#attribute("test")
277fn parse_u64_digit_too_big() {
278 parse_unsigned(u64, "123a", 10) %% |err| {
269fn parseU64DigitTooBig() {
270 parseUnsigned(u64, "123a", 10) %% |err| {
279271 if (err == error.InvalidChar) return;
280272 unreachable{};
281273 };
std/linux.zig+9-9
......@@ -1,4 +1,4 @@
1const arch = switch (@compile_var("arch")) {
1const arch = switch (@compileVar("arch")) {
22 x86_64 => @import("linux_x86_64.zig"),
33 i386 => @import("linux_i386.zig"),
44 else => @compile_err("unsupported arch"),
......@@ -221,7 +221,7 @@ pub const AF_VSOCK = PF_VSOCK;
221221pub const AF_MAX = PF_MAX;
222222
223223/// Get the errno from a syscall return value, or 0 for no error.
224pub fn get_errno(r: usize) -> usize {
224pub fn getErrno(r: usize) -> usize {
225225 const signed_r = *(&isize)(&r);
226226 if (signed_r > -4096 && signed_r < 0) usize(-signed_r) else 0
227227}
......@@ -291,22 +291,22 @@ const app_mask = []u8 { 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, };
291291
292292pub fn raise(sig: i32) -> i32 {
293293 var set: sigset_t = undefined;
294 block_app_signals(&set);
294 blockAppSignals(&set);
295295 const tid = i32(arch.syscall0(arch.SYS_gettid));
296296 const ret = i32(arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig)));
297 restore_signals(&set);
297 restoreSignals(&set);
298298 return ret;
299299}
300300
301fn block_all_signals(set: &sigset_t) {
301fn blockAllSignals(set: &sigset_t) {
302302 arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&all_mask), usize(set), NSIG/8);
303303}
304304
305fn block_app_signals(set: &sigset_t) {
305fn blockAppSignals(set: &sigset_t) {
306306 arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&app_mask), usize(set), NSIG/8);
307307}
308308
309fn restore_signals(set: &sigset_t) {
309fn restoreSignals(set: &sigset_t) {
310310 arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, usize(set), 0, NSIG/8);
311311}
312312
......@@ -442,7 +442,7 @@ pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags:
442442// }
443443//
444444// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
445// const socket_err = get_errno(socket_ret);
445// const socket_err = getErrno(socket_ret);
446446// if (socket_err > 0) {
447447// return error.SystemResources;
448448// }
......@@ -451,7 +451,7 @@ pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags:
451451// ifr.ifr_name[name.len] = 0;
452452// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr);
453453// close(socket_fd);
454// const ioctl_err = get_errno(ioctl_ret);
454// const ioctl_err = getErrno(ioctl_ret);
455455// if (ioctl_err > 0) {
456456// return error.Io;
457457// }
std/list.zig+10-10
......@@ -4,17 +4,17 @@ const mem = @import("mem.zig");
44const Allocator = mem.Allocator;
55
66pub fn List(inline T: type) -> type {
7 SmallList(T, @sizeof(usize))
7 SmallList(T, @sizeOf(usize))
88}
99
10// TODO: make sure that setting STATIC_SIZE to 0 codegens to the same code
11// as if this were programmed without STATIC_SIZE at all.
12pub struct SmallList(T: type, STATIC_SIZE: usize) {
13 const Self = SmallList(T, STATIC_SIZE);
10// TODO: make sure that setting static_size to 0 codegens to the same code
11// as if this were programmed without static_size at all.
12pub struct SmallList(T: type, static_size: usize) {
13 const Self = SmallList(T, static_size);
1414
1515 items: []T,
1616 len: usize,
17 prealloc_items: [STATIC_SIZE]T,
17 prealloc_items: [static_size]T,
1818 allocator: &Allocator,
1919
2020 pub fn init(l: &Self, allocator: &Allocator) {
......@@ -31,17 +31,17 @@ pub struct SmallList(T: type, STATIC_SIZE: usize) {
3131
3232 pub fn append(l: &Self, item: T) -> %void {
3333 const new_length = l.len + 1;
34 %return l.ensure_capacity(new_length);
34 %return l.ensureCapacity(new_length);
3535 l.items[l.len] = item;
3636 l.len = new_length;
3737 }
3838
3939 pub fn resize(l: &Self, new_len: usize) -> %void {
40 %return l.ensure_capacity(new_len);
40 %return l.ensureCapacity(new_len);
4141 l.len = new_len;
4242 }
4343
44 pub fn ensure_capacity(l: &Self, new_capacity: usize) -> %void {
44 pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void {
4545 const old_capacity = l.items.len;
4646 var better_capacity = old_capacity;
4747 while (better_capacity < new_capacity) {
......@@ -59,7 +59,7 @@ pub struct SmallList(T: type, STATIC_SIZE: usize) {
5959}
6060
6161#attribute("test")
62fn basic_list_test() {
62fn basicListTest() {
6363 var list: List(i32) = undefined;
6464 list.init(&debug.global_allocator);
6565 defer list.deinit();
std/math.zig+6-34
......@@ -4,34 +4,6 @@ pub enum Cmp {
44 Less,
55}
66
7pub fn f64_from_bits(bits: u64) -> f64 {
8 *(&f64)(&bits)
9}
10
11pub fn f64_to_bits(f: f64) -> u64 {
12 *(&u64)(&f)
13}
14
15pub fn f64_get_pos_inf() -> f64 {
16 f64_from_bits(0x7FF0000000000000)
17}
18
19pub fn f64_get_neg_inf() -> f64 {
20 f64_from_bits(0xFFF0000000000000)
21}
22
23pub fn f64_is_nan(f: f64) -> bool {
24 const bits = f64_to_bits(f);
25 const exp: i64 = i64((bits >> 52) & ((1 << 11) - 1));
26 const sig = (bits & ((1 << 52) - 1)) | (1 << 52);
27
28 sig != 0 && exp == (1 << 11) - 1
29}
30
31pub fn f64_is_inf(f: f64) -> bool {
32 f == f64_get_neg_inf() || f == f64_get_pos_inf()
33}
34
357pub fn min(inline T: type, x: T, y: T) -> T {
368 if (x < y) x else y
379}
......@@ -41,15 +13,15 @@ pub fn max(inline T: type, x: T, y: T) -> T {
4113}
4214
4315pub error Overflow;
44pub fn mul_overflow(inline T: type, a: T, b: T) -> %T {
16pub fn mulOverflow(inline T: type, a: T, b: T) -> %T {
4517 var answer: T = undefined;
46 if (@mul_with_overflow(T, a, b, &answer)) error.Overflow else answer
18 if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer
4719}
48pub fn add_overflow(inline T: type, a: T, b: T) -> %T {
20pub fn addOverflow(inline T: type, a: T, b: T) -> %T {
4921 var answer: T = undefined;
50 if (@add_with_overflow(T, a, b, &answer)) error.Overflow else answer
22 if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer
5123}
52pub fn sub_overflow(inline T: type, a: T, b: T) -> %T {
24pub fn subOverflow(inline T: type, a: T, b: T) -> %T {
5325 var answer: T = undefined;
54 if (@sub_with_overflow(T, a, b, &answer)) error.Overflow else answer
26 if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer
5527}
std/mem.zig+11-11
......@@ -9,34 +9,34 @@ pub error NoMem;
99
1010pub type Context = u8;
1111pub struct Allocator {
12 alloc_fn: fn (self: &Allocator, n: usize) -> %[]u8,
13 realloc_fn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,
14 free_fn: fn (self: &Allocator, mem: []u8),
12 allocFn: fn (self: &Allocator, n: usize) -> %[]u8,
13 reallocFn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,
14 freeFn: fn (self: &Allocator, mem: []u8),
1515 context: ?&Context,
1616
1717 /// Aborts the program if an allocation fails.
18 fn checked_alloc(self: &Allocator, inline T: type, n: usize) -> []T {
18 fn checkedAlloc(self: &Allocator, inline T: type, n: usize) -> []T {
1919 alloc(self, T, n) %% |err| {
2020 // TODO var args printf
2121 %%io.stderr.write("allocation failure: ");
22 %%io.stderr.write(@err_name(err));
22 %%io.stderr.write(@errName(err));
2323 %%io.stderr.printf("\n");
2424 os.abort()
2525 }
2626 }
2727
2828 fn alloc(self: &Allocator, inline T: type, n: usize) -> %[]T {
29 const byte_count = %return math.mul_overflow(usize, @sizeof(T), n);
30 ([]T)(%return self.alloc_fn(self, byte_count))
29 const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);
30 ([]T)(%return self.allocFn(self, byte_count))
3131 }
3232
3333 fn realloc(self: &Allocator, inline T: type, old_mem: []T, n: usize) -> %[]T {
34 const byte_count = %return math.mul_overflow(usize, @sizeof(T), n);
35 ([]T)(%return self.realloc_fn(self, ([]u8)(old_mem), byte_count))
34 const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);
35 ([]T)(%return self.reallocFn(self, ([]u8)(old_mem), byte_count))
3636 }
3737
3838 fn free(self: &Allocator, inline T: type, mem: []T) {
39 self.free_fn(self, ([]u8)(mem));
39 self.freeFn(self, ([]u8)(mem));
4040 }
4141}
4242
......@@ -44,7 +44,7 @@ pub struct Allocator {
4444/// dest.len must be >= source.len.
4545pub fn copy(inline T: type, dest: []T, source: []const T) {
4646 assert(dest.len >= source.len);
47 @memcpy(dest.ptr, source.ptr, @sizeof(T) * source.len);
47 @memcpy(dest.ptr, source.ptr, @sizeOf(T) * source.len);
4848}
4949
5050/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,
std/net.zig+43-43
......@@ -17,7 +17,7 @@ struct Connection {
1717
1818 pub fn send(c: Connection, buf: []const u8) -> %usize {
1919 const send_ret = linux.sendto(c.socket_fd, buf.ptr, buf.len, 0, null, 0);
20 const send_err = linux.get_errno(send_ret);
20 const send_err = linux.getErrno(send_ret);
2121 switch (send_err) {
2222 0 => return send_ret,
2323 errno.EINVAL => unreachable{},
......@@ -31,7 +31,7 @@ struct Connection {
3131
3232 pub fn recv(c: Connection, buf: []u8) -> %[]u8 {
3333 const recv_ret = linux.recvfrom(c.socket_fd, buf.ptr, buf.len, 0, null, null);
34 const recv_err = linux.get_errno(recv_ret);
34 const recv_err = linux.getErrno(recv_ret);
3535 switch (recv_err) {
3636 0 => return buf[0...recv_ret],
3737 errno.EINVAL => unreachable{},
......@@ -47,7 +47,7 @@ struct Connection {
4747 }
4848
4949 pub fn close(c: Connection) -> %void {
50 switch (linux.get_errno(linux.close(c.socket_fd))) {
50 switch (linux.getErrno(linux.close(c.socket_fd))) {
5151 0 => return,
5252 errno.EBADF => unreachable{},
5353 errno.EINTR => return error.SigInterrupt,
......@@ -76,7 +76,7 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address {
7676 unreachable{} // TODO
7777 }
7878
79 switch (parse_ip_literal(hostname)) {
79 switch (parseIpLiteral(hostname)) {
8080 Ok => |addr| {
8181 out_addrs[0] = addr;
8282 return out_addrs[0...1];
......@@ -87,9 +87,9 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address {
8787 unreachable{} // TODO
8888}
8989
90pub fn connect_addr(addr: &Address, port: u16) -> %Connection {
90pub fn connectAddr(addr: &Address, port: u16) -> %Connection {
9191 const socket_ret = linux.socket(addr.family, linux.SOCK_STREAM, linux.PROTO_tcp);
92 const socket_err = linux.get_errno(socket_ret);
92 const socket_err = linux.getErrno(socket_ret);
9393 if (socket_err > 0) {
9494 // TODO figure out possible errors from socket()
9595 return error.Unexpected;
......@@ -99,22 +99,22 @@ pub fn connect_addr(addr: &Address, port: u16) -> %Connection {
9999 const connect_ret = if (addr.family == linux.AF_INET) {
100100 var os_addr: linux.sockaddr_in = undefined;
101101 os_addr.family = addr.family;
102 os_addr.port = swap_if_little_endian(u16, port);
102 os_addr.port = swapIfLittleEndian(u16, port);
103103 @memcpy((&u8)(&os_addr.addr), &addr.addr[0], 4);
104 @memset(&os_addr.zero, 0, @sizeof(@typeof(os_addr.zero)));
105 linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeof(linux.sockaddr_in))
104 @memset(&os_addr.zero, 0, @sizeOf(@typeOf(os_addr.zero)));
105 linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in))
106106 } else if (addr.family == linux.AF_INET6) {
107107 var os_addr: linux.sockaddr_in6 = undefined;
108108 os_addr.family = addr.family;
109 os_addr.port = swap_if_little_endian(u16, port);
109 os_addr.port = swapIfLittleEndian(u16, port);
110110 os_addr.flowinfo = 0;
111111 os_addr.scope_id = addr.scope_id;
112112 @memcpy(&os_addr.addr[0], &addr.addr[0], 16);
113 linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeof(linux.sockaddr_in6))
113 linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in6))
114114 } else {
115115 unreachable{}
116116 };
117 const connect_err = linux.get_errno(connect_ret);
117 const connect_err = linux.getErrno(connect_ret);
118118 if (connect_err > 0) {
119119 switch (connect_err) {
120120 errno.ETIMEDOUT => return error.TimedOut,
......@@ -135,23 +135,23 @@ pub fn connect(hostname: []const u8, port: u16) -> %Connection {
135135 const addrs_slice = %return lookup(hostname, addrs_buf);
136136 const main_addr = &addrs_slice[0];
137137
138 return connect_addr(main_addr, port);
138 return connectAddr(main_addr, port);
139139}
140140
141141pub error InvalidIpLiteral;
142142
143pub fn parse_ip_literal(buf: []const u8) -> %Address {
144 switch (parse_ip4(buf)) {
143pub fn parseIpLiteral(buf: []const u8) -> %Address {
144 switch (parseIp4(buf)) {
145145 Ok => |ip4| {
146146 var result: Address = undefined;
147 @memcpy(&result.addr[0], (&u8)(&ip4), @sizeof(u32));
147 @memcpy(&result.addr[0], (&u8)(&ip4), @sizeOf(u32));
148148 result.family = linux.AF_INET;
149149 result.scope_id = 0;
150150 return result;
151151 },
152152 else => {},
153153 }
154 switch (parse_ip6(buf)) {
154 switch (parseIp6(buf)) {
155155 Ok => |addr| {
156156 return addr;
157157 },
......@@ -161,7 +161,7 @@ pub fn parse_ip_literal(buf: []const u8) -> %Address {
161161 return error.InvalidIpLiteral;
162162}
163163
164fn hex_digit(c: u8) -> u8 {
164fn hexDigit(c: u8) -> u8 {
165165 // TODO use switch with range
166166 if ('0' <= c && c <= '9') {
167167 c - '0'
......@@ -170,7 +170,7 @@ fn hex_digit(c: u8) -> u8 {
170170 } else if ('a' <= c && c <= 'z') {
171171 c - 'a' + 10
172172 } else {
173 @max_value(u8)
173 @maxValue(u8)
174174 }
175175}
176176
......@@ -180,7 +180,7 @@ error JunkAtEnd;
180180error Incomplete;
181181
182182#static_eval_enable(false)
183fn parse_ip6(buf: []const u8) -> %Address {
183fn parseIp6(buf: []const u8) -> %Address {
184184 var result: Address = undefined;
185185 result.family = linux.AF_INET6;
186186 result.scope_id = 0;
......@@ -194,10 +194,10 @@ fn parse_ip6(buf: []const u8) -> %Address {
194194 if (scope_id) {
195195 if (c >= '0' && c <= '9') {
196196 const digit = c - '0';
197 if (@mul_with_overflow(u32, result.scope_id, 10, &result.scope_id)) {
197 if (@mulWithOverflow(u32, result.scope_id, 10, &result.scope_id)) {
198198 return error.Overflow;
199199 }
200 if (@add_with_overflow(u32, result.scope_id, digit, &result.scope_id)) {
200 if (@addWithOverflow(u32, result.scope_id, digit, &result.scope_id)) {
201201 return error.Overflow;
202202 }
203203 } else {
......@@ -230,14 +230,14 @@ fn parse_ip6(buf: []const u8) -> %Address {
230230 scope_id = true;
231231 saw_any_digits = false;
232232 } else {
233 const digit = hex_digit(c);
234 if (digit == @max_value(u8)) {
233 const digit = hexDigit(c);
234 if (digit == @maxValue(u8)) {
235235 return error.InvalidChar;
236236 }
237 if (@mul_with_overflow(u16, x, 16, &x)) {
237 if (@mulWithOverflow(u16, x, 16, &x)) {
238238 return error.Overflow;
239239 }
240 if (@add_with_overflow(u16, x, digit, &x)) {
240 if (@addWithOverflow(u16, x, digit, &x)) {
241241 return error.Overflow;
242242 }
243243 saw_any_digits = true;
......@@ -276,7 +276,7 @@ fn parse_ip6(buf: []const u8) -> %Address {
276276 return error.Incomplete;
277277}
278278
279fn parse_ip4(buf: []const u8) -> %u32 {
279fn parseIp4(buf: []const u8) -> %u32 {
280280 var result: u32 = undefined;
281281 const out_ptr = ([]u8)((&result)[0...1]);
282282
......@@ -298,10 +298,10 @@ fn parse_ip4(buf: []const u8) -> %u32 {
298298 } else if (c >= '0' && c <= '9') {
299299 saw_any_digits = true;
300300 const digit = c - '0';
301 if (@mul_with_overflow(u8, x, 10, &x)) {
301 if (@mulWithOverflow(u8, x, 10, &x)) {
302302 return error.Overflow;
303303 }
304 if (@add_with_overflow(u8, x, digit, &x)) {
304 if (@addWithOverflow(u8, x, digit, &x)) {
305305 return error.Overflow;
306306 }
307307 } else {
......@@ -318,19 +318,19 @@ fn parse_ip4(buf: []const u8) -> %u32 {
318318
319319
320320#attribute("test")
321fn test_parse_ip4() {
322 assert(%%parse_ip4("127.0.0.1") == swap_if_little_endian(u32, 0x7f000001));
323 switch (parse_ip4("256.0.0.1")) { Overflow => {}, else => unreachable {}, }
324 switch (parse_ip4("x.0.0.1")) { InvalidChar => {}, else => unreachable {}, }
325 switch (parse_ip4("127.0.0.1.1")) { JunkAtEnd => {}, else => unreachable {}, }
326 switch (parse_ip4("127.0.0.")) { Incomplete => {}, else => unreachable {}, }
327 switch (parse_ip4("100..0.1")) { InvalidChar => {}, else => unreachable {}, }
321fn testParseIp4() {
322 assert(%%parseIp4("127.0.0.1") == swapIfLittleEndian(u32, 0x7f000001));
323 switch (parseIp4("256.0.0.1")) { Overflow => {}, else => unreachable {}, }
324 switch (parseIp4("x.0.0.1")) { InvalidChar => {}, else => unreachable {}, }
325 switch (parseIp4("127.0.0.1.1")) { JunkAtEnd => {}, else => unreachable {}, }
326 switch (parseIp4("127.0.0.")) { Incomplete => {}, else => unreachable {}, }
327 switch (parseIp4("100..0.1")) { InvalidChar => {}, else => unreachable {}, }
328328}
329329
330330#attribute("test")
331fn test_parse_ip6() {
331fn testParseIp6() {
332332 {
333 const addr = %%parse_ip6("FF01:0:0:0:0:0:0:FB");
333 const addr = %%parseIp6("FF01:0:0:0:0:0:0:FB");
334334 assert(addr.addr[0] == 0xff);
335335 assert(addr.addr[1] == 0x01);
336336 assert(addr.addr[2] == 0x00);
......@@ -338,7 +338,7 @@ fn test_parse_ip6() {
338338}
339339
340340#attribute("test")
341fn test_lookup_simple_ip() {
341fn testLookupSimpleIp() {
342342 {
343343 var addrs_buf: [5]Address = undefined;
344344 const addrs = %%lookup("192.168.1.1", addrs_buf);
......@@ -352,16 +352,16 @@ fn test_lookup_simple_ip() {
352352 }
353353}
354354
355fn swap_if_little_endian(inline T: type, x: T) -> T {
356 if (@compile_var("is_big_endian")) x else endian_swap(T, x)
355fn swapIfLittleEndian(inline T: type, x: T) -> T {
356 if (@compileVar("is_big_endian")) x else endianSwap(T, x)
357357}
358358
359fn endian_swap(inline T: type, x: T) -> T {
359fn endianSwap(inline T: type, x: T) -> T {
360360 const x_slice = ([]u8)((&const x)[0...1]);
361361 var result: T = undefined;
362362 const result_slice = ([]u8)((&result)[0...1]);
363363 for (result_slice) |*b, i| {
364 *b = x_slice[@sizeof(T) - i - 1];
364 *b = x_slice[@sizeOf(T) - i - 1];
365365 }
366366 return result;
367367}
std/os.zig+2-2
......@@ -5,10 +5,10 @@ pub error SigInterrupt;
55pub error Unexpected;
66
77pub fn get_random_bytes(buf: []u8) -> %void {
8 switch (@compile_var("os")) {
8 switch (@compileVar("os")) {
99 linux => {
1010 const ret = linux.getrandom(buf.ptr, buf.len, 0);
11 const err = linux.get_errno(ret);
11 const err = linux.getErrno(ret);
1212 if (err > 0) {
1313 return switch (err) {
1414 errno.EINVAL => unreachable{},
std/rand.zig+29-31
......@@ -19,15 +19,13 @@ pub const MT19937_64 = MersenneTwister(
1919
2020/// Use `init` to initialize this state.
2121pub struct Rand {
22 const Rng = if (@sizeof(usize) >= 8) MT19937_64 else MT19937_32;
22 const Rng = if (@sizeOf(usize) >= 8) MT19937_64 else MT19937_32;
2323
2424 rng: Rng,
2525
2626 /// Initialize random state with the given seed.
27 pub fn init(seed: usize) -> Rand {
28 var r: Rand = undefined;
29 r.rng = Rng.init(seed);
30 return r;
27 pub fn init(r: &Rand, seed: usize) {
28 r.rng.init(seed);
3129 }
3230
3331 /// Get an integer with random bits.
......@@ -35,24 +33,24 @@ pub struct Rand {
3533 if (T == usize) {
3634 return r.rng.get();
3735 } else {
38 var result: [@sizeof(T)]u8 = undefined;
39 r.fill_bytes(result);
36 var result: [@sizeOf(T)]u8 = undefined;
37 r.fillBytes(result);
4038 return ([]T)(result)[0];
4139 }
4240 }
4341
4442 /// Fill `buf` with randomness.
45 pub fn fill_bytes(r: &Rand, buf: []u8) {
43 pub fn fillBytes(r: &Rand, buf: []u8) {
4644 var bytes_left = buf.len;
47 while (bytes_left >= @sizeof(usize)) {
45 while (bytes_left >= @sizeOf(usize)) {
4846 ([]usize)(buf[buf.len - bytes_left...])[0] = r.rng.get();
49 bytes_left -= @sizeof(usize);
47 bytes_left -= @sizeOf(usize);
5048 }
5149 if (bytes_left > 0) {
52 var rand_val_array : [@sizeof(usize)]u8 = undefined;
50 var rand_val_array : [@sizeOf(usize)]u8 = undefined;
5351 ([]usize)(rand_val_array)[0] = r.rng.get();
5452 while (bytes_left > 0) {
55 buf[buf.len - bytes_left] = rand_val_array[@sizeof(usize) - bytes_left];
53 buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left];
5654 bytes_left -= 1;
5755 }
5856 }
......@@ -61,14 +59,14 @@ pub struct Rand {
6159 /// Get a random unsigned integer with even distribution between `start`
6260 /// inclusive and `end` exclusive.
6361 // TODO support signed integers and then rename to "range"
64 pub fn range_unsigned(r: &Rand, inline T: type, start: T, end: T) -> T {
62 pub fn rangeUnsigned(r: &Rand, inline T: type, start: T, end: T) -> T {
6563 const range = end - start;
66 const leftover = @max_value(T) % range;
67 const upper_bound = @max_value(T) - leftover;
68 var rand_val_array : [@sizeof(T)]u8 = undefined;
64 const leftover = @maxValue(T) % range;
65 const upper_bound = @maxValue(T) - leftover;
66 var rand_val_array : [@sizeOf(T)]u8 = undefined;
6967
7068 while (true) {
71 r.fill_bytes(rand_val_array);
69 r.fillBytes(rand_val_array);
7270 const rand_val = ([]T)(rand_val_array)[0];
7371 if (rand_val < upper_bound) {
7472 return start + (rand_val % range);
......@@ -79,19 +77,19 @@ pub struct Rand {
7977 /// Get a floating point value in the range 0.0..1.0.
8078 pub fn float(r: &Rand, inline T: type) -> T {
8179 // TODO Implement this way instead:
82 // const int = @int_type(false, @sizeof(T) * 8);
80 // const int = @int_type(false, @sizeOf(T) * 8);
8381 // const mask = ((1 << @float_mantissa_bit_count(T)) - 1);
8482 // const rand_bits = r.rng.scalar(int) & mask;
8583 // return @float_compose(T, false, 0, rand_bits) - 1.0
86 const int_type = @int_type(false, @sizeof(T) * 8);
84 const int_type = @intType(false, @sizeOf(T) * 8);
8785 const precision = if (T == f32) {
8886 16777216
8987 } else if (T == f64) {
9088 9007199254740992
9189 } else {
92 @compile_err("unknown floating point type" ++ @type_name(T))
90 @compile_err("unknown floating point type" ++ @typeName(T))
9391 };
94 return T(r.range_unsigned(int_type, 0, precision)) / T(precision);
92 return T(r.rangeUnsigned(int_type, 0, precision)) / T(precision);
9593 }
9694}
9795
......@@ -110,8 +108,7 @@ struct MersenneTwister(
110108
111109 // TODO improve compile time eval code and then allow this function to be executed at compile time.
112110 #static_eval_enable(false)
113 pub fn init(seed: int) -> Self {
114 var mt: Self = undefined;
111 pub fn init(mt: &Self, seed: int) {
115112 mt.index = n;
116113
117114 var prev_value = seed;
......@@ -120,8 +117,6 @@ struct MersenneTwister(
120117 prev_value = int(i) +% f *% (prev_value ^ (prev_value >> (int.bit_count - 2)));
121118 mt.array[i] = prev_value;
122119 }};
123
124 return mt;
125120 }
126121
127122 pub fn get(mt: &Self) -> int {
......@@ -161,8 +156,9 @@ struct MersenneTwister(
161156}
162157
163158#attribute("test")
164fn test_float32() {
165 var r = Rand.init(42);
159fn testFloat32() {
160 var r: Rand = undefined;
161 r.init(42);
166162
167163 {var i: usize = 0; while (i < 1000; i += 1) {
168164 const val = r.float(f32);
......@@ -172,16 +168,18 @@ fn test_float32() {
172168}
173169
174170#attribute("test")
175fn test_MT19937_64() {
176 const rng = MT19937_64.init(rand_test.mt64_seed);
171fn testMT19937_64() {
172 var rng: MT19937_64 = undefined;
173 rng.init(rand_test.mt64_seed);
177174 for (rand_test.mt64_data) |value| {
178175 assert(value == rng.get());
179176 }
180177}
181178
182179#attribute("test")
183fn test_MT19937_32() {
184 const rng = MT19937_32.init(rand_test.mt32_seed);
180fn testMT19937_32() {
181 var rng: MT19937_32 = undefined;
182 rng.init(rand_test.mt32_seed);
185183 for (rand_test.mt32_data) |value| {
186184 assert(value == rng.get());
187185 }
std/str.zig+3-3
......@@ -1,10 +1,10 @@
11const assert = @import("debug.zig").assert;
22
33pub fn eql(a: []const u8, b: []const u8) -> bool {
4 slice_eql(u8, a, b)
4 sliceEql(u8, a, b)
55}
66
7pub fn slice_eql(inline T: type, a: []const T, b: []const T) -> bool {
7pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
88 if (a.len != b.len) return false;
99 for (a) |item, index| {
1010 if (b[index] != item) return false;
......@@ -13,7 +13,7 @@ pub fn slice_eql(inline T: type, a: []const T, b: []const T) -> bool {
1313}
1414
1515#attribute("test")
16fn string_equality() {
16fn stringEquality() {
1717 assert(eql("abcd", "abcd"));
1818 assert(!eql("abcdef", "abZdef"));
1919 assert(!eql("abcdefg", "abcdef"));
std/test_runner.zig+4-4
......@@ -7,19 +7,19 @@ struct TestFn {
77
88extern var zig_test_fn_list: []TestFn;
99
10pub fn run_tests() -> %void {
11 for (zig_test_fn_list) |test_fn, i| {
10pub fn runTests() -> %void {
11 for (zig_test_fn_list) |testFn, i| {
1212 // TODO: print var args
1313 %%io.stderr.write("Test ");
1414 %%io.stderr.print_u64(i + 1);
1515 %%io.stderr.write("/");
1616 %%io.stderr.print_u64(zig_test_fn_list.len);
1717 %%io.stderr.write(" ");
18 %%io.stderr.write(test_fn.name);
18 %%io.stderr.write(testFn.name);
1919 %%io.stderr.write("...");
2020 %%io.stderr.flush();
2121
22 test_fn.func();
22 testFn.func();
2323
2424
2525 %%io.stderr.write("OK\n");
std/test_runner_libc.zig+1-1
......@@ -1,6 +1,6 @@
11const test_runner = @import("test_runner.zig");
22
33export fn main(argc: c_int, argv: &&u8) -> c_int {
4 test_runner.run_tests() %% return -1;
4 test_runner.runTests() %% return -1;
55 return 0;
66}
std/test_runner_nolibc.zig+1-1
......@@ -1,5 +1,5 @@
11const test_runner = @import("test_runner.zig");
22
33pub fn main(args: [][]u8) -> %void {
4 return test_runner.run_tests();
4 return test_runner.runTests();
55}
test/cases/sizeof_and_typeof.zig created+9
......@@ -0,0 +1,9 @@
1const assert = @import("std").debug.assert;
2
3#attribute("test")
4fn sizeofAndTypeOf() {
5 const y: @typeOf(x) = 120;
6 assert(@sizeOf(@typeOf(y)) == 2);
7}
8const x: u16 = 13;
9const z: @typeOf(x) = 19;
test/run_tests.cpp+35-35
......@@ -202,7 +202,7 @@ static TestCase *add_parseh_case(const char *case_name, const char *source, int
202202
203203static void add_compiling_test_cases(void) {
204204 add_simple_case_libc("hello world with libc", R"SOURCE(
205const c = @c_import(@c_include("stdio.h"));
205const c = @cImport(@cInclude("stdio.h"));
206206export fn main(argc: c_int, argv: &&u8) -> c_int {
207207 c.puts(c"Hello, world!");
208208 return 0;
......@@ -215,12 +215,12 @@ use @import("std").io;
215215use @import("foo.zig");
216216
217217pub fn main(args: [][]u8) -> %void {
218 private_function();
218 privateFunction();
219219 %%stdout.printf("OK 2\n");
220220}
221221
222fn private_function() {
223 print_text();
222fn privateFunction() {
223 printText();
224224}
225225 )SOURCE", "OK 1\nOK 2\n");
226226
......@@ -229,12 +229,12 @@ use @import("std").io;
229229
230230// purposefully conflicting function with main.zig
231231// but it's private so it should be OK
232fn private_function() {
232fn privateFunction() {
233233 %%stdout.printf("OK 1\n");
234234}
235235
236pub fn print_text() {
237 private_function();
236pub fn printText() {
237 privateFunction();
238238}
239239 )SOURCE");
240240 }
......@@ -316,7 +316,7 @@ pub fn main(args: [][]u8) -> %void {
316316
317317
318318 add_simple_case_libc("number literals", R"SOURCE(
319const c = @c_import(@c_include("stdio.h"));
319const c = @cImport(@cInclude("stdio.h"));
320320
321321export fn main(argc: c_int, argv: &&u8) -> c_int {
322322 c.printf(c"\n");
......@@ -444,12 +444,12 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
444444 add_simple_case("order-independent declarations", R"SOURCE(
445445const io = @import("std").io;
446446const z = io.stdin_fileno;
447const x : @typeof(y) = 1234;
447const x : @typeOf(y) = 1234;
448448const y : u16 = 5678;
449449pub fn main(args: [][]u8) -> %void {
450450 var x_local : i32 = print_ok(x);
451451}
452fn print_ok(val: @typeof(x)) -> @typeof(foo) {
452fn print_ok(val: @typeOf(x)) -> @typeOf(foo) {
453453 %%io.stdout.printf("OK\n");
454454 return 0;
455455}
......@@ -482,7 +482,7 @@ pub fn main(args: [][]u8) -> %void {
482482 )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n");
483483
484484 add_simple_case_libc("expose function pointer to C land", R"SOURCE(
485const c = @c_import(@c_include("stdlib.h"));
485const c = @cImport(@cInclude("stdlib.h"));
486486
487487export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
488488 const a_int = (&i32)(a ?? unreachable{});
......@@ -499,7 +499,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
499499export fn main(args: c_int, argv: &&u8) -> c_int {
500500 var array = []u32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
501501
502 c.qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn);
502 c.qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeOf(i32), compare_fn);
503503
504504 for (array) |item, i| {
505505 if (item != i) {
......@@ -514,7 +514,7 @@ export fn main(args: c_int, argv: &&u8) -> c_int {
514514
515515
516516 add_simple_case_libc("casting between float and integer types", R"SOURCE(
517const c = @c_import(@c_include("stdio.h"));
517const c = @cImport(@cInclude("stdio.h"));
518518export fn main(argc: c_int, argv: &&u8) -> c_int {
519519 const small: f32 = 3.25;
520520 const x: f64 = small;
......@@ -654,8 +654,8 @@ fn its_gonna_pass() -> %void { }
654654
655655
656656 {
657 TestCase *tc = add_simple_case("@embed_file", R"SOURCE(
658const foo_txt = @embed_file("foo.txt");
657 TestCase *tc = add_simple_case("@embedFile", R"SOURCE(
658const foo_txt = @embedFile("foo.txt");
659659const io = @import("std").io;
660660
661661pub fn main(args: [][]u8) -> %void {
......@@ -956,8 +956,8 @@ fn f() -> @bogus(foo) {
956956 )SOURCE", 1, ".tmp_source.zig:2:11: error: invalid builtin function: 'bogus'");
957957
958958 add_compile_fail_case("top level decl dependency loop", R"SOURCE(
959const a : @typeof(b) = 0;
960const b : @typeof(a) = 0;
959const a : @typeOf(b) = 0;
960const b : @typeOf(a) = 0;
961961 )SOURCE", 1, ".tmp_source.zig:2:1: error: 'a' depends on itself");
962962
963963 add_compile_fail_case("noalias on non pointer param", R"SOURCE(
......@@ -1012,8 +1012,8 @@ fn f(s: [10]u8) -> []u8 {
10121012}
10131013 )SOURCE", 1, ".tmp_source.zig:3:5: error: array concatenation requires constant expression");
10141014
1015 add_compile_fail_case("c_import with bogus include", R"SOURCE(
1016const c = @c_import(@c_include("bogus.h"));
1015 add_compile_fail_case("@cImport with bogus include", R"SOURCE(
1016const c = @cImport(@cInclude("bogus.h"));
10171017 )SOURCE", 2, ".tmp_source.zig:2:11: error: C import failed",
10181018 ".h:1:10: note: 'bogus.h' file not found");
10191019
......@@ -1022,12 +1022,12 @@ const x = 3;
10221022const y = &x;
10231023 )SOURCE", 1, ".tmp_source.zig:3:12: error: unable to get address of type '(integer literal)'");
10241024
1025 add_compile_fail_case("@typeof number literal", R"SOURCE(
1025 add_compile_fail_case("@typeOf number literal", R"SOURCE(
10261026const x = 3;
10271027struct Foo {
1028 index: @typeof(x),
1028 index: @typeOf(x),
10291029}
1030 )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeof");
1030 )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeOf");
10311031
10321032 add_compile_fail_case("integer overflow error", R"SOURCE(
10331033const x : u8 = 300;
......@@ -1050,7 +1050,7 @@ struct Foo {
10501050 }
10511051}
10521052
1053const member_fn_type = @typeof(Foo.member_a);
1053const member_fn_type = @typeOf(Foo.member_a);
10541054const members = []member_fn_type {
10551055 Foo.member_a,
10561056 Foo.member_b,
......@@ -1101,15 +1101,15 @@ fn func() -> bogus {}
11011101
11021102
11031103 add_compile_fail_case("bogus compile var", R"SOURCE(
1104const x = @compile_var("bogus");
1105 )SOURCE", 1, ".tmp_source.zig:2:24: error: unrecognized compile variable: 'bogus'");
1104const x = @compileVar("bogus");
1105 )SOURCE", 1, ".tmp_source.zig:2:23: error: unrecognized compile variable: 'bogus'");
11061106
11071107
1108 add_compile_fail_case("@const_eval", R"SOURCE(
1108 add_compile_fail_case("@constEval", R"SOURCE(
11091109fn a(x: i32) {
1110 const y = @const_eval(x);
1110 const y = @constEval(x);
11111111}
1112 )SOURCE", 1, ".tmp_source.zig:3:27: error: unable to evaluate constant expression");
1112 )SOURCE", 1, ".tmp_source.zig:3:26: error: unable to evaluate constant expression");
11131113
11141114 add_compile_fail_case("non constant expression in array size outside function", R"SOURCE(
11151115struct Foo {
......@@ -1245,8 +1245,8 @@ fn fibbonaci(x: i32) -> i32 {
12451245 ".tmp_source.zig:2:37: note: called from here",
12461246 ".tmp_source.zig:4:40: note: quota exceeded here");
12471247
1248 add_compile_fail_case("@embed_file with bogus file", R"SOURCE(
1249const resource = @embed_file("bogus.txt");
1248 add_compile_fail_case("@embedFile with bogus file", R"SOURCE(
1249const resource = @embedFile("bogus.txt");
12501250 )SOURCE", 1, ".tmp_source.zig:2:18: error: unable to find './bogus.txt'");
12511251
12521252
......@@ -1555,23 +1555,23 @@ fn div0(a: i32, b: i32) -> i32 {
15551555 add_debug_safety_case("exact division failure", R"SOURCE(
15561556error Whatever;
15571557pub fn main(args: [][]u8) -> %void {
1558 const x = div_exact(10, 3);
1558 const x = divExact(10, 3);
15591559 if (x == 0) return error.Whatever;
15601560}
15611561#static_eval_enable(false)
1562fn div_exact(a: i32, b: i32) -> i32 {
1563 @div_exact(a, b)
1562fn divExact(a: i32, b: i32) -> i32 {
1563 @divExact(a, b)
15641564}
15651565 )SOURCE");
15661566
15671567 add_debug_safety_case("cast []u8 to bigger slice of wrong size", R"SOURCE(
15681568error Whatever;
15691569pub fn main(args: [][]u8) -> %void {
1570 const x = widen_slice([]u8{1, 2, 3, 4, 5});
1570 const x = widenSlice([]u8{1, 2, 3, 4, 5});
15711571 if (x.len == 0) return error.Whatever;
15721572}
15731573#static_eval_enable(false)
1574fn widen_slice(slice: []u8) -> []i32 {
1574fn widenSlice(slice: []u8) -> []i32 {
15751575 ([]i32)(slice)
15761576}
15771577 )SOURCE");
test/self_hosted.zig+357-364
......@@ -3,29 +3,30 @@ const assert = std.debug.assert;
33const str = std.str;
44const cstr = std.cstr;
55const other = @import("other.zig");
6const cases_return_type_type = @import("cases/return_type_type.zig");
6const test_return_type_type = @import("cases/return_type_type.zig");
77const test_zeroes = @import("cases/zeroes.zig");
8const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig");
89
910// normal comment
1011/// this is a documentation comment
1112/// doc comment line 2
1213#attribute("test")
13fn empty_function_with_comments() {}
14fn emptyFunctionWithComments() {}
1415
1516
1617#attribute("test")
17fn if_statements() {
18 should_be_equal(1, 1);
19 first_eql_third(2, 1, 2);
18fn ifStatements() {
19 shouldBeEqual(1, 1);
20 firstEqlThird(2, 1, 2);
2021}
21fn should_be_equal(a: i32, b: i32) {
22fn shouldBeEqual(a: i32, b: i32) {
2223 if (a != b) {
2324 unreachable{};
2425 } else {
2526 return;
2627 }
2728}
28fn first_eql_third(a: i32, b: i32, c: i32) {
29fn firstEqlThird(a: i32, b: i32, c: i32) {
2930 if (a == b) {
3031 unreachable{};
3132 } else if (b == c) {
......@@ -40,33 +41,33 @@ fn first_eql_third(a: i32, b: i32, c: i32) {
4041
4142#attribute("test")
4243fn params() {
43 assert(test_params_add(22, 11) == 33);
44 assert(testParamsAdd(22, 11) == 33);
4445}
45fn test_params_add(a: i32, b: i32) -> i32 {
46fn testParamsAdd(a: i32, b: i32) -> i32 {
4647 a + b
4748}
4849
4950
5051#attribute("test")
51fn local_variables() {
52 test_loc_vars(2);
52fn localVariables() {
53 testLocVars(2);
5354}
54fn test_loc_vars(b: i32) {
55fn testLocVars(b: i32) {
5556 const a: i32 = 1;
5657 if (a + b != 3) unreachable{};
5758}
5859
5960#attribute("test")
60fn bool_literals() {
61fn boolLiterals() {
6162 assert(true);
6263 assert(!false);
6364}
6465
6566#attribute("test")
66fn void_parameters() {
67 void_fun(1, void{}, 2, {});
67fn voidParameters() {
68 voidFun(1, void{}, 2, {});
6869}
69fn void_fun(a : i32, b : void, c : i32, d : void) {
70fn voidFun(a : i32, b : void, c : i32, d : void) {
7071 const v = b;
7172 const vv : void = if (a == 1) {v} else {};
7273 assert(a + c == 3);
......@@ -74,7 +75,7 @@ fn void_fun(a : i32, b : void, c : i32, d : void) {
7475}
7576
7677#attribute("test")
77fn mutable_local_variables() {
78fn mutableLocalVariables() {
7879 var zero : i32 = 0;
7980 assert(zero == 0);
8081
......@@ -104,31 +105,31 @@ fn arrays() {
104105 }
105106
106107 assert(accumulator == 15);
107 assert(get_array_len(array) == 5);
108 assert(getArrayLen(array) == 5);
108109}
109fn get_array_len(a: []u32) -> usize {
110fn getArrayLen(a: []u32) -> usize {
110111 a.len
111112}
112113
113114#attribute("test")
114fn short_circuit() {
115fn shortCircuit() {
115116 var hit_1 = false;
116117 var hit_2 = false;
117118 var hit_3 = false;
118119 var hit_4 = false;
119120
120 if (true || {assert_runtime(false); false}) {
121 if (true || {assertRuntime(false); false}) {
121122 hit_1 = true;
122123 }
123124 if (false || { hit_2 = true; false }) {
124 assert_runtime(false);
125 assertRuntime(false);
125126 }
126127
127128 if (true && { hit_3 = true; false }) {
128 assert_runtime(false);
129 assertRuntime(false);
129130 }
130 if (false && {assert_runtime(false); false}) {
131 assert_runtime(false);
131 if (false && {assertRuntime(false); false}) {
132 assertRuntime(false);
132133 } else {
133134 hit_4 = true;
134135 }
......@@ -139,12 +140,12 @@ fn short_circuit() {
139140}
140141
141142#static_eval_enable(false)
142fn assert_runtime(b: bool) {
143fn assertRuntime(b: bool) {
143144 if (!b) unreachable{}
144145}
145146
146147#attribute("test")
147fn modify_operators() {
148fn modifyOperators() {
148149 var i : i32 = 0;
149150 i += 5; assert(i == 5);
150151 i -= 2; assert(i == 3);
......@@ -162,7 +163,7 @@ fn modify_operators() {
162163
163164
164165#attribute("test")
165fn separate_block_scopes() {
166fn separateBlockScopes() {
166167 {
167168 const no_conflict : i32 = 5;
168169 assert(no_conflict == 5);
......@@ -177,14 +178,14 @@ fn separate_block_scopes() {
177178
178179
179180#attribute("test")
180fn void_struct_fields() {
181fn voidStructFields() {
181182 const foo = VoidStructFieldsFoo {
182183 .a = void{},
183184 .b = 1,
184185 .c = void{},
185186 };
186187 assert(foo.b == 1);
187 assert(@sizeof(VoidStructFieldsFoo) == 4);
188 assert(@sizeOf(VoidStructFieldsFoo) == 4);
188189}
189190struct VoidStructFieldsFoo {
190191 a : void,
......@@ -197,11 +198,11 @@ struct VoidStructFieldsFoo {
197198#attribute("test")
198199pub fn structs() {
199200 var foo : StructFoo = undefined;
200 @memset(&foo, 0, @sizeof(StructFoo));
201 @memset(&foo, 0, @sizeOf(StructFoo));
201202 foo.a += 1;
202203 foo.b = foo.a == 1;
203 test_foo(foo);
204 test_mutation(&foo);
204 testFoo(foo);
205 testMutation(&foo);
205206 assert(foo.c == 100);
206207}
207208struct StructFoo {
......@@ -209,10 +210,10 @@ struct StructFoo {
209210 b : bool,
210211 c : f32,
211212}
212fn test_foo(foo : StructFoo) {
213fn testFoo(foo : StructFoo) {
213214 assert(foo.b);
214215}
215fn test_mutation(foo : &StructFoo) {
216fn testMutation(foo : &StructFoo) {
216217 foo.c = 100;
217218}
218219struct Node {
......@@ -225,7 +226,7 @@ struct Val {
225226}
226227
227228#attribute("test")
228fn struct_point_to_self() {
229fn structPointToSelf() {
229230 var root : Node = undefined;
230231 root.val.x = 1;
231232
......@@ -239,7 +240,7 @@ fn struct_point_to_self() {
239240}
240241
241242#attribute("test")
242fn struct_byval_assign() {
243fn structByvalAssign() {
243244 var foo1 : StructFoo = undefined;
244245 var foo2 : StructFoo = undefined;
245246
......@@ -250,7 +251,7 @@ fn struct_byval_assign() {
250251 assert(foo2.a == 1234);
251252}
252253
253fn struct_initializer() {
254fn structInitializer() {
254255 const val = Val { .x = 42 };
255256 assert(val.x == 42);
256257}
......@@ -260,7 +261,7 @@ const g1 : i32 = 1233 + 1;
260261var g2 : i32 = 0;
261262
262263#attribute("test")
263fn global_variables() {
264fn globalVariables() {
264265 assert(g2 == 0);
265266 g2 = g1;
266267 assert(g2 == 1234);
......@@ -268,55 +269,55 @@ fn global_variables() {
268269
269270
270271#attribute("test")
271fn while_loop() {
272fn whileLoop() {
272273 var i : i32 = 0;
273274 while (i < 4) {
274275 i += 1;
275276 }
276277 assert(i == 4);
277 assert(while_loop_1() == 1);
278 assert(whileLoop1() == 1);
278279}
279fn while_loop_1() -> i32 {
280 return while_loop_2();
280fn whileLoop1() -> i32 {
281 return whileLoop2();
281282}
282fn while_loop_2() -> i32 {
283fn whileLoop2() -> i32 {
283284 while (true) {
284285 return 1;
285286 }
286287}
287288
288289#attribute("test")
289fn void_arrays() {
290fn voidArrays() {
290291 var array: [4]void = undefined;
291292 array[0] = void{};
292293 array[1] = array[2];
293 assert(@sizeof(@typeof(array)) == 0);
294 assert(@sizeOf(@typeOf(array)) == 0);
294295 assert(array.len == 4);
295296}
296297
297298
298299#attribute("test")
299fn three_expr_in_a_row() {
300 assert_false(false || false || false);
301 assert_false(true && true && false);
302 assert_false(1 | 2 | 4 != 7);
303 assert_false(3 ^ 6 ^ 8 != 13);
304 assert_false(7 & 14 & 28 != 4);
305 assert_false(9 << 1 << 2 != 9 << 3);
306 assert_false(90 >> 1 >> 2 != 90 >> 3);
307 assert_false(100 - 1 + 1000 != 1099);
308 assert_false(5 * 4 / 2 % 3 != 1);
309 assert_false(i32(i32(5)) != 5);
310 assert_false(!!false);
311 assert_false(i32(7) != --(i32(7)));
300fn threeExprInARow() {
301 assertFalse(false || false || false);
302 assertFalse(true && true && false);
303 assertFalse(1 | 2 | 4 != 7);
304 assertFalse(3 ^ 6 ^ 8 != 13);
305 assertFalse(7 & 14 & 28 != 4);
306 assertFalse(9 << 1 << 2 != 9 << 3);
307 assertFalse(90 >> 1 >> 2 != 90 >> 3);
308 assertFalse(100 - 1 + 1000 != 1099);
309 assertFalse(5 * 4 / 2 % 3 != 1);
310 assertFalse(i32(i32(5)) != 5);
311 assertFalse(!!false);
312 assertFalse(i32(7) != --(i32(7)));
312313}
313fn assert_false(b: bool) {
314fn assertFalse(b: bool) {
314315 assert(!b);
315316}
316317
317318
318319#attribute("test")
319fn maybe_type() {
320fn maybeType() {
320321 const x : ?bool = true;
321322
322323 if (const y ?= x) {
......@@ -344,21 +345,21 @@ fn maybe_type() {
344345
345346
346347#attribute("test")
347fn enum_type() {
348fn enumType() {
348349 const foo1 = EnumTypeFoo.One {13};
349350 const foo2 = EnumTypeFoo.Two {EnumType { .x = 1234, .y = 5678, }};
350351 const bar = EnumTypeBar.B;
351352
352353 assert(bar == EnumTypeBar.B);
353 assert(@member_count(EnumTypeFoo) == 3);
354 assert(@member_count(EnumTypeBar) == 4);
355 const expected_foo_size = switch (@compile_var("arch")) {
354 assert(@memberCount(EnumTypeFoo) == 3);
355 assert(@memberCount(EnumTypeBar) == 4);
356 const expected_foo_size = switch (@compileVar("arch")) {
356357 i386 => 20,
357358 x86_64 => 24,
358359 else => unreachable{},
359360 };
360 assert(@sizeof(EnumTypeFoo) == expected_foo_size);
361 assert(@sizeof(EnumTypeBar) == 1);
361 assert(@sizeOf(EnumTypeFoo) == expected_foo_size);
362 assert(@sizeOf(EnumTypeBar) == 1);
362363}
363364struct EnumType {
364365 x: u64,
......@@ -378,16 +379,16 @@ enum EnumTypeBar {
378379
379380
380381#attribute("test")
381fn array_literal() {
382 const HEX_MULT = []u16{4096, 256, 16, 1};
382fn arrayLiteral() {
383 const hex_mult = []u16{4096, 256, 16, 1};
383384
384 assert(HEX_MULT.len == 4);
385 assert(HEX_MULT[1] == 256);
385 assert(hex_mult.len == 4);
386 assert(hex_mult[1] == 256);
386387}
387388
388389
389390#attribute("test")
390fn const_number_literal() {
391fn constNumberLiteral() {
391392 const one = 1;
392393 const eleven = ten + one;
393394
......@@ -397,7 +398,7 @@ const ten = 10;
397398
398399
399400#attribute("test")
400fn error_values() {
401fn errorValues() {
401402 const a = i32(error.err1);
402403 const b = i32(error.err2);
403404 assert(a != b);
......@@ -408,30 +409,30 @@ error err2;
408409
409410
410411#attribute("test")
411fn fn_call_of_struct_field() {
412 assert(call_struct_field(Foo {.ptr = a_func,}) == 13);
412fn fnCallOfStructField() {
413 assert(callStructField(Foo {.ptr = aFunc,}) == 13);
413414}
414415
415416struct Foo {
416417 ptr: fn() -> i32,
417418}
418419
419fn a_func() -> i32 { 13 }
420fn aFunc() -> i32 { 13 }
420421
421fn call_struct_field(foo: Foo) -> i32 {
422fn callStructField(foo: Foo) -> i32 {
422423 return foo.ptr();
423424}
424425
425426
426427
427428#attribute("test")
428fn redefinition_of_error_values_allowed() {
429 should_be_not_equal(error.AnError, error.SecondError);
429fn redefinitionOfErrorValuesAllowed() {
430 shouldBeNotEqual(error.AnError, error.SecondError);
430431}
431432error AnError;
432433error AnError;
433434error SecondError;
434fn should_be_not_equal(a: error, b: error) {
435fn shouldBeNotEqual(a: error, b: error) {
435436 if (a == b) unreachable{}
436437}
437438
......@@ -439,21 +440,21 @@ fn should_be_not_equal(a: error, b: error) {
439440
440441
441442#attribute("test")
442fn constant_enum_with_payload() {
443fn constantEnumWithPayload() {
443444 var empty = AnEnumWithPayload.Empty;
444445 var full = AnEnumWithPayload.Full {13};
445 should_be_empty(empty);
446 should_be_not_empty(full);
446 shouldBeEmpty(empty);
447 shouldBeNotEmpty(full);
447448}
448449
449fn should_be_empty(x: AnEnumWithPayload) {
450fn shouldBeEmpty(x: AnEnumWithPayload) {
450451 switch (x) {
451452 Empty => {},
452453 else => unreachable{},
453454 }
454455}
455456
456fn should_be_not_empty(x: AnEnumWithPayload) {
457fn shouldBeNotEmpty(x: AnEnumWithPayload) {
457458 switch (x) {
458459 Empty => unreachable{},
459460 else => {},
......@@ -467,7 +468,7 @@ enum AnEnumWithPayload {
467468
468469
469470#attribute("test")
470fn continue_in_for_loop() {
471fn continueInForLoop() {
471472 const array = []i32 {1, 2, 3, 4, 5};
472473 var sum : i32 = 0;
473474 for (array) |x| {
......@@ -482,24 +483,24 @@ fn continue_in_for_loop() {
482483
483484
484485#attribute("test")
485fn cast_bool_to_int() {
486fn castBoolToInt() {
486487 const t = true;
487488 const f = false;
488489 assert(i32(t) == i32(1));
489490 assert(i32(f) == i32(0));
490 non_const_cast_bool_to_int(t, f);
491 nonConstCastBoolToInt(t, f);
491492}
492493
493fn non_const_cast_bool_to_int(t: bool, f: bool) {
494fn nonConstCastBoolToInt(t: bool, f: bool) {
494495 assert(i32(t) == i32(1));
495496 assert(i32(f) == i32(0));
496497}
497498
498499
499500#attribute("test")
500fn switch_on_enum() {
501fn switchOnEnum() {
501502 const fruit = Fruit.Orange;
502 non_const_switch_on_enum(fruit);
503 nonConstSwitchOnEnum(fruit);
503504}
504505enum Fruit {
505506 Apple,
......@@ -507,7 +508,7 @@ enum Fruit {
507508 Banana,
508509}
509510#static_eval_enable(false)
510fn non_const_switch_on_enum(fruit: Fruit) {
511fn nonConstSwitchOnEnum(fruit: Fruit) {
511512 switch (fruit) {
512513 Apple => unreachable{},
513514 Orange => {},
......@@ -516,11 +517,11 @@ fn non_const_switch_on_enum(fruit: Fruit) {
516517}
517518
518519#attribute("test")
519fn switch_statement() {
520 non_const_switch(SwitchStatmentFoo.C);
520fn switchStatement() {
521 nonConstSwitch(SwitchStatmentFoo.C);
521522}
522523#static_eval_enable(false)
523fn non_const_switch(foo: SwitchStatmentFoo) {
524fn nonConstSwitch(foo: SwitchStatmentFoo) {
524525 const val: i32 = switch (foo) {
525526 A => 1,
526527 B => 2,
......@@ -538,10 +539,10 @@ enum SwitchStatmentFoo {
538539
539540
540541#attribute("test")
541fn switch_prong_with_var() {
542 switch_prong_with_var_fn(SwitchProngWithVarEnum.One {13});
543 switch_prong_with_var_fn(SwitchProngWithVarEnum.Two {13.0});
544 switch_prong_with_var_fn(SwitchProngWithVarEnum.Meh);
542fn switchProngWithVar() {
543 switchProngWithVarFn(SwitchProngWithVarEnum.One {13});
544 switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0});
545 switchProngWithVarFn(SwitchProngWithVarEnum.Meh);
545546}
546547enum SwitchProngWithVarEnum {
547548 One: i32,
......@@ -549,7 +550,7 @@ enum SwitchProngWithVarEnum {
549550 Meh,
550551}
551552#static_eval_enable(false)
552fn switch_prong_with_var_fn(a: SwitchProngWithVarEnum) {
553fn switchProngWithVarFn(a: SwitchProngWithVarEnum) {
553554 switch(a) {
554555 One => |x| {
555556 if (x != 13) unreachable{};
......@@ -565,54 +566,54 @@ fn switch_prong_with_var_fn(a: SwitchProngWithVarEnum) {
565566
566567
567568#attribute("test")
568fn err_return_in_assignment() {
569 %%do_err_return_in_assignment();
569fn errReturnInAssignment() {
570 %%doErrReturnInAssignment();
570571}
571572
572573#static_eval_enable(false)
573fn do_err_return_in_assignment() -> %void {
574fn doErrReturnInAssignment() -> %void {
574575 var x : i32 = undefined;
575 x = %return make_a_non_err();
576 x = %return makeANonErr();
576577}
577578
578fn make_a_non_err() -> %i32 {
579fn makeANonErr() -> %i32 {
579580 return 1;
580581}
581582
582583
583584
584585#attribute("test")
585fn rhs_maybe_unwrap_return() {
586fn rhsMaybeUnwrapReturn() {
586587 const x = ?true;
587588 const y = x ?? return;
588589}
589590
590591
591592#attribute("test")
592fn implicit_cast_fn_unreachable_return() {
593 wants_fn_with_void(fn_with_unreachable);
593fn implicitCastFnUnreachableReturn() {
594 wantsFnWithVoid(fnWithUnreachable);
594595}
595596
596fn wants_fn_with_void(f: fn()) { }
597fn wantsFnWithVoid(f: fn()) { }
597598
598fn fn_with_unreachable() -> unreachable {
599fn fnWithUnreachable() -> unreachable {
599600 unreachable {}
600601}
601602
602603
603604#attribute("test")
604fn explicit_cast_maybe_pointers() {
605fn explicitCastMaybePointers() {
605606 const a: ?&i32 = undefined;
606607 const b: ?&f32 = (?&f32)(a);
607608}
608609
609610
610611#attribute("test")
611fn const_expr_eval_on_single_expr_blocks() {
612 assert(const_expr_eval_on_single_expr_blocks_fn(1, true) == 3);
612fn constExprEvalOnSingleExprBlocks() {
613 assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
613614}
614615
615fn const_expr_eval_on_single_expr_blocks_fn(x: i32, b: bool) -> i32 {
616fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 {
616617 const literal = 3;
617618
618619 const result = if (b) {
......@@ -626,9 +627,9 @@ fn const_expr_eval_on_single_expr_blocks_fn(x: i32, b: bool) -> i32 {
626627
627628
628629#attribute("test")
629fn builtin_const_eval() {
630 const x : i32 = @const_eval(1 + 2 + 3);
631 assert(x == @const_eval(6));
630fn builtinConstEval() {
631 const x : i32 = @constEval(1 + 2 + 3);
632 assert(x == @constEval(6));
632633}
633634
634635#attribute("test")
......@@ -650,7 +651,7 @@ fn slicing() {
650651
651652
652653#attribute("test")
653fn memcpy_and_memset_intrinsics() {
654fn memcpyAndMemsetIntrinsics() {
654655 var foo : [20]u8 = undefined;
655656 var bar : [20]u8 = undefined;
656657
......@@ -662,22 +663,22 @@ fn memcpy_and_memset_intrinsics() {
662663
663664
664665#attribute("test")
665fn array_dot_len_const_expr() { }
666fn arrayDotLenConstExpr() { }
666667struct ArrayDotLenConstExpr {
667 y: [@const_eval(some_array.len)]u8,
668 y: [@constEval(some_array.len)]u8,
668669}
669670const some_array = []u8 {0, 1, 2, 3};
670671
671672
672673#attribute("test")
673fn count_leading_zeroes() {
674fn countLeadingZeroes() {
674675 assert(@clz(u8, 0b00001010) == 4);
675676 assert(@clz(u8, 0b10001010) == 0);
676677 assert(@clz(u8, 0b00000000) == 8);
677678}
678679
679680#attribute("test")
680fn count_trailing_zeroes() {
681fn countTrailingZeroes() {
681682 assert(@ctz(u8, 0b10100000) == 5);
682683 assert(@ctz(u8, 0b10001010) == 1);
683684 assert(@ctz(u8, 0b00000000) == 8);
......@@ -685,7 +686,7 @@ fn count_trailing_zeroes() {
685686
686687
687688#attribute("test")
688fn multiline_string() {
689fn multilineString() {
689690 const s1 =
690691 \\one
691692 \\two)
......@@ -696,7 +697,7 @@ fn multiline_string() {
696697}
697698
698699#attribute("test")
699fn multiline_c_string() {
700fn multilineCString() {
700701 const s1 =
701702 c\\one
702703 c\\two)
......@@ -709,7 +710,7 @@ fn multiline_c_string() {
709710
710711
711712#attribute("test")
712fn simple_generic_fn() {
713fn simpleGenericFn() {
713714 assert(max(i32, 3, -1) == 3);
714715 assert(max(f32, 0.123, 0.456) == 0.456);
715716 assert(add(2, 3) == 5);
......@@ -720,42 +721,42 @@ fn max(inline T: type, a: T, b: T) -> T {
720721}
721722
722723fn add(inline a: i32, b: i32) -> i32 {
723 return @const_eval(a) + b;
724 return @constEval(a) + b;
724725}
725726
726727
727728#attribute("test")
728fn constant_equal_function_pointers() {
729 const alias = empty_fn;
730 assert(@const_eval(empty_fn == alias));
729fn constantEqualFunctionPointers() {
730 const alias = emptyFn;
731 assert(@constEval(emptyFn == alias));
731732}
732733
733fn empty_fn() {}
734fn emptyFn() {}
734735
735736
736737#attribute("test")
737fn generic_malloc_free() {
738 const a = %%mem_alloc(u8, 10);
739 mem_free(u8, a);
738fn genericMallocFree() {
739 const a = %%memAlloc(u8, 10);
740 memFree(u8, a);
740741}
741742const some_mem : [100]u8 = undefined;
742743#static_eval_enable(false)
743fn mem_alloc(inline T: type, n: usize) -> %[]T {
744fn memAlloc(inline T: type, n: usize) -> %[]T {
744745 return (&T)(&some_mem[0])[0...n];
745746}
746fn mem_free(inline T: type, mem: []T) { }
747fn memFree(inline T: type, mem: []T) { }
747748
748749
749750#attribute("test")
750fn call_fn_with_empty_string() {
751 accepts_string("");
751fn callFnWithEmptyString() {
752 acceptsString("");
752753}
753754
754fn accepts_string(foo: []u8) { }
755fn acceptsString(foo: []u8) { }
755756
756757
757758#attribute("test")
758fn hex_escape() {
759fn hexEscape() {
759760 assert(str.eql("\x68\x65\x6c\x6c\x6f", "hello"));
760761}
761762
......@@ -763,18 +764,18 @@ fn hex_escape() {
763764error AnError;
764765error ALongerErrorName;
765766#attribute("test")
766fn error_name_string() {
767 assert(str.eql(@err_name(error.AnError), "AnError"));
768 assert(str.eql(@err_name(error.ALongerErrorName), "ALongerErrorName"));
767fn errorNameString() {
768 assert(str.eql(@errName(error.AnError), "AnError"));
769 assert(str.eql(@errName(error.ALongerErrorName), "ALongerErrorName"));
769770}
770771
771772
772773#attribute("test")
773fn goto_and_labels() {
774 goto_loop();
774fn gotoAndLabels() {
775 gotoLoop();
775776 assert(goto_counter == 10);
776777}
777fn goto_loop() {
778fn gotoLoop() {
778779 var i: i32 = 0;
779780 goto cond;
780781loop:
......@@ -790,11 +791,11 @@ var goto_counter: i32 = 0;
790791
791792
792793#attribute("test")
793fn goto_leave_defer_scope() {
794 test_goto_leave_defer_scope(true);
794fn gotoLeaveDeferScope() {
795 testGotoLeaveDeferScope(true);
795796}
796797#static_eval_enable(false)
797fn test_goto_leave_defer_scope(b: bool) {
798fn testGotoLeaveDeferScope(b: bool) {
798799 var it_worked = false;
799800
800801 goto entry;
......@@ -810,25 +811,25 @@ entry:
810811
811812
812813#attribute("test")
813fn cast_undefined() {
814fn castUndefined() {
814815 const array: [100]u8 = undefined;
815816 const slice = ([]u8)(array);
816 test_cast_undefined(slice);
817 testCastUndefined(slice);
817818}
818fn test_cast_undefined(x: []u8) {}
819fn testCastUndefined(x: []u8) {}
819820
820821
821822#attribute("test")
822fn cast_small_unsigned_to_larger_signed() {
823 assert(cast_small_unsigned_to_larger_signed_1(200) == i16(200));
824 assert(cast_small_unsigned_to_larger_signed_2(9999) == i64(9999));
823fn castSmallUnsignedToLargerSigned() {
824 assert(castSmallUnsignedToLargerSigned1(200) == i16(200));
825 assert(castSmallUnsignedToLargerSigned2(9999) == i64(9999));
825826}
826fn cast_small_unsigned_to_larger_signed_1(x: u8) -> i16 { x }
827fn cast_small_unsigned_to_larger_signed_2(x: u16) -> i64 { x }
827fn castSmallUnsignedToLargerSigned1(x: u8) -> i16 { x }
828fn castSmallUnsignedToLargerSigned2(x: u16) -> i64 { x }
828829
829830
830831#attribute("test")
831fn implicit_cast_after_unreachable() {
832fn implicitCastAfterUnreachable() {
832833 assert(outer() == 1234);
833834}
834835fn inner() -> i32 { 1234 }
......@@ -838,10 +839,10 @@ fn outer() -> i64 {
838839
839840
840841#attribute("test")
841fn else_if_expression() {
842 assert(else_if_expression_f(1) == 1);
842fn elseIfExpression() {
843 assert(elseIfExpressionF(1) == 1);
843844}
844fn else_if_expression_f(c: u8) -> u8 {
845fn elseIfExpressionF(c: u8) -> u8 {
845846 if (c == 0) {
846847 0
847848 } else if (c == 1) {
......@@ -852,14 +853,14 @@ fn else_if_expression_f(c: u8) -> u8 {
852853}
853854
854855#attribute("test")
855fn err_binary_operator() {
856 const a = err_binary_operator_g(true) %% 3;
857 const b = err_binary_operator_g(false) %% 3;
856fn errBinaryOperator() {
857 const a = errBinaryOperatorG(true) %% 3;
858 const b = errBinaryOperatorG(false) %% 3;
858859 assert(a == 3);
859860 assert(b == 10);
860861}
861862error ItBroke;
862fn err_binary_operator_g(x: bool) -> %isize {
863fn errBinaryOperatorG(x: bool) -> %isize {
863864 if (x) {
864865 error.ItBroke
865866 } else {
......@@ -868,18 +869,18 @@ fn err_binary_operator_g(x: bool) -> %isize {
868869}
869870
870871#attribute("test")
871fn unwrap_simple_value_from_error() {
872 const i = %%unwrap_simple_value_from_error_do();
872fn unwrapSimpleValueFromError() {
873 const i = %%unwrapSimpleValueFromErrorDo();
873874 assert(i == 13);
874875}
875fn unwrap_simple_value_from_error_do() -> %isize { 13 }
876fn unwrapSimpleValueFromErrorDo() -> %isize { 13 }
876877
877878
878879#attribute("test")
879fn store_member_function_in_variable() {
880fn storeMemberFunctionInVariable() {
880881 const instance = MemberFnTestFoo { .x = 1234, };
881 const member_fn = MemberFnTestFoo.member;
882 const result = member_fn(instance);
882 const memberFn = MemberFnTestFoo.member;
883 const result = memberFn(instance);
883884 assert(result == 1234);
884885}
885886struct MemberFnTestFoo {
......@@ -888,34 +889,34 @@ struct MemberFnTestFoo {
888889}
889890
890891#attribute("test")
891fn call_member_function_directly() {
892fn callMemberFunctionDirectly() {
892893 const instance = MemberFnTestFoo { .x = 1234, };
893894 const result = MemberFnTestFoo.member(instance);
894895 assert(result == 1234);
895896}
896897
897898#attribute("test")
898fn member_functions() {
899fn memberFunctions() {
899900 const r = MemberFnRand {.seed = 1234};
900 assert(r.get_seed() == 1234);
901 assert(r.getSeed() == 1234);
901902}
902903struct MemberFnRand {
903904 seed: u32,
904 pub fn get_seed(r: MemberFnRand) -> u32 {
905 pub fn getSeed(r: MemberFnRand) -> u32 {
905906 r.seed
906907 }
907908}
908909
909910#attribute("test")
910fn static_function_evaluation() {
911fn staticFunctionEvaluation() {
911912 assert(statically_added_number == 3);
912913}
913const statically_added_number = static_add(1, 2);
914fn static_add(a: i32, b: i32) -> i32 { a + b }
914const statically_added_number = staticAdd(1, 2);
915fn staticAdd(a: i32, b: i32) -> i32 { a + b }
915916
916917
917918#attribute("test")
918fn statically_initalized_list() {
919fn staticallyInitalizedList() {
919920 assert(static_point_list[0].x == 1);
920921 assert(static_point_list[0].y == 2);
921922 assert(static_point_list[1].x == 3);
......@@ -925,8 +926,8 @@ struct Point {
925926 x: i32,
926927 y: i32,
927928}
928const static_point_list = []Point { make_point(1, 2), make_point(3, 4) };
929fn make_point(x: i32, y: i32) -> Point {
929const static_point_list = []Point { makePoint(1, 2), makePoint(3, 4) };
930fn makePoint(x: i32, y: i32) -> Point {
930931 return Point {
931932 .x = x,
932933 .y = y,
......@@ -935,7 +936,7 @@ fn make_point(x: i32, y: i32) -> Point {
935936
936937
937938#attribute("test")
938fn static_eval_recursive() {
939fn staticEvalRecursive() {
939940 assert(seventh_fib_number == 21);
940941}
941942const seventh_fib_number = fibbonaci(7);
......@@ -945,21 +946,21 @@ fn fibbonaci(x: i32) -> i32 {
945946}
946947
947948#attribute("test")
948fn static_eval_while() {
949fn staticEvalWhile() {
949950 assert(static_eval_while_number == 1);
950951}
951const static_eval_while_number = static_while_loop_1();
952fn static_while_loop_1() -> i32 {
953 return while_loop_2();
952const static_eval_while_number = staticWhileLoop1();
953fn staticWhileLoop1() -> i32 {
954 return whileLoop2();
954955}
955fn static_while_loop_2() -> i32 {
956fn staticWhileLoop2() -> i32 {
956957 while (true) {
957958 return 1;
958959 }
959960}
960961
961962#attribute("test")
962fn static_eval_list_init() {
963fn staticEvalListInit() {
963964 assert(static_vec3.data[2] == 1.0);
964965}
965966const static_vec3 = vec3(0.0, 0.0, 1.0);
......@@ -974,22 +975,22 @@ pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
974975
975976
976977#attribute("test")
977fn generic_fn_with_implicit_cast() {
978 assert(get_first_byte(u8, []u8 {13}) == 13);
979 assert(get_first_byte(u16, []u16 {0, 13}) == 0);
978fn genericFnWithImplicitCast() {
979 assert(getFirstByte(u8, []u8 {13}) == 13);
980 assert(getFirstByte(u16, []u16 {0, 13}) == 0);
980981}
981fn get_byte(ptr: ?&u8) -> u8 {*??ptr}
982fn get_first_byte(inline T: type, mem: []T) -> u8 {
983 get_byte((&u8)(&mem[0]))
982fn getByte(ptr: ?&u8) -> u8 {*??ptr}
983fn getFirstByte(inline T: type, mem: []T) -> u8 {
984 getByte((&u8)(&mem[0]))
984985}
985986
986987#attribute("test")
987fn continue_and_break() {
988 run_continue_and_break_test();
988fn continueAndBreak() {
989 runContinueAndBreakTest();
989990 assert(continue_and_break_counter == 8);
990991}
991992var continue_and_break_counter: i32 = 0;
992fn run_continue_and_break_test() {
993fn runContinueAndBreakTest() {
993994 var i : i32 = 0;
994995 while (true) {
995996 continue_and_break_counter += 2;
......@@ -1002,17 +1003,9 @@ fn run_continue_and_break_test() {
10021003 assert(i == 4);
10031004}
10041005
1005#attribute("test")
1006fn sizeof_and_typeof() {
1007 const y: @typeof(sizeof_and_typeof_x) = 120;
1008 assert(@sizeof(@typeof(y)) == 2);
1009}
1010const sizeof_and_typeof_x: u16 = 13;
1011const sizeof_and_typeof_z: @typeof(sizeof_and_typeof_x) = 19;
1012
10131006
10141007#attribute("test")
1015fn pointer_dereferencing() {
1008fn pointerDereferencing() {
10161009 var x = i32(3);
10171010 const y = &x;
10181011
......@@ -1023,47 +1016,47 @@ fn pointer_dereferencing() {
10231016}
10241017
10251018#attribute("test")
1026fn constant_expressions() {
1027 var array : [ARRAY_SIZE]u8 = undefined;
1028 assert(@sizeof(@typeof(array)) == 20);
1019fn constantExpressions() {
1020 var array : [array_size]u8 = undefined;
1021 assert(@sizeOf(@typeOf(array)) == 20);
10291022}
1030const ARRAY_SIZE : u8 = 20;
1023const array_size : u8 = 20;
10311024
10321025
10331026#attribute("test")
1034fn min_value_and_max_value() {
1035 assert(@max_value(u8) == 255);
1036 assert(@max_value(u16) == 65535);
1037 assert(@max_value(u32) == 4294967295);
1038 assert(@max_value(u64) == 18446744073709551615);
1027fn minValueAndMaxValue() {
1028 assert(@maxValue(u8) == 255);
1029 assert(@maxValue(u16) == 65535);
1030 assert(@maxValue(u32) == 4294967295);
1031 assert(@maxValue(u64) == 18446744073709551615);
10391032
1040 assert(@max_value(i8) == 127);
1041 assert(@max_value(i16) == 32767);
1042 assert(@max_value(i32) == 2147483647);
1043 assert(@max_value(i64) == 9223372036854775807);
1033 assert(@maxValue(i8) == 127);
1034 assert(@maxValue(i16) == 32767);
1035 assert(@maxValue(i32) == 2147483647);
1036 assert(@maxValue(i64) == 9223372036854775807);
10441037
1045 assert(@min_value(u8) == 0);
1046 assert(@min_value(u16) == 0);
1047 assert(@min_value(u32) == 0);
1048 assert(@min_value(u64) == 0);
1038 assert(@minValue(u8) == 0);
1039 assert(@minValue(u16) == 0);
1040 assert(@minValue(u32) == 0);
1041 assert(@minValue(u64) == 0);
10491042
1050 assert(@min_value(i8) == -128);
1051 assert(@min_value(i16) == -32768);
1052 assert(@min_value(i32) == -2147483648);
1053 assert(@min_value(i64) == -9223372036854775808);
1043 assert(@minValue(i8) == -128);
1044 assert(@minValue(i16) == -32768);
1045 assert(@minValue(i32) == -2147483648);
1046 assert(@minValue(i64) == -9223372036854775808);
10541047}
10551048
10561049#attribute("test")
1057fn overflow_intrinsics() {
1050fn overflowIntrinsics() {
10581051 var result: u8 = undefined;
1059 assert(@add_with_overflow(u8, 250, 100, &result));
1060 assert(!@add_with_overflow(u8, 100, 150, &result));
1052 assert(@addWithOverflow(u8, 250, 100, &result));
1053 assert(!@addWithOverflow(u8, 100, 150, &result));
10611054 assert(result == 250);
10621055}
10631056
10641057
10651058#attribute("test")
1066fn nested_arrays() {
1059fn nestedArrays() {
10671060 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
10681061 for (array_of_strings) |s, i| {
10691062 if (i == 0) assert(str.eql(s, "hello"));
......@@ -1075,7 +1068,7 @@ fn nested_arrays() {
10751068}
10761069
10771070#attribute("test")
1078fn int_to_ptr_cast() {
1071fn intToPtrCast() {
10791072 const x = isize(13);
10801073 const y = (&u8)(x);
10811074 const z = usize(y);
......@@ -1083,12 +1076,12 @@ fn int_to_ptr_cast() {
10831076}
10841077
10851078#attribute("test")
1086fn string_concatenation() {
1079fn stringConcatenation() {
10871080 assert(str.eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
10881081}
10891082
10901083#attribute("test")
1091fn constant_struct_with_negation() {
1084fn constantStructWithNegation() {
10921085 assert(vertices[0].x == -0.6);
10931086}
10941087struct Vertex {
......@@ -1106,25 +1099,25 @@ const vertices = []Vertex {
11061099
11071100
11081101#attribute("test")
1109fn return_with_implicit_cast_from_while_loop() {
1110 %%return_with_implicit_cast_from_while_loop_test();
1102fn returnWithImplicitCastFromWhileLoop() {
1103 %%returnWithImplicitCastFromWhileLoopTest();
11111104}
1112fn return_with_implicit_cast_from_while_loop_test() -> %void {
1105fn returnWithImplicitCastFromWhileLoopTest() -> %void {
11131106 while (true) {
11141107 return;
11151108 }
11161109}
11171110
11181111#attribute("test")
1119fn return_struct_byval_from_function() {
1120 const bar = make_bar(1234, 5678);
1112fn returnStructByvalFromFunction() {
1113 const bar = makeBar(1234, 5678);
11211114 assert(bar.y == 5678);
11221115}
11231116struct Bar {
11241117 x: i32,
11251118 y: i32,
11261119}
1127fn make_bar(x: i32, y: i32) -> Bar {
1120fn makeBar(x: i32, y: i32) -> Bar {
11281121 Bar {
11291122 .x = x,
11301123 .y = y,
......@@ -1132,8 +1125,8 @@ fn make_bar(x: i32, y: i32) -> Bar {
11321125}
11331126
11341127#attribute("test")
1135fn function_pointers() {
1136 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
1128fn functionPointers() {
1129 const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, };
11371130 for (fns) |f, i| {
11381131 assert(f() == u32(i) + 5);
11391132 }
......@@ -1146,7 +1139,7 @@ fn fn4() -> u32 {8}
11461139
11471140
11481141#attribute("test")
1149fn statically_initalized_struct() {
1142fn staticallyInitalizedStruct() {
11501143 st_init_str_foo.x += 1;
11511144 assert(st_init_str_foo.x == 14);
11521145}
......@@ -1157,7 +1150,7 @@ struct StInitStrFoo {
11571150var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
11581151
11591152#attribute("test")
1160fn statically_initialized_array_literal() {
1153fn staticallyInitializedArrayLiteral() {
11611154 const y : [4]u8 = st_init_arr_lit_x;
11621155 assert(y[3] == 4);
11631156}
......@@ -1166,33 +1159,33 @@ const st_init_arr_lit_x = []u8{1,2,3,4};
11661159
11671160
11681161#attribute("test")
1169fn pointer_to_void_return_type() {
1170 %%test_pointer_to_void_return_type();
1162fn pointerToVoidReturnType() {
1163 %%testPointerToVoidReturnType();
11711164}
1172fn test_pointer_to_void_return_type() -> %void {
1173 const a = test_pointer_to_void_return_type_2();
1165fn testPointerToVoidReturnType() -> %void {
1166 const a = testPointerToVoidReturnType2();
11741167 return *a;
11751168}
11761169const test_pointer_to_void_return_type_x = void{};
1177fn test_pointer_to_void_return_type_2() -> &void {
1170fn testPointerToVoidReturnType2() -> &void {
11781171 return &test_pointer_to_void_return_type_x;
11791172}
11801173
11811174
11821175#attribute("test")
1183fn call_result_of_if_else_expression() {
1176fn callResultOfIfElseExpression() {
11841177 assert(str.eql(f2(true), "a"));
11851178 assert(str.eql(f2(false), "b"));
11861179}
11871180fn f2(x: bool) -> []u8 {
1188 return (if (x) f_a else f_b)();
1181 return (if (x) fA else fB)();
11891182}
1190fn f_a() -> []u8 { "a" }
1191fn f_b() -> []u8 { "b" }
1183fn fA() -> []u8 { "a" }
1184fn fB() -> []u8 { "b" }
11921185
11931186
11941187#attribute("test")
1195fn const_expression_eval_handling_of_variables() {
1188fn constExpressionEvalHandlingOfVariables() {
11961189 var x = true;
11971190 while (x) {
11981191 x = false;
......@@ -1202,7 +1195,7 @@ fn const_expression_eval_handling_of_variables() {
12021195
12031196
12041197#attribute("test")
1205fn constant_enum_initialization_with_differing_sizes() {
1198fn constantEnumInitializationWithDifferingSizes() {
12061199 test3_1(test3_foo);
12071200 test3_2(test3_bar);
12081201}
......@@ -1240,22 +1233,22 @@ fn test3_2(f: Test3Foo) {
12401233
12411234
12421235#attribute("test")
1243fn pub_enum() {
1244 pub_enum_test(other.APubEnum.Two);
1236fn pubEnum() {
1237 pubEnumTest(other.APubEnum.Two);
12451238}
1246fn pub_enum_test(foo: other.APubEnum) {
1239fn pubEnumTest(foo: other.APubEnum) {
12471240 assert(foo == other.APubEnum.Two);
12481241}
12491242
12501243
12511244#attribute("test")
1252fn cast_with_imported_symbol() {
1245fn castWithImportedSymbol() {
12531246 assert(other.size_t(42) == 42);
12541247}
12551248
12561249
12571250#attribute("test")
1258fn while_with_continue_expr() {
1251fn whileWithContinueExpr() {
12591252 var sum: i32 = 0;
12601253 {var i: i32 = 0; while (i < 10; i += 1) {
12611254 if (i == 5) continue;
......@@ -1266,22 +1259,22 @@ fn while_with_continue_expr() {
12661259
12671260
12681261#attribute("test")
1269fn for_loop_with_pointer_elem_var() {
1262fn forLoopWithPointerElemVar() {
12701263 const source = "abcdefg";
12711264 var target: [source.len]u8 = undefined;
12721265 @memcpy(&target[0], &source[0], source.len);
1273 mangle_string(target);
1266 mangleString(target);
12741267 assert(str.eql(target, "bcdefgh"));
12751268}
12761269#static_eval_enable(false)
1277fn mangle_string(s: []u8) {
1270fn mangleString(s: []u8) {
12781271 for (s) |*c| {
12791272 *c += 1;
12801273 }
12811274}
12821275
12831276#attribute("test")
1284fn empty_struct_method_call() {
1277fn emptyStructMethodCall() {
12851278 const es = EmptyStruct{};
12861279 assert(es.method() == 1234);
12871280}
......@@ -1296,48 +1289,48 @@ fn @"weird function name"() { }
12961289
12971290
12981291#attribute("test")
1299fn return_empty_struct_from_fn() {
1300 test_return_empty_struct_from_fn();
1301 test_return_empty_struct_from_fn_noeval();
1292fn returnEmptyStructFromFn() {
1293 testReturnEmptyStructFromFn();
1294 testReturnEmptyStructFromFnNoeval();
13021295}
13031296struct EmptyStruct2 {}
1304fn test_return_empty_struct_from_fn() -> EmptyStruct2 {
1297fn testReturnEmptyStructFromFn() -> EmptyStruct2 {
13051298 EmptyStruct2 {}
13061299}
13071300#static_eval_enable(false)
1308fn test_return_empty_struct_from_fn_noeval() -> EmptyStruct2 {
1301fn testReturnEmptyStructFromFnNoeval() -> EmptyStruct2 {
13091302 EmptyStruct2 {}
13101303}
13111304
13121305#attribute("test")
1313fn pass_slice_of_empty_struct_to_fn() {
1314 assert(test_pass_slice_of_empty_struct_to_fn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
1306fn passSliceOfEmptyStructToFn() {
1307 assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
13151308}
1316fn test_pass_slice_of_empty_struct_to_fn(slice: []EmptyStruct2) -> usize {
1309fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize {
13171310 slice.len
13181311}
13191312
13201313
13211314#attribute("test")
1322fn pointer_comparison() {
1315fn pointerComparison() {
13231316 const a = ([]u8)("a");
13241317 const b = &a;
1325 assert(ptr_eql(b, b));
1318 assert(ptrEql(b, b));
13261319}
1327fn ptr_eql(a: &[]u8, b: &[]u8) -> bool {
1320fn ptrEql(a: &[]u8, b: &[]u8) -> bool {
13281321 a == b
13291322}
13301323
13311324#attribute("test")
1332fn character_literals() {
1325fn characterLiterals() {
13331326 assert('\'' == single_quote);
13341327}
13351328const single_quote = '\'';
13361329
13371330
13381331#attribute("test")
1339fn switch_with_multiple_expressions() {
1340 const x: i32 = switch (returns_five()) {
1332fn switchWithMultipleExpressions() {
1333 const x: i32 = switch (returnsFive()) {
13411334 1, 2, 3 => 1,
13421335 4, 5, 6 => 2,
13431336 else => 3,
......@@ -1345,12 +1338,12 @@ fn switch_with_multiple_expressions() {
13451338 assert(x == 2);
13461339}
13471340#static_eval_enable(false)
1348fn returns_five() -> i32 { 5 }
1341fn returnsFive() -> i32 { 5 }
13491342
13501343
13511344#attribute("test")
1352fn switch_on_error_union() {
1353 const x = switch (returns_ten()) {
1345fn switchOnErrorUnion() {
1346 const x = switch (returnsTen()) {
13541347 Ok => |val| val + 1,
13551348 ItBroke, NoMem => 1,
13561349 CrappedOut => 2,
......@@ -1361,40 +1354,40 @@ error ItBroke;
13611354error NoMem;
13621355error CrappedOut;
13631356#static_eval_enable(false)
1364fn returns_ten() -> %i32 { 10 }
1357fn returnsTen() -> %i32 { 10 }
13651358
13661359
13671360#attribute("test")
1368fn bool_cmp() {
1369 assert(test_bool_cmp(true, false) == false);
1361fn boolCmp() {
1362 assert(testBoolCmp(true, false) == false);
13701363}
13711364#static_eval_enable(false)
1372fn test_bool_cmp(a: bool, b: bool) -> bool { a == b }
1365fn testBoolCmp(a: bool, b: bool) -> bool { a == b }
13731366
13741367
13751368#attribute("test")
1376fn take_address_of_parameter() {
1377 test_take_address_of_parameter(12.34);
1378 test_take_address_of_parameter_noeval(12.34);
1369fn takeAddressOfParameter() {
1370 testTakeAddressOfParameter(12.34);
1371 testTakeAddressOfParameterNoeval(12.34);
13791372}
1380fn test_take_address_of_parameter(f: f32) {
1373fn testTakeAddressOfParameter(f: f32) {
13811374 const f_ptr = &f;
13821375 assert(*f_ptr == 12.34);
13831376}
13841377#static_eval_enable(false)
1385fn test_take_address_of_parameter_noeval(f: f32) {
1378fn testTakeAddressOfParameterNoeval(f: f32) {
13861379 const f_ptr = &f;
13871380 assert(*f_ptr == 12.34);
13881381}
13891382
13901383
13911384#attribute("test")
1392fn array_mult_operator() {
1385fn arrayMultOperator() {
13931386 assert(str.eql("ab" ** 5, "ababababab"));
13941387}
13951388
13961389#attribute("test")
1397fn string_escapes() {
1390fn stringEscapes() {
13981391 assert(str.eql("\"", "\x22"));
13991392 assert(str.eql("\'", "\x27"));
14001393 assert(str.eql("\n", "\x0a"));
......@@ -1405,11 +1398,11 @@ fn string_escapes() {
14051398}
14061399
14071400#attribute("test")
1408fn if_var_maybe_pointer() {
1409 assert(should_be_a_plus_1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
1401fn ifVarMaybePointer() {
1402 assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
14101403}
14111404#static_eval_enable(false)
1412fn should_be_a_plus_1(p: Particle) -> u64 {
1405fn shouldBeAPlus1(p: Particle) -> u64 {
14131406 var maybe_particle: ?Particle = p;
14141407 if (const *particle ?= maybe_particle) {
14151408 particle.a += 1;
......@@ -1427,7 +1420,7 @@ struct Particle {
14271420}
14281421
14291422#attribute("test")
1430fn assign_to_if_var_ptr() {
1423fn assignToIfVarPtr() {
14311424 var maybe_bool: ?bool = true;
14321425
14331426 if (const *b ?= maybe_bool) {
......@@ -1452,85 +1445,85 @@ fn fence() {
14521445}
14531446
14541447#attribute("test")
1455fn unsigned_wrapping() {
1456 test_unsigned_wrapping_eval(@max_value(u32));
1457 test_unsigned_wrapping_noeval(@max_value(u32));
1448fn unsignedWrapping() {
1449 testUnsignedWrappingEval(@maxValue(u32));
1450 testUnsignedWrappingNoeval(@maxValue(u32));
14581451}
1459fn test_unsigned_wrapping_eval(x: u32) {
1452fn testUnsignedWrappingEval(x: u32) {
14601453 const zero = x +% 1;
14611454 assert(zero == 0);
14621455 const orig = zero -% 1;
1463 assert(orig == @max_value(u32));
1456 assert(orig == @maxValue(u32));
14641457}
14651458#static_eval_enable(false)
1466fn test_unsigned_wrapping_noeval(x: u32) {
1459fn testUnsignedWrappingNoeval(x: u32) {
14671460 const zero = x +% 1;
14681461 assert(zero == 0);
14691462 const orig = zero -% 1;
1470 assert(orig == @max_value(u32));
1463 assert(orig == @maxValue(u32));
14711464}
14721465
14731466#attribute("test")
1474fn signed_wrapping() {
1475 test_signed_wrapping_eval(@max_value(i32));
1476 test_signed_wrapping_noeval(@max_value(i32));
1467fn signedWrapping() {
1468 testSignedWrappingEval(@maxValue(i32));
1469 testSignedWrappingNoeval(@maxValue(i32));
14771470}
1478fn test_signed_wrapping_eval(x: i32) {
1471fn testSignedWrappingEval(x: i32) {
14791472 const min_val = x +% 1;
1480 assert(min_val == @min_value(i32));
1473 assert(min_val == @minValue(i32));
14811474 const max_val = min_val -% 1;
1482 assert(max_val == @max_value(i32));
1475 assert(max_val == @maxValue(i32));
14831476}
14841477#static_eval_enable(false)
1485fn test_signed_wrapping_noeval(x: i32) {
1478fn testSignedWrappingNoeval(x: i32) {
14861479 const min_val = x +% 1;
1487 assert(min_val == @min_value(i32));
1480 assert(min_val == @minValue(i32));
14881481 const max_val = min_val -% 1;
1489 assert(max_val == @max_value(i32));
1482 assert(max_val == @maxValue(i32));
14901483}
14911484
14921485#attribute("test")
1493fn negation_wrapping() {
1494 test_negation_wrapping_eval(@min_value(i16));
1495 test_negation_wrapping_noeval(@min_value(i16));
1486fn negationWrapping() {
1487 testNegationWrappingEval(@minValue(i16));
1488 testNegationWrappingNoeval(@minValue(i16));
14961489}
1497fn test_negation_wrapping_eval(x: i16) {
1490fn testNegationWrappingEval(x: i16) {
14981491 assert(x == -32768);
14991492 const neg = -%x;
15001493 assert(neg == -32768);
15011494}
15021495#static_eval_enable(false)
1503fn test_negation_wrapping_noeval(x: i16) {
1496fn testNegationWrappingNoeval(x: i16) {
15041497 assert(x == -32768);
15051498 const neg = -%x;
15061499 assert(neg == -32768);
15071500}
15081501
15091502#attribute("test")
1510fn shl_wrapping() {
1511 test_shl_wrapping_eval(@max_value(u16));
1512 test_shl_wrapping_noeval(@max_value(u16));
1503fn shlWrapping() {
1504 testShlWrappingEval(@maxValue(u16));
1505 testShlWrappingNoeval(@maxValue(u16));
15131506}
1514fn test_shl_wrapping_eval(x: u16) {
1507fn testShlWrappingEval(x: u16) {
15151508 const shifted = x <<% 1;
15161509 assert(shifted == 65534);
15171510}
15181511#static_eval_enable(false)
1519fn test_shl_wrapping_noeval(x: u16) {
1512fn testShlWrappingNoeval(x: u16) {
15201513 const shifted = x <<% 1;
15211514 assert(shifted == 65534);
15221515}
15231516
15241517#attribute("test")
1525fn shl_with_overflow() {
1518fn shlWithOverflow() {
15261519 var result: u16 = undefined;
1527 assert(@shl_with_overflow(u16, 0b0010111111111111, 3, &result));
1528 assert(!@shl_with_overflow(u16, 0b0010111111111111, 2, &result));
1520 assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
1521 assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
15291522 assert(result == 0b1011111111111100);
15301523}
15311524
15321525#attribute("test")
1533fn c_string_concatenation() {
1526fn cStringConcatenation() {
15341527 const a = c"OK" ++ c" IT " ++ c"WORKED";
15351528 const b = c"OK IT WORKED";
15361529
......@@ -1544,22 +1537,22 @@ fn c_string_concatenation() {
15441537}
15451538
15461539#attribute("test")
1547fn generic_struct() {
1540fn genericStruct() {
15481541 var a1 = GenNode(i32) {.value = 13, .next = null,};
15491542 var b1 = GenNode(bool) {.value = true, .next = null,};
15501543 assert(a1.value == 13);
1551 assert(a1.value == a1.get_val());
1552 assert(b1.get_val());
1544 assert(a1.value == a1.getVal());
1545 assert(b1.getVal());
15531546}
15541547struct GenNode(T: type) {
15551548 value: T,
15561549 next: ?&GenNode(T),
1557 fn get_val(n: &const GenNode(T)) -> T { n.value }
1550 fn getVal(n: &const GenNode(T)) -> T { n.value }
15581551}
15591552
15601553#attribute("test")
1561fn cast_slice_to_u8_slice() {
1562 assert(@sizeof(i32) == 4);
1554fn castSliceToU8Slice() {
1555 assert(@sizeOf(i32) == 4);
15631556 var big_thing_array = []i32{1, 2, 3, 4};
15641557 const big_thing_slice: []i32 = big_thing_array;
15651558 const bytes = ([]u8)(big_thing_slice);
......@@ -1572,14 +1565,14 @@ fn cast_slice_to_u8_slice() {
15721565 const big_thing_again = ([]i32)(bytes);
15731566 assert(big_thing_again[2] == 3);
15741567 big_thing_again[2] = -1;
1575 assert(bytes[8] == @max_value(u8));
1576 assert(bytes[9] == @max_value(u8));
1577 assert(bytes[10] == @max_value(u8));
1578 assert(bytes[11] == @max_value(u8));
1568 assert(bytes[8] == @maxValue(u8));
1569 assert(bytes[9] == @maxValue(u8));
1570 assert(bytes[10] == @maxValue(u8));
1571 assert(bytes[11] == @maxValue(u8));
15791572}
15801573
15811574#attribute("test")
1582fn float_division() {
1575fn floatDivision() {
15831576 assert(fdiv32(12.0, 3.0) == 4.0);
15841577}
15851578#static_eval_enable(false)
......@@ -1588,16 +1581,16 @@ fn fdiv32(a: f32, b: f32) -> f32 {
15881581}
15891582
15901583#attribute("test")
1591fn exact_division() {
1592 assert(div_exact(55, 11) == 5);
1584fn exactDivision() {
1585 assert(divExact(55, 11) == 5);
15931586}
15941587#static_eval_enable(false)
1595fn div_exact(a: u32, b: u32) -> u32 {
1596 @div_exact(a, b)
1588fn divExact(a: u32, b: u32) -> u32 {
1589 @divExact(a, b)
15971590}
15981591
15991592#attribute("test")
1600fn null_literal_outside_function() {
1593fn nullLiteralOutsideFunction() {
16011594 const is_null = if (const _ ?= here_is_a_null_literal.context) false else true;
16021595 assert(is_null);
16031596}
......@@ -1610,15 +1603,15 @@ const here_is_a_null_literal = SillyStruct {
16101603
16111604#attribute("test")
16121605fn truncate() {
1613 assert(test_truncate(0x10fd) == 0xfd);
1606 assert(testTruncate(0x10fd) == 0xfd);
16141607}
16151608#static_eval_enable(false)
1616fn test_truncate(x: u32) -> u8 {
1609fn testTruncate(x: u32) -> u8 {
16171610 @truncate(u8, x)
16181611}
16191612
16201613#attribute("test")
1621fn const_decls_in_struct() {
1614fn constDeclsInStruct() {
16221615 assert(GenericDataThing(3).count_plus_one == 4);
16231616}
16241617struct GenericDataThing(count: isize) {
......@@ -1626,30 +1619,30 @@ struct GenericDataThing(count: isize) {
16261619}
16271620
16281621#attribute("test")
1629fn use_generic_param_in_generic_param() {
1630 assert(a_generic_fn(i32, 3, 4) == 7);
1622fn useGenericParamInGenericParam() {
1623 assert(aGenericFn(i32, 3, 4) == 7);
16311624}
1632fn a_generic_fn(inline T: type, inline a: T, b: T) -> T {
1625fn aGenericFn(inline T: type, inline a: T, b: T) -> T {
16331626 return a + b;
16341627}
16351628
16361629
16371630#attribute("test")
1638fn namespace_depends_on_compile_var() {
1631fn namespaceDependsOnCompileVar() {
16391632 if (some_namespace.a_bool) {
16401633 assert(some_namespace.a_bool);
16411634 } else {
16421635 assert(!some_namespace.a_bool);
16431636 }
16441637}
1645const some_namespace = switch(@compile_var("os")) {
1638const some_namespace = switch(@compileVar("os")) {
16461639 linux => @import("a.zig"),
16471640 else => @import("b.zig"),
16481641};
16491642
16501643
16511644#attribute("test")
1652fn unsigned_64_bit_division() {
1645fn unsigned64BitDivision() {
16531646 const result = div(1152921504606846976, 34359738365);
16541647 assert(result.quotient == 33554432);
16551648 assert(result.remainder == 100663296);
......@@ -1667,16 +1660,16 @@ struct DivResult {
16671660}
16681661
16691662#attribute("test")
1670fn int_type_builtin() {
1671 assert(@int_type(true, 8) == i8);
1672 assert(@int_type(true, 16) == i16);
1673 assert(@int_type(true, 32) == i32);
1674 assert(@int_type(true, 64) == i64);
1663fn intTypeBuiltin() {
1664 assert(@intType(true, 8) == i8);
1665 assert(@intType(true, 16) == i16);
1666 assert(@intType(true, 32) == i32);
1667 assert(@intType(true, 64) == i64);
16751668
1676 assert(@int_type(false, 8) == u8);
1677 assert(@int_type(false, 16) == u16);
1678 assert(@int_type(false, 32) == u32);
1679 assert(@int_type(false, 64) == u64);
1669 assert(@intType(false, 8) == u8);
1670 assert(@intType(false, 16) == u16);
1671 assert(@intType(false, 32) == u32);
1672 assert(@intType(false, 64) == u64);
16801673
16811674 assert(i8.bit_count == 8);
16821675 assert(i16.bit_count == 16);
......@@ -1698,15 +1691,15 @@ fn int_type_builtin() {
16981691}
16991692
17001693#attribute("test")
1701fn int_to_enum() {
1702 test_int_to_enum_eval(3);
1703 test_int_to_enum_noeval(3);
1694fn intToEnum() {
1695 testIntToEnumEval(3);
1696 testIntToEnumNoeval(3);
17041697}
1705fn test_int_to_enum_eval(x: i32) {
1698fn testIntToEnumEval(x: i32) {
17061699 assert(IntToEnumNumber(x) == IntToEnumNumber.Three);
17071700}
17081701#static_eval_enable(false)
1709fn test_int_to_enum_noeval(x: i32) {
1702fn testIntToEnumNoeval(x: i32) {
17101703 assert(IntToEnumNumber(x) == IntToEnumNumber.Three);
17111704}
17121705enum IntToEnumNumber {