Files
nushell/tests/repl
Albert O'SheaandBahex 63ca014ef3 fix(parser): infer for loop items from iterable unions (#18551)
## Description

`parse_for` inferred the loop variable from the type of the iterable
expression. That works for direct list, table, and range types, but
fails for iterable unions returned by commands like `skip`.

For `oneof<table, binary, list<any>>`, the loop variable was typed as
the whole iterable union instead of the yielded row or item type. With
`enforce-runtime-annotations` enabled, assigning the yielded value to
that loop variable raised a runtime type mismatch.

The parser now maps iterable types to their yielded types when inferring
the loop variable type, including alternatives inside `oneof`.

### example: filter output union

<table>
<tr>
<td>Code</td>
<td width="1000">

```nushell
let pending = ([a] | each {} | collect | skip 0)
for item in $pending { print $item }
```

</td>
</tr>

<tr>
<td>Before</td>
<td>

```ansi
Error: nu::shell::type_mismatch

  × Type mismatch.
   ╭─[source:1:17]
 1 │ let pending = ([a] | each {} | collect | skip 0); for item in $pending { print $item }
   ·                 ┬                                     ──┬─
   ·                 │                                       ╰── expected oneof<table, binary, list<any>>, got string
   ·                 ╰── the value is a string
   ╰────
```

</td>
</tr>

<tr>
<td>After</td>
<td>

```ansi
a
```

</td>
</tr>
</table>

## User-facing changes (Release notes)

- Fixed an issue in `for` loops where type of the looping variable was
inferred incorrectly when iterating over a value/stream with a union
type (`oneof<list, table, binary>`). This could manifest as a runtime
type checking error with `enforce-runtime-annotations` enabled.

---------

Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com>
2026-07-09 14:03:20 +03:00
..
2026-01-26 10:17:45 +08:00
2026-06-17 11:44:36 +03:00