Skip to main content

Types

Struct Types 

Source
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

Source

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);
Source

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);
Source

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());
Source

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));
Source

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);
Source

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 });
Source

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));
Source

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 });
Source

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 });
Source

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));
Source

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);
Source

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 { .. }));
Source

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);
Source

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: &[] });
Source

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));
Source

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: &[] });
Source

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] });
Source

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: &[] });
Source

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>");
Source

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));
Source

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);
Source

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));
Source

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));
Source

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 });
Source

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)]");

Trait Implementations§

Source§

impl Clone for Types

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Types

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Types

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Types

Source§

impl PartialEq for Types

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Types

Auto Trait Implementations§

§

impl Freeze for Types

§

impl RefUnwindSafe for Types

§

impl Send for Types

§

impl Sync for Types

§

impl Unpin for Types

§

impl UnsafeUnpin for Types

§

impl UnwindSafe for Types

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.