Skip to content

Sema: a function value meets &T only when T is a function type, so a closure is no &str needle (#2326) - #2660

Merged
ehartford merged 1 commit into
mainfrom
fix-2326
Oct 25, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-2326

Conversation

@ehartford

@ehartford ehartford commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Root cause: types_compatible_fast (src/Sema.w, and its frozen twin):
if (exp_k == TY_PTR or exp_k == TY_REF) and act_k == TY_FN: return 1, and
the reverse. Any function value met any reference. lldb on the installed
compiler, stepping builtin_arg_type_compatible(expected=17 &str,
actual=727 fn(u8) -> bool): types_compatible -> types_compatible_fast
returns 1 at that line, so s.contains(c => …) and
names.iter().any(s.contains(it)) passed Sema and codegen aborted: "BUG: a
&str runtime argument is not a str value (LLVM type kind 10)".
analyze audit:resolution stayed silent (violations=0) on the fn operand.

A raw pointer and a function value still convert both ways (a C callback's
*const c_void). A reference and a function value meet only when the
reference's pointee is a function type or still unresolved
(ref_admits_fn_value).

Test: test/compile_errors/err_2326_closure_is_not_a_str_needle.w ("wrong
argument type in call to 'str.contains'"). The installed compiler checks it
clean and aborts in codegen.

The it half of #2326 (whether any(s.contains(it)) means
any(n => s.contains(n))) is a scoping question for §9.3.1 and stays open.

Part of #2326 (the it scoping question stays open on the issue).

…a closure is no `&str` needle (#2326)

Root cause: types_compatible_fast (src/Sema.w, and its frozen twin):
`if (exp_k == TY_PTR or exp_k == TY_REF) and act_k == TY_FN: return 1`, and
the reverse. Any function value met any reference. lldb on the installed
compiler, stepping builtin_arg_type_compatible(expected=17 `&str`,
actual=727 `fn(u8) -> bool`): types_compatible -> types_compatible_fast
returns 1 at that line, so `s.contains(c => …)` and
`names.iter().any(s.contains(it))` passed Sema and codegen aborted: "BUG: a
`&str` runtime argument is not a str value (LLVM type kind 10)".
`analyze audit:resolution` stayed silent (violations=0) on the fn operand.

A raw pointer and a function value still convert both ways (a C callback's
`*const c_void`). A reference and a function value meet only when the
reference's pointee is a function type or still unresolved
(ref_admits_fn_value).

Test: test/compile_errors/err_2326_closure_is_not_a_str_needle.w ("wrong
argument type in call to 'str.contains'"). The installed compiler checks it
clean and aborts in codegen.

The `it` half of #2326 (whether `any(s.contains(it))` means
`any(n => s.contains(n))`) is a scoping question for §9.3.1 and stays open.
@ehartford
ehartford added this pull request to stack #2668 October 25, 2026 22:01
@ehartford ehartford changed the title fix 2326 Sema: a function value meets &T only when T is a function type, so a closure is no &str needle (#2326) Oct 25, 2026
@ehartford
ehartford marked this pull request as ready for review October 25, 2026 22:13
@ehartford
ehartford merged commit 2a4b3e3 into main Oct 25, 2026
@ehartford
ehartford deleted the fix-2326 branch October 25, 2026 22:14
ehartford added a commit that referenced this pull request Oct 26, 2026
…unction (#2326, D161) (#2746)

`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.

Co-authored-by: Eric Hartford <[email protected]>
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.

1 participant