aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-04-18 00:15:53 +0100
committerAndrew Kelley <andrew@ziglang.org>2024-04-17 22:47:54 -0700
commit21a6a1b0f2d7241594a9aa123e48cf2e3ebaccb9 (patch)
tree106fe6c9dfc11e73b3b3f04e5cc43d93b3da4685 /src/Sema.zig
parent187f0c1e262cbeb37d72c571381b5b94c1eb3a63 (diff)
downloadzig-21a6a1b0f2d7241594a9aa123e48cf2e3ebaccb9.tar.gz
zig-21a6a1b0f2d7241594a9aa123e48cf2e3ebaccb9.zip
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.
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig44
1 files changed, 27 insertions, 17 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 => {},