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>
impl<'h> Checker<'h>
Sourcepub fn new(hir: &'h Hir, profile: Profile) -> Self
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)));Sourcepub fn set_budget(&mut self, budget: Budget) -> &mut Self
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 }));Sourcepub fn types_mut(&mut self) -> &mut Types
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)));Sourcepub fn declare_extern(&mut self, name: Symbol, ty: TypeId) -> &mut Self
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)));Sourcepub fn declare_foreign(&mut self, def: DefId, ty: TypeId) -> &mut Self
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());Sourcepub fn declare_intrinsic(&mut self, name: Symbol, ty: TypeId) -> &mut Self
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());Sourcepub fn declare_member(
&mut self,
owner: TypeId,
name: Symbol,
ty: TypeId,
) -> &mut Self
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());Sourcepub fn declare_mono_attribute(
&mut self,
name: Symbol,
strategy: Strategy,
) -> &mut Self
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());Sourcepub fn check(self) -> Result<Typed, CheckError>
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)));