preserve variable capture spans in blocks (#15334)

Closes #15160

# User-Facing Changes

Certain "variable not found" errors no longer highlight the surrounding
block.

Before:

```nushell
do {
  match foo {
    _ => $in
  }
}

Error: nu:🐚:variable_not_found

  × Variable not found
   ╭─[entry #1:1:1]
 1 │ ╭─▶ do {
 2 │ │     match foo {
 3 │ │       _ => $in
 4 │ │     }
 5 │ ├─▶ }
   · ╰──── variable not found
```

After:

```nushell
Error: nu:🐚:variable_not_found

  × Variable not found
   ╭─[entry #1:3:10]
 2 │   match foo {
 3 │     _ => $in
   ·          ─┬─
   ·           ╰── variable not found
```
This commit is contained in:
Solomon
2025-03-20 18:20:28 +00:00
committed by GitHub
parent 7a6cfa24fc
commit dd56c813f9
6 changed files with 17 additions and 15 deletions

View File

@ -642,17 +642,17 @@ impl Eval for EvalRuntime {
.get_block(block_id)
.captures
.iter()
.map(|&id| {
.map(|(id, span)| {
stack
.get_var(id, span)
.get_var(*id, *span)
.or_else(|_| {
engine_state
.get_var(id)
.get_var(*id)
.const_val
.clone()
.ok_or(ShellError::VariableNotFoundAtRuntime { span })
.ok_or(ShellError::VariableNotFoundAtRuntime { span: *span })
})
.map(|var| (id, var))
.map(|var| (*id, var))
})
.collect::<Result<_, _>>()?;

View File

@ -857,7 +857,7 @@ fn literal_value(
let captures = block
.captures
.iter()
.map(|var_id| get_var(ctx, *var_id, span).map(|val| (*var_id, val)))
.map(|(var_id, span)| get_var(ctx, *var_id, *span).map(|val| (*var_id, val)))
.collect::<Result<Vec<_>, ShellError>>()?;
Value::closure(
Closure {