authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-24 15:15:20+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-24 16:30:37+03:00
logc4400e8aa58c9ba5e4cc51836a2cfa6c2e22bfcc
treeda0e7fe7edb37c0367964c1b0d43d94eabeb9543
parent8e4d0ae4f5c50621a402192f2ff7f9b156030257

AstGen: reset anon_name_strategy for sub expressions

Closes #12910

2 files changed, 57 insertions(+), 0 deletions(-)

src/AstGen.zig+32
...@@ -577,6 +577,12 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr...@@ -577,6 +577,12 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr
577 const node_datas = tree.nodes.items(.data);577 const node_datas = tree.nodes.items(.data);
578 const node_tags = tree.nodes.items(.tag);578 const node_tags = tree.nodes.items(.tag);
579579
580 const prev_anon_name_strategy = gz.anon_name_strategy;
581 defer gz.anon_name_strategy = prev_anon_name_strategy;
582 if (!nodeUsesAnonNameStrategy(tree, node)) {
583 gz.anon_name_strategy = .anon;
584 }
585
580 switch (node_tags[node]) {586 switch (node_tags[node]) {
581 .root => unreachable, // Top-level declaration.587 .root => unreachable, // Top-level declaration.
582 .@"usingnamespace" => unreachable, // Top-level declaration.588 .@"usingnamespace" => unreachable, // Top-level declaration.
...@@ -9344,6 +9350,32 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {...@@ -9344,6 +9350,32 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
9344 }9350 }
9345}9351}
93469352
9353/// Returns `true` if the node uses `gz.anon_name_strategy`.
9354fn nodeUsesAnonNameStrategy(tree: *const Ast, node: Ast.Node.Index) bool {
9355 const node_tags = tree.nodes.items(.tag);
9356 switch (node_tags[node]) {
9357 .container_decl,
9358 .container_decl_trailing,
9359 .container_decl_two,
9360 .container_decl_two_trailing,
9361 .container_decl_arg,
9362 .container_decl_arg_trailing,
9363 .tagged_union,
9364 .tagged_union_trailing,
9365 .tagged_union_two,
9366 .tagged_union_two_trailing,
9367 .tagged_union_enum_tag,
9368 .tagged_union_enum_tag_trailing,
9369 => return true,
9370 .builtin_call_two, .builtin_call_two_comma, .builtin_call, .builtin_call_comma => {
9371 const builtin_token = tree.nodes.items(.main_token)[node];
9372 const builtin_name = tree.tokenSlice(builtin_token);
9373 return std.mem.eql(u8, builtin_name, "@Type");
9374 },
9375 else => return false,
9376 }
9377}
9378
9347/// Applies `rl` semantics to `result`. Expressions which do not do their own handling of9379/// Applies `rl` semantics to `result`. Expressions which do not do their own handling of
9348/// result locations must call this function on their result.9380/// result locations must call this function on their result.
9349/// As an example, if the `ResultLoc` is `ptr`, it will write the result to the pointer.9381/// As an example, if the `ResultLoc` is `ptr`, it will write the result to the pointer.
test/behavior/typename.zig+25
...@@ -246,3 +246,28 @@ test "comptime parameters not converted to anytype in function type" {...@@ -246,3 +246,28 @@ test "comptime parameters not converted to anytype in function type" {
246 const T = fn (fn (type) void, void) void;246 const T = fn (fn (type) void, void) void;
247 try expectEqualStrings("fn(comptime fn(comptime type) void, void) void", @typeName(T));247 try expectEqualStrings("fn(comptime fn(comptime type) void, void) void", @typeName(T));
248}248}
249
250test "anon name strategy used in sub expression" {
251 if (builtin.zig_backend == .stage1) {
252 // stage1 uses line/column for the names but we're moving away from that for
253 // incremental compilation purposes.
254 return error.SkipZigTest;
255 }
256
257 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
258 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
259 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
260 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
261
262 const S = struct {
263 fn getTheName() []const u8 {
264 return struct {
265 const name = @typeName(@This());
266 }.name;
267 }
268 };
269 try expectEqualStringsIgnoreDigits(
270 "behavior.typename.test.anon name strategy used in sub expression.S.getTheName__struct_0",
271 S.getTheName(),
272 );
273}