Skip to content

stage2: builtin.returnError is always called, whether returning an error or not #12174

Description

@topolarity

Zig Version

0.10.0-dev.3040+0efc6a35b

Steps to Reproduce

pub fn main() error{Apple, Banana}!u8 {
    var x: u8 = 1;
    return x;
}

zig build-exe test.zig --verbose-air shows:

# Begin Function AIR: test.main:
  ...

  %0!= dbg_block_begin()
  %1!= dbg_stmt(2:5)
  %2 = alloc(*u8)
  %4!= store(%2, %3!)
  %6!= dbg_var_ptr(%2, x)
  %7!= dbg_stmt(3:5)
  %9 = load(u8, %2!)
  %11 = wrap_errunion_payload(error{Apple,Banana}!u8, %9!)
  %12!= dbg_block_end()
  %27 = err_return_trace(?*builtin.StackTrace)
  %28!= call_never_inline(%19!, [%27!])
  %29!= ret(%11!)
# End Function AIR: test.main

and indeed, calls returnError with a non-null pointer:

; Function Attrs: nounwind
define internal fastcc void @test.main({ i16, i8, [1 x i8] }* noalias nonnull sret({ i16, i8, [1 x i8] }) %0, %builtin.StackTrace* nonnull %1) unnamed_addr #0 {
Entry:
  ...
  call fastcc void @builtin.returnError(%builtin.StackTrace* %1) #11
  %7 = bitcast { i16, i8, [1 x i8] }* %0 to i8*
  %8 = bitcast { i16, i8, [1 x i8] }* %2 to i8*
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 2 %7, i8* align 2 %8, i64 4, i1 false)
  ret void
}

Expected Behavior

main should not call returnError (or if it does, the argument should be null)

Actual Behavior

main calls returnError unconditionally

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions