0075. Canonical forms and per-item hashes
- Status: Accepted
- Date: 2026-10-31
- Plan: M9.3 Step 1
- Review: Making Tenon plugin-first
Context
Per-item compatibility needs stable API and implementation
hashes. Formatter output depends on comments, spans and layout;
ast::dump omits integer patterns. Neither is a
complete stable serialization of item meaning.
Decision
For each item, compute SHA-256 hashes printed as
sha256:<64 lowercase hex>. Canonical identity
is <package>::<module.path>.<item>;
root module paths use the file stem. Versions are excluded, so
version-only bumps preserve hashes; package renames change them.
Names come from Rewrite; canonical package loading is Step 2, the
CLI Step 3.
Signature forms are single-line ASCII. All generic groups use
angle brackets, including empty <>; types
render as NAME<arg1,arg2>, bounds sort by
bytewise canonical name, function type parameters render
$0, $1, etc., and value parameter names
are omitted; missing returns use (unit). Forms
identify functions, declaration-order struct fields and enum
variants, opaque newtype names, interfaces with methods sorted by
name, and impl interface/type pairs. Keys retain separate
function/type namespaces; edges resolve to types even when a
function has the same canonical name. Serialize AST structure, not
formatter or dump text.
Exact byte preimages (each displayed \n is one LF
byte):
sig = "harness-sig-1\n" + form(x) + "\n" + concat(form(y) + "\n" for y in closure(x) - {x})
body = "harness-body-1\n" + body_form(x) + "\n"
closure(x) is the reflexive-transitive set reached
through signature references, computed with a
BTreeSet worklist ordered bytewise by canonical name;
exclude x from appended forms. Impls are not in a
type's signature closure. Public-closure private-type filtering is
the caller's job in Step 3, after E0225/coherence checks; this
pass includes private declarations.
Body forms are structurally tagged S-expressions over the
post-Rewrite AST. Function bodies include checkfree,
source generic and parameter names, and the block; impl bodies
include methods sorted by name. A newtype body includes its repr
only in that newtype's own shallow body hash; dependent hashes are
unchanged. ADR-0076
supersedes the compatibility body-hash clause: compatibility
compares signatures only. Structs, enums and interfaces have no
body ((none)). Bodies are shallow: changing a private
helper does not change its caller's hash. Omit spans, ExprId,
op_span, MatchArm.braced and pattern qualifiers; integer patterns
use their i128 interval and expressions their value, not spelling.
Parentheses and tail semicolons are absent from the AST. Missing
returns use (unit); absent bodies/options use
(none). Loop binders use (none) or
(some NAME), distinguishing a binder named
none from absence. Canonicalizer-owned string
escaping and operator tables isolate hashes from formatter
changes. Duplicate item keys and missing root/closure map items
return errors.
The temporary #![allow(dead_code)] on the
canonicalizer module is removed in Step 3.
Alternatives considered
- Hash formatted source — comments, source spans and layout decisions affect formatter output without changing API or implementation meaning.
- Reuse
parse --dump— the dump omits AST information (including integer patterns), so distinct forms can collide before hashing.
Consequences
Signature changes propagate through reachable signature dependencies, while body hashes describe only the item's own shallow implementation. The canonicalizer must evolve with AST forms. Package identity loading, public-item CLI exposure and requirements generation remain later M9.3 steps.
Canonical loading treats the root's file stem as its module
path even without imports. Consequently, an import of
lib from a loaded module under root
lib.hn refers back to the root and reports an import
cycle (E0221), matching the dependency view. Ordinary compilation
retains its existing root identity.