nushell/crates/nu-parser/src
Yash Thakur 0303d709e6
Spread operator in record literals (#11144)
Goes towards implementing #10598, which asks for a spread operator in
lists, in records, and when calling commands (continuation of #11006,
which only implements it in lists)

# Description
This PR is for adding a spread operator that can be used when building
records. Additional functionality can be added later.

Changes:

- Previously, the `Expr::Record` variant held `(Expression, Expression)`
pairs. It now holds instances of an enum `RecordItem` (the name isn't
amazing) that allows either a key-value mapping or a spread operator.
- `...` will be treated as the spread operator when it appears before
`$`, `{`, or `(` inside records (no whitespace allowed in between) (not
implemented yet)
- The error message for duplicate columns now includes the column name
itself, because if two spread records are involved in such an error, you
can't tell which field was duplicated from the spans alone

`...` will still be treated as a normal string outside records, and even
in records, it is not treated as a spread operator when not followed
immediately by a `$`, `{`, or `(`.

# User-Facing Changes
Users will be able to use `...` when building records.

```
> let rec = { x: 1, ...{ a: 2 } }
> $rec
╭───┬───╮
│ x │ 1 │
│ a │ 2 │
╰───┴───╯
> { foo: bar, ...$rec, baz: blah }
╭─────┬──────╮
│ foo │ bar  │
│ x   │ 1    │
│ a   │ 2    │
│ baz │ blah │
╰─────┴──────╯
```
If you want to update a field of a record, you'll have to use `merge`
instead:
```
> { ...$rec, x: 5 }
Error: nu:🐚:column_defined_twice

  × Record field or table column used twice: x
   ╭─[entry #2:1:1]
 1 │  { ...$rec, x: 5 }
   ·       ──┬─  ┬
   ·         │   ╰── field redefined here
   ·         ╰── field first defined here
   ╰────
> $rec | merge { x: 5 }
╭───┬───╮
│ x │ 5 │
│ a │ 2 │
╰───┴───╯
```

# Tests + Formatting

# After Submitting
2023-11-29 18:31:31 +01:00
..
deparse.rs allow empty string arguments (#9420) 2023-06-13 07:30:30 -05:00
flatten.rs Spread operator in record literals (#11144) 2023-11-29 18:31:31 +01:00
known_external.rs Sort entries in scope commands; Fix usage of externs (#10039) 2023-08-17 16:37:01 +02:00
lex.rs Support o>>, e>>, o+e>> to append output to an external file (#10764) 2023-11-27 07:52:39 -06:00
lib.rs Move SyntaxShape specifier parsing into own file (#10448) 2023-10-05 23:31:40 +02:00
lite_parser.rs Support o>>, e>>, o+e>> to append output to an external file (#10764) 2023-11-27 07:52:39 -06:00
parse_keywords.rs Forbid reserved variable names for function arguments (#11169) 2023-11-29 18:29:07 +01:00
parse_patterns.rs add match guards (#9621) 2023-07-16 12:25:12 +12:00
parse_shape_specs.rs Move SyntaxShape specifier parsing into own file (#10448) 2023-10-05 23:31:40 +02:00
parser_path.rs Add virtual path abstraction layer (#9245) 2023-05-23 23:48:50 +03:00
parser.rs Spread operator in record literals (#11144) 2023-11-29 18:31:31 +01:00
type_check.rs Relax type-check of key-less table/record (#10629) 2023-10-08 13:26:36 +02:00