authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-23 23:08:09-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-23 23:08:09-05:00
logb3a6faf13ebccb82bb5fa82012bb62699a8ff6fb
treec0daed782354e52721845e87ecb62ef491ec70d7
parentad2527d47af6b6f22e0e9d127417a30c50b69c35

replace %defer with errdefer

See #632 now we have 1 less sigil

21 files changed, 87 insertions(+), 89 deletions(-)

doc/docgen.zig+2-2
......@@ -318,7 +318,7 @@ const Action = enum {
318318
319319fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) -> %Toc {
320320 var urls = std.HashMap([]const u8, Token, mem.hash_slice_u8, mem.eql_slice_u8).init(allocator);
321 %defer urls.deinit();
321 errdefer urls.deinit();
322322
323323 var header_stack_size: usize = 0;
324324 var last_action = Action.Open;
......@@ -399,7 +399,7 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) -> %Toc {
399399 }
400400 } else if (mem.eql(u8, tag_name, "see_also")) {
401401 var list = std.ArrayList(SeeAlsoItem).init(allocator);
402 %defer list.deinit();
402 errdefer list.deinit();
403403
404404 while (true) {
405405 const see_also_tok = tokenizer.next();
doc/langref.html.in+7-7
......@@ -2533,7 +2533,7 @@ test "defer unwinding" {
25332533 deferUnwindExample();
25342534}
25352535
2536// The %defer keyword is similar to defer, but will only execute if the
2536// The errdefer keyword is similar to defer, but will only execute if the
25372537// scope returns with an error.
25382538//
25392539// This is especially useful in allowing a function to clean up properly
......@@ -2547,7 +2547,7 @@ fn deferErrorExample(is_error: bool) -> %void {
25472547 warn("end of function\n");
25482548 }
25492549
2550 %defer {
2550 errdefer {
25512551 warn("encountered an error!\n");
25522552 }
25532553
......@@ -2556,7 +2556,7 @@ fn deferErrorExample(is_error: bool) -> %void {
25562556 }
25572557}
25582558
2559test "%defer unwinding" {
2559test "errdefer unwinding" {
25602560 _ = deferErrorExample(false);
25612561 _ = deferErrorExample(true);
25622562}
......@@ -2922,7 +2922,7 @@ fn doAThing(str: []u8) {
29222922 {#code_end#}
29232923 <p>
29242924 The other component to error handling is defer statements.
2925 In addition to an unconditional <code>defer</code>, Zig has <code>%defer</code>,
2925 In addition to an unconditional <code>defer</code>, Zig has <code>errdefer</code>,
29262926 which evaluates the deferred expression on block exit path if and only if
29272927 the function returned with an error from the block.
29282928 </p>
......@@ -2934,7 +2934,7 @@ fn createFoo(param: i32) -> %Foo {
29342934 const foo = try tryToAllocateFoo();
29352935 // now we have allocated foo. we need to free it if the function fails.
29362936 // but we want to return it if the function succeeds.
2937 %defer deallocateFoo(foo);
2937 errdefer deallocateFoo(foo);
29382938
29392939 const tmp_buf = allocateTmpBuffer() ?? return error.OutOfMemory;
29402940 // tmp_buf is truly a temporary resource, and we for sure want to clean it up
......@@ -2943,7 +2943,7 @@ fn createFoo(param: i32) -> %Foo {
29432943
29442944 if (param > 1337) return error.InvalidParam;
29452945
2946 // here the %defer will not run since we're returning success from the function.
2946 // here the errdefer will not run since we're returning success from the function.
29472947 // but the defer will run!
29482948 return foo;
29492949}
......@@ -5619,7 +5619,7 @@ TryExpression = "try" Expression
56195619
56205620BreakExpression = "break" option(":" Symbol) option(Expression)
56215621
5622Defer(body) = option("%") "defer" body
5622Defer(body) = ("defer" | "deferror") body
56235623
56245624IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))
56255625
src-self-hosted/main.zig+2-2
......@@ -371,7 +371,7 @@ pub fn main2() -> %void {
371371 defer allocator.free(full_cache_dir);
372372
373373 const zig_lib_dir = try resolveZigLibDir(allocator, zig_install_prefix);
374 %defer allocator.free(zig_lib_dir);
374 errdefer allocator.free(zig_lib_dir);
375375
376376 const module = try Module.create(allocator, root_name, zig_root_source_file,
377377 Target.Native, build_kind, build_mode, zig_lib_dir, full_cache_dir);
......@@ -587,7 +587,7 @@ fn resolveZigLibDir(allocator: &mem.Allocator, zig_install_prefix_arg: ?[]const
587587/// Caller must free result
588588fn testZigInstallPrefix(allocator: &mem.Allocator, test_path: []const u8) -> %[]u8 {
589589 const test_zig_dir = try os.path.join(allocator, test_path, "lib", "zig");
590 %defer allocator.free(test_zig_dir);
590 errdefer allocator.free(test_zig_dir);
591591
592592 const test_index_file = try os.path.join(allocator, test_zig_dir, "std", "index.zig");
593593 defer allocator.free(test_index_file);
src-self-hosted/module.zig+7-7
......@@ -113,19 +113,19 @@ pub const Module = struct {
113113 kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) -> %&Module
114114 {
115115 var name_buffer = try Buffer.init(allocator, name);
116 %defer name_buffer.deinit();
116 errdefer name_buffer.deinit();
117117
118118 const context = c.LLVMContextCreate() ?? return error.OutOfMemory;
119 %defer c.LLVMContextDispose(context);
119 errdefer c.LLVMContextDispose(context);
120120
121121 const module = c.LLVMModuleCreateWithNameInContext(name_buffer.ptr(), context) ?? return error.OutOfMemory;
122 %defer c.LLVMDisposeModule(module);
122 errdefer c.LLVMDisposeModule(module);
123123
124124 const builder = c.LLVMCreateBuilderInContext(context) ?? return error.OutOfMemory;
125 %defer c.LLVMDisposeBuilder(builder);
125 errdefer c.LLVMDisposeBuilder(builder);
126126
127127 const module_ptr = try allocator.create(Module);
128 %defer allocator.destroy(module_ptr);
128 errdefer allocator.destroy(module_ptr);
129129
130130 *module_ptr = Module {
131131 .allocator = allocator,
......@@ -211,13 +211,13 @@ pub const Module = struct {
211211 try printError("unable to get real path '{}': {}", root_src_path, err);
212212 return err;
213213 };
214 %defer self.allocator.free(root_src_real_path);
214 errdefer self.allocator.free(root_src_real_path);
215215
216216 const source_code = io.readFileAllocExtra(root_src_real_path, self.allocator, 3) catch |err| {
217217 try printError("unable to open '{}': {}", root_src_real_path, err);
218218 return err;
219219 };
220 %defer self.allocator.free(source_code);
220 errdefer self.allocator.free(source_code);
221221 source_code[source_code.len - 3] = '\n';
222222 source_code[source_code.len - 2] = '\n';
223223 source_code[source_code.len - 1] = '\n';
src-self-hosted/parser.zig+15-15
......@@ -127,7 +127,7 @@ pub const Parser = struct {
127127
128128 const root_node = x: {
129129 const root_node = try self.createRoot();
130 %defer self.allocator.destroy(root_node);
130 errdefer self.allocator.destroy(root_node);
131131 // This stack append has to succeed for freeAst to work
132132 try stack.append(State.TopLevel);
133133 break :x root_node;
......@@ -577,7 +577,7 @@ pub const Parser = struct {
577577
578578 fn createRoot(self: &Parser) -> %&ast.NodeRoot {
579579 const node = try self.allocator.create(ast.NodeRoot);
580 %defer self.allocator.destroy(node);
580 errdefer self.allocator.destroy(node);
581581
582582 *node = ast.NodeRoot {
583583 .base = ast.Node {.id = ast.Node.Id.Root},
......@@ -590,7 +590,7 @@ pub const Parser = struct {
590590 extern_token: &const ?Token) -> %&ast.NodeVarDecl
591591 {
592592 const node = try self.allocator.create(ast.NodeVarDecl);
593 %defer self.allocator.destroy(node);
593 errdefer self.allocator.destroy(node);
594594
595595 *node = ast.NodeVarDecl {
596596 .base = ast.Node {.id = ast.Node.Id.VarDecl},
......@@ -613,7 +613,7 @@ pub const Parser = struct {
613613 cc_token: &const ?Token, visib_token: &const ?Token, inline_token: &const ?Token) -> %&ast.NodeFnProto
614614 {
615615 const node = try self.allocator.create(ast.NodeFnProto);
616 %defer self.allocator.destroy(node);
616 errdefer self.allocator.destroy(node);
617617
618618 *node = ast.NodeFnProto {
619619 .base = ast.Node {.id = ast.Node.Id.FnProto},
......@@ -635,7 +635,7 @@ pub const Parser = struct {
635635
636636 fn createParamDecl(self: &Parser) -> %&ast.NodeParamDecl {
637637 const node = try self.allocator.create(ast.NodeParamDecl);
638 %defer self.allocator.destroy(node);
638 errdefer self.allocator.destroy(node);
639639
640640 *node = ast.NodeParamDecl {
641641 .base = ast.Node {.id = ast.Node.Id.ParamDecl},
......@@ -650,7 +650,7 @@ pub const Parser = struct {
650650
651651 fn createBlock(self: &Parser, begin_token: &const Token) -> %&ast.NodeBlock {
652652 const node = try self.allocator.create(ast.NodeBlock);
653 %defer self.allocator.destroy(node);
653 errdefer self.allocator.destroy(node);
654654
655655 *node = ast.NodeBlock {
656656 .base = ast.Node {.id = ast.Node.Id.Block},
......@@ -663,7 +663,7 @@ pub const Parser = struct {
663663
664664 fn createInfixOp(self: &Parser, op_token: &const Token, op: &const ast.NodeInfixOp.InfixOp) -> %&ast.NodeInfixOp {
665665 const node = try self.allocator.create(ast.NodeInfixOp);
666 %defer self.allocator.destroy(node);
666 errdefer self.allocator.destroy(node);
667667
668668 *node = ast.NodeInfixOp {
669669 .base = ast.Node {.id = ast.Node.Id.InfixOp},
......@@ -677,7 +677,7 @@ pub const Parser = struct {
677677
678678 fn createPrefixOp(self: &Parser, op_token: &const Token, op: &const ast.NodePrefixOp.PrefixOp) -> %&ast.NodePrefixOp {
679679 const node = try self.allocator.create(ast.NodePrefixOp);
680 %defer self.allocator.destroy(node);
680 errdefer self.allocator.destroy(node);
681681
682682 *node = ast.NodePrefixOp {
683683 .base = ast.Node {.id = ast.Node.Id.PrefixOp},
......@@ -690,7 +690,7 @@ pub const Parser = struct {
690690
691691 fn createIdentifier(self: &Parser, name_token: &const Token) -> %&ast.NodeIdentifier {
692692 const node = try self.allocator.create(ast.NodeIdentifier);
693 %defer self.allocator.destroy(node);
693 errdefer self.allocator.destroy(node);
694694
695695 *node = ast.NodeIdentifier {
696696 .base = ast.Node {.id = ast.Node.Id.Identifier},
......@@ -701,7 +701,7 @@ pub const Parser = struct {
701701
702702 fn createIntegerLiteral(self: &Parser, token: &const Token) -> %&ast.NodeIntegerLiteral {
703703 const node = try self.allocator.create(ast.NodeIntegerLiteral);
704 %defer self.allocator.destroy(node);
704 errdefer self.allocator.destroy(node);
705705
706706 *node = ast.NodeIntegerLiteral {
707707 .base = ast.Node {.id = ast.Node.Id.IntegerLiteral},
......@@ -712,7 +712,7 @@ pub const Parser = struct {
712712
713713 fn createFloatLiteral(self: &Parser, token: &const Token) -> %&ast.NodeFloatLiteral {
714714 const node = try self.allocator.create(ast.NodeFloatLiteral);
715 %defer self.allocator.destroy(node);
715 errdefer self.allocator.destroy(node);
716716
717717 *node = ast.NodeFloatLiteral {
718718 .base = ast.Node {.id = ast.Node.Id.FloatLiteral},
......@@ -723,14 +723,14 @@ pub const Parser = struct {
723723
724724 fn createAttachIdentifier(self: &Parser, dest_ptr: &const DestPtr, name_token: &const Token) -> %&ast.NodeIdentifier {
725725 const node = try self.createIdentifier(name_token);
726 %defer self.allocator.destroy(node);
726 errdefer self.allocator.destroy(node);
727727 try dest_ptr.store(&node.base);
728728 return node;
729729 }
730730
731731 fn createAttachParamDecl(self: &Parser, list: &ArrayList(&ast.Node)) -> %&ast.NodeParamDecl {
732732 const node = try self.createParamDecl();
733 %defer self.allocator.destroy(node);
733 errdefer self.allocator.destroy(node);
734734 try list.append(&node.base);
735735 return node;
736736 }
......@@ -740,7 +740,7 @@ pub const Parser = struct {
740740 inline_token: &const ?Token) -> %&ast.NodeFnProto
741741 {
742742 const node = try self.createFnProto(fn_token, extern_token, cc_token, visib_token, inline_token);
743 %defer self.allocator.destroy(node);
743 errdefer self.allocator.destroy(node);
744744 try list.append(&node.base);
745745 return node;
746746 }
......@@ -749,7 +749,7 @@ pub const Parser = struct {
749749 mut_token: &const Token, comptime_token: &const ?Token, extern_token: &const ?Token) -> %&ast.NodeVarDecl
750750 {
751751 const node = try self.createVarDecl(visib_token, mut_token, comptime_token, extern_token);
752 %defer self.allocator.destroy(node);
752 errdefer self.allocator.destroy(node);
753753 try list.append(&node.base);
754754 return node;
755755 }
src/parser.cpp+5-10
......@@ -1495,7 +1495,7 @@ static AstNode *ast_parse_break_expr(ParseContext *pc, size_t *token_index) {
14951495}
14961496
14971497/*
1498Defer(body) = option("%") "defer" body
1498Defer(body) = ("defer" | "errdefer") body
14991499*/
15001500static AstNode *ast_parse_defer_expr(ParseContext *pc, size_t *token_index) {
15011501 Token *token = &pc->tokens->at(*token_index);
......@@ -1503,15 +1503,10 @@ static AstNode *ast_parse_defer_expr(ParseContext *pc, size_t *token_index) {
15031503 NodeType node_type;
15041504 ReturnKind kind;
15051505
1506 if (token->id == TokenIdPercent) {
1507 Token *next_token = &pc->tokens->at(*token_index + 1);
1508 if (next_token->id == TokenIdKeywordDefer) {
1509 kind = ReturnKindError;
1510 node_type = NodeTypeDefer;
1511 *token_index += 2;
1512 } else {
1513 return nullptr;
1514 }
1506 if (token->id == TokenIdKeywordErrdefer) {
1507 kind = ReturnKindError;
1508 node_type = NodeTypeDefer;
1509 *token_index += 1;
15151510 } else if (token->id == TokenIdKeywordDefer) {
15161511 kind = ReturnKindUnconditional;
15171512 node_type = NodeTypeDefer;
src/tokenizer.cpp+2
......@@ -118,6 +118,7 @@ static const struct ZigKeyword zig_keywords[] = {
118118 {"defer", TokenIdKeywordDefer},
119119 {"else", TokenIdKeywordElse},
120120 {"enum", TokenIdKeywordEnum},
121 {"errdefer", TokenIdKeywordErrdefer},
121122 {"error", TokenIdKeywordError},
122123 {"export", TokenIdKeywordExport},
123124 {"extern", TokenIdKeywordExtern},
......@@ -1514,6 +1515,7 @@ const char * token_name(TokenId id) {
15141515 case TokenIdKeywordDefer: return "defer";
15151516 case TokenIdKeywordElse: return "else";
15161517 case TokenIdKeywordEnum: return "enum";
1518 case TokenIdKeywordErrdefer: return "errdefer";
15171519 case TokenIdKeywordError: return "error";
15181520 case TokenIdKeywordExport: return "export";
15191521 case TokenIdKeywordExtern: return "extern";
src/tokenizer.hpp+1
......@@ -57,6 +57,7 @@ enum TokenId {
5757 TokenIdKeywordDefer,
5858 TokenIdKeywordElse,
5959 TokenIdKeywordEnum,
60 TokenIdKeywordErrdefer,
6061 TokenIdKeywordError,
6162 TokenIdKeywordExport,
6263 TokenIdKeywordExtern,
std/buf_map.zig+3-3
......@@ -30,14 +30,14 @@ pub const BufMap = struct {
3030 pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void {
3131 if (self.hash_map.get(key)) |entry| {
3232 const value_copy = try self.copy(value);
33 %defer self.free(value_copy);
33 errdefer self.free(value_copy);
3434 _ = try self.hash_map.put(key, value_copy);
3535 self.free(entry.value);
3636 } else {
3737 const key_copy = try self.copy(key);
38 %defer self.free(key_copy);
38 errdefer self.free(key_copy);
3939 const value_copy = try self.copy(value);
40 %defer self.free(value_copy);
40 errdefer self.free(value_copy);
4141 _ = try self.hash_map.put(key_copy, value_copy);
4242 }
4343 }
std/buf_set.zig+1-1
......@@ -27,7 +27,7 @@ pub const BufSet = struct {
2727 pub fn put(self: &BufSet, key: []const u8) -> %void {
2828 if (self.hash_map.get(key) == null) {
2929 const key_copy = try self.copy(key);
30 %defer self.free(key_copy);
30 errdefer self.free(key_copy);
3131 _ = try self.hash_map.put(key_copy, {});
3232 }
3333 }
std/cstr.zig+1-1
......@@ -71,7 +71,7 @@ pub const NullTerminated2DArray = struct {
7171 byte_count += index_size;
7272
7373 const buf = try allocator.alignedAlloc(u8, @alignOf(?&u8), byte_count);
74 %defer allocator.free(buf);
74 errdefer allocator.free(buf);
7575
7676 var write_index = index_size;
7777 const index_buf = ([]?&u8)(buf);
std/debug/index.zig+4-4
......@@ -248,10 +248,10 @@ pub fn openSelfDebugInfo(allocator: &mem.Allocator) -> %&ElfStackTrace {
248248 .compile_unit_list = ArrayList(CompileUnit).init(allocator),
249249 };
250250 st.self_exe_file = try os.openSelfExe();
251 %defer st.self_exe_file.close();
251 errdefer st.self_exe_file.close();
252252
253253 try st.elf.openFile(allocator, &st.self_exe_file);
254 %defer st.elf.close();
254 errdefer st.elf.close();
255255
256256 st.debug_info = (try st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo;
257257 st.debug_abbrev = (try st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo;
......@@ -524,7 +524,7 @@ const LineNumberProgram = struct {
524524 return error.InvalidDebugInfo;
525525 } else self.include_dirs[file_entry.dir_index];
526526 const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name);
527 %defer self.file_entries.allocator.free(file_name);
527 errdefer self.file_entries.allocator.free(file_name);
528528 return LineInfo {
529529 .line = if (self.prev_line >= 0) usize(self.prev_line) else 0,
530530 .column = self.prev_column,
......@@ -563,7 +563,7 @@ fn getString(st: &ElfStackTrace, offset: u64) -> %[]u8 {
563563
564564fn readAllocBytes(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %[]u8 {
565565 const buf = try global_allocator.alloc(u8, size);
566 %defer global_allocator.free(buf);
566 errdefer global_allocator.free(buf);
567567 if ((try in_stream.read(buf)) < size) return error.EndOfFile;
568568 return buf;
569569}
std/elf.zig+1-1
......@@ -183,7 +183,7 @@ pub const Elf = struct {
183183 try elf.in_file.seekTo(elf.section_header_offset);
184184
185185 elf.section_headers = try elf.allocator.alloc(SectionHeader, sh_entry_count);
186 %defer elf.allocator.free(elf.section_headers);
186 errdefer elf.allocator.free(elf.section_headers);
187187
188188 if (elf.is_64) {
189189 if (sh_entry_size != 64) return error.InvalidFormat;
std/io.zig+1-1
......@@ -550,7 +550,7 @@ pub fn readFileAllocExtra(path: []const u8, allocator: &mem.Allocator, extra_len
550550
551551 const size = try file.getEndPos();
552552 const buf = try allocator.alloc(u8, size + extra_len);
553 %defer allocator.free(buf);
553 errdefer allocator.free(buf);
554554
555555 var adapter = FileInStream.init(&file);
556556 try adapter.stream.readNoEof(buf[0..size]);
std/mem.zig+1-1
......@@ -440,7 +440,7 @@ pub fn join(allocator: &Allocator, sep: u8, strings: ...) -> %[]u8 {
440440 }
441441
442442 const buf = try allocator.alloc(u8, total_strings_len);
443 %defer allocator.free(buf);
443 errdefer allocator.free(buf);
444444
445445 var buf_index: usize = 0;
446446 comptime var string_i = 0;
std/os/child_process.zig+10-10
......@@ -76,7 +76,7 @@ pub const ChildProcess = struct {
7676 /// On success must call deinit.
7777 pub fn init(argv: []const []const u8, allocator: &mem.Allocator) -> %&ChildProcess {
7878 const child = try allocator.create(ChildProcess);
79 %defer allocator.destroy(child);
79 errdefer allocator.destroy(child);
8080
8181 *child = ChildProcess {
8282 .allocator = allocator,
......@@ -336,13 +336,13 @@ pub const ChildProcess = struct {
336336 install_SIGCHLD_handler();
337337
338338 const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined;
339 %defer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); };
339 errdefer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); };
340340
341341 const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try makePipe() else undefined;
342 %defer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); };
342 errdefer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); };
343343
344344 const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try makePipe() else undefined;
345 %defer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); };
345 errdefer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); };
346346
347347 const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
348348 const dev_null_fd = if (any_ignore)
......@@ -367,7 +367,7 @@ pub const ChildProcess = struct {
367367 // This pipe is used to communicate errors between the time of fork
368368 // and execve from the child process to the parent process.
369369 const err_pipe = try makePipe();
370 %defer destroyPipe(err_pipe);
370 errdefer destroyPipe(err_pipe);
371371
372372 block_SIGCHLD();
373373 const pid_result = posix.fork();
......@@ -479,7 +479,7 @@ pub const ChildProcess = struct {
479479 g_hChildStd_IN_Rd = null;
480480 },
481481 }
482 %defer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr); };
482 errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr); };
483483
484484 var g_hChildStd_OUT_Rd: ?windows.HANDLE = null;
485485 var g_hChildStd_OUT_Wr: ?windows.HANDLE = null;
......@@ -497,7 +497,7 @@ pub const ChildProcess = struct {
497497 g_hChildStd_OUT_Wr = null;
498498 },
499499 }
500 %defer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr); };
500 errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr); };
501501
502502 var g_hChildStd_ERR_Rd: ?windows.HANDLE = null;
503503 var g_hChildStd_ERR_Wr: ?windows.HANDLE = null;
......@@ -515,7 +515,7 @@ pub const ChildProcess = struct {
515515 g_hChildStd_ERR_Wr = null;
516516 },
517517 }
518 %defer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); };
518 errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); };
519519
520520 const cmd_line = try windowsCreateCommandLine(self.allocator, self.argv);
521521 defer self.allocator.free(cmd_line);
......@@ -722,7 +722,7 @@ fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const S
722722 var rd_h: windows.HANDLE = undefined;
723723 var wr_h: windows.HANDLE = undefined;
724724 try windowsMakePipe(&rd_h, &wr_h, sattr);
725 %defer windowsDestroyPipe(rd_h, wr_h);
725 errdefer windowsDestroyPipe(rd_h, wr_h);
726726 try windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0);
727727 *rd = rd_h;
728728 *wr = wr_h;
......@@ -732,7 +732,7 @@ fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const
732732 var rd_h: windows.HANDLE = undefined;
733733 var wr_h: windows.HANDLE = undefined;
734734 try windowsMakePipe(&rd_h, &wr_h, sattr);
735 %defer windowsDestroyPipe(rd_h, wr_h);
735 errdefer windowsDestroyPipe(rd_h, wr_h);
736736 try windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0);
737737 *rd = rd_h;
738738 *wr = wr_h;
std/os/index.zig+12-12
......@@ -311,7 +311,7 @@ pub fn createNullDelimitedEnvMap(allocator: &Allocator, env_map: &const BufMap)
311311 const envp_count = env_map.count();
312312 const envp_buf = try allocator.alloc(?&u8, envp_count + 1);
313313 mem.set(?&u8, envp_buf, null);
314 %defer freeNullDelimitedEnvMap(allocator, envp_buf);
314 errdefer freeNullDelimitedEnvMap(allocator, envp_buf);
315315 {
316316 var it = env_map.iterator();
317317 var i: usize = 0;
......@@ -421,7 +421,7 @@ pub var posix_environ_raw: []&u8 = undefined;
421421/// Caller must free result when done.
422422pub fn getEnvMap(allocator: &Allocator) -> %BufMap {
423423 var result = BufMap.init(allocator);
424 %defer result.deinit();
424 errdefer result.deinit();
425425
426426 if (is_windows) {
427427 const ptr = windows.GetEnvironmentStringsA() ?? return error.OutOfMemory;
......@@ -489,7 +489,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 {
489489 defer allocator.free(key_with_null);
490490
491491 var buf = try allocator.alloc(u8, 256);
492 %defer allocator.free(buf);
492 errdefer allocator.free(buf);
493493
494494 while (true) {
495495 const windows_buf_len = try math.cast(windows.DWORD, buf.len);
......@@ -521,7 +521,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 {
521521 switch (builtin.os) {
522522 Os.windows => {
523523 var buf = try allocator.alloc(u8, 256);
524 %defer allocator.free(buf);
524 errdefer allocator.free(buf);
525525
526526 while (true) {
527527 const result = windows.GetCurrentDirectoryA(windows.WORD(buf.len), buf.ptr);
......@@ -543,7 +543,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 {
543543 },
544544 else => {
545545 var buf = try allocator.alloc(u8, 1024);
546 %defer allocator.free(buf);
546 errdefer allocator.free(buf);
547547 while (true) {
548548 const err = posix.getErrno(posix.getcwd(buf.ptr, buf.len));
549549 if (err == posix.ERANGE) {
......@@ -724,7 +724,7 @@ pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: [
724724
725725 var out_file = try io.File.openWriteMode(tmp_path, mode, allocator);
726726 defer out_file.close();
727 %defer _ = deleteFile(allocator, tmp_path);
727 errdefer _ = deleteFile(allocator, tmp_path);
728728
729729 var in_file = try io.File.openRead(source_path, allocator);
730730 defer in_file.close();
......@@ -1074,7 +1074,7 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
10741074 path_buf[pathname.len] = 0;
10751075
10761076 var result_buf = try allocator.alloc(u8, 1024);
1077 %defer allocator.free(result_buf);
1077 errdefer allocator.free(result_buf);
10781078 while (true) {
10791079 const ret_val = posix.readlink(path_buf.ptr, result_buf.ptr, result_buf.len);
10801080 const err = posix.getErrno(ret_val);
......@@ -1443,7 +1443,7 @@ pub fn argsAlloc(allocator: &mem.Allocator) -> %[]const []u8 {
14431443 const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len);
14441444 const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len);
14451445 const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes);
1446 %defer allocator.free(buf);
1446 errdefer allocator.free(buf);
14471447
14481448 const result_slice_list = ([][]u8)(buf[0..slice_list_bytes]);
14491449 const result_contents = buf[slice_list_bytes..];
......@@ -1556,7 +1556,7 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 {
15561556 },
15571557 Os.windows => {
15581558 var out_path = try Buffer.initSize(allocator, 0xff);
1559 %defer out_path.deinit();
1559 errdefer out_path.deinit();
15601560 while (true) {
15611561 const dword_len = try math.cast(windows.DWORD, out_path.len());
15621562 const copied_amt = windows.GetModuleFileNameA(null, out_path.ptr(), dword_len);
......@@ -1579,7 +1579,7 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 {
15791579 const ret1 = c._NSGetExecutablePath(undefined, &u32_len);
15801580 assert(ret1 != 0);
15811581 const bytes = try allocator.alloc(u8, u32_len);
1582 %defer allocator.free(bytes);
1582 errdefer allocator.free(bytes);
15831583 const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len);
15841584 assert(ret2 == 0);
15851585 return bytes;
......@@ -1598,13 +1598,13 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 {
15981598 // This path cannot be opened, but it's valid for determining the directory
15991599 // the executable was in when it was run.
16001600 const full_exe_path = try readLink(allocator, "/proc/self/exe");
1601 %defer allocator.free(full_exe_path);
1601 errdefer allocator.free(full_exe_path);
16021602 const dir = path.dirname(full_exe_path);
16031603 return allocator.shrink(u8, full_exe_path, dir.len);
16041604 },
16051605 Os.windows, Os.macosx, Os.ios => {
16061606 const self_exe_path = try selfExePath(allocator);
1607 %defer allocator.free(self_exe_path);
1607 errdefer allocator.free(self_exe_path);
16081608 const dirname = os.path.dirname(self_exe_path);
16091609 return allocator.shrink(u8, self_exe_path, dirname.len);
16101610 },
std/os/path.zig+6-6
......@@ -468,7 +468,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8
468468 }
469469 have_drive_kind = parsed_cwd.kind;
470470 }
471 %defer allocator.free(result);
471 errdefer allocator.free(result);
472472
473473 // Now we know the disk designator to use, if any, and what kind it is. And our result
474474 // is big enough to append all the paths to.
......@@ -551,7 +551,7 @@ pub fn resolvePosix(allocator: &Allocator, paths: []const []const u8) -> %[]u8 {
551551 mem.copy(u8, result, cwd);
552552 result_index += cwd.len;
553553 }
554 %defer allocator.free(result);
554 errdefer allocator.free(result);
555555
556556 for (paths[first_index..]) |p, i| {
557557 var it = mem.split(p, "/");
......@@ -943,7 +943,7 @@ pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8)
943943 }
944944 const up_index_end = up_count * "..\\".len;
945945 const result = try allocator.alloc(u8, up_index_end + to_rest.len);
946 %defer allocator.free(result);
946 errdefer allocator.free(result);
947947
948948 var result_index: usize = 0;
949949 while (result_index < up_index_end) {
......@@ -993,7 +993,7 @@ pub fn relativePosix(allocator: &Allocator, from: []const u8, to: []const u8) ->
993993 }
994994 const up_index_end = up_count * "../".len;
995995 const result = try allocator.alloc(u8, up_index_end + to_rest.len);
996 %defer allocator.free(result);
996 errdefer allocator.free(result);
997997
998998 var result_index: usize = 0;
999999 while (result_index < up_index_end) {
......@@ -1100,7 +1100,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
11001100 }
11011101 defer os.close(h_file);
11021102 var buf = try allocator.alloc(u8, 256);
1103 %defer allocator.free(buf);
1103 errdefer allocator.free(buf);
11041104 while (true) {
11051105 const buf_len = math.cast(windows.DWORD, buf.len) catch return error.NameTooLong;
11061106 const result = windows.GetFinalPathNameByHandleA(h_file, buf.ptr, buf_len, windows.VOLUME_NAME_DOS);
......@@ -1144,7 +1144,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
11441144 defer allocator.free(pathname_buf);
11451145
11461146 const result_buf = try allocator.alloc(u8, posix.PATH_MAX);
1147 %defer allocator.free(result_buf);
1147 errdefer allocator.free(result_buf);
11481148
11491149 mem.copy(u8, pathname_buf, pathname);
11501150 pathname_buf[pathname.len] = 0;
std/os/windows/util.zig+1-1
......@@ -133,7 +133,7 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap)
133133 break :x bytes_needed;
134134 };
135135 const result = try allocator.alloc(u8, bytes_needed);
136 %defer allocator.free(result);
136 errdefer allocator.free(result);
137137
138138 var it = env_map.iterator();
139139 var i: usize = 0;
test/cases/defer.zig+1-1
......@@ -8,7 +8,7 @@ error FalseNotAllowed;
88fn runSomeErrorDefers(x: bool) -> %bool {
99 index = 0;
1010 defer {result[index] = 'a'; index += 1;}
11 %defer {result[index] = 'b'; index += 1;}
11 errdefer {result[index] = 'b'; index += 1;}
1212 defer {result[index] = 'c'; index += 1;}
1313 return if (x) x else error.FalseNotAllowed;
1414}
test/compare_output.zig+4-4
......@@ -392,7 +392,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
392392 \\}
393393 , "before\ndefer2\ndefer1\n");
394394
395 cases.add("%defer and it fails",
395 cases.add("errdefer and it fails",
396396 \\const io = @import("std").io;
397397 \\pub fn main() -> %void {
398398 \\ do_test() catch return;
......@@ -401,7 +401,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
401401 \\ const stdout = &(io.FileOutStream.init(&(io.getStdOut() catch unreachable)).stream);
402402 \\ stdout.print("before\n") catch unreachable;
403403 \\ defer stdout.print("defer1\n") catch unreachable;
404 \\ %defer stdout.print("deferErr\n") catch unreachable;
404 \\ errdefer stdout.print("deferErr\n") catch unreachable;
405405 \\ try its_gonna_fail();
406406 \\ defer stdout.print("defer3\n") catch unreachable;
407407 \\ stdout.print("after\n") catch unreachable;
......@@ -412,7 +412,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
412412 \\}
413413 , "before\ndeferErr\ndefer1\n");
414414
415 cases.add("%defer and it passes",
415 cases.add("errdefer and it passes",
416416 \\const io = @import("std").io;
417417 \\pub fn main() -> %void {
418418 \\ do_test() catch return;
......@@ -421,7 +421,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
421421 \\ const stdout = &(io.FileOutStream.init(&(io.getStdOut() catch unreachable)).stream);
422422 \\ stdout.print("before\n") catch unreachable;
423423 \\ defer stdout.print("defer1\n") catch unreachable;
424 \\ %defer stdout.print("deferErr\n") catch unreachable;
424 \\ errdefer stdout.print("deferErr\n") catch unreachable;
425425 \\ try its_gonna_pass();
426426 \\ defer stdout.print("defer3\n") catch unreachable;
427427 \\ stdout.print("after\n") catch unreachable;