From 03537405283ee834ef2244a6ee3af257d47b4bc5 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Wed, 20 Nov 2024 11:14:54 +0100 Subject: [PATCH] fix position context crash on doc comment followed by an enum literal Thank you zigtools/sus --- src/analysis.zig | 6 +++--- tests/utility/position_context.zig | 33 ++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index 65db2cda6..889468283 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -3570,9 +3570,9 @@ pub fn getPositionContext( .period, .period_asterisk => switch (curr_ctx.ctx) { .empty => curr_ctx.ctx = .{ .enum_literal = tok.loc }, .enum_literal => curr_ctx.ctx = .empty, - .keyword => return .other, // no keyword can be `.`/`.*` accessed - .other, .field_access, .global_error_set => {}, - else => curr_ctx.ctx = .{ .field_access = tokenLocAppend(curr_ctx.ctx.loc().?, tok) }, + .keyword => curr_ctx.ctx = .other, // no keyword can be `.`/`.*` accessed + .comment, .other, .field_access, .global_error_set => {}, + else => curr_ctx.ctx = .{ .field_access = tokenLocAppend(curr_ctx.ctx.loc() orelse tok.loc, tok) }, }, .question_mark => switch (curr_ctx.ctx) { .field_access => {}, diff --git a/tests/utility/position_context.zig b/tests/utility/position_context.zig index 927683fa6..61b59c8b9 100644 --- a/tests/utility/position_context.zig +++ b/tests/utility/position_context.zig @@ -188,6 +188,23 @@ test "field access" { , .field_access, .{}); } +test "field access across multiple lines" { + // ErrorBuilder doesn't support locs across multiple lines so don't let the test fail :) + try testContext( + \\test { + \\ item + \\ .foo() + \\ .bar() + \\ .baz(); + \\} + , .field_access, .{}); + + try testContext( + \\/// some comment + \\ .foo() + , .var_access, .{}); +} + test "builtin" { try testContext( \\var foo = @ @@ -506,20 +523,20 @@ fn testContext(source: []const u8, expected_tag: std.meta.Tag(Analyser.PositionC if (lookahead) "with lookahead" else "without lookahead", }); - if (ctx.loc()) |actual_loc| { - try error_builder.msgAtLoc("actual range here", "file.zig", actual_loc, .info, .{}); - } - - if (expected_loc) |expected| { - try error_builder.msgAtLoc("expected range here", "file.zig", expected, .info, .{}); - } - if (std.meta.activeTag(ctx) != expected_tag) { std.debug.print("Expected tag `{s}`, got `{s}`\n", .{ @tagName(expected_tag), @tagName(std.meta.activeTag(ctx)) }); return error.DifferentTag; } if (!std.meta.eql(expected_loc, ctx.loc())) { + if (ctx.loc()) |actual_loc| { + try error_builder.msgAtLoc("actual range here", "file.zig", actual_loc, .info, .{}); + } + + if (expected_loc) |expected| { + try error_builder.msgAtLoc("expected range here", "file.zig", expected, .info, .{}); + } + std.debug.print("expected_loc: {?}\n", .{expected_loc}); std.debug.print("actual_loc : {?}\n", .{ctx.loc()}); return error.DifferentRange;