diff --git a/reid/src/mir/scopehints.rs b/reid/src/mir/scopehints.rs index b718d87..210bd72 100644 --- a/reid/src/mir/scopehints.rs +++ b/reid/src/mir/scopehints.rs @@ -1,8 +1,6 @@ use std::{ - any::TypeId, cell::RefCell, collections::{HashMap, HashSet}, - hint::black_box, rc::Rc, }; @@ -12,14 +10,14 @@ use super::{ }; #[derive(Clone)] -pub struct ScopeHint<'scope>(TypeIdRef, &'scope ScopeHints<'scope>); +pub struct TypeHint<'scope>(TypeIdRef, &'scope ScopeHints<'scope>); -impl<'scope> ScopeHint<'scope> { +impl<'scope> TypeHint<'scope> { pub unsafe fn resolve_type(&self) -> TypeKind { unsafe { *self.1.types.hints.borrow().get_unchecked(*self.0.borrow()) } } - pub fn narrow(&mut self, other: &ScopeHint) -> Result, ErrorKind> { + pub fn narrow(&mut self, other: &TypeHint) -> Result, ErrorKind> { self.1.combine_vars(self, other) } @@ -28,7 +26,7 @@ impl<'scope> ScopeHint<'scope> { } } -impl<'scope> std::fmt::Debug for ScopeHint<'scope> { +impl<'scope> std::fmt::Debug for TypeHint<'scope> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_tuple("Hint") .field(&self.0) @@ -117,7 +115,7 @@ impl<'outer> ScopeHints<'outer> { name: String, mutable: bool, initial_ty: TypeKind, - ) -> Result, ErrorKind> { + ) -> Result, ErrorKind> { if self.variables.borrow().contains_key(&name) { return Err(ErrorKind::VariableAlreadyDefined(name)); } @@ -125,10 +123,10 @@ impl<'outer> ScopeHints<'outer> { self.variables .borrow_mut() .insert(name, (mutable, idx.clone())); - Ok(ScopeHint(idx, self)) + Ok(TypeHint(idx, self)) } - pub fn from_type(&'outer self, ty: &TypeKind) -> Option> { + pub fn from_type(&'outer self, ty: &TypeKind) -> Option> { let idx = match ty { TypeKind::Vague(super::VagueType::Hinted(idx)) => { let inner_idx = unsafe { *self.types.recurse_type_ref(*idx).borrow() }; @@ -143,27 +141,27 @@ impl<'outer> ScopeHints<'outer> { } } }; - Some(ScopeHint(idx, self)) + Some(TypeHint(idx, self)) } fn narrow_to_type( &'outer self, - hint: &ScopeHint, + hint: &TypeHint, ty: &TypeKind, - ) -> Result, ErrorKind> { + ) -> Result, ErrorKind> { unsafe { let mut hints = self.types.hints.borrow_mut(); let existing = hints.get_unchecked_mut(*hint.0.borrow()); *existing = existing.collapse_into(&ty)?; - Ok(ScopeHint(hint.0.clone(), self)) + Ok(TypeHint(hint.0.clone(), self)) } } fn combine_vars( &'outer self, - hint1: &ScopeHint, - hint2: &ScopeHint, - ) -> Result, ErrorKind> { + hint1: &TypeHint, + hint2: &TypeHint, + ) -> Result, ErrorKind> { unsafe { let ty = self .types @@ -177,7 +175,7 @@ impl<'outer> ScopeHints<'outer> { *idx.borrow_mut() = *hint1.0.borrow(); } } - Ok(ScopeHint(hint1.0.clone(), self)) + Ok(TypeHint(hint1.0.clone(), self)) } } @@ -189,20 +187,20 @@ impl<'outer> ScopeHints<'outer> { } } - pub fn find_hint(&'outer self, name: &String) -> Option<(bool, ScopeHint<'outer>)> { + pub fn find_hint(&'outer self, name: &String) -> Option<(bool, TypeHint<'outer>)> { self.variables .borrow() .get(name) - .map(|(mutable, idx)| (*mutable, ScopeHint(idx.clone(), self))) + .map(|(mutable, idx)| (*mutable, TypeHint(idx.clone(), self))) .or(self.outer.map(|o| o.find_hint(name)).flatten()) } pub fn binop( &'outer self, op: &BinaryOperator, - lhs: &mut ScopeHint<'outer>, - rhs: &mut ScopeHint<'outer>, - ) -> Result, ErrorKind> { + lhs: &mut TypeHint<'outer>, + rhs: &mut TypeHint<'outer>, + ) -> Result, ErrorKind> { let ty = lhs.narrow(rhs)?; Ok(match op { BinaryOperator::Add => ty, diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index c609232..766d372 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -8,7 +8,7 @@ use VagueType::*; use super::{ pass::{Pass, PassState, ScopeFunction, ScopeVariable}, - scopehints::{ScopeHint, ScopeHints, TypeHints}, + scopehints::{ScopeHints, TypeHint, TypeHints}, types::{pick_return, ReturnType}, }; @@ -105,7 +105,7 @@ impl Block { &mut self, state: &mut PassState, outer_hints: &'s ScopeHints, - ) -> Result<(ReturnKind, ScopeHint<'s>), ErrorKind> { + ) -> Result<(ReturnKind, TypeHint<'s>), ErrorKind> { let mut state = state.inner(); let inner_hints = outer_hints.inner(); @@ -313,7 +313,7 @@ impl Expression { &mut self, state: &mut PassState, hints: &'s ScopeHints<'s>, - ) -> Result, ErrorKind> { + ) -> Result, ErrorKind> { match &mut self.0 { ExprKind::Variable(var) => { let hint = hints