Files
Darren Schroeder 973c590918 Fix the bugs found by the nu-conformance open questions (#19271)
## Description

The [nu-conformance](https://github.com/Fikoko/nu-conformance) suite
(#19206) recorded 35 behaviors as open questions. This PR fixes the ones
that are bugs, plus five that needed a decision (T2, T4, T8, T12 and
T13). The IDs are the question IDs in that repo's
[`open-questions.md`](https://github.com/Fikoko/nu-conformance/blob/master/open-questions.md).

### Before and after

| | Code | main | branch |
|---|---|---|---|
| S2 | `"\xFF"` | error `nu::parser::parse_mismatch`: expected any shape
| error `nu::parser::error`: invalid UTF-8 from `\x` escapes in string |
| S3 | `r#'abc` | error `nu::parser::unexpected_eof`: expected closing
'# | error `nu::parser::unclosed_delimiter`: expected `'#` here |
| T1 | `inf == 1` | `true` | `false` |
| T1 | `inf > 1` | `false` | `true` |
| T1 | `[1.0 inf] \| math max` | `1.0` | `inf` |
| T2 | `let x = 5; -$x` | `-$x` | `-5` |
| T2 | `let x = 5; 1 + -$x` | error
`nu::parser::operator_incompatible_types`: string | `-4` |
| T2 | `let x = 5sec; -$x` | `-$x` | `-5sec` |
| T2 | `-(2 + 3) \| describe` | `string` | `int` |
| T2 | `-(1)px` | `-1px` | `-1px` |
| T3 | `-0x10` | `-0x10` | `-16` |
| T3 | `+0b11` | error `nu::shell::external_command`: Command `+0b11`
not found | `3` |
| T4 | `9223372036854775808` | `9223372036854775808.0` | error
`nu::parser::error`: number too large (add `.0` for a float) in int |
| T4 | `9223372036854775808.0` | `9223372036854775808.0` |
`9223372036854775808.0` |
| T6 | `[INF Nan Infinity] \| to nuon` | `[inf, NaN, inf]` | `["INF",
"Nan", "Infinity"]` |
| T6 | `[inf nan NaN] \| to nuon` | `[inf, NaN, NaN]` | `[inf, NaN,
NaN]` |
| T7 | `9223372036854775808ns` | `15250wk 1day 23hr 47min 16sec 854ms
775µs 807ns` | error `nu::parser::literal_too_large`: duration too large
|
| T7 | `8EiB` | `9.2 EB` | error `nu::parser::literal_too_large`:
filesize too large |
| T8 | `[2023-02-29] \| to nuon` | `["2023-02-29"]` | error
`nu::parser::error`: not a valid date in datetime |
| T8 | `[2023-02-29.log] \| to nuon` | `["2023-02-29.log"]` |
`["2023-02-29.log"]` |
| T10 | `+5sec` | error `nu::shell::external_command`: Command `+5sec`
not found | `5sec` |
| T11 | `[1 2]] \| to nuon` | `[1, "2]"]` | error
`nu::parser::unbalanced_delimiter`: unexpected `]` (unbalanced with `[`)
|
| T12 | `{true: 1, null: 2} \| to nuon` | error
`nu::parser::parse_mismatch_with_full_string_msg`: expected string |
`{"true": 1, "null": 2}` |
| T12 | `[[1 2]; [3 4]] \| to nuon` | error `nu::parser::error`: must be
a string | `[["1", "2"]; [3, 4]]` |
| T13 | `print before; {a: 1, a: 2}` | error
`nu::shell::column_defined_twice`: field first defined here | error
`nu::parser::column_defined_twice`: field redefined here |
| O1 | `not "a" == "b"` | error `nu::shell::cant_convert`: can't convert
string to boolean | `true` |
| O1 | `let x = 1; not $x == 2` | error `nu::shell::cant_convert`: can't
convert int to boolean | `true` |
| O3 | `(-9223372036854775808) mod -1` | error
`nu::shell::operator_overflow`: modulo operation overflowed | `0` |
| O4 | `1 in [1.0]` | error `nu::parser::operator_incompatible_types`:
list<float> | `true` |
| D1 | `const x = 1; unlet $x; $x` | `1` | error
`nu::compile::invalid_literal`: constants cannot be deleted |
| D2 | `unlet $in` | `nothing` | error `nu::compile::invalid_literal`:
'$in' is a built-in variable and cannot be deleted |
| D3 | `mut x = 0; [1 2 3] \| where {\|v\| $x += 1; true } \| ignore;
$x` | `0` | error `nu::parser::expected_keyword`: capture of mutable
variable |
| F2 | `while false { } \| describe` | `nothing` | error
`nu::parser::unexpected_keyword`: not allowed in pipeline |
| F3 | `if 1 { }` | error `nu::shell::cant_convert`: can't convert int
to boolean | error `nu::shell::type_mismatch`: expected bool |
| F4 | `2.0 in 1.0..2.0` | `false` | `true` |
| F4 | `2.0 in 1.0..<2.0` | `true` | `false` |
| C1 | `def f [...r, a] { {r: $r, a: $a} }; f 1 2 3 \| to nuon` | `{r:
[2, 3], a: 1}` | error `nu::parser::positional_after_rest`: parameter a
after the rest parameter |
| C2 | `def f [a, a] { $a }; f 1 2` | `2` | error
`nu::parser::duplicate_parameter`: `a` uses a name that is already taken
|
| C2 | `def f [--x, --x] { $x }; f --x` | `false` | error
`nu::parser::duplicate_parameter`: `--x` uses a name that is already
taken |
| C2 | `def f [--help] { 'mine' }; f --help \| lines \| first` |
`Usage:` | error `nu::parser::error`: every command already has `--help`
|

C3, a script that calls two deprecated commands:

```nushell
$ nu two_deprecated.nu    # main
Warning: nu::parser::deprecated
   : `-- g is deprecated and will be removed in a future release.
```
```nushell
$ nu two_deprecated.nu    # branch
Warning: nu::parser::deprecated
   : `-- g is deprecated and will be removed in a future release.
Warning: nu::parser::deprecated
   : `-- k is deprecated and will be removed in a future release.
```
## User-facing changes (Release notes)

### Breaking changes

- `not` now has the precedence `help operators` lists: `not $a == $b`
means `not ($a == $b)`. Before, `not` applied to the next value only.
- `-$x` and `-(expr)` negate their value, also as a flag's value or an
argument after `--`. Before, `-$x` was the string `"-$x"` and `-(2 + 3)`
the string `"-5"`. Arguments to external commands are unchanged.
- An integer literal too large for 64 bits, such as
`9223372036854775808`, is an error. Add `.0` to write a float.
- Filesize and duration literals that don't fit are a parse error
(`nu::parser::literal_too_large`) instead of saturating to the largest
value.
- A word written like a date that isn't a valid date, such as
`2023-02-29`, is an error.
- `INF`, `Nan`, `Infinity` and other spellings of special floats are
strings. Use `inf`, `nan` or `NaN`. `from nuon` reads them the same way.
- A record or table literal that names a field twice is a parse error.
- `[1 2]]` is an error instead of `[1, "2]"]`.
- In a custom command, repeated parameter or flag names, repeated short
flags, a parameter after `...rest`, and a `--help` flag are errors.
- `while` and `loop` can't be piped, like `for`.
- `unlet` errors on constants and on `$in`.
- Assigning to a `mut` variable inside a `where` condition is an error;
the assignment never took effect.

### Fixes

- `inf` compares as larger than every finite number, and `[1.0 inf] |
math max` is `inf`.
- `2.0 in 1.0..2.0` is `true`, and `2.0 in 1.0..<2.0` is `false`.
- `-0x10`, `+0b11`, `+5sec` and `+1kB` are numbers.
- `1 in [1.0]` is `true`.
- `{true: 1}` and `[[1 2]; [3 4]]` parse, with the keys `"true"`, `"1"`
and `"2"`.
- `(-9223372036854775808) mod -1` is `0`.
- `if 1 { }` reports the same type mismatch as `while 1 { }`, and an
`if` or `while` condition that fails while it runs reports its own
error.
- `9223372036854775807ns` and `9223372036854775807b` are exact, and
integer filesize and duration literals no longer lose precision.
- A script or `nu -c` command shows every parse warning once, not only
the first.
- Clearer errors for invalid UTF-8 from `\x` escapes and for unclosed
raw strings.

## Additional notes
2026-10-17 14:54:47 -05:00

266 lines
6.0 KiB
Rust

use crate::repl::tests::{TestResult, fail_test, run_test};
use nu_test_support::prelude::*;
use rstest::rstest;
#[test]
fn add_simple() -> TestResult {
run_test("3 + 4", "7")
}
#[test]
fn add_simple2() -> TestResult {
run_test("3 + 4 + 9", "16")
}
#[test]
fn broken_math() -> TestResult {
fail_test("3 + ", "incomplete")
}
#[test]
fn modulo1() -> TestResult {
run_test("5 mod 2", "1")
}
#[test]
fn modulo2() -> TestResult {
run_test("5.25 mod 2", "1.25")
}
/// Infinity orders above every finite number and is equal only to itself.
#[rstest]
#[case::eq_int("inf == 1", false)]
#[case::eq_neg_inf("inf == -inf", false)]
#[case::eq_self("inf == inf", true)]
#[case::gt_int("inf > 1", true)]
#[case::lt_inf("1 < inf", true)]
#[case::neg_inf_lt_zero("-inf < 0", true)]
#[case::max("[1.0 inf] | math max | $in == inf", true)]
fn infinity_comparison(#[case] code: &str, #[case] expected: bool) -> Result {
test().run(code).expect_value_eq(expected)
}
/// `in` accepts an int in a list of floats (and the reverse), like `==` does.
#[rstest]
#[case::int_in_floats("1 in [1.0]", true)]
#[case::float_in_ints("1.0 in [1]", true)]
#[case::int_in_mixed("1 in [1.0 'a']", true)]
#[case::string_in_mixed("'a' in [1 'a']", true)]
#[case::not_in("2 not-in [1.0]", true)]
#[case::has("[1.0] has 1", true)]
fn in_operator_int_float(#[case] code: &str, #[case] expected: bool) -> Result {
test().run(code).expect_value_eq(expected)
}
#[test]
fn bit_shr() -> TestResult {
run_test("16 bit-shr 1", "8")
}
#[test]
fn bit_shl() -> TestResult {
run_test("5 bit-shl 1", "10")
}
#[test]
fn bit_shr_overflow() -> TestResult {
fail_test("16 bit-shr 10000", "exceeds available bits")
}
#[test]
fn bit_shl_overflow() -> TestResult {
fail_test("5 bit-shl 10000000", "exceeds available bits")
}
#[test]
fn bit_shl_neg_operand() -> TestResult {
// This would overflow the `u32` in the right hand side to 2
fail_test(
"9 bit-shl -9_223_372_036_854_775_806",
"exceeds available bits",
)
}
#[test]
fn bit_shr_neg_operand() -> TestResult {
// This would overflow the `u32` in the right hand side
fail_test("9 bit-shr -2", "exceeds available bits")
}
#[test]
fn bit_shl_add() -> TestResult {
run_test("2 bit-shl 1 + 2", "16")
}
#[test]
fn sub_bit_shr() -> TestResult {
run_test("10 - 2 bit-shr 2", "2")
}
#[test]
fn and() -> TestResult {
run_test("true and false", "false")
}
#[test]
fn or() -> TestResult {
run_test("true or false", "true")
}
#[test]
fn xor_1() -> TestResult {
run_test("false xor true", "true")
}
#[test]
fn xor_2() -> TestResult {
run_test("true xor true", "false")
}
#[test]
fn bit_xor() -> TestResult {
run_test("4 bit-xor 4", "0")
}
#[test]
fn bit_xor_add() -> TestResult {
run_test("4 bit-xor 2 + 2", "0")
}
#[test]
fn bit_and() -> TestResult {
run_test("2 bit-and 4", "0")
}
#[test]
fn bit_or() -> TestResult {
run_test("2 bit-or 4", "6")
}
#[test]
fn bit_and_or() -> TestResult {
run_test("2 bit-or 4 bit-and 1 + 2", "2")
}
#[test]
fn pow() -> TestResult {
run_test("3 ** 3", "27").unwrap();
run_test("2 ** 1 ** 2", "2").unwrap();
run_test("2 ** 3 ** 2 ** 1 ** 5", "512").unwrap();
run_test("1.42 ** 2 ** 1.01 ** 9", "2.1135450418757156").unwrap();
run_test("2.571 ** 3.1 ** 2.18", "67804.81966071267").unwrap();
run_test("2.0 ** 3.0 ** 4.0", "2417851639229258349412352.0")
}
#[test]
fn contains() -> TestResult {
run_test("'testme' =~ 'test'", "true")
}
#[test]
fn not_contains() -> TestResult {
run_test("'testme' !~ 'test'", "false")
}
#[test]
fn not_precedence() -> TestResult {
run_test("not false and false", "false")
}
#[test]
fn not_precedence2() -> TestResult {
run_test("(not false) and false", "false")
}
#[test]
fn not_precedence3() -> TestResult {
run_test("not not true and true", "true")
}
#[test]
fn not_precedence4() -> TestResult {
run_test("not not true and not not true", "true")
}
#[test]
fn floating_add() -> TestResult {
run_test("10.1 + 0.8", "10.9")
}
#[test]
fn precedence_of_or_groups() -> TestResult {
run_test("4 mod 3 == 0 or 5 mod 5 == 0", "true")
}
#[test]
fn test_filesize_op() -> TestResult {
run_test("-5kb + 4.5kb", "-500 B")
}
#[test]
fn test_duration_op() -> TestResult {
run_test("4min + 20sec", "4min 20sec").unwrap();
run_test("42sec * 2", "1min 24sec").unwrap();
run_test("(3min + 14sec) / 2", "1min 37sec").unwrap();
run_test("(4min + 20sec) mod 69sec", "53sec")
}
#[test]
fn lt() -> TestResult {
run_test("1 < 3", "true").unwrap();
run_test("3 < 3", "false").unwrap();
run_test("3 < 1", "false")
}
// Comparison operators return null if 1 side or both side is null.
// The motivation for this behaviour: JT asked the C# devs and they said this is
// the behaviour they would choose if they were starting from scratch.
#[test]
fn lt_null() -> TestResult {
run_test("3 < null | to nuon", "null").unwrap();
run_test("null < 3 | to nuon", "null").unwrap();
run_test("null < null | to nuon", "null")
}
#[test]
fn lte() -> TestResult {
run_test("1 <= 3", "true").unwrap();
run_test("3 <= 3", "true").unwrap();
run_test("3 <= 1", "false")
}
#[test]
fn lte_null() -> TestResult {
run_test("3 <= null | to nuon", "null").unwrap();
run_test("null <= 3 | to nuon", "null").unwrap();
run_test("null <= null | to nuon", "null")
}
#[test]
fn gt() -> TestResult {
run_test("1 > 3", "false").unwrap();
run_test("3 > 3", "false").unwrap();
run_test("3 > 1", "true")
}
#[test]
fn gt_null() -> TestResult {
run_test("3 > null | to nuon", "null").unwrap();
run_test("null > 3 | to nuon", "null").unwrap();
run_test("null > null | to nuon", "null")
}
#[test]
fn gte() -> TestResult {
run_test("1 >= 3", "false").unwrap();
run_test("3 >= 3", "true").unwrap();
run_test("3 >= 1", "true")
}
#[test]
fn gte_null() -> TestResult {
run_test("3 >= null | to nuon", "null").unwrap();
run_test("null >= 3 | to nuon", "null").unwrap();
run_test("null >= null | to nuon", "null")
}