pub struct Types { /* private fields */ }Expand description
The store of interned type terms.
A Typed result owns the store its side tables point into;
Checker::types_mut exposes the store before a
check, to build the types of host declarations.
§Examples
use typeck_lang::{Type, TypeId, Types};
use hir_lang::{Effects, Prim};
let mut types = Types::new();
let i32_ = TypeId::prim(Prim::I32);
let f = types.func(&[i32_, i32_], i32_, Effects::NONE);
match types.get(f) {
Type::Fn { params, ret, .. } => {
assert_eq!(params, &[i32_, i32_]);
assert_eq!(ret, i32_);
}
other => panic!("{other:?}"),
}Implementations§
Source§impl Types
impl Types
Sourcepub fn new() -> Self
pub fn new() -> Self
A store holding only the fixed types.
§Examples
use typeck_lang::{Type, TypeId, Types};
let types = Types::new();
assert_eq!(types.get(TypeId::NEVER), Type::Never);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of nodes in the store (variables included).
§Examples
use typeck_lang::Types;
let mut types = Types::new();
let before = types.len();
let _ = types.slice(typeck_lang::TypeId::UNIT);
assert_eq!(types.len(), before + 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the store holds no nodes (never: the fixed types are
always present).
§Examples
assert!(!typeck_lang::Types::new().is_empty());Sourcepub fn get(&self, id: TypeId) -> Type<'_>
pub fn get(&self, id: TypeId) -> Type<'_>
Returns a view of a type. Ids from another store, and inference
variables (which never appear in results), read as Type::Error.
§Examples
use typeck_lang::{Type, TypeId, Types};
let mut types = Types::new();
let n = types.nullable(TypeId::UNIT);
assert_eq!(types.get(n), Type::Nullable(TypeId::UNIT));Sourcepub fn tuple(&mut self, elems: &[TypeId]) -> TypeId
pub fn tuple(&mut self, elems: &[TypeId]) -> TypeId
A tuple type; tuple(&[]) is TypeId::UNIT.
§Examples
use typeck_lang::{TypeId, Types};
assert_eq!(Types::new().tuple(&[]), TypeId::UNIT);Sourcepub fn array(&mut self, elem: TypeId, len: u64) -> TypeId
pub fn array(&mut self, elem: TypeId, len: u64) -> TypeId
A fixed-length array type.
§Examples
use typeck_lang::{Type, TypeId, Types};
use hir_lang::Prim;
let mut types = Types::new();
let a = types.array(TypeId::prim(Prim::I32), 3);
assert_eq!(types.get(a), Type::Array { elem: TypeId::prim(Prim::I32), len: 3 });Sourcepub fn slice(&mut self, elem: TypeId) -> TypeId
pub fn slice(&mut self, elem: TypeId) -> TypeId
A slice type.
§Examples
use typeck_lang::{Type, TypeId, Types};
let mut types = Types::new();
let s = types.slice(TypeId::UNIT);
assert_eq!(types.get(s), Type::Slice(TypeId::UNIT));Sourcepub fn reference(&mut self, mutable: bool, inner: TypeId) -> TypeId
pub fn reference(&mut self, mutable: bool, inner: TypeId) -> TypeId
A reference type.
§Examples
use typeck_lang::{Type, TypeId, Types};
let mut types = Types::new();
let r = types.reference(true, TypeId::UNIT);
assert_eq!(types.get(r), Type::Ref { mutable: true, inner: TypeId::UNIT });Sourcepub fn pointer(&mut self, mutable: bool, inner: TypeId) -> TypeId
pub fn pointer(&mut self, mutable: bool, inner: TypeId) -> TypeId
A raw pointer type.
§Examples
use typeck_lang::{Type, TypeId, Types};
let mut types = Types::new();
let p = types.pointer(false, TypeId::UNIT);
assert_eq!(types.get(p), Type::Ptr { mutable: false, inner: TypeId::UNIT });Sourcepub fn func(
&mut self,
params: &[TypeId],
ret: TypeId,
effects: Effects,
) -> TypeId
pub fn func( &mut self, params: &[TypeId], ret: TypeId, effects: Effects, ) -> TypeId
A function type with positional parameters.
§Examples
use typeck_lang::{TypeId, Types};
use hir_lang::Effects;
let mut types = Types::new();
let thunk = types.func(&[], TypeId::UNIT, Effects::NONE);
assert_eq!(thunk, types.func(&[], TypeId::UNIT, Effects::NONE));Sourcepub fn nullable(&mut self, inner: TypeId) -> TypeId
pub fn nullable(&mut self, inner: TypeId) -> TypeId
T?. Nullable types do not nest: nullable(T?) is T?, and
nullable(null) is null.
§Examples
use typeck_lang::{TypeId, Types};
let mut types = Types::new();
let once = types.nullable(TypeId::UNIT);
assert_eq!(types.nullable(once), once);Sourcepub fn map(&mut self, key: TypeId, value: TypeId) -> TypeId
pub fn map(&mut self, key: TypeId, value: TypeId) -> TypeId
A map type.
§Examples
use typeck_lang::{Type, TypeId, Types};
use hir_lang::Prim;
let mut types = Types::new();
let m = types.map(TypeId::prim(Prim::Str), TypeId::prim(Prim::I64));
assert!(matches!(types.get(m), Type::Map { .. }));Sourcepub fn record(&mut self, fields: &[Field]) -> TypeId
pub fn record(&mut self, fields: &[Field]) -> TypeId
A structural record type. Fields are sorted by symbol; a repeated name keeps its first type.
§Examples
use typeck_lang::{Field, TypeId, Types};
use intern_lang::Interner;
let mut names = Interner::new();
let (a, b) = (names.intern("a"), names.intern("b"));
let mut types = Types::new();
let ab = types.record(&[Field { name: a, ty: TypeId::UNIT }, Field { name: b, ty: TypeId::NULL }]);
let ba = types.record(&[Field { name: b, ty: TypeId::NULL }, Field { name: a, ty: TypeId::UNIT }]);
assert_eq!(ab, ba);Sourcepub fn adt(&mut self, item: ItemId) -> TypeId
pub fn adt(&mut self, item: ItemId) -> TypeId
The nominal type of a record, sum, or class item of the checked unit, without generic arguments.
§Examples
use typeck_lang::{Type, Types};
use hir_lang::ItemId;
let mut types = Types::new();
let item = ItemId::from_index(3).unwrap();
let t = types.adt(item);
assert_eq!(types.get(t), Type::Adt { item, args: &[] });Sourcepub fn adt_with(&mut self, item: ItemId, args: &[TypeId]) -> TypeId
pub fn adt_with(&mut self, item: ItemId, args: &[TypeId]) -> TypeId
The nominal type of a generic record, sum, or class item applied to
args.
§Examples
use typeck_lang::{Type, TypeId, Types};
use hir_lang::{ItemId, Prim};
let mut types = Types::new();
let item = ItemId::from_index(3).unwrap();
let i32_ = TypeId::prim(Prim::I32);
let t = types.adt_with(item, &[i32_]);
assert_eq!(types.get(t), Type::Adt { item, args: &[i32_] });
assert_ne!(t, types.adt(item));Sourcepub fn foreign(&mut self, def: DefId) -> TypeId
pub fn foreign(&mut self, def: DefId) -> TypeId
The nominal type of another unit’s definition, without generic arguments.
§Examples
use typeck_lang::{Type, Types};
use hir_lang::{Def, DefId, ItemId, UnitId};
let mut types = Types::new();
let def = DefId::foreign(UnitId::new(2), Def::Item(ItemId::from_index(0).unwrap()));
let t = types.foreign(def);
assert_eq!(types.get(t), Type::Foreign { def, args: &[] });Sourcepub fn foreign_with(&mut self, def: DefId, args: &[TypeId]) -> TypeId
pub fn foreign_with(&mut self, def: DefId, args: &[TypeId]) -> TypeId
The nominal type of another unit’s generic definition applied to
args.
§Examples
use typeck_lang::{Type, TypeId, Types};
use hir_lang::{Def, DefId, ItemId, UnitId};
let mut types = Types::new();
let def = DefId::foreign(UnitId::new(2), Def::Item(ItemId::from_index(0).unwrap()));
let t = types.foreign_with(def, &[TypeId::UNIT]);
assert_eq!(types.get(t), Type::Foreign { def, args: &[TypeId::UNIT] });Sourcepub fn external(&mut self, name: Symbol) -> TypeId
pub fn external(&mut self, name: Symbol) -> TypeId
The nominal type of a host symbol (Res::Extern), without generic
arguments.
§Examples
use typeck_lang::{Type, Types};
use intern_lang::Interner;
let mut names = Interner::new();
let vec = names.intern("Vec");
let mut types = Types::new();
let t = types.external(vec);
assert_eq!(types.get(t), Type::Extern { name: vec, args: &[] });Sourcepub fn external_with(&mut self, name: Symbol, args: &[TypeId]) -> TypeId
pub fn external_with(&mut self, name: Symbol, args: &[TypeId]) -> TypeId
A generic host type applied to args (Vec<i32>).
§Examples
use typeck_lang::{Type, TypeId, Types};
use hir_lang::Prim;
use intern_lang::Interner;
let mut names = Interner::new();
let vec = names.intern("Vec");
let mut types = Types::new();
let t = types.external_with(vec, &[TypeId::prim(Prim::I32)]);
assert_eq!(types.display(t, &names).to_string(), "Vec<i32>");Sourcepub fn union(&mut self, members: &[TypeId]) -> TypeId
pub fn union(&mut self, members: &[TypeId]) -> TypeId
A union of members, in canonical form: nested unions are
flattened, null and nullable members make the whole union nullable
(A | B | null is (A | B)?), members are sorted by id and
deduplicated, a single member is itself, ! members vanish (an empty
union is !), and any or {error} absorb the union.
§Examples
use typeck_lang::{Type, TypeId, Types};
use hir_lang::Prim;
let mut types = Types::new();
let (i, s) = (TypeId::prim(Prim::I64), TypeId::prim(Prim::Str));
let a = types.union(&[s, i]);
assert_eq!(a, types.union(&[i, s, i]));
assert!(matches!(types.get(a), Type::Union(&[_, _])));
let n = types.union(&[i, TypeId::NULL]);
assert_eq!(n, types.nullable(i));Sourcepub fn intersection(&mut self, members: &[TypeId]) -> TypeId
pub fn intersection(&mut self, members: &[TypeId]) -> TypeId
An intersection of members, in canonical form: nested intersections
are flattened, members sorted by id and deduplicated, a single member
is itself, and ! or {error} absorb it.
§Examples
use typeck_lang::{Type, TypeId, Types};
use hir_lang::ItemId;
let mut types = Types::new();
let a = types.adt(ItemId::from_index(1).unwrap());
let b = types.adt(ItemId::from_index(2).unwrap());
let ab = types.intersection(&[b, a]);
assert_eq!(types.get(ab), Type::Intersection(&[a, b]));
assert_eq!(types.intersection(&[a]), a);Sourcepub fn awaitable(&mut self, result: TypeId) -> TypeId
pub fn awaitable(&mut self, result: TypeId) -> TypeId
The awaitable a call of an ASYNC function returns.
§Examples
use typeck_lang::{Type, TypeId, Types};
let mut types = Types::new();
let a = types.awaitable(TypeId::UNIT);
assert_eq!(types.get(a), Type::Async(TypeId::UNIT));Sourcepub fn task(&mut self, result: TypeId) -> TypeId
pub fn task(&mut self, result: TypeId) -> TypeId
The handle spawn returns.
§Examples
use typeck_lang::{Type, TypeId, Types};
let mut types = Types::new();
let t = types.task(TypeId::UNIT);
assert_eq!(types.get(t), Type::Task(TypeId::UNIT));Sourcepub fn generator(&mut self, item: TypeId, is_async: bool) -> TypeId
pub fn generator(&mut self, item: TypeId, is_async: bool) -> TypeId
A generator yielding item.
§Examples
use typeck_lang::{Type, TypeId, Types};
let mut types = Types::new();
let g = types.generator(TypeId::UNIT, false);
assert_eq!(types.get(g), Type::Generator { item: TypeId::UNIT, is_async: false });Sourcepub fn display<'a, L: Lookup>(
&'a self,
id: TypeId,
names: &'a L,
) -> TypeDisplay<'a, L>
pub fn display<'a, L: Lookup>( &'a self, id: TypeId, names: &'a L, ) -> TypeDisplay<'a, L>
Renders a type for messages, resolving names with names.
Rendering is iterative and capped (about 200 characters, then …), so
a type of any depth or sharing renders in bounded time.
§Examples
use typeck_lang::{TypeId, Types};
use hir_lang::Prim;
use intern_lang::Interner;
let names = Interner::new();
let mut types = Types::new();
let t = types.tuple(&[TypeId::prim(Prim::I32), TypeId::prim(Prim::Bool)]);
let s = types.slice(t);
assert_eq!(types.display(s, &names).to_string(), "[(i32, bool)]");