Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AstGen: properly generate errdefer expressions when returning #8821

Closed
andrewrk opened this issue May 18, 2021 · 0 comments
Closed

AstGen: properly generate errdefer expressions when returning #8821

andrewrk opened this issue May 18, 2021 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@andrewrk
Copy link
Member

test {
    try b();
}

fn b() !void {
    errdefer c();
    return d();
}

fn c() void {}
fn d() !void {
    return error.ItBroke;
}

b is incorrectly missing a call to c:

%36 = extended(struct_decl(parent, Auto, {
  test line(0) hash(7a16482b43fe07ab27b060b2e4fde1b2): %0 = block_inline({
    %13 = extended(func(test, [], @Ref.void_type, inferror, {
      %1 = dbg_stmt(1, 4)
      %6 = block({
        %2 = decl_val("b") token_offset:2:9
        %3 = call(%2, []) node_offset:2:10
        %4 = is_err(%3) node_offset:2:5
        %5 = condbr(%4, {
          %7 = err_union_code(%3) node_offset:2:5
          %8 = ret_node(%7) node_offset:2:5
        }, {
          %9 = err_union_payload_unsafe(%3) node_offset:2:5
          %10 = break(%6, %9)
        }) node_offset:2:5
      }) node_offset:2:5
      %11 = ensure_result_used(%6) node_offset:2:5
      %12 = ret_coerce(@Ref.void_value) token_offset:3:1
    }) (lbrace=0:5,rbrace=2:0) node_offset:1:1
    %14 = break_inline(%0, %13)
  }) node_offset:1:1
  [85] b line(4) hash(63555bdd8ed37e1f2091af1bd701efa2): %15 = block_inline({
    %22 = func_inferred([], @Ref.void_type, inferror, {
      %16 = dbg_stmt(2, 4)
      %17 = extended(ret_ptr()) node_offset:7:5
      %18 = decl_val("d") token_offset:7:12
      %19 = call(%18, []) node_offset:7:13
      %20 = store_node(%17, %19) node_offset:7:13
      %21 = ret_node(%19) node_offset:7:5
    }) (lbrace=0:13,rbrace=3:0) node_offset:5:1
    %23 = break_inline(%15, %22)
  }) node_offset:5:1
  [92] c line(9) hash(2ade917b8710803e72e76bed4b8facb1): %24 = block_inline({
    %26 = func([], @Ref.void_type, {
      %25 = ret_coerce(@Ref.void_value) token_offset:10:14
    }) (lbrace=0:12,rbrace=0:1) node_offset:10:1
    %27 = break_inline(%24, %26)
  }) node_offset:10:1
  [99] d line(10) hash(e5c856032dd7aba1c16dec4fb2d2081a): %28 = block_inline({
    %34 = func_inferred([], @Ref.void_type, inferror, {
      %29 = dbg_stmt(1, 4)
      %30 = extended(ret_type()) node_offset:12:5
      %31 = error_value("ItBroke") token_offset:12:18
      %32 = as_node(%30, %31) node_offset:12:12
      %33 = ret_node(%32) node_offset:12:5
    }) (lbrace=0:13,rbrace=2:0) node_offset:11:1
    %35 = break_inline(%28, %34)
  }) node_offset:11:1
}, {}, {})

Make sure when it is return error.Foo that it does not needlessly emit branching logic.

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels May 18, 2021
@andrewrk andrewrk added this to the 0.8.0 milestone May 18, 2021
@andrewrk andrewrk modified the milestones: 0.8.0, 0.8.1 Jun 4, 2021
andrewrk added a commit that referenced this issue Jun 20, 2021
`return` statements use a new function `nodeMayEvalToError` which does
some basic checks on the AST node to return never, always, or maybe.

Depending on this result, AstGen skips the errdefers, always includes
the errdefers, or emits a conditional branch to check whether the return
value is an error that Sema will have to evaluate.

Closes #8821
Unblocks #9047
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

1 participant