Skip to content

Incorrect span in type_mismatch error message #7288

Closed

Description

@epbuennig

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

  1. nu
  2. let val = 10
  3. ($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

  1. epbuennig commented on Nov 30, 2022

    @epbuennig
    Author

    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.
    image

  2. added
    A:error-unhelpful
    on Nov 30, 2022
  3. sophiajt commented on Nov 30, 2022

    @sophiajt
    Contributor

    My guess is that into string is 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.

  4. Vctr-w commented on Feb 28, 2023

    @Vctr-w
    Contributor

    I'll take a look into this one

  5. added this to the v0.77.0 milestone on Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:error-unhelpfulgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions