@@ -3328,7 +3328,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
3328
3328
if (comp .zcu ) | zcu | zcu_errors : {
3329
3329
for (zcu .failed_files .keys (), zcu .failed_files .values ()) | file , error_msg | {
3330
3330
if (error_msg ) | msg | {
3331
- try addModuleErrorMsg (zcu , & bundle , msg .* );
3331
+ try addModuleErrorMsg (zcu , & bundle , msg .* , false );
3332
3332
} else {
3333
3333
// Must be ZIR or Zoir errors. Note that this may include AST errors.
3334
3334
_ = try file .getTree (gpa ); // Tree must be loaded.
@@ -3378,6 +3378,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
3378
3378
break :s entries .slice ();
3379
3379
};
3380
3380
defer sorted_failed_analysis .deinit (gpa );
3381
+ var added_any_analysis_error = false ;
3381
3382
for (sorted_failed_analysis .items (.key ), sorted_failed_analysis .items (.value )) | anal_unit , error_msg | {
3382
3383
if (comp .incremental ) {
3383
3384
const refs = try zcu .resolveReferences ();
@@ -3389,7 +3390,9 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
3389
3390
zcu .fmtAnalUnit (anal_unit ),
3390
3391
});
3391
3392
3392
- try addModuleErrorMsg (zcu , & bundle , error_msg .* );
3393
+ try addModuleErrorMsg (zcu , & bundle , error_msg .* , added_any_analysis_error );
3394
+ added_any_analysis_error = true ;
3395
+
3393
3396
if (zcu .cimport_errors .get (anal_unit )) | errors | {
3394
3397
for (errors .getMessages ()) | err_msg_index | {
3395
3398
const err_msg = errors .getErrorMessage (err_msg_index );
@@ -3412,13 +3415,13 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
3412
3415
}
3413
3416
}
3414
3417
for (zcu .failed_codegen .values ()) | error_msg | {
3415
- try addModuleErrorMsg (zcu , & bundle , error_msg .* );
3418
+ try addModuleErrorMsg (zcu , & bundle , error_msg .* , false );
3416
3419
}
3417
3420
for (zcu .failed_types .values ()) | error_msg | {
3418
- try addModuleErrorMsg (zcu , & bundle , error_msg .* );
3421
+ try addModuleErrorMsg (zcu , & bundle , error_msg .* , false );
3419
3422
}
3420
3423
for (zcu .failed_exports .values ()) | value | {
3421
- try addModuleErrorMsg (zcu , & bundle , value .* );
3424
+ try addModuleErrorMsg (zcu , & bundle , value .* , false );
3422
3425
}
3423
3426
3424
3427
const actual_error_count = zcu .intern_pool .global_error_set .getNamesFromMainThread ().len ;
@@ -3527,7 +3530,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
3527
3530
// We don't actually include the error here if `!include_compile_log_sources`.
3528
3531
// The sorting above was still necessary, though, to get `log_text` in the right order.
3529
3532
if (include_compile_log_sources ) {
3530
- try addModuleErrorMsg (zcu , & bundle , messages .items [0 ]);
3533
+ try addModuleErrorMsg (zcu , & bundle , messages .items [0 ], false );
3531
3534
}
3532
3535
3533
3536
break :compile_log_text try log_text .toOwnedSlice (gpa );
@@ -3631,10 +3634,14 @@ pub const ErrorNoteHashContext = struct {
3631
3634
}
3632
3635
};
3633
3636
3637
+ const default_reference_trace_len = 2 ;
3634
3638
pub fn addModuleErrorMsg (
3635
3639
zcu : * Zcu ,
3636
3640
eb : * ErrorBundle.Wip ,
3637
3641
module_err_msg : Zcu.ErrorMsg ,
3642
+ /// If `-freference-trace` is not specified, we only want to show the one reference trace.
3643
+ /// So, this is whether we have already emitted an error with a reference trace.
3644
+ already_added_error : bool ,
3638
3645
) ! void {
3639
3646
const gpa = eb .gpa ;
3640
3647
const ip = & zcu .intern_pool ;
@@ -3657,45 +3664,44 @@ pub fn addModuleErrorMsg(
3657
3664
var ref_traces : std .ArrayListUnmanaged (ErrorBundle .ReferenceTrace ) = .empty ;
3658
3665
defer ref_traces .deinit (gpa );
3659
3666
3660
- if (module_err_msg .reference_trace_root .unwrap ()) | rt_root | {
3667
+ rt : {
3668
+ const rt_root = module_err_msg .reference_trace_root .unwrap () orelse break :rt ;
3669
+ const max_references = zcu .comp .reference_trace orelse refs : {
3670
+ if (already_added_error ) break :rt ;
3671
+ break :refs default_reference_trace_len ;
3672
+ };
3673
+
3661
3674
const all_references = try zcu .resolveReferences ();
3662
3675
3663
3676
var seen : std .AutoHashMapUnmanaged (InternPool.AnalUnit , void ) = .empty ;
3664
3677
defer seen .deinit (gpa );
3665
3678
3666
- const max_references = zcu .comp .reference_trace orelse Sema .default_reference_trace_len ;
3667
-
3668
3679
var referenced_by = rt_root ;
3669
3680
while (all_references .get (referenced_by )) | maybe_ref | {
3670
3681
const ref = maybe_ref orelse break ;
3671
3682
const gop = try seen .getOrPut (gpa , ref .referencer );
3672
3683
if (gop .found_existing ) break ;
3673
- if (ref_traces .items .len < max_references ) skip : {
3674
- const src = ref .src .upgrade (zcu );
3675
- const source = try src .file_scope .getSource (gpa );
3676
- const span = try src .span (gpa );
3677
- const loc = std .zig .findLineColumn (source .bytes , span .main );
3678
- const rt_file_path = try src .file_scope .fullPath (gpa );
3679
- defer gpa .free (rt_file_path );
3680
- const name = switch (ref .referencer .unwrap ()) {
3684
+ if (ref_traces .items .len < max_references ) {
3685
+ var last_call_src = ref .src ;
3686
+ var opt_inline_frame = ref .inline_frame ;
3687
+ while (opt_inline_frame .unwrap ()) | inline_frame | {
3688
+ const f = inline_frame .ptr (zcu ).* ;
3689
+ const func_nav = ip .indexToKey (f .callee ).func .owner_nav ;
3690
+ const func_name = ip .getNav (func_nav ).name .toSlice (ip );
3691
+ try addReferenceTraceFrame (zcu , eb , & ref_traces , func_name , last_call_src , true );
3692
+ last_call_src = f .call_src ;
3693
+ opt_inline_frame = f .parent ;
3694
+ }
3695
+ const root_name : ? []const u8 = switch (ref .referencer .unwrap ()) {
3681
3696
.@"comptime" = > "comptime" ,
3682
3697
.nav_val , .nav_ty = > | nav | ip .getNav (nav ).name .toSlice (ip ),
3683
3698
.type = > | ty | Type .fromInterned (ty ).containerTypeName (ip ).toSlice (ip ),
3684
3699
.func = > | f | ip .getNav (zcu .funcInfo (f ).owner_nav ).name .toSlice (ip ),
3685
- .memoized_state = > break : skip ,
3700
+ .memoized_state = > null ,
3686
3701
};
3687
- try ref_traces .append (gpa , .{
3688
- .decl_name = try eb .addString (name ),
3689
- .src_loc = try eb .addSourceLocation (.{
3690
- .src_path = try eb .addString (rt_file_path ),
3691
- .span_start = span .start ,
3692
- .span_main = span .main ,
3693
- .span_end = span .end ,
3694
- .line = @intCast (loc .line ),
3695
- .column = @intCast (loc .column ),
3696
- .source_line = 0 ,
3697
- }),
3698
- });
3702
+ if (root_name ) | n | {
3703
+ try addReferenceTraceFrame (zcu , eb , & ref_traces , n , last_call_src , false );
3704
+ }
3699
3705
}
3700
3706
referenced_by = ref .referencer ;
3701
3707
}
@@ -3775,6 +3781,35 @@ pub fn addModuleErrorMsg(
3775
3781
}
3776
3782
}
3777
3783
3784
+ fn addReferenceTraceFrame (
3785
+ zcu : * Zcu ,
3786
+ eb : * ErrorBundle.Wip ,
3787
+ ref_traces : * std .ArrayListUnmanaged (ErrorBundle.ReferenceTrace ),
3788
+ name : []const u8 ,
3789
+ lazy_src : Zcu.LazySrcLoc ,
3790
+ inlined : bool ,
3791
+ ) ! void {
3792
+ const gpa = zcu .gpa ;
3793
+ const src = lazy_src .upgrade (zcu );
3794
+ const source = try src .file_scope .getSource (gpa );
3795
+ const span = try src .span (gpa );
3796
+ const loc = std .zig .findLineColumn (source .bytes , span .main );
3797
+ const rt_file_path = try src .file_scope .fullPath (gpa );
3798
+ defer gpa .free (rt_file_path );
3799
+ try ref_traces .append (gpa , .{
3800
+ .decl_name = try eb .printString ("{s}{s}" , .{ name , if (inlined ) " [inlined]" else "" }),
3801
+ .src_loc = try eb .addSourceLocation (.{
3802
+ .src_path = try eb .addString (rt_file_path ),
3803
+ .span_start = span .start ,
3804
+ .span_main = span .main ,
3805
+ .span_end = span .end ,
3806
+ .line = @intCast (loc .line ),
3807
+ .column = @intCast (loc .column ),
3808
+ .source_line = 0 ,
3809
+ }),
3810
+ });
3811
+ }
3812
+
3778
3813
pub fn addZirErrorMessages (eb : * ErrorBundle.Wip , file : * Zcu.File ) ! void {
3779
3814
const gpa = eb .gpa ;
3780
3815
const src_path = try file .fullPath (gpa );
0 commit comments