Skip to content
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

Harden peek() / peek_data() call pairs in the parser #830

Open
goto-bus-stop opened this issue Feb 9, 2024 · 0 comments · May be fixed by #900
Open

Harden peek() / peek_data() call pairs in the parser #830

goto-bus-stop opened this issue Feb 9, 2024 · 0 comments · May be fixed by #900

Comments

@goto-bus-stop
Copy link
Member

goto-bus-stop commented Feb 9, 2024

We have a few places where we call peek() to check an upcoming token's kind, and then peek_data().unwrap() to check its value. For example, to identify what type of definition is coming up: type, union, or something else. This is an actual self-contained snippet showing what I mean:

if let Some(TokenKind::Name) = p.peek() {
if p.peek_data().unwrap() == "implements" {
implements_interfaces(p);
} else {
p.err("unexpected Name");
}
}

The unwrap() call is valid in those cases, but a bit icky. For it to be valid, the current token must not change between those calls, and this is not statically verifiable. A mistake in a refactor could disconnect the peek() and peek_data() calls and then the unwrap could panic.

I'd like to see these refactored to use let Some(token) = peek_token() where possible, and then use token.kind / token.data directly, so there is no way that it could ever panic.

@goto-bus-stop goto-bus-stop changed the title Simplify peek_token() calls in the parser Harden peek() / peek_data() call pairs in the parser Feb 9, 2024
surajk-m added a commit to surajk-m/apollo-rs that referenced this issue Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant