@@ -1427,6 +1427,7 @@ pub const Fn = struct {
14271427 state : Analysis ,
14281428 is_cold : bool = false ,
14291429 is_noinline : bool = false ,
1430+ calls_or_awaits_errorable_fn : bool = false ,
14301431
14311432 /// Any inferred error sets that this function owns, both its own inferred error set and
14321433 /// inferred error sets of any inline/comptime functions called. Not to be confused
@@ -4838,6 +4839,9 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
48384839 };
48394840 defer sema .deinit ();
48404841
4842+ // reset in case calls to errorable functions are removed.
4843+ func .calls_or_awaits_errorable_fn = false ;
4844+
48414845 // First few indexes of extra are reserved and set at the end.
48424846 const reserved_count = @typeInfo (Air .ExtraIndex ).Enum .fields .len ;
48434847 try sema .air_extra .ensureTotalCapacity (gpa , reserved_count );
@@ -4936,6 +4940,8 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
49364940 func .state = .in_progress ;
49374941 log .debug ("set {s} to in_progress" , .{decl .name });
49384942
4943+ const last_arg_index = inner_block .instructions .items .len ;
4944+
49394945 sema .analyzeBody (& inner_block , fn_info .body ) catch | err | switch (err ) {
49404946 // TODO make these unreachable instead of @panic
49414947 error .NeededSourceLocation = > @panic ("zig compiler bug: NeededSourceLocation" ),
@@ -4944,6 +4950,21 @@ pub fn analyzeFnBody(mod: *Module, func: *Fn, arena: Allocator) SemaError!Air {
49444950 else = > | e | return e ,
49454951 };
49464952
4953+ // If we don't get an error return trace from a caller, create our own.
4954+ if (func .calls_or_awaits_errorable_fn and
4955+ mod .comp .bin_file .options .error_return_tracing and
4956+ ! sema .fn_ret_ty .isError ())
4957+ {
4958+ sema .setupErrorReturnTrace (& inner_block , last_arg_index ) catch | err | switch (err ) {
4959+ // TODO make these unreachable instead of @panic
4960+ error .NeededSourceLocation = > @panic ("zig compiler bug: NeededSourceLocation" ),
4961+ error .GenericPoison = > @panic ("zig compiler bug: GenericPoison" ),
4962+ error .ComptimeReturn = > @panic ("zig compiler bug: ComptimeReturn" ),
4963+ error .ComptimeBreak = > @panic ("zig compiler bug: ComptimeBreak" ),
4964+ else = > | e | return e ,
4965+ };
4966+ }
4967+
49474968 try wip_captures .finalize ();
49484969
49494970 // Copy the block into place and mark that as the main block.
0 commit comments