Skip to content

Commit

Permalink
fix position context crash on doc comment followed by an enum literal
Browse files Browse the repository at this point in the history
Thank you zigtools/sus
  • Loading branch information
Techatrix committed Nov 20, 2024
1 parent 93b9fcb commit 0353740
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {},
Expand Down
33 changes: 25 additions & 8 deletions tests/utility/position_context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
\\ <loc>item
\\ .foo()
\\ .bar()
\\ .baz<cursor></loc>();
\\}
, .field_access, .{});

try testContext(
\\/// some comment
\\ .<loc>foo</loc><cursor>()
, .var_access, .{});
}

test "builtin" {
try testContext(
\\var foo = <cursor>@
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0353740

Please sign in to comment.