authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-03 11:51:29-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-03 11:51:29-05:00
logabf5ae6897bb23e49e4232ab8be7ed61ea9520b6
treea0ff1d117e79a63827995c65d22a3fc4e46a37e1
parentb8f59e14cdbf90cf724ed9e721c1909293f41b3b

*WIP* error sets - support fns called at comptime


8 files changed, 55 insertions(+), 20 deletions(-)

TODO+4
......@@ -25,3 +25,7 @@ comptime test for err
2525
2626
2727undefined in infer error
28
29change readlink back to inferred error
30
31syntax - ?a!b should be ?(a!b) but it's (?a)!b
src/ir.cpp+29-2
......@@ -10123,6 +10123,13 @@ static TypeTableEntry *ir_analyze_instruction_error_union(IrAnalyze *ira,
1012310123 if (type_is_invalid(payload_type))
1012410124 return ira->codegen->builtin_types.entry_invalid;
1012510125
10126 if (err_set_type->id != TypeTableEntryIdErrorSet) {
10127 ir_add_error(ira, instruction->err_set->other,
10128 buf_sprintf("expected error set type, found type '%s'",
10129 buf_ptr(&err_set_type->name)));
10130 return ira->codegen->builtin_types.entry_invalid;
10131 }
10132
1012610133 TypeTableEntry *result_type = get_error_union_type(ira->codegen, err_set_type, payload_type);
1012710134
1012810135 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
......@@ -10412,9 +10419,17 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
1041210419 }
1041310420
1041410421 AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;
10415 TypeTableEntry *return_type = analyze_type_expr(ira->codegen, exec_scope, return_type_node);
10416 if (type_is_invalid(return_type))
10422 TypeTableEntry *specified_return_type = analyze_type_expr(ira->codegen, exec_scope, return_type_node);
10423 if (type_is_invalid(specified_return_type))
1041710424 return ira->codegen->builtin_types.entry_invalid;
10425 TypeTableEntry *return_type;
10426 TypeTableEntry *inferred_err_set_type = nullptr;
10427 if (fn_proto_node->data.fn_proto.auto_err_set) {
10428 inferred_err_set_type = get_auto_err_set_type(ira->codegen, fn_entry);
10429 return_type = get_error_union_type(ira->codegen, inferred_err_set_type, specified_return_type);
10430 } else {
10431 return_type = specified_return_type;
10432 }
1041810433
1041910434 IrInstruction *result;
1042010435
......@@ -10428,6 +10443,18 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
1042810443 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
1042910444 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec);
1043010445
10446 if (inferred_err_set_type != nullptr) {
10447 inferred_err_set_type->data.error_set.infer_fn = nullptr;
10448 if (result->value.type->id == TypeTableEntryIdErrorUnion) {
10449 TypeTableEntry *fn_inferred_err_set_type = result->value.type->data.error_union.err_set_type;
10450 inferred_err_set_type->data.error_set.err_count = fn_inferred_err_set_type->data.error_set.err_count;
10451 inferred_err_set_type->data.error_set.errors = fn_inferred_err_set_type->data.error_set.errors;
10452 } else if (result->value.type->id == TypeTableEntryIdErrorSet) {
10453 inferred_err_set_type->data.error_set.err_count = result->value.type->data.error_set.err_count;
10454 inferred_err_set_type->data.error_set.errors = result->value.type->data.error_set.errors;
10455 }
10456 }
10457
1043110458 ira->codegen->memoized_fn_eval_table.put(exec_scope, result);
1043210459
1043310460 if (type_is_invalid(result->value.type))
src/parser.cpp+1-1
......@@ -2589,7 +2589,7 @@ static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index,
25892589 Token *colon_token = &pc->tokens->at(*token_index);
25902590 if (colon_token->id == TokenIdColon) {
25912591 *token_index += 1;
2592 field_node->data.struct_field.type = ast_parse_prefix_op_expr(pc, token_index, true);
2592 field_node->data.struct_field.type = ast_parse_type_expr(pc, token_index, true);
25932593 }
25942594 Token *eq_token = &pc->tokens->at(*token_index);
25952595 if (eq_token->id == TokenIdEq) {
std/base64.zig+3-3
......@@ -181,7 +181,7 @@ pub const Base64DecoderWithIgnore = struct {
181181 }
182182
183183 /// If no characters end up being ignored or padding, this will be the exact decoded size.
184 pub fn calcSizeUpperBound(encoded_len: usize) !usize {
184 pub fn calcSizeUpperBound(encoded_len: usize) usize {
185185 return @divTrunc(encoded_len, 4) * 3;
186186 }
187187
......@@ -430,7 +430,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
430430 const standard_decoder_ignore_nothing = Base64DecoderWithIgnore.init(
431431 standard_alphabet_chars, standard_pad_char, "");
432432 var buffer: [0x100]u8 = undefined;
433 var decoded = buffer[0..try Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)];
433 var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)];
434434 var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded);
435435 assert(written <= decoded.len);
436436 assert(mem.eql(u8, decoded[0..written], expected_decoded));
......@@ -449,7 +449,7 @@ fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !voi
449449 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(
450450 standard_alphabet_chars, standard_pad_char, " ");
451451 var buffer: [0x100]u8 = undefined;
452 var decoded = buffer[0..try Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)];
452 var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)];
453453 var written = try standard_decoder_ignore_space.decode(decoded, encoded);
454454 assert(mem.eql(u8, decoded[0..written], expected_decoded));
455455}
std/build.zig+5-5
......@@ -554,7 +554,7 @@ pub const Builder = struct {
554554 }
555555
556556 fn spawnChildEnvMap(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap,
557 argv: []const []const u8) %void
557 argv: []const []const u8) !void
558558 {
559559 if (self.verbose) {
560560 printCmd(cwd, argv);
......@@ -1942,12 +1942,12 @@ pub const RemoveDirStep = struct {
19421942
19431943pub const Step = struct {
19441944 name: []const u8,
1945 makeFn: fn(self: &Step) %void,
1945 makeFn: fn(self: &Step) error!void,
19461946 dependencies: ArrayList(&Step),
19471947 loop_flag: bool,
19481948 done_flag: bool,
19491949
1950 pub fn init(name: []const u8, allocator: &Allocator, makeFn: fn (&Step)%void) Step {
1950 pub fn init(name: []const u8, allocator: &Allocator, makeFn: fn (&Step)error!void) Step {
19511951 return Step {
19521952 .name = name,
19531953 .makeFn = makeFn,
......@@ -1972,11 +1972,11 @@ pub const Step = struct {
19721972 self.dependencies.append(other) catch unreachable;
19731973 }
19741974
1975 fn makeNoOp(self: &Step) %void {}
1975 fn makeNoOp(self: &Step) (error{}!void) {}
19761976};
19771977
19781978fn doAtomicSymLinks(allocator: &Allocator, output_path: []const u8, filename_major_only: []const u8,
1979 filename_name_only: []const u8) %void
1979 filename_name_only: []const u8) !void
19801980{
19811981 const out_dir = os.path.dirname(output_path);
19821982 const out_basename = os.path.basename(output_path);
std/fmt/index.zig+3-3
......@@ -198,7 +198,7 @@ pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn(@
198198 return formatInt(value, 10, false, 0, context, Errors, output);
199199 },
200200 builtin.TypeId.Float => {
201 return formatFloat(value, context, output);
201 return formatFloat(value, context, Errors, output);
202202 },
203203 builtin.TypeId.Void => {
204204 return output(context, "void");
......@@ -417,7 +417,7 @@ const FormatIntBuf = struct {
417417 out_buf: []u8,
418418 index: usize,
419419};
420fn formatIntCallback(context: &FormatIntBuf, bytes: []const u8) !void {
420fn formatIntCallback(context: &FormatIntBuf, bytes: []const u8) (error{}!void) {
421421 mem.copy(u8, context.out_buf[context.index..], bytes);
422422 context.index += bytes.len;
423423}
......@@ -499,7 +499,7 @@ fn bufPrintWrite(context: &BufPrintContext, bytes: []const u8) !void {
499499
500500pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) ![]u8 {
501501 var context = BufPrintContext { .remaining = buf, };
502 try format(&context, bufPrintWrite, fmt, args);
502 try format(&context, error{BufferTooSmall}, bufPrintWrite, fmt, args);
503503 return buf[0..buf.len - context.remaining.len];
504504}
505505
std/os/child_process.zig+8-4
......@@ -28,7 +28,7 @@ pub const ChildProcess = struct {
2828 pub stdout: ?io.File,
2929 pub stderr: ?io.File,
3030
31 pub term: ?%Term,
31 pub term: ?SpawnError!Term,
3232
3333 pub argv: []const []const u8,
3434
......@@ -54,6 +54,10 @@ pub const ChildProcess = struct {
5454 err_pipe: if (is_windows) void else [2]i32,
5555 llnode: if (is_windows) void else LinkedList(&ChildProcess).Node,
5656
57 pub const SpawnError = error {
58
59 };
60
5761 pub const Term = union(enum) {
5862 Exited: i32,
5963 Signal: i32,
......@@ -185,7 +189,7 @@ pub const ChildProcess = struct {
185189 /// Spawns a child process, waits for it, collecting stdout and stderr, and then returns.
186190 /// If it succeeds, the caller owns result.stdout and result.stderr memory.
187191 pub fn exec(allocator: &mem.Allocator, argv: []const []const u8, cwd: ?[]const u8,
188 env_map: ?&const BufMap, max_output_size: usize) %ExecResult
192 env_map: ?&const BufMap, max_output_size: usize) !ExecResult
189193 {
190194 const child = try ChildProcess.init(argv, allocator);
191195 defer child.deinit();
......@@ -246,7 +250,7 @@ pub const ChildProcess = struct {
246250 fn waitUnwrappedWindows(self: &ChildProcess) !void {
247251 const result = os.windowsWaitSingle(self.handle, windows.INFINITE);
248252
249 self.term = (%Term)(x: {
253 self.term = (SpawnError!Term)(x: {
250254 var exit_code: windows.DWORD = undefined;
251255 if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) {
252256 break :x Term { .Unknown = 0 };
......@@ -631,7 +635,7 @@ pub const ChildProcess = struct {
631635};
632636
633637fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?&u8,
634 lpStartupInfo: &windows.STARTUPINFOA, lpProcessInformation: &windows.PROCESS_INFORMATION) %void
638 lpStartupInfo: &windows.STARTUPINFOA, lpProcessInformation: &windows.PROCESS_INFORMATION) !void
635639{
636640 if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0,
637641 @ptrCast(?&c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0)
std/os/index.zig+2-2
......@@ -1072,7 +1072,7 @@ pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) !void {
10721072}
10731073
10741074/// Read value of a symbolic link.
1075pub fn readLink(allocator: &Allocator, pathname: []const u8) ![]u8 {
1075pub fn readLink(allocator: &Allocator, pathname: []const u8) error![]u8 {
10761076 const path_buf = try allocator.alloc(u8, pathname.len + 1);
10771077 defer allocator.free(path_buf);
10781078
......@@ -1267,7 +1267,7 @@ pub const ArgIteratorWindows = struct {
12671267 }
12681268
12691269 /// You must free the returned memory when done.
1270 pub fn next(self: &ArgIteratorWindows, allocator: &Allocator) ?internalNext.errors![]u8 {
1270 pub fn next(self: &ArgIteratorWindows, allocator: &Allocator) ?(@typeOf(internalNext).ReturnType.ErrorSet![]u8) {
12711271 // march forward over whitespace
12721272 while (true) : (self.index += 1) {
12731273 const byte = self.cmd_line[self.index];