pub struct TypeChecker {Show 14 fields
pub scopes: Vec<HashMap<String, ScopeItem>>,
pub source_file: Option<SourceFile>,
pub root_module: Module,
pub errors: Vec<Error>,
pub return_type_stack: Vec<TypeAnnotation>,
pub loop_depth: u32,
pub stdlib_fn_names: HashSet<String>,
pub hovers: Vec<(Span, String)>,
pub base_dir: Option<PathBuf>,
pub importing: Vec<PathBuf>,
pub imported: HashSet<PathBuf>,
pub ast_arena: Ast,
pub records: HashMap<String, Vec<(String, TypeAnnotation)>>,
pub tags: HashMap<String, Vec<String>>,
}Expand description
The stateful type checker, threaded through the entire AST walk.
Fields§
§scopes: Vec<HashMap<String, ScopeItem>>Stack of scopes, each mapping variable/function names to their ScopeItem.
source_file: Option<SourceFile>Source file attached for Ariadne error rendering; None in LSP-less contexts.
root_module: ModuleThe stdlib module tree, used to resolve stdlib call paths.
errors: Vec<Error>All type errors accumulated during the check pass.
return_type_stack: Vec<TypeAnnotation>Stack of expected return types, pushed/popped on function and lambda entry/exit.
loop_depth: u32Nesting depth of loops - used to validate break and continue.
stdlib_fn_names: HashSet<String>Flat set of all stdlib function names for fast single-name lookup.
hovers: Vec<(Span, String)>(span, markdown) pairs collected at every declaration and usage site,
consumed by the LSP hover provider.
base_dir: Option<PathBuf>§importing: Vec<PathBuf>§imported: HashSet<PathBuf>§ast_arena: Ast§records: HashMap<String, Vec<(String, TypeAnnotation)>>Maps record type names to their declared (field name, field type) list.
Maps tag (enum) type names to their declared variant name list.
Implementations§
Source§impl TypeChecker
impl TypeChecker
Source§impl TypeChecker
impl TypeChecker
Source§impl TypeChecker
impl TypeChecker
Source§impl TypeChecker
impl TypeChecker
Sourcepub fn assign(&mut self, name: &str, value_type: CheckType, span: Span)
pub fn assign(&mut self, name: &str, value_type: CheckType, span: Span)
Checks that assigning value_type to name is valid.
Walks scopes from innermost to outermost. Emits an error if:
- The variable is declared
const - The value type doesn’t match the declared type
- The name is not declared in any scope (with a “did you mean?” suggestion)
Source§impl TypeChecker
impl TypeChecker
Sourcepub fn check_call_path(
&mut self,
path: &[String],
arg_types: &[(CheckType, Span)],
span: Span,
) -> CheckType
pub fn check_call_path( &mut self, path: &[String], arg_types: &[(CheckType, Span)], span: Span, ) -> CheckType
Resolves a call path and checks argument types.
Resolution order:
- Full stdlib path (
std::io::print) via [root_module] - Single-name stdlib shorthand (
print) via [stdlib_fn_names] - User-defined name via [
lookup] - Error - unknown function, with a “did you mean?” suggestion from stdlib keywords
Stdlib calls always return CheckType::Unknown since their return
types are not tracked statically.
Sourcepub fn check_call_value(
&mut self,
callee_type: CheckType,
arg_types: &[(CheckType, Span)],
span: Span,
) -> CheckType
pub fn check_call_value( &mut self, callee_type: CheckType, arg_types: &[(CheckType, Span)], span: Span, ) -> CheckType
Checks that a resolved callee value is callable and receives the correct arguments.
UnknownandKnown(Fn)pass through without argument checkingFunction { params, return_type }validates arity and argument types, then returnsKnown(return_type)- Anything else emits a “not callable” error
Source§impl TypeChecker
impl TypeChecker
Source§impl TypeChecker
impl TypeChecker
Sourcepub fn push_scope(&mut self)
pub fn push_scope(&mut self)
Pushes a new empty scope onto the scope stack.
Sourcepub fn lookup(&mut self, name: &str, span: Span) -> CheckType
pub fn lookup(&mut self, name: &str, span: Span) -> CheckType
Looks up name in all scopes from innermost to outermost.
On success, pushes a hover entry with the variable’s type and kind,
then returns the CheckType. On failure, emits an undefined variable
error with a “did you mean?” suggestion and returns CheckType::Unknown.
Source§impl TypeChecker
impl TypeChecker
pub fn check_expression(&mut self, expression: ExprId) -> CheckType
Source§impl TypeChecker
impl TypeChecker
pub fn check_statement(&mut self, statement: &Statement)
fn import_module(&mut self, path: &[String], span: Span)
Source§impl TypeChecker
impl TypeChecker
Sourcepub fn current_return_type(&self) -> Option<&TypeAnnotation>
pub fn current_return_type(&self) -> Option<&TypeAnnotation>
Returns the expected return type of the current function or lambda, if any.
Sourcepub fn push_return_type(&mut self, ty: TypeAnnotation)
pub fn push_return_type(&mut self, ty: TypeAnnotation)
Pushes ty as the expected return type when entering a function or lambda body.
Sourcepub fn pop_return_type(&mut self)
pub fn pop_return_type(&mut self)
Pops the expected return type when exiting a function or lambda body.
pub fn loop_depth(&self) -> u32
pub fn enter_loop(&mut self)
pub fn exit_loop(&mut self)
Sourcepub fn check_block(&mut self, statements: &[Statement])
pub fn check_block(&mut self, statements: &[Statement])
Checks all statements in statements inside a fresh scope.
Sourcepub fn check_is_null(&mut self, item_type: &CheckType, span: Span) -> bool
pub fn check_is_null(&mut self, item_type: &CheckType, span: Span) -> bool
Emits a "value is null" error if item_type is [CheckType::Known(Null)].
Returns false if null, true otherwise.
Sourcepub(crate) fn to_type_annotation(item_type: &CheckType) -> TypeAnnotation
pub(crate) fn to_type_annotation(item_type: &CheckType) -> TypeAnnotation
Converts a CheckType to a TypeAnnotation, mapping Unknown to Null.
pub(crate) fn is_hashable_key_type(ty: &TypeAnnotation) -> bool
Source§impl TypeChecker
impl TypeChecker
pub fn new() -> Self
pub fn with_source_file(self, file: SourceFile) -> Self
pub fn set_source_file(&mut self, file: SourceFile)
pub fn with_base_dir(self, dir: impl Into<PathBuf>) -> Self
pub fn with_ast_arena(self, arena: Ast) -> Self
pub fn check(&mut self, statements: &[Statement]) -> &[Error]
pub fn err(&self, message: impl Into<String>, span: Span) -> Error
pub fn error(&mut self, message: impl Into<String>, span: Span)
pub fn error_with_help( &mut self, message: impl Into<String>, span: Span, help: Option<&str>, )
pub fn push_hover(&mut self, span: Span, text: impl Into<String>)
fn push_stdlib_hover(&mut self, path: &[String], span: Span)
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TypeChecker
impl !RefUnwindSafe for TypeChecker
impl !Send for TypeChecker
impl !Sync for TypeChecker
impl Unpin for TypeChecker
impl UnsafeUnpin for TypeChecker
impl !UnwindSafe for TypeChecker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);