From 21a6a1b0f2d7241594a9aa123e48cf2e3ebaccb9 Mon Sep 17 00:00:00 2001 From: mlugg Date: Thu, 18 Apr 2024 00:15:53 +0100 Subject: [PATCH] Sema: cap depth of value printing in type names Certain types (notably, `std.ComptimeStringMap`) were resulting in excessively long type names when instantiated, which in turn resulted in excessively long symbol names. These are problematic for two reasons: * Symbol names are sometimes read by humans -- they ought to be readable. * Some other applications (looking at you, xcode) trip on very long symbol names. To work around this for now, we cap the depth of value printing at 1, as opposed to the normal 3. This doesn't guarantee anything -- there could still be, for instance, an incredibly long aggregate -- but it works around the issue in practice for the time being. --- src/Sema.zig | 44 +++++++++++++++++++++++++++----------------- src/Value.zig | 5 +++++ src/print_value.zig | 5 +++-- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 52ca2a531d..d80cf81888 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2869,14 +2869,14 @@ fn createAnonymousDeclTypeNamed( anon_prefix: []const u8, inst: ?Zir.Inst.Index, ) !InternPool.DeclIndex { - const mod = sema.mod; - const ip = &mod.intern_pool; + const zcu = sema.mod; + const ip = &zcu.intern_pool; const gpa = sema.gpa; const namespace = block.namespace; - const src_decl = mod.declPtr(block.src_decl); + const src_decl = zcu.declPtr(block.src_decl); const src_node = src_decl.relativeToNodeIndex(src.node_offset.x); - const new_decl_index = try mod.allocateNewDecl(namespace, src_node); - errdefer mod.destroyDecl(new_decl_index); + const new_decl_index = try zcu.allocateNewDecl(namespace, src_node); + errdefer zcu.destroyDecl(new_decl_index); switch (name_strategy) { .anon => { @@ -2887,15 +2887,15 @@ fn createAnonymousDeclTypeNamed( // This name is also used as the key in the parent namespace so it cannot be // renamed. - const name = mod.intern_pool.getOrPutStringFmt(gpa, "{}__{s}_{d}", .{ - src_decl.name.fmt(&mod.intern_pool), anon_prefix, @intFromEnum(new_decl_index), + const name = ip.getOrPutStringFmt(gpa, "{}__{s}_{d}", .{ + src_decl.name.fmt(ip), anon_prefix, @intFromEnum(new_decl_index), }, .no_embedded_nulls) catch unreachable; - try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name); + try zcu.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name); return new_decl_index; }, .parent => { - const name = mod.declPtr(block.src_decl).name; - try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name); + const name = zcu.declPtr(block.src_decl).name; + try zcu.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name); return new_decl_index; }, .func => { @@ -2906,7 +2906,7 @@ fn createAnonymousDeclTypeNamed( defer buf.deinit(); const writer = buf.writer(); - try writer.print("{}(", .{mod.declPtr(block.src_decl).name.fmt(&mod.intern_pool)}); + try writer.print("{}(", .{zcu.declPtr(block.src_decl).name.fmt(ip)}); var arg_i: usize = 0; for (fn_info.param_body) |zir_inst| switch (zir_tags[@intFromEnum(zir_inst)]) { @@ -2921,7 +2921,17 @@ fn createAnonymousDeclTypeNamed( return sema.createAnonymousDeclTypeNamed(block, src, val, .anon, anon_prefix, null); if (arg_i != 0) try writer.writeByte(','); - try writer.print("{}", .{arg_val.fmtValue(sema.mod, sema)}); + + // Limiting the depth here helps avoid type names getting too long, which + // in turn helps to avoid unreasonably long symbol names for namespaced + // symbols. Such names should ideally be human-readable, and additionally, + // some tooling may not support very long symbol names. + try writer.print("{}", .{Value.fmtValueFull(.{ + .val = arg_val, + .mod = zcu, + .opt_sema = sema, + .depth = 1, + })}); arg_i += 1; continue; @@ -2930,8 +2940,8 @@ fn createAnonymousDeclTypeNamed( }; try writer.writeByte(')'); - const name = try mod.intern_pool.getOrPutString(gpa, buf.items, .no_embedded_nulls); - try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name); + const name = try ip.getOrPutString(gpa, buf.items, .no_embedded_nulls); + try zcu.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name); return new_decl_index; }, .dbg_var => { @@ -2942,10 +2952,10 @@ fn createAnonymousDeclTypeNamed( .dbg_var_ptr, .dbg_var_val => { if (zir_data[i].str_op.operand != ref) continue; - const name = try mod.intern_pool.getOrPutStringFmt(gpa, "{}.{s}", .{ - src_decl.name.fmt(&mod.intern_pool), zir_data[i].str_op.getStr(sema.code), + const name = try ip.getOrPutStringFmt(gpa, "{}.{s}", .{ + src_decl.name.fmt(ip), zir_data[i].str_op.getStr(sema.code), }, .no_embedded_nulls); - try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name); + try zcu.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name); return new_decl_index; }, else => {}, diff --git a/src/Value.zig b/src/Value.zig index 2d140073da..99817d79a9 100644 --- a/src/Value.zig +++ b/src/Value.zig @@ -44,9 +44,14 @@ pub fn fmtValue(val: Value, mod: *Module, opt_sema: ?*Sema) std.fmt.Formatter(pr .val = val, .mod = mod, .opt_sema = opt_sema, + .depth = 3, } }; } +pub fn fmtValueFull(ctx: print_value.FormatContext) std.fmt.Formatter(print_value.format) { + return .{ .data = ctx }; +} + /// Converts `val` to a null-terminated string stored in the InternPool. /// Asserts `val` is an array of `u8` pub fn toIpString(val: Value, ty: Type, mod: *Module) !InternPool.NullTerminatedString { diff --git a/src/print_value.zig b/src/print_value.zig index 9c9e2fd718..06ac9f4cd1 100644 --- a/src/print_value.zig +++ b/src/print_value.zig @@ -14,10 +14,11 @@ const Target = std.Target; const max_aggregate_items = 100; const max_string_len = 256; -const FormatContext = struct { +pub const FormatContext = struct { val: Value, mod: *Module, opt_sema: ?*Sema, + depth: u8, }; pub fn format( @@ -28,7 +29,7 @@ pub fn format( ) !void { _ = options; comptime std.debug.assert(fmt.len == 0); - return print(ctx.val, writer, 3, ctx.mod, ctx.opt_sema) catch |err| switch (err) { + return print(ctx.val, writer, ctx.depth, ctx.mod, ctx.opt_sema) catch |err| switch (err) { error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function error.ComptimeBreak, error.ComptimeReturn => unreachable, error.AnalysisFail, error.NeededSourceLocation => unreachable, // TODO: re-evaluate when we use `opt_sema` more fully