Skip to content

Lay the compiler out by phase and concept - #192

Merged
itsfuad merged 25 commits into
mainfrom
refactor/carbon-layout
Nov 20, 2026
Merged

itsfuad merged 25 commits into
mainfrom
refactor/carbon-layout

Conversation

@itsfuad

@itsfuad itsfuad commented Nov 18, 2026

Copy link
Copy Markdown
Member

Reshapes the compiler so each phase has one closed list of what it knows, one exhaustive match that reaches each kind's work, and one file per language concept, named the same in the parser and the checker. The rules and the steps, with what each one measured, are in docs/compiler-layout.md.

What changes

  • No features crate. Each feature's parse half is a concept file of the parser (parser/src/pipes.rs) and its check half one of the checker (check/src/pipes.rs). The parser's rule table, RULES, is built at compile time; two rules for one token fail the build.
  • Type facts in one place. How a value is held, whether it borrows or holds loans, its text and hash: each is one exhaustive function in semantic_types or layout.rs, so a new type is a compile error at each. One builtin_types! table names every built-in type, range[T] included.
  • Conversions in one file. semantic_types/conversions.rs decides, by the pair of types, whether a value converts and the mir::Cast that does it (renamed from mir::Conversion). Lowering and the constant folder read the cast instead of deciding it again.
  • Layout. crates/kernel/* moved to crates/*; the tree crate is tree, the checker check. The parser's decl.rs/expr.rs/stmt.rs are declaration.rs/expression.rs/statement.rs, as in the checker. Test-only folders are inlined as mod tests.
  • Lints. Every dispatch carries #[deny(clippy::wildcard_enum_match_arm, clippy::match_wildcard_for_single_variants)]: the checker, the dump, lowering, the interpreter, LLVM and MIR's own walks. scripts/check.sh also fails on a source file no mod line names.

No language behavior changes.

This branch also carries the earlier unmerged commits on it (generic structs, functions on any declared type, the kernel move).

Validation

  • bash scripts/check.sh: format, clippy, tests, bundle, test programs on the bundle (64-bit and i686-linux), examples, LLVM parity, browser build and smoke, whitespace.
  • peeper lsp over every fixture: 276 files, 10180 hovers, no panics.
  • Each new lint and the duplicate-rule check were proven to fail by breaking them on purpose.

The checker kept one list of facts per module, and the compile handed
"the facts found so far" to the next function it built. A fact naming
a local was then read against another function's body. A `where` that
names a parameter is checked whenever the signature is first asked
for, often during an earlier function, so the editor showed the wrong
local (`main`'s `const Ratio` for `Pick`'s `N`) or panicked when that
body had no such local, as `negative_where_conditions.peep` did.

The checker now keeps facts by the declaration they were found in and
owns the rule for handing them on: `function_facts` gives a function
the facts of its own signature and body, whose locals that body
declares, and a copy none; `other_facts` gives the rest of a module in
declaration order, without names of locals, as no body declares those
of a function never handed on. The driver only calls the two.

A new LSP test opens every test program in the editor's check and
fails on any panic, which no other test exercised.
A struct can now declare `<T>`: `struct Box<T> { value: T }`, named as
`Box<i32>`. Each list of type arguments makes its own copy of the
struct, checked once and kept, so phases after the checker see only
ordinary struct types.

Copies of functions and of types now share one table and one
`copy_type` (check/src/copies.rs): it makes each copy once, finds a
copy that depends on itself, limits how deep copies nest, and moves a
copy's errors to its use. `FunctionCopy` is renamed `DeclarationCopy`
to match. A function copy then queues its body; a type copy runs its
declaration's check.

One list gives the type parameters of any declaration
(`declaration_copy_parameters`), read for binding `T`, counting the
arguments of `Box<i32>`, and refusing a bare `Box`.

A type's own check, with no type for `T`, keeps which declaration a
field names when an argument names `T` (`named_without_types`), and
makes the copies its fields hold. So a struct that holds a new copy of
itself by value, `struct Two<T> { a: Box<Two<[2]T>> }`, is reported
once at its declaration as holding itself, instead of making copies
without end; a pointer only names its target, so `*Node<T>` stays
fine. A generic function's own check still makes no copy.

When a copy's error already marks the use, no second label is added
there.

Hashing a type for an importer's rebuild moves from the driver into
`Types::hash`, which now also hashes a copy's arguments; the driver
only says how to name a declaration.

The shared copy messages now say "the copy made here" and "makes a new
copy of itself", as a type's copy is not made by a call.

Functions of generic types are refused for now.
`fn Owner::name` was allowed only on a struct. Any type a module
declares can now have functions: a struct, an interface, later an enum.
An alias still cannot, as it names a type declared elsewhere.

A call with a dot finds the function by the type's declaration rather
than its fields. On an interface or an interface reference, the
interface's own function (`fn Shape::Twice(&self)`) is found before its
methods; inside it, `self.Area()` still goes through the method table,
so it runs for every type that implements the interface. A function
that shares a name with one of the interface's methods is refused as
declared twice, so a call never means both.

An unknown name on an interface now suggests its functions too, not
another module's private ones.

The refusal of an owner now reads "is not a type declared in this
module". `Status::Ready` on a refused enum reads as an unknown name in
the type, as the type can have functions.
The front and middle crates move to one kernel folder, as agreed in
docs/structure-mapping.md (step 1): token, lexer, syntax (in tree/) and
parser under kernel/syntax/; check (in semantic/), model, lower, flow
and eval under kernel/. project moves to driver/, since it gathers a
compile's input. Package names stay, so no Rust file changes.

The feature-boundary check in check.sh still reads the same 17 crate
manifests, project among them.
Each of the four features only wrapped what the core already owned: a
bool or str/cstr constant, the core's conversion, the THIR panic. They
become plain tree nodes (Expr::Bool, Expr::String, Expr::Cast,
Stmt::Panic), read by the core parser and checked by the core checker;
the feature files go (docs/structure-mapping.md, step 2).

Language::string_kind goes with them: it only let the core ask the text
feature whether an expression was a string literal, for an extern's
name and a panic message, which now match Expr::String directly.

The features' tests move to the core crates and run in Bare; the cast
cases join the parser's precedence table.
Split declaration.rs and its neighbours into one module per concept:
functions (signatures, bodies, calls, #[builtin] functions), generics
(type arguments, copies, where), conformance, locals (let and const)
and expression/names. No checking logic changed.

function_declaration moves to model::program beside declaration, and
argument_count_message to lib.rs, so generics needs nothing from
functions and the two modules no longer depend on each other.
unsupported_type moves next to the type-name lookup that reads it, and
the signature tests move to functions.rs with the code they test.

Imports in the new and moved files are at most two levels deep.
A feature's files may not name another feature. The check now also
refuses super paths to another feature (super::enums from structs.rs,
super::super::enums from structs/tests.rs), brace imports such as
crate::{enums}, and files nested at any depth, not only */tests.rs.

The rule moves from a quoted bash -c string into a function, and reads
the feature list from the pub mod lines of features/src/lib.rs, so a
new feature is covered without editing the check.
The copy table held TypeId::INVALID while a copy was checked, and a
later lookup read INVALID as a cycle. That was right only because a
copy with an error is always removed. Copied::ty is now an Option,
None while the copy is checked, so the two meanings no longer share a
value. The cycle fixture in negative_where_conditions fails without
the None branch.

"No type for T yet" stays INVALID: every reader treats it as a
reported error and stays silent, so a second marker would have no
consumer. The mapping page records why.
Peeper is one language, so the phases are no longer generic over L.
The syntax tree lists every node, feature nodes too (FeatureExpr,
FeatureType, FeatureStmt and their node types), and answers the shape
questions the Language trait answered (precedence, fields, methods,
pointee, element, written_range) with exhaustive matches.

The parser carries its rule table: parse and project::load take the
rules, which the driver gives as features::RULES. The checker's
Checked trait becomes CheckHooks, four plain functions the driver gives
as features::CHECK_HOOKS; each is one exhaustive match that hands a
node to its feature's file. ExprNode, StmtNode and TypeNode go: a
feature file has one rows function and free check, resolve and declare
functions taking only what each node uses. Bare goes too: core tests
use Rules::core() and hooks that are unreachable.

check.sh now refuses a feature node's variant named outside ast.rs and
the features crate, which the opaque Feature(L::Expr) used to prevent
by type. LiteralType and Fallback are renamed ArrayLiteralType and
OrElseFallback, as they now sit among every node in ast.

No behaviour changes: string literals are the same but the new
unreachable messages; all tests pass, and the LSP opens every test
program and answers hovers without a panic.
Follow docs/compiler-layout.md: one closed list per phase, one
exhaustive dispatch per phase, one file per concept named the same in
the parser and the checker, and each fact about a type decided in one
place.

- Dissolve the `features` crate into concept files of the parser and
  the checker; one `parser::RULES` table built at compile time, where
  two rules for one token fail the build.
- Type facts are one exhaustive function each (`borrows`, `holds_loans`,
  `holders_in`, `layout::held`); `layout::in_memory` and lowering's own
  list of held types are gone. One `builtin_types!` table names every
  built-in type, `range[T]` included.
- Conversions are decided in `semantic_types/conversions.rs`, carrying
  the `mir::Cast` (renamed from `mir::Conversion`); lowering and the
  constant folder no longer decide them again.
- Crates move out of `crates/kernel/`; the tree crate is `tree`, the
  checker `check`. Parser files are named as the checker's
  (`expression.rs`, `statement.rs`, `declaration.rs`).
- The no-wildcard lints sit on every dispatch: the checker, the dump,
  lowering, the interpreter, LLVM and MIR's own walks.
- Test-only folders are inlined as `mod tests`; check.sh fails on a
  source file no `mod` names.

No language behavior changes.
The .claude/vault notes are no longer kept in the repository: remove them, their .gitignore exception and their import in CLAUDE.md.
The check that every source file is named by a `mod` line matched
`mod name;` anywhere, so a comment such as `// mod x;` counted as naming
the file. It now matches only a `mod` line, with an optional `pub` or
`pub(...)`.
A generic declaration's own check gave its type parameters no type, so
anything they reached was guessed from the written syntax, and an error
in one place hid or invented errors in others.

Each type parameter now stands for itself in its own check
(`Type::Parameter`). A type holding one, or holding `Type::Undecided`,
is open (stored at intern): what depends on it is left to each copy
(`ExprKind::Undecided`, `Conversion::Undecided`) and `Type::Invalid`
keeps one meaning, an error was reported.

- A call finds its type arguments by matching types, not syntax
  (`Types::find_parameters`), for `Box<T>`, `&[]T` and nested copies
  alike. The syntax walks it replaces are deleted.
- A generic body calling another with open arguments makes no copy: the
  call's type is the callee's type with the arguments substituted.
- An open struct such as `Box<T>` stays `Type::Named`, with no copy or
  layout; its field types are read with what it is given, once per
  argument list (cached).
- A range's step type is derived from its element type, not stored.
- `where` on an open subject, and a compile-time call given one, is not
  known in the own check, so it reports nothing there.

Fixtures cover struct copies through references, slices and nested
copies, and the copy labels on errors found in a copy.
docs/generics.md describes the design.
`expression/places.rs` decided `value.field`, `array[index]`,
`&array[i..j]` and `reference.*` for every concept itself: array length
and elements, slice ends, struct fields, raw pointer targets and open
types. Each operation is now one exhaustive match on the value's type
that hands the work to the concept owning it:

- `.len` and element reads move to `arrays.rs`;
- slicing, its ends and the slice help for `.*` move to the new
  `slices.rs`;
- finding and reading a struct field moves to `structs.rs`, with the
  field types of an open struct, which `generics/copies.rs` read from
  struct syntax;
- an open type gives `Expr::UNDECIDED` in one arm per match.

The dispatch keeps the errors for a type no concept takes and the
shared reach through a pointer. Diagnostics and their order are
unchanged.

scripts/cross-concepts.py counts the decisions each checker concept file
makes about another concept, to measure this layout: 155 before, 127
after, places.rs from 27 to 0.
Each of the four dispatches in expression/places.rs listed every checked
type to pick an owner, so the same lists were written four times. They
now ask the type table what the value's type has: `elements`,
`is_slice_shaped`, `pointee`, and two new questions, `has_fields` (a
struct, a copy or one given an open type) and `is_decided_per_copy` (a
bare type parameter or undecided). `is_open` would be wrong there: `[2]T`
and `Box<T>` are open but still have elements and fields.

The questions keep their `_ =>`, as docs/compiler-layout.md rule 4 asks
of a match that only asks "is it one of these?".
Three pages still named `crates/middle/...`, which the layout commit
moved to `crates/...`. The flow page's link to where the checker runs the
flow checks now names `functions/bodies.rs`, as `function.rs` was split.
A generic body's own check stayed silent for every type that held a
`T`. Now only a bare `T` (or an undecided type) is left to each copy;
`[2]T`, `*T` or `Box<T>` are still arrays, pointers and structs, so
`-pair` or `let flag: bool = pair` is reported for `pair: [2]T`.

Conversions are three-valued: the same, the same in some copy
(`Types::could_be_same`, `Conversion::Undecided`), or never.

A spread's count is one type, `EntryCount`, in place of the
`(known, later)` pair: counted now, counted when the program runs, or
counted by each copy. `[_]i32{ 1, x... }` with `x: T` is an array of an
undecided type; `[3]i32{ x... }` keeps its type, and more values than
its length counted now is reported.

Behavior change: in a `...` argument list, an error in one entry now
stops the count, as it already did for array literals, so a later slice
spread is no longer also reported.
In a generic declaration's own check, a `const` parameter had no value,
so it was stored as `None`, the same as a constant with an error, and
lost its type too. `let flag: bool = N;` for `const N: i32` was not
reported, and `const RUN: i32 = Double(N);` gave a false "cannot be
worked out at compile time" error.

A body's constants are now `thir::CheckedConstant`: `Known(constant)`,
or `PerCopy(type)` when each copy gives the value. `None` is only an
error. Read, a per-copy constant is `ExprKind::Undecided` of its type,
so `N`, `N * 2` and `Double(N)` are an `i32` whose value each copy
gives. A `where` constraint takes a per-copy answer as unknown.

shortcut: a per-copy value given to another function's `const`
parameter, `H(N)`, still makes the call silent, until `ConstArguments`
can hold such a value.
A const argument each copy gives (CheckedConstant::PerCopy) is now held
in ConstArguments and makes them open, so the call keeps its type in a
generic body's own check instead of staying silent.

substitute, find_parameters, could_be_same and holds_open now share one
walk over a type's parts (Types::parts); only data that is not a part,
such as mutability or variadic, is compared by kind.
An array's length is a CheckedConstant, the same hole a const argument
uses. A spread counted by each copy, as [_]i32{1, x...} with x of a type
parameter, gives an array of i32 whose length is CheckedConstant::PerCopy
(written [_]i32), not an undecided type, so what does not depend on the
length is checked once in the generic body's own check.

Types::values walks the values a type holds, beside Types::parts, so
open, could-be-same, hash and text treat an array's length and a struct's
const arguments alike. .len reads the length through constant_expr, as
any const value. Layout reads the total count only for a type whose
lengths are all known.
A call finds a type parameter from a `const` argument as from any other,
then checks the value against the type the call gives, so
`fn Positive<T>(const n: T)` is accepted instead of refused with T0043.
A value's type may name only the type parameters before it, as a call
gives those first.

A value of a type each copy gives, such as `const Y: U = 3` in a generic
body, is now known per copy; it crashed layout before. When a call has an
error, each place it gives nothing keeps the function's own parameter, so
later values' types still line up.

Hover shows a copy's const value with the type the copy gives: `i32`
for `T`, and `u64` for a `usize`, as its other parameters show.
One copy nesting past COPY_DEPTH_LIMIT set a flag for the whole compile,
after which no queued copy was built and no compile-time run went on: an
unrelated generic's errors went unreported, and a later constant failed
without a message. Each copy now keeps the first copy it grew from, its
root, and only copies grown from a root that went too deep are skipped,
which still stops a family that doubles at each depth at its first copy
too deep.
Each operator on a `T` runs for the copies whose types take it and is
refused for each copy whose type does not; a spread into a written array
length fills it in the copy that fits and is reported for one too short
and one too long.
@itsfuad
itsfuad enabled auto-merge November 20, 2026 15:28
@itsfuad
itsfuad added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit bfa2510 Nov 20, 2026
10 checks passed
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