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

non-function type error, with argument details. #7873

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

bburdette
Copy link
Contributor

@bburdette bburdette commented Feb 20, 2023

Motivation

Add some extra context when someone mistakenly uses something that isn't a function where a function should be.

Context

Issue #7405 provides an example where part of the nix expression is evaluated and assumed to be a function, since arguments follow. In this case, which part of the expression evaluates to the function is arguably unclear to the user.

Current error:

nix-repl> {a = 3;}.a or throw "what"  
error: attempt to call something which is not a function but an integer

       at «string»:1:1:

            1| {a = 3;}.a or throw "what"
             | ^

Error with extra context:

nix-repl> {a = 3;}.a or throw "what"
error: attempt to call something which is not a function but an integer: 3
       function arguments:
       a string: "what"

       at «string»:1:1:

            1| {a = 3;}.a or throw "what"
             | ^

To hopefully make cases like this more clear, I print the type and value of the non-function, followed by the type and value of its arguments. Because printing sets and lists can result in enormous walls of text, I only print the expr types for sets and lists.

nix-repl> np = import <nixos-22.11> {}

nix-repl> np
{ AAAAAASomeThingsFailToEvaluate = «error: error:
       … while calling the 'throw' builtin

         at /nix/store/ ^Cerror: interrupted by the user
«derivation
nix-repl> {a = 3;}.a or throw "what" 12 21 34 np
error: attempt to call something which is not a function but an integer: 3
       function arguments:
       a string: "what"
       an integer: 12
       an integer: 21
       an integer: 34
       a set: <set>

       at «string»:1:1:

            1| {a = 3;}.a or throw "what" 12 21 34 np
             | ^

Implementation notes

Because I wanted to make an error message with a dynamic number of arguments, I needed to create a new constructor for ErrorBuilder that takes a hintformat. Then I added a function to create this hintformat. Since allocating hintformats on the stack is discouraged, the build function and etc use a unique_ptr.

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

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.

1 participant