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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
// ===============================================================================
// BUILDLANG TYPE SYSTEM - UNIFICATION
// ===============================================================================
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2022-2026 Zain Dana Harper.
// ===============================================================================
//! Type unification algorithm.
//!
//! Unification finds a substitution that makes two types equal.
//! This is the core algorithm for Hindley-Milner type inference.
use super::error::{TypeError, TypeResult};
use super::ty::*;
/// True when a numeric literal of this inference kind can never have type `ty`.
/// Only concrete non-numeric types are listed; variables, parameters,
/// references and projections are left to the ordinary rules.
fn literal_cannot_be(kind: &InferKind, ty: &TyKind) -> bool {
let non_numeric = matches!(
ty,
TyKind::Str
| TyKind::Bool
| TyKind::Char
| TyKind::Adt(..)
| TyKind::Tuple(_)
| TyKind::Array(..)
| TyKind::Slice(_)
| TyKind::Fn(_)
// A pointer (and `Box<T>`, issue #83) is never an integer literal;
// `let b: Box<i64> = 5` passed check and segfaulted in 1.7.0.
| TyKind::Ptr(..)
);
match kind {
InferKind::Int => non_numeric,
InferKind::Float => non_numeric || matches!(ty, TyKind::Int(_)),
_ => false,
}
}
/// Unifier for type inference.
#[derive(Debug)]
pub struct Unifier {
/// The current substitution.
subst: Substitution,
}
impl Unifier {
/// Create a new unifier.
pub fn new() -> Self {
Self {
subst: Substitution::new(),
}
}
/// Create a unifier with an initial substitution.
pub fn with_subst(subst: Substitution) -> Self {
Self { subst }
}
/// Get the current substitution.
pub fn substitution(&self) -> &Substitution {
&self.subst
}
/// Take the substitution.
pub fn into_substitution(self) -> Substitution {
self.subst
}
/// Apply the current substitution to a type.
pub fn apply(&self, ty: &Ty) -> Ty {
ty.substitute(&self.subst)
}
/// Unify two types, updating the substitution.
///
/// Reference and pointer pointees unify invariantly: a concrete integer
/// or float width difference behind `&`, `&mut`, or `*` is a type error,
/// not a silent coercion. In value position, integer width coercion stays
/// permissive so array indexing with an i32 variable still checks.
pub fn unify(&mut self, t1: &Ty, t2: &Ty) -> TypeResult<()> {
self.unify_mode(t1, t2, false)
}
/// Unify with an explicit variance mode. `invariant` is true when the two
/// types sit behind a reference or pointer, where a width coercion would
/// let a store or load reach the wrong number of bytes.
fn unify_mode(&mut self, t1: &Ty, t2: &Ty, invariant: bool) -> TypeResult<()> {
let t1 = self.apply(t1);
let t2 = self.apply(t2);
self.unify_impl(&t1, &t2, invariant)
}
/// Internal unification implementation.
fn unify_impl(&mut self, t1: &Ty, t2: &Ty, invariant: bool) -> TypeResult<()> {
// If types are equal (including annotations), we're done
if t1 == t2 {
return Ok(());
}
// Check color space / annotation compatibility.
// If BOTH types have annotations, they must match.
// If only one has annotations, the unannotated type is compatible
// (allows mixing annotated APIs with unannotated code).
if !t1.annotations.is_empty() && !t2.annotations.is_empty() {
// Both have annotations - check for conflicts
// Extract the category (e.g., "ColorSpace") and value (e.g., "Linear")
for ann1 in &t1.annotations {
for ann2 in &t2.annotations {
if let (Some(cat1), Some(cat2)) =
(ann1.split(':').next(), ann2.split(':').next())
{
if cat1 == cat2 && ann1 != ann2 {
// Same category, different value - color space mismatch!
return Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
});
}
}
}
}
}
match (&t1.kind, &t2.kind) {
// Type variable on the left
(TyKind::Var(v1), _) => {
self.bind(*v1, t2.clone())?;
Ok(())
}
// Type variable on the right
(_, TyKind::Var(v2)) => {
self.bind(*v2, t1.clone())?;
Ok(())
}
// A numeric literal's inference variable only binds to a type a
// number can be. `let s: str = 4` and `f(3)` with `fn f(p: P)`
// used to bind and pass the check, then fail in the C compiler.
(TyKind::Infer(infer), other) | (other, TyKind::Infer(infer))
if literal_cannot_be(&infer.kind, other) =>
{
Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
})
}
// An integer literal meeting a float literal becomes a float, as
// `let s = 60.3; let c = 0; s / c` should check (issue #167). The
// generic arm below bound the float literal's variable to the
// integer one instead, so `60.3` checked as an integer and the
// division was emitted as an integer division.
(TyKind::Infer(i1), TyKind::Infer(i2))
if i1.kind == InferKind::Int && i2.kind == InferKind::Float =>
{
self.bind(i1.var, t2.clone())?;
Ok(())
}
(TyKind::Infer(i1), TyKind::Infer(i2))
if i1.kind == InferKind::Float && i2.kind == InferKind::Int =>
{
self.bind(i2.var, t1.clone())?;
Ok(())
}
// Inference variables
(TyKind::Infer(infer1), _) => {
self.bind(infer1.var, t2.clone())?;
Ok(())
}
(_, TyKind::Infer(infer2)) => {
self.bind(infer2.var, t1.clone())?;
Ok(())
}
// Error type unifies with anything (for error recovery)
(TyKind::Error, _) | (_, TyKind::Error) => Ok(()),
// Never type can unify with any type (subtype of everything)
(TyKind::Never, _) | (_, TyKind::Never) => Ok(()),
// Primitive types must be equal
(TyKind::Int(i1), TyKind::Int(i2)) if i1 == i2 => Ok(()),
// Value-position integer width coercion (e.g. i32 <-> usize for
// array indexing) stays permissive so common programs check.
// Behind a reference or pointer the widths must match exactly: a
// differing width falls through to the mismatch error below, since
// an 8-byte store through a 4-byte place corrupts adjacent memory.
(TyKind::Int(_), TyKind::Int(_)) if !invariant => Ok(()),
// Float-to-float coercion (f32 <-> f64) stays allowed for
// ecosystem compatibility, exactly as before. A unit dimension
// (`f64<m/s>`, experimental) is the one thing that still hard
// fails here: two DIFFERENT `Some` dimensions never unify.
// Equal dimensions, or at least one `None` (unconstrained,
// compatible with any unit), unify same as an unannotated float.
(TyKind::Float(f1), TyKind::Float(f2)) => {
// Behind a reference or pointer, f32 and f64 are different
// widths and must not coerce; fall through to the error.
if invariant && f1 != f2 {
return Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
});
}
match (&t1.unit_dim, &t2.unit_dim) {
(Some(a), Some(b)) if a != b => Err(TypeError::UnitMismatch {
expected: a.to_canonical_string(),
found: b.to_canonical_string(),
}),
_ => Ok(()),
}
}
(TyKind::Bool, TyKind::Bool) => Ok(()),
(TyKind::Char, TyKind::Char) => Ok(()),
(TyKind::Str, TyKind::Str) => Ok(()),
// String coercion: `str` and `&str` / `&'static str` are
// interchangeable in BuildLang (both map to BuildString).
(TyKind::Str, TyKind::Ref(_, _, inner)) | (TyKind::Ref(_, _, inner), TyKind::Str)
if inner.kind == TyKind::Str =>
{
Ok(())
}
// Reference coercion: `&T` unifies with `T` (auto-deref).
// Only for concrete types (ADT, primitives), not for Never/Error.
// Excludes Ref-vs-Ref: when both sides are references, fall through
// to the Ref/Ref arm which checks lifetime compatibility.
(TyKind::Ref(_, _, inner), other)
if !invariant
&& !matches!(
other,
TyKind::Never | TyKind::Error | TyKind::Var(_) | TyKind::Ref(_, _, _)
) =>
{
self.unify_impl(inner, t2, invariant)
}
(other, TyKind::Ref(_, _, inner))
if !invariant
&& !matches!(
other,
TyKind::Never | TyKind::Error | TyKind::Var(_) | TyKind::Ref(_, _, _)
) =>
{
self.unify_impl(t1, inner, invariant)
}
// Tuples: must have same length and unify element-wise
(TyKind::Tuple(elems1), TyKind::Tuple(elems2)) => {
if elems1.len() != elems2.len() {
return Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
});
}
for (e1, e2) in elems1.iter().zip(elems2.iter()) {
self.unify_mode(e1, e2, invariant)?;
}
Ok(())
}
// Arrays: same element type and length
(TyKind::Array(elem1, len1), TyKind::Array(elem2, len2)) => {
if len1 != len2 {
return Err(TypeError::ArrayLengthMismatch {
expected: *len1,
found: *len2,
});
}
self.unify_mode(elem1, elem2, invariant)
}
// Slices: same element type
(TyKind::Slice(elem1), TyKind::Slice(elem2)) => {
self.unify_mode(elem1, elem2, invariant)
}
// Array-to-slice coercion: [T; N] unifies with [T]
// This allows passing fixed-size arrays where slices are expected.
(TyKind::Array(elem1, _), TyKind::Slice(elem2)) => {
self.unify_mode(elem1, elem2, invariant)
}
(TyKind::Slice(elem1), TyKind::Array(elem2, _)) => {
self.unify_mode(elem1, elem2, invariant)
}
// References: same mutability and unified pointee
(TyKind::Ref(lt1, mut1, ty1), TyKind::Ref(lt2, mut2, ty2)) => {
if mut1 != mut2 {
return Err(TypeError::MutabilityMismatch {
expected: *mut1,
found: *mut2,
});
}
// Lifetime unification: lifetimes unify if they're identical,
// or if either is elided (None), allowing inference to proceed
match (lt1, lt2) {
(Some(l1), Some(l2)) if l1 != l2 => {
return Err(TypeError::LifetimeMismatch {
expected: l1.clone(),
found: l2.clone(),
});
}
// Elided lifetimes or matching lifetimes are acceptable
_ => {}
}
// A mutable or shared reference pins its pointee width: reading
// or writing through it moves a fixed number of bytes, so the
// pointee unifies invariantly.
self.unify_mode(ty1, ty2, true)
}
// Pointers: same mutability and unified pointee
(TyKind::Ptr(mut1, ty1), TyKind::Ptr(mut2, ty2)) => {
if mut1 != mut2 {
return Err(TypeError::MutabilityMismatch {
expected: *mut1,
found: *mut2,
});
}
// Same reasoning as references: the pointee width is fixed.
self.unify_mode(ty1, ty2, true)
}
// Functions: unify parameters and return type
(TyKind::Fn(fn1), TyKind::Fn(fn2)) => {
if fn1.params.len() != fn2.params.len() {
return Err(TypeError::ArityMismatch {
expected: fn1.params.len(),
found: fn2.params.len(),
});
}
if fn1.is_unsafe != fn2.is_unsafe {
return Err(TypeError::UnsafetyMismatch);
}
// ABI matching: ABIs must be compatible for function pointers
// None (default Build ABI) is compatible with explicit "build"
// Different explicit ABIs are incompatible
match (&fn1.abi, &fn2.abi) {
(None, None) => {}
(None, Some(a)) | (Some(a), None) if &**a == "build" => {}
(Some(a1), Some(a2)) if a1 == a2 => {}
(Some(a1), Some(a2)) => {
return Err(TypeError::AbiMismatch {
expected: a1.clone(),
found: a2.clone(),
});
}
_ => {}
}
for (p1, p2) in fn1.params.iter().zip(fn2.params.iter()) {
self.unify_mode(p1, p2, invariant)?;
}
self.unify_mode(&fn1.ret, &fn2.ret, invariant)?;
// Effect rows are part of the callable contract. Allowing an
// effectful function to unify with a pure function type erases
// the capability gate at callback and assignment boundaries.
if fn1.effects != fn2.effects {
return Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
});
}
Ok(())
}
// ADTs: same definition and unified type arguments
(TyKind::Adt(def1, args1), TyKind::Adt(def2, args2)) => {
if def1 != def2 {
return Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
});
}
if args1.len() != args2.len() {
return Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
});
}
for (a1, a2) in args1.iter().zip(args2.iter()) {
self.unify_mode(a1, a2, invariant)?;
}
Ok(())
}
// Type parameters: must be identical
(TyKind::Param(n1, i1), TyKind::Param(n2, i2)) => {
if n1 == n2 && i1 == i2 {
Ok(())
} else {
Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
})
}
}
// Associated type projections: same trait item and unified inputs.
(
TyKind::Projection {
trait_ref: trait_ref1,
item: item1,
self_ty: self_ty1,
substs: substs1,
},
TyKind::Projection {
trait_ref: trait_ref2,
item: item2,
self_ty: self_ty2,
substs: substs2,
},
) => {
if trait_ref1 != trait_ref2 || item1 != item2 || substs1.len() != substs2.len() {
return Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
});
}
self.unify_mode(self_ty1, self_ty2, invariant)?;
for (subst1, subst2) in substs1.iter().zip(substs2.iter()) {
self.unify_mode(subst1, subst2, invariant)?;
}
Ok(())
}
// All other combinations are mismatches
_ => Err(TypeError::TypeMismatch {
expected: t1.clone(),
found: t2.clone(),
}),
}
}
/// Bind a type variable to a type.
fn bind(&mut self, var: TyVarId, ty: Ty) -> TypeResult<()> {
// Check if already bound
if let Some(existing) = self.subst.get(var) {
return self.unify(&existing.clone(), &ty);
}
// Apply current substitution to resolve already-bound variables
// before the occurs check. Without this, the occurs check can
// false-positive when ?T appears inside &?U and ?U is already
// bound to the same struct type (common with auto-deref on
// reference parameters in functions returning struct literals).
let resolved = self.apply(&ty);
// Occurs check: prevent infinite types
if self.occurs_in(var, &resolved) {
return Err(TypeError::InfiniteType { var, ty: resolved });
}
// Add the binding
self.subst.insert(var, resolved);
Ok(())
}
/// Check if a type variable occurs in a type (for occurs check).
fn occurs_in(&self, var: TyVarId, ty: &Ty) -> bool {
match &ty.kind {
TyKind::Var(v) if *v == var => true,
TyKind::Var(v) => {
if let Some(bound) = self.subst.get(*v) {
self.occurs_in(var, bound)
} else {
false
}
}
TyKind::Infer(infer) if infer.var == var => true,
TyKind::Infer(infer) => {
if let Some(bound) = self.subst.get(infer.var) {
self.occurs_in(var, bound)
} else {
false
}
}
TyKind::Tuple(elems) => elems.iter().any(|t| self.occurs_in(var, t)),
TyKind::Array(elem, _) | TyKind::Slice(elem) => self.occurs_in(var, elem),
TyKind::Ref(_, _, ty) | TyKind::Ptr(_, ty) => self.occurs_in(var, ty),
TyKind::Fn(fn_ty) => {
fn_ty.params.iter().any(|t| self.occurs_in(var, t))
|| self.occurs_in(var, &fn_ty.ret)
}
TyKind::Adt(_, args) => args.iter().any(|t| self.occurs_in(var, t)),
TyKind::Projection {
self_ty, substs, ..
} => self.occurs_in(var, self_ty) || substs.iter().any(|t| self.occurs_in(var, t)),
_ => false,
}
}
}
impl Default for Unifier {
fn default() -> Self {
Self::new()
}
}
/// Unify two types and return the resulting substitution.
pub fn unify(t1: &Ty, t2: &Ty) -> TypeResult<Substitution> {
let mut unifier = Unifier::new();
unifier.unify(t1, t2)?;
Ok(unifier.into_substitution())
}
/// Unify two types with an existing substitution.
pub fn unify_with(subst: Substitution, t1: &Ty, t2: &Ty) -> TypeResult<Substitution> {
let mut unifier = Unifier::with_subst(subst);
unifier.unify(t1, t2)?;
Ok(unifier.into_substitution())
}
/// Check if two types can be unified.
pub fn can_unify(t1: &Ty, t2: &Ty) -> bool {
unify(t1, t2).is_ok()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_unify_same_types() {
let t = Ty::int(IntTy::I32);
let subst = unify(&t, &t).unwrap();
assert!(subst.is_empty());
}
#[test]
fn test_unify_var_with_concrete() {
let v = TyVarId::fresh();
let var = Ty::var(v);
let concrete = Ty::int(IntTy::I32);
let subst = unify(&var, &concrete).unwrap();
assert_eq!(subst.get(v), Some(&concrete));
}
#[test]
fn test_unify_tuples() {
let v = TyVarId::fresh();
let t1 = Ty::tuple(vec![Ty::var(v), Ty::bool()]);
let t2 = Ty::tuple(vec![Ty::int(IntTy::I32), Ty::bool()]);
let subst = unify(&t1, &t2).unwrap();
assert_eq!(subst.get(v), Some(&Ty::int(IntTy::I32)));
}
#[test]
fn test_unify_different_lengths() {
let t1 = Ty::tuple(vec![Ty::int(IntTy::I32)]);
let t2 = Ty::tuple(vec![Ty::int(IntTy::I32), Ty::bool()]);
assert!(unify(&t1, &t2).is_err());
}
#[test]
fn test_occurs_check() {
let v = TyVarId::fresh();
let var = Ty::var(v);
// Try to unify ?T with (?T, bool) - should fail
let t = Ty::tuple(vec![var.clone(), Ty::bool()]);
assert!(unify(&var, &t).is_err());
}
#[test]
fn test_unify_functions() {
let v1 = TyVarId::fresh();
let v2 = TyVarId::fresh();
let t1 = Ty::function(vec![Ty::var(v1)], Ty::var(v2));
let t2 = Ty::function(vec![Ty::int(IntTy::I32)], Ty::bool());
let subst = unify(&t1, &t2).unwrap();
assert_eq!(subst.get(v1), Some(&Ty::int(IntTy::I32)));
assert_eq!(subst.get(v2), Some(&Ty::bool()));
}
#[test]
fn test_unify_rejects_erased_function_effects() {
let pure = Ty::function(vec![Ty::str()], Ty::str());
let file_effect =
super::super::effects::EffectRow::closed([super::super::effects::Effect::new(
"FileSystem",
)]);
let effectful = Ty::function_with_effects(vec![Ty::str()], Ty::str(), file_effect);
let result = unify(&pure, &effectful);
assert!(
result.is_err(),
"effectful functions must not unify with pure function types"
);
}
#[test]
fn test_unify_never() {
// Never type can unify with anything
let never = Ty::never();
let concrete = Ty::int(IntTy::I32);
assert!(unify(&never, &concrete).is_ok());
}
#[test]
fn test_transitive_unification() {
let v1 = TyVarId::fresh();
let v2 = TyVarId::fresh();
let mut unifier = Unifier::new();
unifier.unify(&Ty::var(v1), &Ty::var(v2)).unwrap();
unifier.unify(&Ty::var(v2), &Ty::int(IntTy::I32)).unwrap();
let result = unifier.apply(&Ty::var(v1));
assert_eq!(result, Ty::int(IntTy::I32));
}
#[test]
fn test_ref_lifetime_mismatch_is_error() {
let t1 = Ty::reference(
Some(Lifetime::new("a")),
Mutability::Immutable,
Ty::int(IntTy::I32),
);
let t2 = Ty::reference(
Some(Lifetime::new("b")),
Mutability::Immutable,
Ty::int(IntTy::I32),
);
let result = unify(&t1, &t2);
assert!(
result.is_err(),
"expected LifetimeMismatch error for 'a vs 'b"
);
}
#[test]
fn test_ref_same_lifetime_ok() {
let t1 = Ty::reference(
Some(Lifetime::new("a")),
Mutability::Immutable,
Ty::int(IntTy::I32),
);
let t2 = Ty::reference(
Some(Lifetime::new("a")),
Mutability::Immutable,
Ty::int(IntTy::I32),
);
assert!(unify(&t1, &t2).is_ok());
}
#[test]
fn test_ref_elided_lifetime_ok() {
let t1 = Ty::reference(None, Mutability::Immutable, Ty::int(IntTy::I32));
let t2 = Ty::reference(
Some(Lifetime::new("a")),
Mutability::Immutable,
Ty::int(IntTy::I32),
);
assert!(
unify(&t1, &t2).is_ok(),
"elided lifetime should unify with named"
);
}
}