Skip to content

Commit 4c393c7

Browse files
committed
Update usages of fmtId/isValidId
`{}` for decls `{p}` for enum fields `{p_}` for struct fields and in contexts following a `.` Elsewhere, `{p}` was used since it's equivalent to the old behavior.
1 parent d8e7eda commit 4c393c7

11 files changed

+77
-78
lines changed

lib/compiler/aro_translate_c/ast.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ const Context = struct {
828828
fn addIdentifier(c: *Context, bytes: []const u8) Allocator.Error!TokenIndex {
829829
if (std.zig.primitives.isPrimitive(bytes))
830830
return c.addTokenFmt(.identifier, "@\"{s}\"", .{bytes});
831-
return c.addTokenFmt(.identifier, "{s}", .{std.zig.fmtId(bytes)});
831+
return c.addTokenFmt(.identifier, "{p}", .{std.zig.fmtId(bytes)});
832832
}
833833

834834
fn listToSpan(c: *Context, list: []const NodeIndex) Allocator.Error!NodeSubRange {
@@ -2106,7 +2106,7 @@ fn renderRecord(c: *Context, node: Node) !NodeIndex {
21062106
members[1] = 0;
21072107

21082108
for (payload.fields, 0..) |field, i| {
2109-
const name_tok = try c.addTokenFmt(.identifier, "{s}", .{std.zig.fmtId(field.name)});
2109+
const name_tok = try c.addTokenFmt(.identifier, "{p}", .{std.zig.fmtId(field.name)});
21102110
_ = try c.addToken(.colon, ":");
21112111
const type_expr = try renderNode(c, field.type);
21122112

@@ -2199,7 +2199,7 @@ fn renderFieldAccess(c: *Context, lhs: NodeIndex, field_name: []const u8) !NodeI
21992199
.main_token = try c.addToken(.period, "."),
22002200
.data = .{
22012201
.lhs = lhs,
2202-
.rhs = try c.addTokenFmt(.identifier, "{s}", .{std.zig.fmtId(field_name)}),
2202+
.rhs = try c.addTokenFmt(.identifier, "{p}", .{std.zig.fmtId(field_name)}),
22032203
},
22042204
});
22052205
}

lib/std/Build/Step/Options.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fn printType(self: *Options, out: anytype, comptime T: type, value: T, indent: u
234234
try printEnum(self, out, T, info, indent);
235235

236236
if (name) |some| {
237-
try out.print("pub const {}: {s} = .{s};\n", .{
237+
try out.print("pub const {}: {} = .{p_};\n", .{
238238
std.zig.fmtId(some),
239239
std.zig.fmtId(@typeName(T)),
240240
std.zig.fmtId(@tagName(value)),
@@ -246,7 +246,7 @@ fn printType(self: *Options, out: anytype, comptime T: type, value: T, indent: u
246246
try printStruct(self, out, T, info, indent);
247247

248248
if (name) |some| {
249-
try out.print("pub const {}: {s} = ", .{
249+
try out.print("pub const {}: {} = ", .{
250250
std.zig.fmtId(some),
251251
std.zig.fmtId(@typeName(T)),
252252
});
@@ -279,7 +279,7 @@ fn printEnum(self: *Options, out: anytype, comptime T: type, comptime val: std.b
279279

280280
inline for (val.fields) |field| {
281281
try out.writeByteNTimes(' ', indent);
282-
try out.print(" {} = {d},\n", .{ std.zig.fmtId(field.name), field.value });
282+
try out.print(" {p} = {d},\n", .{ std.zig.fmtId(field.name), field.value });
283283
}
284284

285285
if (!val.is_exhaustive) {
@@ -313,9 +313,9 @@ fn printStruct(self: *Options, out: anytype, comptime T: type, comptime val: std
313313

314314
// If the type name doesn't contains a '.' the type is from zig builtins.
315315
if (std.mem.containsAtLeast(u8, type_name, 1, ".")) {
316-
try out.print(" {}: {}", .{ std.zig.fmtId(field.name), std.zig.fmtId(type_name) });
316+
try out.print(" {p_}: {}", .{ std.zig.fmtId(field.name), std.zig.fmtId(type_name) });
317317
} else {
318-
try out.print(" {}: {s}", .{ std.zig.fmtId(field.name), type_name });
318+
try out.print(" {p_}: {s}", .{ std.zig.fmtId(field.name), type_name });
319319
}
320320

321321
if (field.default_value != null) {
@@ -355,7 +355,7 @@ fn printStructValue(self: *Options, out: anytype, comptime struct_val: std.built
355355
} else {
356356
inline for (struct_val.fields) |field| {
357357
try out.writeByteNTimes(' ', indent);
358-
try out.print(" .{} = ", .{std.zig.fmtId(field.name)});
358+
try out.print(" .{p_} = ", .{std.zig.fmtId(field.name)});
359359

360360
const field_name = @field(val, field.name);
361361
switch (@typeInfo(@TypeOf(field_name))) {

lib/std/zig/render.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,8 +2847,8 @@ fn renderIdentifier(r: *Render, token_index: Ast.TokenIndex, space: Space, quote
28472847
return renderQuotedIdentifier(r, token_index, space, false);
28482848
}
28492849

2850-
// Special case for _ which would incorrectly be rejected by isValidId below.
2851-
if (contents.len == 1 and contents[0] == '_') switch (quote) {
2850+
// Special case for _.
2851+
if (std.zig.isUnderscore(contents)) switch (quote) {
28522852
.eagerly_unquote => return renderQuotedIdentifier(r, token_index, space, true),
28532853
.eagerly_unquote_except_underscore,
28542854
.preserve_when_shadowing,

src/Builtin.zig

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ pub fn append(opts: @This(), buffer: *std.ArrayList(u8)) Allocator.Error!void {
3535
\\/// feature detection (i.e. with `@hasDecl` or `@hasField`) over version checks.
3636
\\pub const zig_version = std.SemanticVersion.parse(zig_version_string) catch unreachable;
3737
\\pub const zig_version_string = "{s}";
38-
\\pub const zig_backend = std.builtin.CompilerBackend.{};
38+
\\pub const zig_backend = std.builtin.CompilerBackend.{p_};
3939
\\
40-
\\pub const output_mode = std.builtin.OutputMode.{};
41-
\\pub const link_mode = std.builtin.LinkMode.{};
40+
\\pub const output_mode = std.builtin.OutputMode.{p_};
41+
\\pub const link_mode = std.builtin.LinkMode.{p_};
4242
\\pub const is_test = {};
4343
\\pub const single_threaded = {};
44-
\\pub const abi = std.Target.Abi.{};
44+
\\pub const abi = std.Target.Abi.{p_};
4545
\\pub const cpu: std.Target.Cpu = .{{
46-
\\ .arch = .{},
47-
\\ .model = &std.Target.{}.cpu.{},
48-
\\ .features = std.Target.{}.featureSet(&[_]std.Target.{}.Feature{{
46+
\\ .arch = .{p_},
47+
\\ .model = &std.Target.{p_}.cpu.{p_},
48+
\\ .features = std.Target.{p_}.featureSet(&[_]std.Target.{p_}.Feature{{
4949
\\
5050
, .{
5151
build_options.version,
@@ -66,14 +66,14 @@ pub fn append(opts: @This(), buffer: *std.ArrayList(u8)) Allocator.Error!void {
6666
const index = @as(std.Target.Cpu.Feature.Set.Index, @intCast(index_usize));
6767
const is_enabled = target.cpu.features.isEnabled(index);
6868
if (is_enabled) {
69-
try buffer.writer().print(" .{},\n", .{std.zig.fmtId(feature.name)});
69+
try buffer.writer().print(" .{p_},\n", .{std.zig.fmtId(feature.name)});
7070
}
7171
}
7272
try buffer.writer().print(
7373
\\ }}),
7474
\\}};
7575
\\pub const os = std.Target.Os{{
76-
\\ .tag = .{},
76+
\\ .tag = .{p_},
7777
\\ .version_range = .{{
7878
,
7979
.{std.zig.fmtId(@tagName(target.os.tag))},
@@ -180,8 +180,8 @@ pub fn append(opts: @This(), buffer: *std.ArrayList(u8)) Allocator.Error!void {
180180
const link_libc = opts.link_libc;
181181

182182
try buffer.writer().print(
183-
\\pub const object_format = std.Target.ObjectFormat.{};
184-
\\pub const mode = std.builtin.OptimizeMode.{};
183+
\\pub const object_format = std.Target.ObjectFormat.{p_};
184+
\\pub const mode = std.builtin.OptimizeMode.{p_};
185185
\\pub const link_libc = {};
186186
\\pub const link_libcpp = {};
187187
\\pub const have_error_return_tracing = {};
@@ -190,7 +190,7 @@ pub fn append(opts: @This(), buffer: *std.ArrayList(u8)) Allocator.Error!void {
190190
\\pub const position_independent_code = {};
191191
\\pub const position_independent_executable = {};
192192
\\pub const strip_debug_info = {};
193-
\\pub const code_model = std.builtin.CodeModel.{};
193+
\\pub const code_model = std.builtin.CodeModel.{p_};
194194
\\pub const omit_frame_pointer = {};
195195
\\
196196
, .{
@@ -209,11 +209,10 @@ pub fn append(opts: @This(), buffer: *std.ArrayList(u8)) Allocator.Error!void {
209209
});
210210

211211
if (target.os.tag == .wasi) {
212-
const wasi_exec_model_fmt = std.zig.fmtId(@tagName(opts.wasi_exec_model));
213212
try buffer.writer().print(
214-
\\pub const wasi_exec_model = std.builtin.WasiExecModel.{};
213+
\\pub const wasi_exec_model = std.builtin.WasiExecModel.{p_};
215214
\\
216-
, .{wasi_exec_model_fmt});
215+
, .{std.zig.fmtId(@tagName(opts.wasi_exec_model))});
217216
}
218217

219218
if (opts.is_test) {

src/InternPool.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ pub const NullTerminatedString = enum(u32) {
488488
if (comptime std.mem.eql(u8, specifier, "")) {
489489
try writer.writeAll(s);
490490
} else if (comptime std.mem.eql(u8, specifier, "i")) {
491-
try writer.print("{}", .{std.zig.fmtId(s)});
491+
try writer.print("{p}", .{std.zig.fmtId(s)});
492492
} else @compileError("invalid format string '" ++ specifier ++ "' for '" ++ @typeName(NullTerminatedString) ++ "'");
493493
}
494494

src/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6943,7 +6943,7 @@ fn cmdFetch(
69436943
std.zig.fmtEscapes(&hex_digest),
69446944
});
69456945

6946-
const new_node_text = try std.fmt.allocPrint(arena, ".{} = {s},\n", .{
6946+
const new_node_text = try std.fmt.allocPrint(arena, ".{p_} = {s},\n", .{
69476947
std.zig.fmtId(name), new_node_init,
69486948
});
69496949

src/print_zir.zig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ const Writer = struct {
10051005
const decl_name = self.code.nullTerminatedString(extra.decl_name);
10061006

10071007
try self.writeInstRef(stream, extra.namespace);
1008-
try stream.print(", {}, ", .{std.zig.fmtId(decl_name)});
1008+
try stream.print(", {p}, ", .{std.zig.fmtId(decl_name)});
10091009
try self.writeInstRef(stream, extra.options);
10101010
try stream.writeAll(") ");
10111011
try self.writeSrc(stream, inst_data.src());
@@ -1243,7 +1243,7 @@ const Writer = struct {
12431243

12441244
const name = self.code.nullTerminatedString(output.data.name);
12451245
const constraint = self.code.nullTerminatedString(output.data.constraint);
1246-
try stream.print("output({}, \"{}\", ", .{
1246+
try stream.print("output({p}, \"{}\", ", .{
12471247
std.zig.fmtId(name), std.zig.fmtEscapes(constraint),
12481248
});
12491249
try self.writeFlag(stream, "->", is_type);
@@ -1262,7 +1262,7 @@ const Writer = struct {
12621262

12631263
const name = self.code.nullTerminatedString(input.data.name);
12641264
const constraint = self.code.nullTerminatedString(input.data.constraint);
1265-
try stream.print("input({}, \"{}\", ", .{
1265+
try stream.print("input({p}, \"{}\", ", .{
12661266
std.zig.fmtId(name), std.zig.fmtEscapes(constraint),
12671267
});
12681268
try self.writeInstRef(stream, input.data.operand);
@@ -1278,7 +1278,7 @@ const Writer = struct {
12781278
const str_index = self.code.extra[extra_i];
12791279
extra_i += 1;
12801280
const clobber = self.code.nullTerminatedString(@enumFromInt(str_index));
1281-
try stream.print("{}", .{std.zig.fmtId(clobber)});
1281+
try stream.print("{p}", .{std.zig.fmtId(clobber)});
12821282
if (i + 1 < clobbers_len) {
12831283
try stream.writeAll(", ");
12841284
}
@@ -1559,7 +1559,7 @@ const Writer = struct {
15591559
try self.writeFlag(stream, "comptime ", field.is_comptime);
15601560
if (field.name != .empty) {
15611561
const field_name = self.code.nullTerminatedString(field.name);
1562-
try stream.print("{}: ", .{std.zig.fmtId(field_name)});
1562+
try stream.print("{p}: ", .{std.zig.fmtId(field_name)});
15631563
} else {
15641564
try stream.print("@\"{d}\": ", .{i});
15651565
}
@@ -1738,7 +1738,7 @@ const Writer = struct {
17381738

17391739
try self.writeDocComment(stream, doc_comment_index);
17401740
try stream.writeByteNTimes(' ', self.indent);
1741-
try stream.print("{}", .{std.zig.fmtId(field_name)});
1741+
try stream.print("{p}", .{std.zig.fmtId(field_name)});
17421742

17431743
if (has_type) {
17441744
const field_type = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
@@ -1891,7 +1891,7 @@ const Writer = struct {
18911891
try self.writeDocComment(stream, doc_comment_index);
18921892

18931893
try stream.writeByteNTimes(' ', self.indent);
1894-
try stream.print("{}", .{std.zig.fmtId(field_name)});
1894+
try stream.print("{p}", .{std.zig.fmtId(field_name)});
18951895

18961896
if (has_tag_value) {
18971897
const tag_value_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
@@ -1986,7 +1986,7 @@ const Writer = struct {
19861986
const doc_comment_index: Zir.NullTerminatedString = @enumFromInt(self.code.extra[extra_index + 1]);
19871987
try self.writeDocComment(stream, doc_comment_index);
19881988
try stream.writeByteNTimes(' ', self.indent);
1989-
try stream.print("{},\n", .{std.zig.fmtId(name)});
1989+
try stream.print("{p},\n", .{std.zig.fmtId(name)});
19901990
}
19911991

19921992
self.indent -= 2;

0 commit comments

Comments
 (0)