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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote reserved keywords when printing expressions #8110

Merged
merged 3 commits into from May 9, 2023

Conversation

aakropotkin
Copy link
Contributor

@aakropotkin aakropotkin commented Mar 26, 2023

Motivation

This allows the output of nix eval to be re-evaluated without errors when attrsets use keys such as assert.

Context

Related Issue: #7656

You can compare the output of nix eval with these examples:

# Old
$ nix eval --expr '{ "assert" = 1; }';
{ assert = 1; }

# New
$ nix eval --expr '{ "assert" = 1; }';
{ "assert" = 1; }

I covered the keyword symbols recognized by the parser, and added throw and import 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

  • agreed on idea
  • agreed on implementation strategy
  • tests, as appropriate
    • functional tests - tests/**.sh
    • unit tests - src/*/tests
    • integration tests - tests/nixos/*
  • documentation in the manual
  • code and comments are self-explanatory
  • commit message explains why the change was made
  • new feature or incompatible change: updated release notes

Priorities

Add 馃憤 to pull requests you find important.

@aakropotkin aakropotkin changed the title Quote reserved Quote reserved keywords when printing expressions Mar 26, 2023
src/libexpr/eval.cc Outdated Show resolved Hide resolved
@aakropotkin
Copy link
Contributor Author

@edolstra I squashed and gave this a proper commit message.

Let me know if you have any additional review.

@Ericson2314 Ericson2314 added the bug label Apr 3, 2023
src/libexpr/eval.cc Outdated Show resolved Hide resolved
src/libexpr/eval.cc Outdated Show resolved Hide resolved
@Ericson2314
Copy link
Member

Ericson2314 commented Apr 3, 2023

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!

@Ericson2314 Ericson2314 added the idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. label Apr 3, 2023
@Ericson2314
Copy link
Member

Rebasing this after #8193 should be interesting --- I hope in a good way!

@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Apr 17, 2023
@aakropotkin
Copy link
Contributor Author

I rebased and added tests.

src/libexpr/eval.cc Outdated Show resolved Hide resolved
src/libexpr/eval.cc Outdated Show resolved Hide resolved
@roberth roberth added the language The Nix expression language; parser, interpreter, primops, evaluation, etc label Apr 30, 2023
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.
@Ericson2314 Ericson2314 enabled auto-merge May 9, 2023 15:34
@Ericson2314 Ericson2314 dismissed roberth鈥檚 stale review May 9, 2023 15:39

Requested changes were made

@Ericson2314 Ericson2314 merged commit aacde38 into NixOS:master May 9, 2023
11 checks passed
@aakropotkin aakropotkin deleted the quote-reserved branch May 9, 2023 15:43
// 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)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to rename it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When anyone has time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug idea approved The given proposal has been discussed and approved by the Nix team. An implementation is welcome. language The Nix expression language; parser, interpreter, primops, evaluation, etc with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants