parser: fix "previous field here" pointing to wrong field

This commit is contained in:
Veikka Tuominen 2022-02-19 10:10:32 +02:00
parent 2e1c16d649
commit 2f0204aca3
2 changed files with 12 additions and 10 deletions

View file

@ -91,9 +91,6 @@ const Parser = struct {
extra_data: std.ArrayListUnmanaged(Node.Index), extra_data: std.ArrayListUnmanaged(Node.Index),
scratch: std.ArrayListUnmanaged(Node.Index), scratch: std.ArrayListUnmanaged(Node.Index),
/// Used for the error note of decl_between_fields error.
last_field: TokenIndex = undefined,
const SmallSpan = union(enum) { const SmallSpan = union(enum) {
zero_or_one: Node.Index, zero_or_one: Node.Index,
multi: Node.SubRange, multi: Node.SubRange,
@ -252,6 +249,8 @@ const Parser = struct {
err, err,
} = .none; } = .none;
var last_field: TokenIndex = undefined;
// Skip container doc comments. // Skip container doc comments.
while (p.eatToken(.container_doc_comment)) |_| {} while (p.eatToken(.container_doc_comment)) |_| {}
@ -274,7 +273,7 @@ const Parser = struct {
.identifier => { .identifier => {
p.tok_i += 1; p.tok_i += 1;
const identifier = p.tok_i; const identifier = p.tok_i;
defer p.last_field = identifier; defer last_field = identifier;
const container_field = try p.expectContainerFieldRecoverable(); const container_field = try p.expectContainerFieldRecoverable();
if (container_field != 0) { if (container_field != 0) {
switch (field_state) { switch (field_state) {
@ -288,7 +287,7 @@ const Parser = struct {
try p.warnMsg(.{ try p.warnMsg(.{
.tag = .previous_field, .tag = .previous_field,
.is_note = true, .is_note = true,
.token = p.last_field, .token = last_field,
}); });
try p.warnMsg(.{ try p.warnMsg(.{
.tag = .next_field, .tag = .next_field,
@ -389,7 +388,7 @@ const Parser = struct {
}, },
.identifier => { .identifier => {
const identifier = p.tok_i; const identifier = p.tok_i;
defer p.last_field = identifier; defer last_field = identifier;
const container_field = try p.expectContainerFieldRecoverable(); const container_field = try p.expectContainerFieldRecoverable();
if (container_field != 0) { if (container_field != 0) {
switch (field_state) { switch (field_state) {
@ -402,7 +401,7 @@ const Parser = struct {
}); });
try p.warnMsg(.{ try p.warnMsg(.{
.tag = .previous_field, .tag = .previous_field,
.token = p.last_field, .token = last_field,
}); });
try p.warnMsg(.{ try p.warnMsg(.{
.tag = .next_field, .tag = .next_field,

View file

@ -866,7 +866,10 @@ pub fn addCases(ctx: *TestContext) !void {
\\ const foo = 2; \\ const foo = 2;
\\ const bar = 2; \\ const bar = 2;
\\ const baz = 2; \\ const baz = 2;
\\ a: usize, \\ a: struct {
\\ a: u32,
\\ b: u32,
\\ },
\\ const foo1 = 2; \\ const foo1 = 2;
\\ const bar1 = 2; \\ const bar1 = 2;
\\ const baz1 = 2; \\ const baz1 = 2;
@ -876,9 +879,9 @@ pub fn addCases(ctx: *TestContext) !void {
\\ _ = S; \\ _ = S;
\\} \\}
, &[_][]const u8{ , &[_][]const u8{
"tmp.zig:6:5: error: declarations are not allowed between container fields", "tmp.zig:9:5: error: declarations are not allowed between container fields",
"tmp.zig:5:5: note: field before declarations here", "tmp.zig:5:5: note: field before declarations here",
"tmp.zig:9:5: note: field after declarations here", "tmp.zig:12:5: note: field after declarations here",
}); });
ctx.objErrStage1("non-extern function with var args", ctx.objErrStage1("non-extern function with var args",