Repository navigation
Type a function from its body before the callers that have a written type (#1196) - #1205
Merged
Merged
Conversation
added 2 commits
September 19, 2026 00:41
…type
A function with no return annotation takes its type from its body (§9.1,
D43), and bodies were checked in declaration order, so a caller declared
above such a function saw its signature's placeholder (Unit):
fn go(x: i32) -> bool: x > 0 and later_pred() // "must be bool"
fn later_pred(): 1 + 1 == 2
The pinned seed v0.15.1.10 fails the same way; this predates D43.
check_bodies now runs twice over the declarations: first the functions whose
body defines their signature, then the ones with a written return type or a
fixed contract (main, tests). The body check resets per-function state
rather than saving it, so checking a callee on demand in the middle of a
caller is not an option.
What remains is a function with no annotation calling another declared after
it. That call now says so ("the return type of 'later' is not known yet ...
write either function's return type, or declare 'later' first") instead of
whatever the placeholder type broke downstream. A signature records that its
body has typed it; Unit alone cannot tell "returns nothing" from "not typed
yet".
Refs #1196
The first battery stopped in stage2: the compiler's own source calls ci_record_index_reset, a procedure with no annotation, from an unannotated function declared above it. There the placeholder (Unit) is the right type, and demanding an annotation would be ceremony for nothing. A call checked against a not-yet-typed callee is recorded instead. Once every body is typed, the calls whose callee turned out to produce a value are reported; calls to procedures are not. The compiler's source checks clean under the new compiler. Refs #1196
ehartford
added a commit
that referenced
this pull request
Sep 19, 2026
… D45, D46 (#1212) * Main is red after #1205 and #1207: two behavior tests Both merged before their batteries ran. A battery on main (f5cba02) fails behav_tls_module_compiles.w and behav_error_handling.w. #1205: the "return type of 'X' was not known here" report fired on `i31_reduce_once(x, m, mlen)` in std/crypto/bigint.w. That call is a statement: its value is discarded, so the placeholder type cost it nothing. A block now marks the statement it is checking, and a call in that position is not recorded. A call that is a block's tail still is — under D43 the tail is the function's type. #1207: `for s in inputs` over an array of str now binds `&str` (traversal observes), and the test passed `s` to `parse_positive(s: str)`, which only reads it. The function takes `&str`, as an observer should (§3.8). * Hoist only the callee; traverse sequences the same way in comprehensions The battery on the first fix found two more tests red on main. err_thread_spawn_closure_captures_ref.w (#1205): checking every unannotated function first put `bad` ahead of std.thread.spawn_os, whose body gives its closure parameter the escape effect `bad`'s call is judged by, so a weaker diagnostic fired. Bodies are checked in declaration order again; a function that takes its type from its body is checked before the first declaration that calls it, and nothing else moves. The calls come from the AST: nodes are appended children first, so a declaration's subtree is the ids between it and the nearest declaration node below it. A chain of unannotated functions now types (it was reported before); a cycle gets D43's "cannot infer return type ... add `-> T`". spec_ss13_6_comprehensions.w (#1207): lower_comprehension_slice was a copy of lower_for_slice's setup with the same defects — it moved the iterable, took RK_LEN through projections and copied Drop-class elements — while Sema now binds views. The receiver, length and element binding are shared helpers, used by both. A map comprehension stores what it builds: a `&T` key or value with T: Copy materializes a T (D22); a view of a Drop-class value is an error that says to clone it, where it said "`&str` must implement Hash". The test's str keys are cloned; on the pinned seed the same comprehension over a Vec[(str, i32)] already failed, and the array form only passed by making the map a second owner of the array's strings. * The archiver is the compiler invoking itself; record D45 spec-inventory-check on main: "implementation has unspec'd ar / cc / ranlib / --from-source" (from #1208, which merged without a battery). `ar` and `ranlib` are what a CMake build asks of an archiver, not commands a user types: they are `with __ar` and `with __ranlib`, the prefix the inventory already treats as the compiler invoking itself. `with cc` and `with get --from-source` are user surface and wait for spec words. D45 (Eric, 2026-09-19): a copy is never implicit unless it is O(1). The comprehension fix in this branch follows it; #1211 tracks the measurement that could reopen it. * Spec: `with cc`, source builds, and cloned comprehension elements (blessed) Eric blessed this wording on 2026-09-19. §18.5 lists `with cc`. §18.8 lists `with get --from-source c.X` and says how `with get` builds a package from source when Conan Center has no linkable binary: the recipe read as data, the package's own CMake build under `with cc`, installed and locked like a binary package, and a missing tool named rather than worked around. §13.6's examples that build owning collections of `str` from views clone their elements, and the section says why (D22, D45). The inventory checker lists `--from-source` beside `--force-reinstall`: a `with get` flag, documented in §18.8. D46 records the source-build decisions and what was rejected. --------- 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.
Refs #1196.
A function with no return annotation takes its type from its body (§9.1). Bodies were checked in declaration order, so a caller declared above such a function saw the signature's placeholder (Unit):
Predates D43: the pinned seed v0.15.1.10 fails the same way.
Fix.
check_bodiesruns twice over the declarations: first the functions whose body defines their signature, then the ones with a written return type or a fixed contract (main, tests). Checking a callee on demand in the middle of a caller is not possible — the body check resets per-function state rather than saving it.What remains. An unannotated function calling an unannotated function declared after it. Such a call is recorded and, once every body is typed, reported only if the callee turned out to produce a value:
A call to a procedure was typed correctly all along and is not reported — the first battery stopped in stage2 exactly there (
ci_record_index_resetin the compiler's own source), which is why the message is deferred rather than emitted at the call. Ordering those by dependency needs the call graph before Sema; name resolution already records uses per definition.Tests:
behav_unannotated_return_forward_call.w,err_unannotated_return_forward_chain.w. The compiler's source checks clean under the new compiler.Battery: build and fixpoint green;
:test-with-auditsrunning at the time of opening. I will update this line.