Repository navigation
Description
Describe the bug
When declaring an int variable $val and trying to use binary addition on it with a string it would highlight the type mismatch before you evaluated the expression:
let val = 10
let var = "10"
$val + $var
I tried to see how far this on the fly analysis goes by turning a value into a string during evaluation.
let val = 10
($val | into string) + $var
In hindsight, obvious that this wouldn't be picked up without any special handling for into. Attempting to evaluate the expression anyway yields the following error message:
Error: nu::shell::type_mismatch (link)
× Type mismatch during operation.
╭─[entry #11:1:1]
1 │ ($val | into string) + $val
· ──┬─ ┬ ──┬─
· │ │ ╰── int
· │ ╰── type mismatch for operator
· ╰── string
╰────
Here, val is underlined as both a string in the left term and as a int in the right term, which is wrong since the whole left term evaluated to a string, not $val itself.
How to reproduce
nulet val = 10($val | into string) + $val
Expected behavior
An error message underlining the whole left term of the addition expression sort of like this:
Error: nu::shell::type_mismatch (link)
× Type mismatch during operation.
╭─[entry #11:1:1]
1 │ ($val | into string) + $val
· ───┬──────────────── ┬ ──┬─
· │ │ ╰── int
· │ ╰── type mismatch for operator
· ╰── string
╰────
Screenshots
No response
Configuration
| key | value |
|---|---|
| version | 0.71.0 |
| branch | makepkg |
| commit_hash | 6cc4ef6 |
| build_os | linux-x86_64 |
| build_target | x86_64-unknown-linux-gnu |
| rust_version | rustc 1.65.0 (Arch Linux rust 1:1.65.0-1) |
| cargo_version | cargo 1.65.0 |
| pkg_version | 0.71.0 |
| build_time | 2022-11-09 06:34:03 +00:00 |
| build_rust_channel | release |
| features | database, dataframe, default, trash, which, zip |
| installed_plugins |
Additional context
Note the inconsistent highlighting between string + int and int + string, see #7289.
Activity
epbuennig commentedon Nov 30, 2022
Interestingly, something that I just noticed looking back at the error messages, they seem to both be the same underlying problem, just with the type reordered, but spit out different error messages in general.Well this makes sense, the types are known in one case but not the other.
sophiajt commentedon Nov 30, 2022
My guess is that
into stringis allowing the value to get its original span rather than replacing it. I think your example shows why the span should get replaced.We could probably say the same thing for the subexpression in
(..)where a subexpression would replace the span of the values that exit it.Vctr-w commentedon Feb 28, 2023
I'll take a look into this one