Skip to content

Sema: an implicit it closure is the nearest argument that takes a function (#2326, D161) - #2746

Merged
ehartford merged 1 commit into
fix-2646from
fix-2326b
Oct 26, 2026
Merged

ehartford merged 1 commit into
fix-2646from
fix-2326b

Conversation

@ehartford

@ehartford ehartford commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

names.iter().any(s.contains(it)) was refused: the parser wrapped the
closure at the innermost call argument holding it, so the program read
any(s.contains(x => x)). Only any(n => s.contains(n)) type-checks, and
which call owns it depends on parameter types, which the parser cannot
see (#2660 fixed the half where codegen aborted on it).

The fix moves that decision to Sema, the owner of the call's types:

  • Parser.parse_call records, for each argument that is or holds an it
    closure, its extra slot and the enclosing call argument holding it
    (AstPool implicit_it_slots / implicit_it_parents; copied by
    astpool_clone_deep, which every program goes through).
  • Sema.check_expr_with_expected notes such an argument's demand while it
    is checked; check_closure, finding an it closure with no function
    demand, walks the parents to the nearest argument demanding a
    one-parameter function (it_closure_host), records the move and
    suppresses errors until that argument is done.
  • An argument holding an it closure defers like a closure with an
    untyped parameter (arg_waits_for_fn_demand, formerly
    closure_has_untyped_param), so a generic call checks it against the
    function type its other arguments bind.
  • D128's body loop applies the moves (apply_it_closure_moves: slots are
    relinked, no node is created) and checks the body again. The moves are
    the loop's own: a generic instance checked inside it (std's iter_map,
    found in lldb applying main's move and rechecking itself) keeps its
    own list. Past 64 passes it is a loud BUG, never a silent accept.
  • A move that nests one it closure in another is §9.3.1's E0951.

Tests: behav_2326_it_belongs_to_nearest_fn_argument (any, all, a filter
whose closure already owns another it, two value calls in between) and
err_2326_moved_it_closure_nests. The 34 fixtures whose code uses it, the
it diagnostic fixtures and parser_unit_test pass on stage1;
int-local-rechecks over src/main.w stays 21.

Delegated under D98 as D161; §9.3.1 gains the nearest-argument rule.

Closes #2326

…unction (#2326, D161)

`names.iter().any(s.contains(it))` was refused: the parser wrapped the
closure at the innermost call argument holding `it`, so the program read
`any(s.contains(x => x))`. Only `any(n => s.contains(n))` type-checks, and
which call owns `it` depends on parameter types, which the parser cannot
see (#2660 fixed the half where codegen aborted on it).

The fix moves that decision to Sema, the owner of the call's types:

- Parser.parse_call records, for each argument that is or holds an `it`
  closure, its extra slot and the enclosing call argument holding it
  (AstPool implicit_it_slots / implicit_it_parents; copied by
  astpool_clone_deep, which every program goes through).
- Sema.check_expr_with_expected notes such an argument's demand while it
  is checked; check_closure, finding an `it` closure with no function
  demand, walks the parents to the nearest argument demanding a
  one-parameter function (it_closure_host), records the move and
  suppresses errors until that argument is done.
- An argument holding an `it` closure defers like a closure with an
  untyped parameter (arg_waits_for_fn_demand, formerly
  closure_has_untyped_param), so a generic call checks it against the
  function type its other arguments bind.
- D128's body loop applies the moves (apply_it_closure_moves: slots are
  relinked, no node is created) and checks the body again. The moves are
  the loop's own: a generic instance checked inside it (std's iter_map,
  found in lldb applying main's move and rechecking itself) keeps its
  own list. Past 64 passes it is a loud BUG, never a silent accept.
- A move that nests one `it` closure in another is §9.3.1's E0951.

Tests: behav_2326_it_belongs_to_nearest_fn_argument (any, all, a filter
whose closure already owns another `it`, two value calls in between) and
err_2326_moved_it_closure_nests. The 34 fixtures whose code uses `it`, the
`it` diagnostic fixtures and parser_unit_test pass on stage1;
int-local-rechecks over src/main.w stays 21.

Delegated under D98 as D161; §9.3.1 gains the nearest-argument rule.
@ehartford
ehartford added this pull request to stack #2749 October 26, 2026 07:19
@ehartford ehartford changed the title fix 2326b Sema: an implicit it closure is the nearest argument that takes a function (#2326, D161) Oct 26, 2026
@ehartford
ehartford marked this pull request as ready for review October 26, 2026 08:24
@ehartford
ehartford merged commit b920db7 into main Oct 26, 2026
@ehartford
ehartford deleted the fix-2326b branch October 26, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

it shorthand: names.iter().any(s.contains(it)) is refused (wrong argument type to iter_any_views)

1 participant