lang::typecheck::affine
lang/src/typecheck/affine.mo
The affine-by-default usage rule — Milestone 2 of the affine-by-default experiment (Design B: Viability Experiment).
[[lang/typecheck/usage.mo]] counts uses; [[lang/typecheck/copy_class.mo]] decides whether a type may be shared. This module is the judgement that combines them, and it is the first thing in the experiment that says a program is wrong.
The rule
Design B's effective multiplicity, verbatim. There is no
multiplicity annotation syntax — the !/?/% prefixes an earlier
design used were parsed but never enforced and have been removed
from both grammars — so the only rows a source can reach are the
#[linear]/#[proof] and unannotated ones:
1 if the type is #[linear] or #[proof]
ω if `Copy A` resolves // the only route to ω
≤1 otherwise // the default
The declared parameter and the arms that read it stay because
Param.mult is a struct field the runtime matches positionally; the
parser now always fills it with many, so those arms are the field's
total-match requirement rather than a reachable feature.
Multiplicity.many is the parser's default for an UNANNOTATED
binder, and under this rule that means affine, not ω. Reading it
as ω would silently pass every program and disable the whole check
while still looking like it worked — the same shape of blind-
classifier bug that once made the borrow measurement report 99.9%
borrowable. test_unannotated_is_affine_not_many exists to keep it
from coming back.
Warnings, not errors — and why they are still TypeError
Nothing here rejects a program. The corpus has 2,394 binders this would fire on, so enforcement has to arrive per-mote as each one reaches zero, and until then these are advisory.
They are nonetheless TypeError values (lang/types.mo), not a
separate warning type. Warn-versus-error is properly the driver's
decision — whether a diagnostic fails the build — and keeping these
in the existing error type means promotion is a caller-side switch
rather than a second diagnostic pipeline to build and then retire.
The cost was checked before committing to it: exactly one site
(lang/typecheck/diagnostic.mo) matches TypeError exhaustively.
Which diagnostic, and why three
The three cases have genuinely different remedies, and the
measurement in bench/src/affine_report.mo already separates them,
so the diagnostic names the fix instead of just reporting a count:
copy_required— used more than once, but at most one of those uses takes ownership. A borrow serves it, or aCopyinstance does. This is the large, tractable bucket (2,447 in the compiler's own closure).value_used_after_move— two or more OWNING uses: the value really is moved twice. No borrow discipline rescues it; it needsCopy,Clone, or a rewrite. The irreducible bucket (187).linear_unused— a!xnever used. The one case affine's weakening does not excuse, since linear means exactly once.borrowed_value_escapes— aShared.getview stored, returned, or (once call signatures are consulted) passed to an owning parameter. The view aliases a payload its handle points at; handing it out as an owner would give one value two owners.
An erased %x used in runtime position reports through
TypeError.custom: it is real but has no corpus instances at all
(the whole corpus carries zero multiplicity annotations), so it does
not earn its own variant yet.
pub def effective_mult (scope : Scope) (declared : Multiplicity) (typ : Term) : MultiplicityDesign B's effective multiplicity for one binder.
declared is what the source wrote. The corpus writes nothing
today — every binder arrives as Multiplicity.many, the parser's
default — so in practice this function's job is to turn "unannotated"
into "affine unless Copy". It takes declared anyway so the rule is
complete and testable now, ahead of the plumbing that will carry a
real !/% through lowering (which needs the Def.attrs carrier
described in the plan, since Term.lam has nowhere to put one).
pub def effective_mult_with_vars (scope : Scope) (copy_vars : List Identifier) (declared : Multiplicity) (typ : Term) : MultiplicityThe rule with the enclosing def's [Copy A] constraints in hand.
copy_vars is the type-variable half of the Copy gate. Copy's own
instance registry cannot answer for a bare variable -- the carrier is
not a type constructor, and copy_class.mo deliberately fails closed
there. Under Design B a generic def that shares a parameter must
therefore SAY it may ([Copy A]), and this list is where that
permission lands.
pub def effective_mult_with_kinds (kinds : TypeKinds) (scope : Scope) (copy_vars : List Identifier) (declared : Multiplicity) (typ : Term) : MultiplicityThe rule with #[linear]/#[proof] type kinds in hand.
A proof type is ALWAYS linear (view-types rule 1). A #[linear] type
is linear. The ?/% overrides this used to document — the
annotation spellings that relaxed or erased a linear binder — were
removed from the grammar with the rest of the prefix syntax, so no
source can reach those arms; they remain only because declared is a
struct field the runtime matches positionally.
pub def permits_sharing (m : Multiplicity) : BoolDoes this effective multiplicity permit unrestricted use?
pub def check_binder (scope : Scope) (declared : Multiplicity) (u : BinderUse) : Option TypeErrorThe diagnostic this binder earns, if any.
declared is threaded in rather than read off BinderUse by callers
that build one by hand; the always-on rule (check_binders_go) passes
BinderUse.declared u, which carries the source's prefix.
The over-use gates compare the BORROW-AWARE budget count - borrowed, not the raw count: f (Shared.of x); g (Shared.of x)
mentions x twice but consumes it zero times, and gating on the raw
count there was the false positive B1 existed to remove. borrowed
is computed per-branch consistently (usage.mo's BinderUse
doc comment says why a separately-maxed count would under-count),
so the budget is exact across match arms too.
pub def check_binder_with (kinds : TypeKinds) (scope : Scope) (copy_vars : List Identifier) (strict : Bool) (declared : Multiplicity) (u : BinderUse) : Option TypeErrorThe checking variant used by the always-on rule: copy_vars is the
def's [Copy A] permission list, and strict selects fail-closed
(the report's measurement) over fail-open (enforcement).
Fail-open, and why the direction flips here. The report must never
flatter the design, so an unattributable binder counts as affine
there. Enforcement is the opposite question: rejecting a program the
analysis cannot type is a false positive, and a false positive is
worse than a missed one while nothing frees memory yet. So a binder
whose type is still Term.hole -- an unannotated lambda, or a match
binder whose constructor is corpus-ambiguous -- is left alone. The
report keeps calling check_def_strict, so its numbers stay an upper
bound.
pub def typ_is_attributable (t : Term) : BoolCan this binder's type be named at all? A hole (an unannotated
lambda, or an unresolved match field) or an unnamed de-Bruijn
variable carries no answer, and enforcement treats "no answer" as
"not our question" rather than as affine. A NAMED type variable is
attributable: affine is the correct answer for it unless the def
carries [Copy A].
pub def check_def (kinds : TypeKinds) (scope : Scope) (ctors : HashMap String Bool) (borrows : HashMap String Bool) (d : Def) : List TypeErrorEvery affine diagnostic a def's body earns, as enforcement sees it:
binders the syntax-based attribution cannot type are left alone
(check_binder_with's own doc says why the direction flips).
Takes the constructor-name set and the borrow-name set rather than
deriving them, because building either is a scan of every inductive
in scope and a caller checking many defs must build them once.
ctor_name_set/shared_name_set (lang/typecheck/usage.mo) are
the builders.
pub def check_def_strict (kinds : TypeKinds) (scope : Scope) (ctors : HashMap String Bool) (borrows : HashMap String Bool) (d : Def) : List TypeErrorThe report's variant: fail-closed on an unattributable binder, so the
migration cost stays an upper bound. Every number in
bench/src/affine_report.mo comes from this one.
pub def copy_vars_of_def (d : Def) : List IdentifierThe type variables the def's [Copy A] constraints license. This is
the polymorphic half of the Copy gate: copy_class.mo resolves type
CONSTRUCTORS through instances, and deliberately fails closed on a
bare variable, so a generic def must say which variables it may
share. Def.constraints has carried them since the class machinery
landed; nothing read them for this rule until now.
pub def type_var_is_copy (copy_vars : List Identifier) (typ : Term) : BoolIs typ a bare type variable named in copy_vars?
Only a BARE variable. List A is not Copy merely because A is:
the list is still a heap chain, and its own Copy instance would have
to deep-copy. Peeling stops at the first non-ctx constructor, so
the applied case correctly answers no.