Skip to content

Commit 9064907

Browse files
authored
Merge pull request ziglang#23907 from mlugg/ref-trace
compiler: reference trace fixes
2 parents 9279ff8 + 46d7e80 commit 9064907

29 files changed

+253
-150
lines changed

lib/compiler/build_runner.zig

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ fn runStepNames(
750750
if (run.prominent_compile_errors and total_compile_errors > 0) {
751751
for (step_stack.keys()) |s| {
752752
if (s.result_error_bundle.errorMessageCount() > 0) {
753-
s.result_error_bundle.renderToStdErr(.{ .ttyconf = ttyconf, .include_reference_trace = (b.reference_trace orelse 0) > 0 });
753+
s.result_error_bundle.renderToStdErr(.{ .ttyconf = ttyconf });
754754
}
755755
}
756756

@@ -1129,11 +1129,7 @@ fn workerMakeOneStep(
11291129
defer std.debug.unlockStdErr();
11301130

11311131
const gpa = b.allocator;
1132-
const options: std.zig.ErrorBundle.RenderOptions = .{
1133-
.ttyconf = run.ttyconf,
1134-
.include_reference_trace = (b.reference_trace orelse 0) > 0,
1135-
};
1136-
printErrorMessages(gpa, s, options, run.stderr, run.prominent_compile_errors) catch {};
1132+
printErrorMessages(gpa, s, .{ .ttyconf = run.ttyconf }, run.stderr, run.prominent_compile_errors) catch {};
11371133
}
11381134

11391135
handle_result: {

src/Compilation.zig

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,7 +3328,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33283328
if (comp.zcu) |zcu| zcu_errors: {
33293329
for (zcu.failed_files.keys(), zcu.failed_files.values()) |file, error_msg| {
33303330
if (error_msg) |msg| {
3331-
try addModuleErrorMsg(zcu, &bundle, msg.*);
3331+
try addModuleErrorMsg(zcu, &bundle, msg.*, false);
33323332
} else {
33333333
// Must be ZIR or Zoir errors. Note that this may include AST errors.
33343334
_ = try file.getTree(gpa); // Tree must be loaded.
@@ -3378,6 +3378,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33783378
break :s entries.slice();
33793379
};
33803380
defer sorted_failed_analysis.deinit(gpa);
3381+
var added_any_analysis_error = false;
33813382
for (sorted_failed_analysis.items(.key), sorted_failed_analysis.items(.value)) |anal_unit, error_msg| {
33823383
if (comp.incremental) {
33833384
const refs = try zcu.resolveReferences();
@@ -3389,7 +3390,9 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
33893390
zcu.fmtAnalUnit(anal_unit),
33903391
});
33913392

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+
33933396
if (zcu.cimport_errors.get(anal_unit)) |errors| {
33943397
for (errors.getMessages()) |err_msg_index| {
33953398
const err_msg = errors.getErrorMessage(err_msg_index);
@@ -3412,13 +3415,13 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
34123415
}
34133416
}
34143417
for (zcu.failed_codegen.values()) |error_msg| {
3415-
try addModuleErrorMsg(zcu, &bundle, error_msg.*);
3418+
try addModuleErrorMsg(zcu, &bundle, error_msg.*, false);
34163419
}
34173420
for (zcu.failed_types.values()) |error_msg| {
3418-
try addModuleErrorMsg(zcu, &bundle, error_msg.*);
3421+
try addModuleErrorMsg(zcu, &bundle, error_msg.*, false);
34193422
}
34203423
for (zcu.failed_exports.values()) |value| {
3421-
try addModuleErrorMsg(zcu, &bundle, value.*);
3424+
try addModuleErrorMsg(zcu, &bundle, value.*, false);
34223425
}
34233426

34243427
const actual_error_count = zcu.intern_pool.global_error_set.getNamesFromMainThread().len;
@@ -3527,7 +3530,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
35273530
// We don't actually include the error here if `!include_compile_log_sources`.
35283531
// The sorting above was still necessary, though, to get `log_text` in the right order.
35293532
if (include_compile_log_sources) {
3530-
try addModuleErrorMsg(zcu, &bundle, messages.items[0]);
3533+
try addModuleErrorMsg(zcu, &bundle, messages.items[0], false);
35313534
}
35323535

35333536
break :compile_log_text try log_text.toOwnedSlice(gpa);
@@ -3631,10 +3634,14 @@ pub const ErrorNoteHashContext = struct {
36313634
}
36323635
};
36333636

3637+
const default_reference_trace_len = 2;
36343638
pub fn addModuleErrorMsg(
36353639
zcu: *Zcu,
36363640
eb: *ErrorBundle.Wip,
36373641
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,
36383645
) !void {
36393646
const gpa = eb.gpa;
36403647
const ip = &zcu.intern_pool;
@@ -3657,45 +3664,44 @@ pub fn addModuleErrorMsg(
36573664
var ref_traces: std.ArrayListUnmanaged(ErrorBundle.ReferenceTrace) = .empty;
36583665
defer ref_traces.deinit(gpa);
36593666

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+
36613674
const all_references = try zcu.resolveReferences();
36623675

36633676
var seen: std.AutoHashMapUnmanaged(InternPool.AnalUnit, void) = .empty;
36643677
defer seen.deinit(gpa);
36653678

3666-
const max_references = zcu.comp.reference_trace orelse Sema.default_reference_trace_len;
3667-
36683679
var referenced_by = rt_root;
36693680
while (all_references.get(referenced_by)) |maybe_ref| {
36703681
const ref = maybe_ref orelse break;
36713682
const gop = try seen.getOrPut(gpa, ref.referencer);
36723683
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()) {
36813696
.@"comptime" => "comptime",
36823697
.nav_val, .nav_ty => |nav| ip.getNav(nav).name.toSlice(ip),
36833698
.type => |ty| Type.fromInterned(ty).containerTypeName(ip).toSlice(ip),
36843699
.func => |f| ip.getNav(zcu.funcInfo(f).owner_nav).name.toSlice(ip),
3685-
.memoized_state => break :skip,
3700+
.memoized_state => null,
36863701
};
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+
}
36993705
}
37003706
referenced_by = ref.referencer;
37013707
}
@@ -3775,6 +3781,35 @@ pub fn addModuleErrorMsg(
37753781
}
37763782
}
37773783

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+
37783813
pub fn addZirErrorMessages(eb: *ErrorBundle.Wip, file: *Zcu.File) !void {
37793814
const gpa = eb.gpa;
37803815
const src_path = try file.fullPath(gpa);

0 commit comments

Comments
 (0)