Skip to content

zig fmt fails with unhelpful error message when a comment contains a tab #3737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ikskuh opened this issue Nov 21, 2019 · 2 comments · Fixed by #3910
Closed

zig fmt fails with unhelpful error message when a comment contains a tab #3737

ikskuh opened this issue Nov 21, 2019 · 2 comments · Fixed by #3910
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@ikskuh
Copy link
Contributor

ikskuh commented Nov 21, 2019

Formatting the following code with zig fmt fails:

//	this is broken!
fail.zig:1:1: error: Expected test, comptime, var decl, or container field, found 'Invalid'
// this is broken!

It does not matter where the comment with the tab is, the error message stays the same, pointing to filename.zig:1:1, so it's hard to find what causes this

@andrewrk andrewrk added frontend Tokenization, parsing, AstGen, Sema, and Liveness. bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library. labels Nov 27, 2019
@andrewrk andrewrk added this to the 0.7.0 milestone Nov 27, 2019
@LakeByTheWoods
Copy link
Contributor

This is being caused by #2915

At the top of Tokenizer.next there's

        if (self.pending_invalid_token) |token| {
            self.pending_invalid_token = null;
            return token;
        }

token's start and end get set to 0 by self.pending_invalid_token = null; so the resulting error print thinks the token is at the start of the buffer.

I have two potential solutions:

  1. Explicit copy
        if (self.pending_invalid_token) |token| {
            const copy = token;
            self.pending_invalid_token = null;
            return copy;
        }

Which I don't think it looks very nice, but it is at least (hopefully) clear what the writer is intending.

  1. defer setting null
        if (self.pending_invalid_token) |token| {
            defer self.pending_invalid_token = null;
            return token;
        }

Which I'm not sure if it's too clever to the point of being hard to understand or a cute potentially idiomatic way of setting an optional to null when using its value this way.

I've tested both, they both correctly point out the tab token as the problem (though as an aside, the error message doesn't make it clear why it's a problem, ie. tabs and spaces both look the same in the message).

(or there's the 3rd option of disarming the foot-gun and keeping the original code)

@LakeByTheWoods
Copy link
Contributor

Looks like this isn't the only time this has happened, the json.zig tokenizer uses option 1.

andrewrk pushed a commit that referenced this issue Dec 16, 2019
* Tokenizer: Copy optional tokens prior to being set to null #3737

* Add TODO comments, reminder to audit copying optional pattern.
@andrewrk andrewrk modified the milestones: 0.7.0, 0.6.0 Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants