Skip to content

engine: IR-shaped const evaluation for run_const (AST migration phase 2) - #18967

Merged
fdncred merged 8 commits into
nushell:mainfrom
fdncred:reduce_ast_phase_2
Sep 16, 2026
Merged

fdncred merged 8 commits into
nushell:mainfrom
fdncred:reduce_ast_phase_2

Conversation

@fdncred

@fdncred fdncred commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Description

This PR is the 3rd installment of trying to keep working toward minimizing AST and increasing IR.

Runtime block evaluation is already IR-only. Phase 0–1 removed every requires_ast_for_arguments opt-in so IR no longer retains argument AST for builtins. Const evaluation was still the main holdout: eval_const_call passed an AST engine::Call, and *_const helpers forced assert_ast_call.

This PR is phase 2 of the IR-only call migration:

  1. Bridge — eval_const_call evaluates AST args to Values, builds a temporary ir::Call on a Stack, and invokes Command::run_const with that IR-shaped call.
  2. API — run_const now takes stack: &mut Stack so IR args are readable. (this is a breaking api change) engine::Call::{has_flag,get_flag,req,rest}_const work on IR (and still on AST if needed).
  3. if — stays on a dedicated AST const path (eval_const_if) because it needs control-flow structure, not flat Values.
  4. Blocks as const call args — Expr::Block / Closure / RowCondition used as arguments become capture-free Closure values so commands like attr example can extract source text without AST.
  5. Cleanup — remove assert_ast_call.

Parser AST remains the compile-time intermediate. This does not delete the AST or finish dual-CallImpl removal (phases 3–4).

Breaking changes?

No intentional user-facing breaking changes. Const language behavior should be unchanged (const x = if …, attributes, path/math/string const commands).

Public Rust API note (for embedders / out-of-tree commands): Command::run_const gained a stack: &mut Stack parameter. That is a breaking change for anyone implementing Command outside this repo, but not for end users of the shell.

User-facing changes (Release notes)

Additional notes

  • Continues the AST→IR call migration (phases 0–1 prior; this is phase 2).
  • Next: phase 3 — residual AST eval_call producers (open converters, custom completions, documentation, empty Call::new).
  • Then phase 4 — collapse CallImpl Ast variants.

Bridge eval_const_call so const command invocations build an ir::Call with
pre-evaluated Values on a temporary Stack, and extend Command::run_const /
engine::Call const helpers to take that stack. Keep if on a dedicated AST
const path; materialize block/closure call args as capture-free Closures.
Remove assert_ast_call.
Update const command implementations to the new run_const(stack) API and
IR-capable *_const helpers. Special-case if via eval_const_if, and make
attr example const use closure/string values from the const bridge.
@fdncred fdncred added notes:breaking-changes Noted in Breaking Changes section api-change This PR should be mentioned in #api-updates channel on Discord notes:ready Indicates Ready for Release notes labels Sep 4, 2026
@github-actions github-actions Bot added the A:parser Issues related to parsing label Sep 4, 2026
@github-actions

Copy link
Copy Markdown

Hey, just a bot checking in! You edited files related to the configuration.
If you changed any of the default values or added a new config option, don't forget to update the doc_config.nu which documents the options for our users including the defaults provided by the Rust implementation.
If you didn't make a change here, you can just ignore me.

@fdncred
fdncred merged commit 7b0b4b9 into nushell:main Sep 16, 2026
14 checks passed
@fdncred
fdncred deleted the reduce_ast_phase_2 branch September 16, 2026 12:45
@github-actions github-actions Bot added this to the v0.116.0 milestone Sep 16, 2026
@cptpiepmatz cptpiepmatz added notes:mention Noted in "Hall of Fame" section and removed notes:breaking-changes Noted in Breaking Changes section labels Sep 26, 2026
fdncred added a commit that referenced this pull request Oct 28, 2026
…e 3) (#19351)

## Description

This continues the AST → IR call migration (#18808, #18813, #18967). One
commit per step:

1. **Drop argument AST retention.** No command has opted into
`requires_ast_for_arguments` since #18813, so every `IrBlock.ast` entry
was `None`. The opt-in, `IrAstRef`, `IrBlock.ast`, the `ast` field of
`engine::Argument` and `engine::Call::positional_nth` are gone.
`Let::run`, which only an indirect call can reach, now returns an error
like `unlet`'s.
2. **One IR path for running a command by id.**
`nu_engine::eval_ir_call` runs an `ir::Call` the way compiled code does:
input type check, argument normalization, and parameter binding for a
custom command. The VM's call instruction shares the same code. `open`,
`http`, `save`, `%(...)` and a plugin's `call_decl` use it, which fixes
custom commands in three of them.
3. **Display call sites.** `help` (`nu-highlight`, `table`, `ansi`),
`tutor`, `print`, the DAP `print`, `clip copy` and table rendering for
an external's stdin build IR calls. `tutor` highlights through `help`'s
`nu_highlight_string`, so a snippet it can't highlight shows as plain
text instead of disappearing. `engine::Call::new` is deleted.
`CallImpl::to_owned` still turns an `AstRef` into an `AstBox`; phase 4
removes both variants.

## User-facing changes (Release notes)

- `http` commands can convert a response with a custom `from <ext>`
command, as `open` already could.
- `save` binds the flags and defaults of a custom `to <ext>` command.
- A plugin's `call_decl` can call a custom command.

## Breaking changes

### Scripts

- `save` checks a custom `to <ext>` converter's input type, as a direct
call does. A converter whose signature doesn't cover the data now errors
instead of running. Built-in `to` commands are unaffected: inputs they
already rejected now fail with `Input type not supported` instead of the
converter's own message. `open` and `http` hand converters an untyped
byte stream, which any input type accepts, so they don't change.
- `save` keeps a custom `to <ext>` converter's `$env` changes inside it
unless it is `def --env`, the same as `... | to <ext>`.
- `%($cmd)` checks its input and arguments as a direct call does.
Spreading a list into a command without a rest parameter is an error
instead of being ignored.
- `view ir --json` no longer has an `ast` field in `ir_block`. It was
always `null`.

| Command | main | branch |
|---|---|---|
| `def "to foo" []: int -> string { "int" }; [1 2] \| save out.foo; open
--raw out.foo` | `int` | `Input type not supported` (`only int input
data is supported`, input type `list<int>`) |
| `def "to foo" [] { $env.FOO = 1; "x" }; [1] \| save out.foo; $env.FOO?
\| describe` | `int` | `nothing` |
| `def --env "to foo" [] { $env.FOO = 1; "x" }; [1] \| save out.foo;
$env.FOO? \| describe` | `int` | `int` |
| `let cmd = 'length'; let args = [1]; [1 2] \| %($cmd) ...$args` | `2`
(the spread is dropped) | `Cannot spread a list into this command`
(`length ...$args` is a parse error) |
| `view ir --json {\|\| 1 } \| from json \| get ir_block \| columns` |
`[instructions, spans, data, ast, comments, register_count, file_count]`
| `[instructions, spans, data, comments, register_count, file_count]` |

### Plugins

`call_decl` checks the input type, including empty input, as a direct
call does. A call that the command's signature doesn't allow now errors,
the same as when the call is written in a script.

| Command (with `nu_plugin_example`) | main | branch |
|---|---|---|
| `'a' \| example call-decl uniq` | `[a]` | `Input type not supported`
(`only list<any> input data is supported`) |
| `example call-decl 'str join'` | `""` | `Pipeline empty` |

The `IrBlock` returned by the `get_block_ir` engine call no longer has
an `ast` field. Nushell only loads plugins built for the same minor
version, so a plugin never reads the old format from a newer engine.

### Rust API (embedders and out-of-tree `Command` implementations)

Plugins implement `PluginCommand` and are not affected.

| Removed | Instead |
|---|---|
| `engine::Call::new` | Build an `ir::Call` and run it with
`nu_engine::eval_ir_call` (example below). |
| `Command::requires_ast_for_arguments` | Delete the override. No
command has needed it since #18813. |
| `engine::Call::positional_nth`, `ir::Call::positional_ast`,
`engine::Argument::ast_expression` | Read argument values with `CallExt`
(`req`, `opt`, `rest`). On a compiled call these always returned `None`.
|
| The `ast` field of `engine::Argument::{Positional, Spread, Named}` |
Drop it from patterns and struct literals. |
| `ir::IrAstRef`, `IrBlock.ast` | Nothing. |

```diff
-let call = engine::Call::new(span);
-decl.run(engine_state, stack, &call, input)
+let call = ir::Call::build(decl_id, span).finish();
+nu_engine::eval_ir_call(engine_state, stack, &call, input)
```

With arguments, add them to the builder and call `leave` afterwards:

```rust
let mut builder = ir::Call::build(decl_id, span);
builder.add_named(stack, "width", "", span, Value::int(80, span));
let call = builder.finish();
let result = nu_engine::eval_ir_call(engine_state, stack, &call, input);
call.leave(stack);
```

Unlike `decl.run`, `eval_ir_call` also runs custom commands, checks the
input type and normalizes the arguments. Code that can't depend on
`nu-engine` can still pass `&(&call).into()` to `decl.run`.

`ir::Call::leave` on a call without arguments now does nothing. It used
to clear the whole argument stack, including the caller's arguments.

## Additional notes

- Closes #16974.
- Added `nu_engine::eval_ir_call`, which returns an error unless the
call's arguments are the last ones on the stack, and made
`nu_engine::nu_highlight_string` public.
- Next: phase 4 deletes `CallImpl::AstRef`/`AstBox` and the AST
evaluator's call path.
fdncred added a commit that referenced this pull request Nov 23, 2026
## Description

This is phase 4 of the AST to IR call migration, after #18808, #18813,
#18967, #19351 and #19462. Commands now run only from compiled IR, so
`engine::Call` loses its AST variants and the code that handled them
goes too.

Plain `cal` was the last runtime code that built an `ast::Call`, to run
`table --index false`. It now runs `table` through `eval_ir_call`, as
`help` does. The rest is deletion: `CallImpl::AstRef`/`AstBox`,
`eval_call`, `eval_external`, `impl CallExt for ast::Call`, and the AST
branches in `get`, `run`, `run-internal`, known externals and plugin
calls.

The AST evaluator stays, because `explain` and the parser info of
`source`, `source-env` and `run` still use it. Its call arms now return
an error, which fixes two `explain` bugs. `explain` evaluates every
argument, and a keyword's argument can be a call. For `else if` that
call is `if`, whose `run` panics. For `alias x = cmd` it ran `cmd`. Both
now show an error value. An argument that holds a subexpression, like
`(1 + 2)`, now shows its source text and parsed type instead of an
internal debug dump. `explain` doesn't run it, because that would run
its commands.

The AST `has_flag` was the only check that a builtin's switch got a
bool. The parser rejects `ls --all=5`, but `ls --all=$x` with `$x` typed
`any` and set to `5` counted as set. Builtins, const calls and custom
commands now give the error `%$cmd` already gave. A builtin checks when
it reads the switch, so one that never reads it still ignores a bad
value.

## User-facing changes (Release notes)

- `explain` no longer crashes on `else if`, and no longer runs the
command an `alias` points to.
- `explain` shows an argument with a subexpression, like `(1 + 2)`, as
its source text and parsed type.
- Breaking change: a switch given a value that isn't a bool, as in `ls
--all=$x` with `$x` set to `5`, is an error. It used to count as set.

## Additional notes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing api-change This PR should be mentioned in #api-updates channel on Discord notes:mention Noted in "Hall of Fame" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants