Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jul 26, 2024
1 parent cf97d88 commit 135f459
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/rue-typing/src/check/check_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn check_union_against_rhs(
return Ok(Check::True);
}

if let Type::Union(union) = types.get(rhs) {
if let Type::Union(union) = types.get_recursive(rhs) {
let rhs_items = union.clone();
let mut result = Vec::new();
for rhs_item in rhs_items {
Expand Down
33 changes: 13 additions & 20 deletions crates/rue-typing/src/substitute_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ use crate::{Alias, Callable, Enum, Struct, Type, TypeId, TypeSystem, Variant};

pub(crate) fn substitute_type(
types: &mut TypeSystem,
mut type_id: TypeId,
substitutions: &mut HashMap<TypeId, TypeId>,
type_id: TypeId,
substitutions: &mut Vec<HashMap<TypeId, TypeId>>,
) -> TypeId {
let mut substituted = false;

while let Some(new_type_id) = substitutions.get(&type_id) {
type_id = *new_type_id;
substituted = true;
}

if substituted {
return type_id;
for frame in substitutions.iter().rev() {
if let Some(new_type_id) = frame.get(&type_id) {
return *new_type_id;
}
}

let placeholder = types.alloc(Type::Unknown);
substitutions.insert(type_id, placeholder);
substitutions
.last_mut()
.unwrap()
.insert(type_id, placeholder);

let result = match types.get(type_id) {
Type::Ref(..) => unreachable!(),
Expand Down Expand Up @@ -62,15 +60,10 @@ pub(crate) fn substitute_type(
}
}
Type::Lazy(lazy) => {
let mut old_substitutions = HashMap::new();
for (key, val) in lazy.substitutions.clone() {
if let Some(old) = substitutions.insert(key, val) {
old_substitutions.insert(key, old);
}
}
substitutions.push(lazy.substitutions.clone().into_iter().collect());
let result = substitute_type(types, lazy.type_id, substitutions);
substitutions.extend(old_substitutions);
result
substitutions.pop().unwrap();
substitute_type(types, result, substitutions)
}
Type::Alias(alias) => {
let alias = alias.clone();
Expand Down
14 changes: 8 additions & 6 deletions crates/rue-typing/src/test_tools.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use indexmap::IndexMap;
use indexmap::{indexmap, IndexMap};

use crate::{Callable, Rest, Struct, Type, TypeId, TypeSystem};
use crate::{Callable, Lazy, Rest, Struct, Type, TypeId, TypeSystem};

pub fn alloc_list(db: &mut TypeSystem, item_type_id: TypeId) -> TypeId {
let list = db.alloc(Type::Unknown);
let pair = db.alloc(Type::Pair(item_type_id, list));
*db.get_mut(list) = Type::Union(vec![pair, db.std().nil]);
list
db.alloc(Type::Lazy(Lazy {
type_id: db.std().unmapped_list,
substitutions: indexmap! {
db.std().generic_list_item => item_type_id,
},
}))
}

pub fn alloc_callable(
Expand Down
4 changes: 2 additions & 2 deletions crates/rue-typing/src/type_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ impl TypeSystem {
pub fn substitute(
&mut self,
type_id: TypeId,
mut substitutions: HashMap<TypeId, TypeId>,
substitutions: HashMap<TypeId, TypeId>,
) -> TypeId {
substitute_type(self, type_id, &mut substitutions)
substitute_type(self, type_id, &mut vec![substitutions])
}

pub fn check(&mut self, lhs: TypeId, rhs: TypeId) -> Result<Check, CheckError> {
Expand Down

0 comments on commit 135f459

Please sign in to comment.