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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quote reserved keywords when printing expressions #8110
Conversation
4e50e4c
to
f93d833
Compare
@edolstra I squashed and gave this a proper commit message. Let me know if you have any additional review. |
The main thing this needs is tests. I hereby approve of the idea because it fixes a clear bug --- don't print out invalid syntax! |
Rebasing this after #8193 should be interesting --- I hope in a good way! |
I rebased and added tests. |
This fixes a bug in commands like `nix eval' which would emit invalid attribute sets if they contained reserved keywords such as "assert", "let", etc. These keywords will not be quoted when printed, making them valid expressions. All keywords recognized by the lexer are quoted except "or", which does not require quotation.
10f40db
to
82d1d74
Compare
// You can test if a keyword needs to be added by running: | ||
// $ nix eval --expr '{ <KEYWORD> = 1; }' | ||
// For example `or' doesn't need to be quoted. | ||
bool isReservedKeyword(const std::string_view str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, technically reserved keywords would be keywords that aren't in use. I don't think Nix has any of those.
IIRC JavaScript reserved some Java/C/C++ keywords early on, that remain reserved and unused until this day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Want to rename it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When anyone has time.
Motivation
This allows the output of
nix eval
to be re-evaluated without errors when attrsets use keys such asassert
.Context
Related Issue: #7656
You can compare the output of
nix eval
with these examples:I covered the keyword symbols recognized by the parser, and added
throw
andimport
just for good measure.The list is : "if", "then", "else", "assert", "with", "let", "in", "rec", "inherit", "or", "throw", "import".
Checklist for maintainers
Maintainers: tick if completed or explain if not relevant
tests/**.sh
src/*/tests
tests/nixos/*
Priorities
Add 馃憤 to pull requests you find important.