Repository navigation
Merged
Conversation
…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.
&T only when T is a function type, so a closure is no &str needle (#2326)
ehartford
marked this pull request as ready for review
October 25, 2026 22:13
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, andthe 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_fastreturns 1 at that line, so
s.contains(c => …)andnames.iter().any(s.contains(it))passed Sema and codegen aborted: "BUG: a&strruntime argument is not a str value (LLVM type kind 10)".analyze audit:resolutionstayed 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 thereference'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
ithalf of #2326 (whetherany(s.contains(it))meansany(n => s.contains(n))) is a scoping question for §9.3.1 and stays open.Part of #2326 (the
itscoping question stays open on the issue).