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

Better eval error locations for interpolation and + #5287

Merged
merged 1 commit into from
Dec 13, 2021

Conversation

greedy
Copy link
Contributor

@greedy greedy commented Sep 23, 2021

Previously, type or coercion errors for string interpolation, path
interpolation, and plus expressions were always reported at the
beginning of the outer expression. This leads to confusing evaluation
error messages making it hard to accurately diagnose and then fix the
error.

For example, errors were reported as follows.

cannot coerce an integer to a string
1| let foo = 7; in "bar" + foo
 |                 ^

cannot add a string to an integer
1| let foo = "bar"; in 4 + foo
 |                     ^

cannot coerce an integer to a string
1| let foo = 7; in "x${foo}"
 |                  ^

This commit changes the ExprConcatStrings expression vector to store a
sequence of expressions and their expansion locations so that error
locations can be reported accurately. For interpolation, the error is
reported at the beginning of the entire ${foo}, not at the beginning
of foo because I thought this was slightly clearer. The previous
errors are now reported as:

cannot coerce an integer to a string
1| let foo = 7; in "bar" + foo
 |                         ^

cannot add a string to an integer
1| let foo = "bar"; in 4 + foo
 |                         ^

cannot coerce an integer to a string
1| let foo = 7; in "x${foo}"
 |                   ^

The error is reported at this kind of precise location even for
multi-line indented strings.

This probably helps with at least some of the cases mentioned in #561

Previously, type or coercion errors for string interpolation, path
interpolation, and plus expressions were always reported at the
beginning of the outer expression. This leads to confusing evaluation
error messages making it hard to accurately diagnose and then fix the
error.

For example, errors were reported as follows.

```
cannot coerce an integer to a string
1| let foo = 7; in "bar" + foo
 |                 ^

cannot add a string to an integer
1| let foo = "bar"; in 4 + foo
 |                     ^

cannot coerce an integer to a string
1| let foo = 7; in "x${foo}"
 |                  ^
```

This commit changes the ExprConcatStrings expression vector to store a
sequence of expressions *and* their expansion locations so that error
locations can be reported accurately. For interpolation, the error is
reported at the beginning of the entire `${foo}`, not at the beginning
of `foo` because I thought this was slightly clearer. The previous
errors are now reported as:

```
cannot coerce an integer to a string
1| let foo = 7; in "bar" + foo
 |                         ^

cannot add a string to an integer
1| let foo = "bar"; in 4 + foo
 |                         ^

cannot coerce an integer to a string
1| let foo = 7; in "x${foo}"
 |                   ^
```

The error is reported at this kind of precise location even for
multi-line indented strings.

This probably helps with at least some of the cases mentioned in NixOS#561
@greedy
Copy link
Contributor Author

greedy commented Sep 23, 2021

An alternative implementation strategy would be pulling the position field up into the Expr superclass or adding a virtual position accessor to Expr so that ExprConcatStrings can access the nested expressions’ positions directly instead.

@edolstra edolstra added the error-messages Confusing messages and better diagnostics label Sep 29, 2021
@greedy
Copy link
Contributor Author

greedy commented Oct 4, 2021

Any thoughts/feedback on this PR? I think it's a quite nice UX improvement and am keen to see it merged.

@edolstra edolstra added this to the nix-3.0 milestone Oct 6, 2021
@edolstra edolstra merged commit cc6406c into NixOS:master Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error-messages Confusing messages and better diagnostics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants