1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! # 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());
//! # Ok::<(), hir_lang::HirError>(())
//! ```
//!
//! ## 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).
extern crate alloc;
pub use Checker;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
use Hir;
/// Checks `hir` under `profile`, with no host declarations and a budget
/// scaled to the HIR ([`Budget::for_hir`]).
///
/// Type errors in the program are [`Diagnostic`]s in the result; see
/// [`Checker`] to declare host functions and types, or to set a budget.
///
/// # Errors
///
/// See [`Checker::check`].
///
/// # Examples
///
/// ```
/// use hir_lang::{Builder, Lit, Name, Prim};
/// use intern_lang::Interner;
/// use typeck_lang::{check, Profile, TypeId};
///
/// let mut b = Builder::new();
/// let t = b.lit(Lit::Bool(true));
/// let body = b.block(&[], Some(t));
/// // A function without a declared return type returns unit.
/// let f = b.func(Name::new(Interner::new().intern("f")), &[], body);
/// let root = b.module(None, &[f]);
/// let hir = b.finish(root)?;
/// let typed = check(&hir, &Profile::static_explicit()).unwrap();
/// assert_eq!(typed.diagnostics().len(), 1);
/// assert_eq!(typed.expr(t), Some(TypeId::prim(Prim::Bool)));
/// # Ok::<(), hir_lang::HirError>(())
/// ```
/// Compiles and runs the `rust` code blocks in `README.md` and `docs/API.md`
/// as part of `cargo test`, so the published examples cannot drift from the
/// API.
;