Skip to content

Directly call arithmetic primops instead of indirecting through builtins #66

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

Draft
wants to merge 2 commits into
base: detsys-main
Choose a base branch
from

Conversation

gustavderdrache
Copy link

Motivation

Right now, some (but not all) of the Nix language's arithmetic expressions can be shadowed by overriding definitions:

$ nix repl
Nix 2.28.3
Type :? for help.
nix-repl> let __sub = throw "bogus"; in 2 - 2 
error:
       … while calling the 'throw' builtin
         at «string»:1:13:
            1| let __sub = throw "bogus"; in 2 - 2
             |             ^

       error: bogus

As of this PR, this is no longer possible:

$ nix repl
Nix 2.28.3
Type :? for help.
nix-repl> let __sub = throw "bogus"; in 2 - 2
0

In addition, stack traces are annotated with "while evaluating the %s operator" for symmetry with the "while calling the '%s' builtin" from the current implementation:

Old:

nix-repl> 2 - (throw "bogus")
error:
       … while calling the 'sub' builtin
         at «string»:1:3:
            1| 2 - (throw "bogus")
             |   ^

       … while calling the 'throw' builtin
         at «string»:1:6:
            1| 2 - (throw "bogus")
             |      ^

       error: bogus

New:

nix-repl> 2 - (throw "bogus")
error:
       … while evaluating the - operator
         at «string»:1:3:
            1| 2 - (throw "bogus")
             |   ^

       … while calling the 'throw' builtin
         at «string»:1:6:
            1| 2 - (throw "bogus")
             |      ^

       error: bogus

Context

This PR adds binary operation classes for the remaining builtins. As mentioned above, evaluation exceptions thrown have traces added for parity with the old reference to builtin calls. This somewhat inconsistent with other operators: 2 + (throw "bogus") does not mention the operator context. If this PR looks good, I can do a developer experience pass to get the error messages working.

While I've factored out the arithmetic builtins, I haven't touched prim_lessThan because of how it's handled within prim_sort. I'd like to factor it out so that we can just directly use comparison operators instead of essentially backporting the old logic within the AST - maybe just use an enum to get a comparator.

I haven't updated the tests. There are a few failures due to my having changed the error messages. I would like to see if this code is good before finalizing the error messages expected by the test suite.

This gives a stack trace that is similar to calling the equivalent builtin. (That is, the first trace in an error will read either "while evaluating the %s operator" or "while calling the '%s' builtin".)
@piegamesde
Copy link

piegamesde commented May 15, 2025

While we can probably all agree on the end goal and that going in this direction is desirable, it should be noted that this will cause existing code to silently evaluate differently than it did before. This may or may not lead to catastrophic circumstances on some end user's machine. For Lix, we instead chose the approach of forbidding the offensive overrides, which I'd recommend taking. This means that instead of silently changing semantics, code with operator overrides will now error out. (Feel free to take the commit, just make sure to also grab its fixup commit done a bit later which relaxes the rules for __nixPath and __findFile again)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants