Skip to content

Commit 4fc6eab

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 434936c commit 4fc6eab

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
@@ -486,7 +486,7 @@ pub const NullTerminatedString = enum(u32) {
486486
if (comptime std.mem.eql(u8, specifier, "")) {
487487
try writer.writeAll(s);
488488
} else if (comptime std.mem.eql(u8, specifier, "i")) {
489-
try writer.print("{}", .{std.zig.fmtId(s)});
489+
try writer.print("{p}", .{std.zig.fmtId(s)});
490490
} else @compileError("invalid format string '" ++ specifier ++ "' for '" ++ @typeName(NullTerminatedString) ++ "'");
491491
}
492492

src/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6940,7 +6940,7 @@ fn cmdFetch(
69406940
std.zig.fmtEscapes(&hex_digest),
69416941
});
69426942

6943-
const new_node_text = try std.fmt.allocPrint(arena, ".{} = {s},\n", .{
6943+
const new_node_text = try std.fmt.allocPrint(arena, ".{p_} = {s},\n", .{
69446944
std.zig.fmtId(name), new_node_init,
69456945
});
69466946

src/print_zir.zig

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

10021002
try self.writeInstRef(stream, extra.namespace);
1003-
try stream.print(", {}, ", .{std.zig.fmtId(decl_name)});
1003+
try stream.print(", {p}, ", .{std.zig.fmtId(decl_name)});
10041004
try self.writeInstRef(stream, extra.options);
10051005
try stream.writeAll(") ");
10061006
try self.writeSrc(stream, inst_data.src());
@@ -1236,7 +1236,7 @@ const Writer = struct {
12361236

12371237
const name = self.code.nullTerminatedString(output.data.name);
12381238
const constraint = self.code.nullTerminatedString(output.data.constraint);
1239-
try stream.print("output({}, \"{}\", ", .{
1239+
try stream.print("output({p}, \"{}\", ", .{
12401240
std.zig.fmtId(name), std.zig.fmtEscapes(constraint),
12411241
});
12421242
try self.writeFlag(stream, "->", is_type);
@@ -1255,7 +1255,7 @@ const Writer = struct {
12551255

12561256
const name = self.code.nullTerminatedString(input.data.name);
12571257
const constraint = self.code.nullTerminatedString(input.data.constraint);
1258-
try stream.print("input({}, \"{}\", ", .{
1258+
try stream.print("input({p}, \"{}\", ", .{
12591259
std.zig.fmtId(name), std.zig.fmtEscapes(constraint),
12601260
});
12611261
try self.writeInstRef(stream, input.data.operand);
@@ -1271,7 +1271,7 @@ const Writer = struct {
12711271
const str_index = self.code.extra[extra_i];
12721272
extra_i += 1;
12731273
const clobber = self.code.nullTerminatedString(@enumFromInt(str_index));
1274-
try stream.print("{}", .{std.zig.fmtId(clobber)});
1274+
try stream.print("{p}", .{std.zig.fmtId(clobber)});
12751275
if (i + 1 < clobbers_len) {
12761276
try stream.writeAll(", ");
12771277
}
@@ -1552,7 +1552,7 @@ const Writer = struct {
15521552
try self.writeFlag(stream, "comptime ", field.is_comptime);
15531553
if (field.name != .empty) {
15541554
const field_name = self.code.nullTerminatedString(field.name);
1555-
try stream.print("{}: ", .{std.zig.fmtId(field_name)});
1555+
try stream.print("{p}: ", .{std.zig.fmtId(field_name)});
15561556
} else {
15571557
try stream.print("@\"{d}\": ", .{i});
15581558
}
@@ -1731,7 +1731,7 @@ const Writer = struct {
17311731

17321732
try self.writeDocComment(stream, doc_comment_index);
17331733
try stream.writeByteNTimes(' ', self.indent);
1734-
try stream.print("{}", .{std.zig.fmtId(field_name)});
1734+
try stream.print("{p}", .{std.zig.fmtId(field_name)});
17351735

17361736
if (has_type) {
17371737
const field_type = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
@@ -1884,7 +1884,7 @@ const Writer = struct {
18841884
try self.writeDocComment(stream, doc_comment_index);
18851885

18861886
try stream.writeByteNTimes(' ', self.indent);
1887-
try stream.print("{}", .{std.zig.fmtId(field_name)});
1887+
try stream.print("{p}", .{std.zig.fmtId(field_name)});
18881888

18891889
if (has_tag_value) {
18901890
const tag_value_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
@@ -1979,7 +1979,7 @@ const Writer = struct {
19791979
const doc_comment_index: Zir.NullTerminatedString = @enumFromInt(self.code.extra[extra_index + 1]);
19801980
try self.writeDocComment(stream, doc_comment_index);
19811981
try stream.writeByteNTimes(' ', self.indent);
1982-
try stream.print("{},\n", .{std.zig.fmtId(name)});
1982+
try stream.print("{p},\n", .{std.zig.fmtId(name)});
19831983
}
19841984

19851985
self.indent -= 2;

0 commit comments

Comments
 (0)