Skip to content

Type a function from its body before the callers that have a written type (#1196) - #1205

Merged
ehartford merged 2 commits into
mainfrom
fix-filed-issues
Sep 19, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-filed-issues

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

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):

fn go(x: i32) -> bool: x > 0 and later_pred()   // "right operand of logical operator must be bool"
fn later_pred(): 1 + 1 == 2

Predates D43: the pinned seed v0.15.1.10 fails the same way.

Fix. check_bodies 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). 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:

the return type of 'later' was not known here: it comes from a body declared after this function's, and this function has no return type either; write either function's return type, or declare 'later' first

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_reset in 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-audits running at the time of opening. I will update this line.

Eric Hartford 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
ehartford merged commit b11637b into main Sep 19, 2026
1 of 5 checks passed
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]>
@ehartford
ehartford deleted the fix-filed-issues branch September 24, 2026 16:56
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