nushell/crates/nu-protocol/src
Yash Thakur d5946a9667
Parse time type checking for range (#13595)
# Description

As part of fixing https://github.com/nushell/nushell/issues/13586, this
PR checks the types of the operands when creating a range. Stuff like
`0..(glob .)` will be rejected at parse time. Additionally, `0..$x` will
be treated as a range and rejected if `x` is not defined, rather than
being treated as a string. A separate PR will need to be made to do
reject streams at runtime, so that stuff like `0..(open /dev/random)`
doesn't hang.

Internally, this PR adds a `ParseError::UnsupportedOperationTernary`
variant, for when you have a range like `1..2..(glob .)`.

# User-Facing Changes

Users will now receive an error if any of the operands in the ranges
they construct have types that aren't compatible with `Type::Number`.

Additionally, if a piece of code looks like a range but some parse error
is encountered while parsing it, that piece of code will still be
treated as a range and the user will be shown the parse error. This
means that a piece of code like `0..$x` will be treated as a range no
matter what. Previously, if `x` weren't the expression would've been
treated as a string `"0..$x"`. I feel like it makes the language less
complicated if we make it less context-sensitive.

Here's an example of the error you get:
```
> 0..(glob .)
Error: nu::parser::unsupported_operation

  × range is not supported between int and any.
   ╭─[entry #1:1:1]
 1 │ 0..(glob .)
   · ─────┬─────┬┬
   ·      │     │╰── any
   ·      │     ╰── int
   ·      ╰── doesn't support these values
   ╰────
```

And as an image:

![image](https://github.com/user-attachments/assets/5c76168d-27db-481b-b541-861dac899dbf)

Note: I made the operands themselves (above, `(glob .)`) be garbage,
rather than the `..` operator itself. This doesn't match the behavior of
the math operators (if you do `1 + "foo"`, `+` gets highlighted red).
This is because with ranges, the range operators aren't `Expression`s
themselves, so they can't be turned into garbage. I felt like here, it
makes more sense to highlight the individual operand anyway.
2024-08-13 15:05:34 +08:00
..
ast Fix $in in range expressions (#13447) 2024-07-25 18:28:44 +08:00
config Add completions.sort option (#13311) 2024-08-05 20:30:10 -04:00
debugger Overhaul $in expressions (#13357) 2024-07-17 16:02:42 -05:00
engine fix: relay Signals reset to plugins (#13510) 2024-08-06 03:35:40 -07:00
errors Parse time type checking for range (#13595) 2024-08-13 15:05:34 +08:00
ir Make plugins able to find and call other commands (#13407) 2024-07-19 13:54:21 +08:00
pipeline Add --raw switch to print for binary data (#13597) 2024-08-12 17:29:25 +08:00
plugin fix: relay Signals reset to plugins (#13510) 2024-08-06 03:35:40 -07:00
process Document public types in nu-protocol (#12906) 2024-07-11 13:30:12 +02:00
value Lift SharedCow::to_mut out of if let branches (#13524) 2024-08-03 00:26:48 +02:00
alias.rs Document public types in nu-protocol (#12906) 2024-07-11 13:30:12 +02:00
did_you_mean.rs Improve case insensitivity consistency (#10884) 2023-11-08 23:58:54 +01:00
eval_base.rs Overhaul $in expressions (#13357) 2024-07-17 16:02:42 -05:00
eval_const.rs Path migration part 3: $nu paths (#13368) 2024-08-01 10:16:31 +02:00
example.rs Overhaul the plugin cache file with a new msgpack+brotli format (#12579) 2024-04-21 07:36:26 -05:00
id.rs Internal representation (IR) compiler and evaluator (#13330) 2024-07-10 17:33:59 -07:00
lev_distance.rs Rework for new clippy lints (#12736) 2024-05-02 19:29:03 +02:00
lib.rs Add top-level crate documentation/READMEs (#12907) 2024-07-14 10:10:41 +02:00
module.rs Enable reloading changes to a submodule (#13170) 2024-06-25 18:33:37 -07:00
parser_path.rs Enable reloading changes to a submodule (#13170) 2024-06-25 18:33:37 -07:00
signature.rs Clippy fixes from stable and nightly (#13455) 2024-07-31 20:37:40 +02:00
span.rs Document public types in nu-protocol (#12906) 2024-07-11 13:30:12 +02:00
syntax_shape.rs Make parsing for unknown args in known externals like normal external calls (#13414) 2024-07-21 01:32:36 -07:00
ty.rs Deprecate group in favor of chunks (#13377) 2024-07-16 03:49:00 +00:00