Merge pull request #16969 from jacobly0/no-clear-ref-trace

Sema: refactor to use fewer catch expressions
This commit is contained in:
Andrew Kelley 2023-08-29 16:13:39 -04:00 committed by GitHub
commit 49075d2055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1043 additions and 837 deletions

View File

@ -2858,51 +2858,52 @@ pub fn addModuleErrorMsg(mod: *Module, eb: *ErrorBundle.Wip, module_err_msg: Mod
var ref_traces: std.ArrayListUnmanaged(ErrorBundle.ReferenceTrace) = .{};
defer ref_traces.deinit(gpa);
for (module_err_msg.reference_trace) |module_reference| {
if (module_reference.hidden != 0) {
try ref_traces.append(gpa, .{
.decl_name = module_reference.hidden,
.src_loc = .none,
});
break;
} else if (module_reference.decl == .none) {
try ref_traces.append(gpa, .{
.decl_name = 0,
.src_loc = .none,
});
break;
const remaining_references: ?u32 = remaining: {
if (mod.comp.reference_trace) |_| {
if (module_err_msg.hidden_references > 0) break :remaining module_err_msg.hidden_references;
} else {
if (module_err_msg.reference_trace.len > 0) break :remaining 0;
}
break :remaining null;
};
try ref_traces.ensureTotalCapacityPrecise(gpa, module_err_msg.reference_trace.len +
@intFromBool(remaining_references != null));
for (module_err_msg.reference_trace) |module_reference| {
const source = try module_reference.src_loc.file_scope.getSource(gpa);
const span = try module_reference.src_loc.span(gpa);
const loc = std.zig.findLineColumn(source.bytes, span.main);
const rt_file_path = try module_reference.src_loc.file_scope.fullPath(gpa);
defer gpa.free(rt_file_path);
try ref_traces.append(gpa, .{
.decl_name = try eb.addString(ip.stringToSliceUnwrap(module_reference.decl).?),
ref_traces.appendAssumeCapacity(.{
.decl_name = try eb.addString(ip.stringToSlice(module_reference.decl)),
.src_loc = try eb.addSourceLocation(.{
.src_path = try eb.addString(rt_file_path),
.span_start = span.start,
.span_main = span.main,
.span_end = span.end,
.line = @as(u32, @intCast(loc.line)),
.column = @as(u32, @intCast(loc.column)),
.line = @intCast(loc.line),
.column = @intCast(loc.column),
.source_line = 0,
}),
});
}
if (remaining_references) |remaining| ref_traces.appendAssumeCapacity(
.{ .decl_name = remaining, .src_loc = .none },
);
const src_loc = try eb.addSourceLocation(.{
.src_path = try eb.addString(file_path),
.span_start = err_span.start,
.span_main = err_span.main,
.span_end = err_span.end,
.line = @as(u32, @intCast(err_loc.line)),
.column = @as(u32, @intCast(err_loc.column)),
.line = @intCast(err_loc.line),
.column = @intCast(err_loc.column),
.source_line = if (module_err_msg.src_loc.lazy == .entire_file)
0
else
try eb.addString(err_loc.source_line),
.reference_trace_len = @as(u32, @intCast(ref_traces.items.len)),
.reference_trace_len = @intCast(ref_traces.items.len),
});
for (ref_traces.items) |rt| {
@ -2928,8 +2929,8 @@ pub fn addModuleErrorMsg(mod: *Module, eb: *ErrorBundle.Wip, module_err_msg: Mod
.span_start = span.start,
.span_main = span.main,
.span_end = span.end,
.line = @as(u32, @intCast(loc.line)),
.column = @as(u32, @intCast(loc.column)),
.line = @intCast(loc.line),
.column = @intCast(loc.column),
.source_line = if (err_loc.eql(loc)) 0 else try eb.addString(loc.source_line),
}),
}, .{ .eb = eb });
@ -2938,7 +2939,7 @@ pub fn addModuleErrorMsg(mod: *Module, eb: *ErrorBundle.Wip, module_err_msg: Mod
}
}
const notes_len = @as(u32, @intCast(notes.entries.len));
const notes_len: u32 = @intCast(notes.entries.len);
try eb.addRootErrorMessage(.{
.msg = try eb.addString(module_err_msg.msg),

View File

@ -1519,11 +1519,11 @@ pub const ErrorMsg = struct {
msg: []const u8,
notes: []ErrorMsg = &.{},
reference_trace: []Trace = &.{},
hidden_references: u32 = 0,
pub const Trace = struct {
decl: InternPool.OptionalNullTerminatedString,
decl: InternPool.NullTerminatedString,
src_loc: SrcLoc,
hidden: u32 = 0,
};
pub fn create(
@ -4147,7 +4147,9 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 };
const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 };
const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 };
const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, "global variable initializer must be comptime-known");
const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, .{
.needed_comptime_reason = "global variable initializer must be comptime-known",
});
// Note this resolves the type of the Decl, not the value; if this Decl
// is a struct, for example, this resolves `type` (which needs no resolution),
@ -4257,7 +4259,9 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
decl.@"linksection" = blk: {
const linksection_ref = decl.zirLinksectionRef(mod);
if (linksection_ref == .none) break :blk .none;
const bytes = try sema.resolveConstString(&block_scope, section_src, linksection_ref, "linksection must be comptime-known");
const bytes = try sema.resolveConstString(&block_scope, section_src, linksection_ref, .{
.needed_comptime_reason = "linksection must be comptime-known",
});
if (mem.indexOfScalar(u8, bytes, 0) != null) {
return sema.fail(&block_scope, section_src, "linksection cannot contain null bytes", .{});
} else if (bytes.len == 0) {

File diff suppressed because it is too large Load Diff

View File

@ -11,8 +11,25 @@ pub export fn entry() void {
_ = foo(x) == 20;
}
inline fn first() void {
second();
}
inline fn second() void {
third();
}
inline fn third() void {
first();
}
pub export fn entry2() void {
first();
}
// error
// backend=stage2
// target=native
//
// :5:27: error: inline call is recursive
// :23:10: error: inline call is recursive