Files
nushell/tests/eval
user77 3a08560166 Lex quotes inside interpolated string subexpressions (#18812)
# Description

Fixes #18807.

```
> echo $"('" "')"
```

should print `" "` but the string cannot even be closed: the lexer ends
the interpolated string at the `"` inside the parenthesized
subexpression, so the line lexes as `$"('"` followed by `"')"` and the
REPL keeps asking for more input.

`lex_item` treats a quoted region as opaque until the closing quote,
which is right for plain strings and wrong for interpolated ones: there
an unescaped `(` starts a subexpression where quotes and parens nest.
The parser side already knows this. `parse_string_interpolation` scans
the token with a delimiter stack, but it never sees the full text
because the lexer hands it a truncated token.

This change teaches the lexer the same delimiter matching the parser
applies, so the token ends exactly where the parser will end the string:

- a `"` or `'` opening directly after `$` marks the string as
interpolated (backticks never interpolate)
- an unescaped `(` in such a string opens a subexpression; an escaped
`\(` does not, and `$'` has no escapes, both matching the parser's rules
- while a subexpression is open, the innermost open delimiter decides
what matches: a quote is only closed by the same quote, parens nest, and
the string's own closing quote cannot end the string
- at end of input an open subexpression is reported from its oldest open
delimiter, so `$"(1` points at the `(` and asks for `)`. In the `$"foo
(2 + 3"` typo the trailing quote now opens a nested string during
lexing, and reporting the oldest delimiter keeps the error on the
forgotten `(`, which is what the existing
`unclosed_interpolation_subexpression` repl test expects

Behavior checked against an unpatched build:

- `--flag=$"(1 + 2)"` still interpolates (the `$` is mid token there,
which is why the check is `previous char is $` rather than `token starts
with $`)
- the odd bareword form `a$"b(1)"c` still gives `a$"b1"c`
- `^$"($nu.current-exe)"` still runs as an external head
- `$"\('not an expr'\)"` still prints `('not an expr')`

# User-Facing Changes

Quotes can now be used inside `(...)` subexpressions of interpolated
strings: `$"('" "')"` prints `" "`. Unclosed interpolated strings report
the oldest unclosed delimiter of the subexpression instead of a
misleading one.

# Tests + Formatting

Two new lexer test groups in `test_lex.rs`: five inputs that must lex to
a single item token (the issue's expression, a double quote inside the
subexpression, the `$'` form, nested parens, and escaped parens), and
three unclosed inputs asserting which delimiter is reported and where it
opened. Four new `eval_value_eq` cases in `tests/eval/mod.rs`, including
the issue's expression evaluating to `" "`. The issue's lex case, the
unclosed cases, and the issue's eval case all fail on current main and
pass with this change; the remaining cases pass on both and guard
against regressions.

- `cargo fmt --all -- --check` clean
- `cargo clippy --workspace --all-targets -- -D warnings` clean
- suites: nu-parser 359 (includes the new cases), nu-command 3630,
nu-lsp 785, nu-cli 171 (23 file completion tests fail identically with
and without this change on my machine, environment related), repo tests
2026 including all repl string tests
- `cargo check -p nu-parser --target wasm32-unknown-unknown
--no-default-features` compiles

# After Submitting

## Release notes summary - What our users need to know

Quotes inside `(...)` subexpressions of interpolated strings now work:
`$"('" "')"` prints `" "` instead of becoming an unclosable string.
2026-08-11 06:26:19 -05:00
..