Skip to content

Compile a call into another module - #882

Merged
onatozmenn merged 2 commits into
mainfrom
backend-cross-module
Aug 2, 2026
Merged

Compile a call into another module#882
onatozmenn merged 2 commits into
mainfrom
backend-cross-module

Conversation

@onatozmenn

Copy link
Copy Markdown
Collaborator

The interpreter has always been handed every file the compiler checked. The lowering was handed one, so a program with a use in it was refused, which is most programs that do any real work: eight of the thirty-four corpus files were waiting on this and nothing else.

The reason it wasn't just plumbing is that a DefId is an index into one module's table and a Span is an offset into one file, so neither means anything on the other side. A body from another module gets read with that module's resolutions and that module's types, swapped in around the call rather than looked up per read, which keeps every existing lookup written the way it was.

Two decisions I'd want a reviewer to look at:

Only what is reached is lowered. A module that ships thirty functions and is imported for one contributes one. That matters because the rest of it may use shapes this backend can't compile, and refusing a program over a function nobody called would be wrong.

A callee's where clause is kept whatever the other side worked out about its own callers. The check gets dropped when every recorded call proved the clause, and the calls that reach an imported function were answered for in the caller's table, which the callee's module never saw. There's a test for that one.

One thing came along with it: a type parameter that only appears inside a callback's type can't be read off an argument, because a function value reaches this layer as one shape whatever it takes and hands back, and map's B is exactly that. What the call itself came out as says it, so the return type is read when the arguments don't settle everything.

Three more corpus files compile: json, tic_tac_toe, using_list. Twenty-two of thirty-four.

Part of #877.

Written with AI assistance.

The interpreter has always been handed every file the compiler checked.
The lowering was handed one, so a program with a `use` in it was refused,
which is most programs that do any real work: eight of the thirty-four
corpus files were waiting on this and nothing else.

A `DefId` is an index into one module's table and a `Span` is an offset
into one file, so neither means anything on the other side. A body from
another module is read with that module's resolutions and that module's
types, swapped in around the call rather than looked up per read, which
keeps every existing lookup written the way it was.

Only what is reached is lowered. A module that ships thirty functions and
is imported for one contributes one, which matters because the rest of it
may use shapes this backend cannot compile and refusing a program over a
function nobody called would be wrong.

A callee's `where` clause is kept whatever the other side worked out
about its own callers. The check is dropped when every recorded call
proved the clause, and the calls that reach an imported function were
answered for in the caller's table, which the callee's module never saw.

One thing came along with it. A type parameter that only appears inside a
callback's type cannot be read off an argument, because a function value
reaches this layer as one shape whatever it takes and hands back, and
`map`'s `B` is exactly that. What the call itself came out as says it, so
the return type is read when the arguments do not settle everything.

Three more corpus files: json, tic_tac_toe, using_list. Twenty-two of
thirty-four.

Part of #877.
@onatozmenn
onatozmenn merged commit edb7d15 into main Aug 2, 2026
5 checks passed
@onatozmenn
onatozmenn deleted the backend-cross-module branch August 2, 2026 19:26
onatozmenn added a commit that referenced this pull request Aug 2, 2026
Follow-up to #882. A function reached across a module boundary can call its own module's functions, and those have no index in the program being compiled either. The lowering only looked for an import, so `markdown.deed` got refused at `slice`, which `std/string`'s own `replace` calls and nothing imports.

Same path as an import: the callee is lowered from the module being read, as it is reached, into the same program. Nothing here is on the path of a program that imports nothing.

The agreement test grew a function in the library that calls another one in the same library, since that was the case that was missing.

Twenty-eight of thirty-four. Part of #877.

<sub>Written with AI assistance.</sub>
onatozmenn added a commit that referenced this pull request Aug 2, 2026
Follow-up to #882. A call could cross a module boundary and a type could not, so a program that imported a record, a choice, an effect or a handler was refused however plain the call using it was.

A type crosses the same way a function does, and the thing that matters is that it comes out as what the module that declared it built. Two layouts for one record would make a value of it fit neither, so each module's tables gain the names its `use` lines asked for rather than a copy of the shape. By name and only for what was asked for, so two modules that both declare a `Point` keep their own.

The checker carries an imported type by module and name rather than by definition, for the same reason a `DefId` can't cross at all, and the lowering had no arm for that shape. It has one now, and the name is enough because the borrowing above put the right layout under it.

The agreement test reads a field of a record the other module built and matches on a variant of a choice the other module declared, which is what would break if the two sides disagreed on the layout.

Part of #877.

<sub>Written with AI assistance.</sub>
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