Skip to main content

Checker

Struct Checker 

Source
pub struct Checker<'h> { /* private fields */ }
Expand description

The configured path: declare host types, set a budget, then check.

check is this with no declarations and the default budget scaled to the HIR.

§Examples

use hir_lang::{Builder, Effects, Name, Prim, Res};
use intern_lang::Interner;
use typeck_lang::{Checker, Profile, TypeId};

let mut names = Interner::new();
let print = names.intern("print");
let mut b = Builder::new();
// fn main() { print("hi") }   with `print` bound by the host.
let path = b.resolved_path(Name::new(print), hir_lang::Ns::Value, Res::Extern(print));
let callee = b.expr(hir_lang::Expr::Path(path));
let hi = b.str_lit("hi");
let call = b.call(callee, &[hi]);
let s = b.expr_stmt(call);
let body = b.block(&[s], None);
let main = b.func(Name::new(names.intern("main")), &[], body);
let root = b.module(None, &[main]);
let hir = b.finish(root)?;

let mut checker = Checker::new(&hir, Profile::static_explicit());
let print_ty = checker.types_mut().func(&[TypeId::prim(Prim::Str)], TypeId::UNIT, Effects::NONE);
checker.declare_extern(print, print_ty);
let typed = checker.check().unwrap();
assert!(typed.is_ok());
assert_eq!(typed.expr(call), Some(TypeId::UNIT));

Implementations§

Source§

impl<'h> Checker<'h>

Source

pub fn new(hir: &'h Hir, profile: Profile) -> Self

A checker for hir under profile, with Budget::for_hir.

§Examples
use hir_lang::{Builder, Effects, Expr, Name, Ns, Prim, Res};
use intern_lang::Interner;
use typeck_lang::{Checker, Profile, TypeId};

let mut names = Interner::new();
let pi = names.intern("pi");
let mut b = Builder::new();
// fn main() { pi; }   with `pi` bound by the host
let path = b.resolved_path(Name::new(pi), Ns::Value, Res::Extern(pi));
let use_pi = b.expr(Expr::Path(path));
let s = b.expr_stmt(use_pi);
let body = b.block(&[s], None);
let main = b.func(Name::new(names.intern("main")), &[], body);
let root = b.module(None, &[main]);
let hir = b.finish(root)?;

let mut checker = Checker::new(&hir, Profile::static_explicit());
checker.declare_extern(pi, TypeId::prim(Prim::F64));
let typed = checker.check().unwrap();
assert!(typed.is_ok());
assert_eq!(typed.expr(use_pi), Some(TypeId::prim(Prim::F64)));
Source

pub fn set_budget(&mut self, budget: Budget) -> &mut Self

Replaces the budget.

§Examples
use hir_lang::Builder;
use typeck_lang::{Budget, CheckError, Checker, Limit, Profile};

let mut b = Builder::new();
let one = b.int(1);
let body = b.block(&[], Some(one));
let f = b.func(hir_lang::Name::new(intern_lang::Interner::new().intern("f")), &[], body);
let root = b.module(None, &[f]);
let hir = b.finish(root)?;
let mut checker = Checker::new(&hir, Profile::static_explicit());
checker.set_budget(Budget { steps: 2, ..Budget::default() });
assert_eq!(checker.check(), Err(CheckError::BudgetExceeded { limit: Limit::Steps }));
Source

pub fn types_mut(&mut self) -> &mut Types

The type store, to build the types of host declarations.

§Examples
use hir_lang::{Builder, Effects, Expr, Name, Ns, Prim, Res};
use intern_lang::Interner;
use typeck_lang::{Checker, Profile, TypeId};

let mut names = Interner::new();
let pi = names.intern("pi");
let mut b = Builder::new();
// fn main() { pi; }   with `pi` bound by the host
let path = b.resolved_path(Name::new(pi), Ns::Value, Res::Extern(pi));
let use_pi = b.expr(Expr::Path(path));
let s = b.expr_stmt(use_pi);
let body = b.block(&[s], None);
let main = b.func(Name::new(names.intern("main")), &[], body);
let root = b.module(None, &[main]);
let hir = b.finish(root)?;

let mut checker = Checker::new(&hir, Profile::static_explicit());
// A host type built in the checker's own store.
let f64_ty = TypeId::prim(Prim::F64);
let wrapped = checker.types_mut().tuple(&[f64_ty]);
assert_ne!(wrapped, f64_ty);
checker.declare_extern(pi, f64_ty);
let typed = checker.check().unwrap();
assert!(typed.is_ok());
assert_eq!(typed.expr(use_pi), Some(TypeId::prim(Prim::F64)));
Source

pub fn declare_extern(&mut self, name: Symbol, ty: TypeId) -> &mut Self

Declares the value type of a host symbol (Res::Extern(name) in a value path). A later declaration of the same name replaces it.

§Examples
use hir_lang::{Builder, Effects, Expr, Name, Ns, Prim, Res};
use intern_lang::Interner;
use typeck_lang::{Checker, Profile, TypeId};

let mut names = Interner::new();
let pi = names.intern("pi");
let mut b = Builder::new();
// fn main() { pi; }   with `pi` bound by the host
let path = b.resolved_path(Name::new(pi), Ns::Value, Res::Extern(pi));
let use_pi = b.expr(Expr::Path(path));
let s = b.expr_stmt(use_pi);
let body = b.block(&[s], None);
let main = b.func(Name::new(names.intern("main")), &[], body);
let root = b.module(None, &[main]);
let hir = b.finish(root)?;

let mut checker = Checker::new(&hir, Profile::static_explicit());
checker.declare_extern(pi, TypeId::prim(Prim::F64));
let typed = checker.check().unwrap();
assert!(typed.is_ok());
assert_eq!(typed.expr(use_pi), Some(TypeId::prim(Prim::F64)));
Source

pub fn declare_foreign(&mut self, def: DefId, ty: TypeId) -> &mut Self

Declares the value type of another unit’s definition (a value path resolved to a DefId of another unit).

§Examples
use hir_lang::{Builder, Def, DefId, ItemId, UnitId};
use typeck_lang::{Checker, Profile, TypeId};

let mut b = Builder::new();
let root = b.module(None, &[]);
let hir = b.finish(root)?;
let other = DefId::foreign(UnitId::new(7), Def::Item(ItemId::from_index(0).unwrap()));
let mut checker = Checker::new(&hir, Profile::static_explicit());
checker.declare_foreign(other, TypeId::UNIT);
assert!(checker.check().unwrap().is_ok());
Source

pub fn declare_intrinsic(&mut self, name: Symbol, ty: TypeId) -> &mut Self

Declares the function type of a named intrinsic (Intrinsic::Named(name)).

§Examples
use hir_lang::{Builder, Effects, Prim};
use intern_lang::Interner;
use typeck_lang::{Checker, Profile, TypeId};

let mut names = Interner::new();
let mut b = Builder::new();
let root = b.module(None, &[]);
let hir = b.finish(root)?;
let mut checker = Checker::new(&hir, Profile::static_explicit());
let ty = checker.types_mut().func(&[], TypeId::prim(Prim::U64), Effects::NONE);
checker.declare_intrinsic(names.intern("rdtsc"), ty);
assert!(checker.check().is_ok());
Source

pub fn declare_member( &mut self, owner: TypeId, name: Symbol, ty: TypeId, ) -> &mut Self

Declares a method or associated item of a type (typically a host type such as types_mut().external(vec)): owner::name in a path, and value.name(..) in a method call when ty is a function whose first parameter is the receiver.

§Examples
use hir_lang::{Builder, Effects, Prim};
use intern_lang::Interner;
use typeck_lang::{Checker, Profile, TypeId};

let mut names = Interner::new();
let vec = names.intern("Vec");
let mut b = Builder::new();
let root = b.module(None, &[]);
let hir = b.finish(root)?;
let mut checker = Checker::new(&hir, Profile::static_explicit());
let vec_ty = checker.types_mut().external(vec);
let len = checker.types_mut().func(&[vec_ty], TypeId::prim(Prim::Usize), Effects::NONE);
checker.declare_member(vec_ty, names.intern("len"), len);
assert!(checker.check().is_ok());
Source

pub fn declare_mono_attribute( &mut self, name: Symbol, strategy: Strategy, ) -> &mut Self

Makes an attribute named name on a generic item force strategy in the monomorphization plan, whatever the profile’s MonoPolicy says (a language’s @inline or @hot copies; @shared shares). A later declaration of the same name replaces it.

§Examples
use hir_lang::Builder;
use intern_lang::Interner;
use typeck_lang::{Checker, Profile, Strategy};

let mut names = Interner::new();
let mut b = Builder::new();
let root = b.module(None, &[]);
let hir = b.finish(root)?;
let mut checker = Checker::new(&hir, Profile::static_inferred());
checker.declare_mono_attribute(names.intern("hot"), Strategy::Copy);
assert!(checker.check().unwrap().is_ok());
Source

pub fn check(self) -> Result<Typed, CheckError>

Runs the check.

§Errors

CheckError::NotImplemented for a discipline this version does not implement, CheckError::InvalidProfile for inconsistent literal defaults, and CheckError::BudgetExceeded when the budget runs out. Type errors in the program are diagnostics in the Ok result.

§Examples
use hir_lang::{Builder, Effects, Expr, Name, Ns, Prim, Res};
use intern_lang::Interner;
use typeck_lang::{Checker, Profile, TypeId};

let mut names = Interner::new();
let pi = names.intern("pi");
let mut b = Builder::new();
// fn main() { pi; }   with `pi` bound by the host
let path = b.resolved_path(Name::new(pi), Ns::Value, Res::Extern(pi));
let use_pi = b.expr(Expr::Path(path));
let s = b.expr_stmt(use_pi);
let body = b.block(&[s], None);
let main = b.func(Name::new(names.intern("main")), &[], body);
let root = b.module(None, &[main]);
let hir = b.finish(root)?;

let mut checker = Checker::new(&hir, Profile::static_explicit());
checker.declare_extern(pi, TypeId::prim(Prim::F64));
let typed = checker.check().unwrap();
assert!(typed.is_ok());
assert_eq!(typed.expr(use_pi), Some(TypeId::prim(Prim::F64)));

Trait Implementations§

Source§

impl<'h> Debug for Checker<'h>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'h> Freeze for Checker<'h>

§

impl<'h> RefUnwindSafe for Checker<'h>

§

impl<'h> Send for Checker<'h>

§

impl<'h> Sync for Checker<'h>

§

impl<'h> Unpin for Checker<'h>

§

impl<'h> UnsafeUnpin for Checker<'h>

§

impl<'h> UnwindSafe for Checker<'h>

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> 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, 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.