Skip to main content

Crate typeck_lang

Crate typeck_lang 

Source
Expand description

§typeck_lang

Profile-driven type checking over HIR: one checker, configured by a type-system Profile instead of written per language.

use hir_lang::{Builder, Name, OpKind};
use intern_lang::Interner;
use typeck_lang::{check, Profile};

let mut names = Interner::new();
let mut b = Builder::new();
// fn main() { 1 + true; }
let one = b.int(1);
let yes = b.lit(hir_lang::Lit::Bool(true));
let sum = b.op(OpKind::Add, &[one, yes]);
let stmt = b.expr_stmt(sum);
let body = b.block(&[stmt], None);
let main = b.func(Name::new(names.intern("main")), &[], body);
let root = b.module(None, &[main]);
let hir = b.finish(root)?;

// Statically: one diagnostic, at `true`.
let typed = check(&hir, &Profile::static_explicit()).unwrap();
let d = typed.diagnostics()[0];
assert_eq!(d.node, hir_lang::NodeRef::Expr(yes));
assert_eq!(typed.message(&d, &names), "`add` is not defined for `bool`");

// Dynamically: nothing to report; every value is `any`.
assert!(check(&hir, &Profile::dynamic()).unwrap().is_ok());

§What it does

  • Profiles as data (Profile, Discipline): dynamic, gradual, static-explicit, static-inferred, and hm, with toggles for subtyping, nominal or structural records, numeric widening, null safety, exhaustive matches, traits versus interfaces, implicit receiver borrowing, the monomorphization policy, let x = null, and ?. This version implements dynamic, static-explicit, and static-inferred; gradual and hm return CheckError::NotImplemented (see dev/ROADMAP.md).
  • Generics and interfaces: generic functions and types with bounds and where clauses, constant parameters, defaults; interfaces (traits) with methods, default methods, constants, associated types, and supertraits; impl blocks, generic ones indexed like any other; interface method resolution through bounds, dyn types, and implementations; impl Trait parameters and results; coherence (overlapping implementations) and conformance (missing, extra, or mistyped members).
  • Bidirectional checking: expected types flow into arguments of generic calls, record literals, and closures (whose signatures static-inferred infers).
  • Exhaustiveness through match-lang: missing values as witnesses, unreachable arms and redundant alternatives as warnings.
  • Output (Typed): dense side tables of types keyed by HIR expression, pattern, and binder ids; typed diagnostics with origins; the implicit conversions lowering must materialise; how each operation resolved (integer, IEEE, string); the definitions of partial paths’ unresolved tails (Shape::Circle, T::new); what each use of a generic item instantiates; and the monomorphization plan (MonoPlan).
  • One root cause, one diagnostic: anything reported has type TypeId::ERROR, which every later check accepts silently.
  • Termination: union-find over hash-consed type terms (no exponential blow-up on shared structure), iterative traversals everywhere, and a Budget (steps, type-store size, trait-resolution depth) that turns pathological input into CheckError::BudgetExceeded instead of a hang.

The checker does not resolve names (resolve-lang does) and does not compile patterns into decision trees (match-lang decides exhaustiveness for it; lowering compiles them).

Structs§

Budget
Limits on the work one check may do.
Checker
The configured path: declare host types, set a budget, then check.
Diagnostic
One finding about the checked program.
Field
A named field of a structural record type.
Instance
One instance in a MonoPlan.
Instantiation
The generic item an expression uses and its arguments: a path to a generic function, a method call, a constructor of a generic type.
MonoPlan
The instances a program needs.
Profile
A type-system profile.
Stats
Work counters of a finished check.
TypeDisplay
A type rendered for display; see Types::display.
TypeId
A type in a Types store.
Typed
The result of checking one HIR unit.
Types
The store of interned type terms.

Enums§

CheckError
Why a check did not produce a Typed.
Coercion
An implicit conversion the checker accepted where a value meets a context of another type. Lowering must materialise it.
DiagnosticKind
What a diagnostic says. Types are ids into the Typed’s store, resolved (no inference variables).
Discipline
How much a language checks statically, and how much it infers.
Dispatch
How a call reaches its callee.
Feature
A language feature the checker recognises but this version does not check. Each is recorded in dev/ROADMAP.md.
Limit
Which budget ran out.
MonoPolicy
Which generic items a profile copies and which it shares.
NullLet
What let x = null (no annotation) gives x (ISSUES P17).
OpClass
How an OPS operation was resolved by its operand types (HIR leaves this open: the same add is integer, IEEE, or string concatenation).
Polymorphism
How a language attaches behaviour to types.
RecordTyping
Whether record types are identified by declaration or by shape.
Severity
How serious a diagnostic is.
Strategy
How a generic item is compiled.
TryHook
How ?-style error propagation reaches the checker.
Type
A read-only view of one type node.

Functions§

check
Checks hir under profile, with no host declarations and a budget scaled to the HIR (Budget::for_hir).