authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-19 17:07:05-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-19 17:07:05-04:00
logc7dc03fcb16abfac2d914002a609b8144f7cdab2
treea1af2d48810cbdcd34b0904271e19d2248d62a27
parent96931228af745b8e69c138b3b83893dafa166cfe
signaturelock-open Commit is signed but in an unrecognized format.

fix `try` not setting error code on result location


3 files changed, 51 insertions(+), 44 deletions(-)

src/ir.cpp+8-1
......@@ -3657,10 +3657,17 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
36573657 if (!ir_gen_defers_for_block(irb, scope, outer_scope, true)) {
36583658 IrInstruction *err_val_ptr = ir_build_unwrap_err_code(irb, scope, node, err_union_ptr);
36593659 IrInstruction *err_val = ir_build_load_ptr(irb, scope, node, err_val_ptr);
3660
3661 ResultLocReturn *result_loc_ret = allocate<ResultLocReturn>(1);
3662 result_loc_ret->base.id = ResultLocIdReturn;
3663 ir_build_reset_result(irb, scope, node, &result_loc_ret->base);
3664 ir_build_end_expr(irb, scope, node, err_val, &result_loc_ret->base);
3665
36603666 if (irb->codegen->have_err_ret_tracing && !should_inline) {
36613667 ir_build_save_err_ret_addr(irb, scope, node);
36623668 }
3663 ir_gen_async_return(irb, scope, node, err_val, false);
3669 IrInstruction *ret_inst = ir_gen_async_return(irb, scope, node, err_val, false);
3670 result_loc_ret->base.source_instruction = ret_inst;
36643671 }
36653672
36663673 ir_set_cursor_at_end_and_append_block(irb, continue_block);
test/stage1/behavior.zig+1-1
......@@ -47,7 +47,7 @@ comptime {
4747 _ = @import("behavior/defer.zig");
4848 _ = @import("behavior/enum.zig");
4949 _ = @import("behavior/enum_with_members.zig");
50 _ = @import("behavior/error.zig"); // TODO
50 _ = @import("behavior/error.zig");
5151 _ = @import("behavior/eval.zig");
5252 _ = @import("behavior/field_parent_ptr.zig");
5353 _ = @import("behavior/fn.zig");
test/stage1/behavior/error.zig+42-42
......@@ -249,48 +249,48 @@ fn intLiteral(str: []const u8) !?i64 {
249249 return error.T;
250250}
251251
252//test "nested error union function call in optional unwrap" {
253// const S = struct {
254// const Foo = struct {
255// a: i32,
256// };
257//
258// fn errorable() !i32 {
259// var x: Foo = (try getFoo()) orelse return error.Other;
260// return x.a;
261// }
262//
263// fn errorable2() !i32 {
264// var x: Foo = (try getFoo2()) orelse return error.Other;
265// return x.a;
266// }
267//
268// fn errorable3() !i32 {
269// var x: Foo = (try getFoo3()) orelse return error.Other;
270// return x.a;
271// }
272//
273// fn getFoo() anyerror!?Foo {
274// return Foo{ .a = 1234 };
275// }
276//
277// fn getFoo2() anyerror!?Foo {
278// return error.Failure;
279// }
280//
281// fn getFoo3() anyerror!?Foo {
282// return null;
283// }
284// };
285// expect((try S.errorable()) == 1234);
286// expectError(error.Failure, S.errorable2());
287// expectError(error.Other, S.errorable3());
288// comptime {
289// expect((try S.errorable()) == 1234);
290// expectError(error.Failure, S.errorable2());
291// expectError(error.Other, S.errorable3());
292// }
293//}
252test "nested error union function call in optional unwrap" {
253 const S = struct {
254 const Foo = struct {
255 a: i32,
256 };
257
258 fn errorable() !i32 {
259 var x: Foo = (try getFoo()) orelse return error.Other;
260 return x.a;
261 }
262
263 fn errorable2() !i32 {
264 var x: Foo = (try getFoo2()) orelse return error.Other;
265 return x.a;
266 }
267
268 fn errorable3() !i32 {
269 var x: Foo = (try getFoo3()) orelse return error.Other;
270 return x.a;
271 }
272
273 fn getFoo() anyerror!?Foo {
274 return Foo{ .a = 1234 };
275 }
276
277 fn getFoo2() anyerror!?Foo {
278 return error.Failure;
279 }
280
281 fn getFoo3() anyerror!?Foo {
282 return null;
283 }
284 };
285 expect((try S.errorable()) == 1234);
286 expectError(error.Failure, S.errorable2());
287 expectError(error.Other, S.errorable3());
288 comptime {
289 expect((try S.errorable()) == 1234);
290 expectError(error.Failure, S.errorable2());
291 expectError(error.Other, S.errorable3());
292 }
293}
294294
295295test "widen cast integer payload of error union function call" {
296296 const S = struct {