From 4f8169012886a293ddbc72d480f2cee79bbf383f Mon Sep 17 00:00:00 2001 From: pca006132 Date: Wed, 21 Jul 2021 16:10:11 +0800 Subject: [PATCH] modified occur check --- nac3core/src/typecheck/type_inferencer/test.rs | 2 +- nac3core/src/typecheck/typedef/mod.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index af9376232..3e85c2459 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -162,7 +162,7 @@ impl TestEnvironment { #[test_case(indoc! {" f = lambda x: True a = [1, 2, 3] - b = [f(x) for x in a] + b = [f(x) for x in a if f(x)] "}, [("a", "list[int32]"), ("b", "list[bool]"), ("f", "fn[[x=int32], bool]")].iter().cloned().collect() ; "listcomp test")] diff --git a/nac3core/src/typecheck/typedef/mod.rs b/nac3core/src/typecheck/typedef/mod.rs index 4d00c11eb..680bcc673 100644 --- a/nac3core/src/typecheck/typedef/mod.rs +++ b/nac3core/src/typecheck/typedef/mod.rs @@ -259,12 +259,13 @@ impl Unifier { let (ty_a, ty_b) = { (ty_a_cell.borrow(), ty_b_cell.borrow()) }; - self.occur_check(a, b)?; match (&*ty_a, &*ty_b) { (TypeEnum::TVar { .. }, _) => { + self.occur_check(a, b)?; self.set_a_to_b(a, b); } (TSeq { map: map1 }, TSeq { .. }) => { + self.occur_check(a, b)?; drop(ty_b); if let TypeEnum::TSeq { map: map2 } = &mut *ty_b_cell.as_ref().borrow_mut() { // unify them to map2 @@ -281,6 +282,7 @@ impl Unifier { self.set_a_to_b(a, b); } (TSeq { map: map1 }, TTuple { ty: types }) => { + self.occur_check(a, b)?; let len = types.len() as i32; for (k, v) in map1.iter() { // handle negative index @@ -297,6 +299,7 @@ impl Unifier { self.set_a_to_b(a, b); } (TSeq { map: map1 }, TList { ty }) => { + self.occur_check(a, b)?; for v in map1.values() { self.unify(*v, *ty)?; } @@ -320,6 +323,7 @@ impl Unifier { self.set_a_to_b(a, b); } (TRecord { fields: fields1 }, TRecord { .. }) => { + self.occur_check(a, b)?; drop(ty_b); if let TypeEnum::TRecord { fields: fields2 } = &mut *ty_b_cell.as_ref().borrow_mut() { @@ -341,6 +345,7 @@ impl Unifier { fields: fields2, .. }, ) => { + self.occur_check(a, b)?; for (key, value) in fields1.iter() { if let Some(ty) = fields2.get(key) { self.unify(*ty, *value)?; @@ -351,6 +356,7 @@ impl Unifier { self.set_a_to_b(a, b); } (TRecord { .. }, TVirtual { ty }) => { + self.occur_check(a, b)?; self.unify(a, *ty)?; } ( @@ -387,6 +393,7 @@ impl Unifier { self.set_a_to_b(a, b); } (TCall { calls }, TFunc(signature)) => { + self.occur_check(a, b)?; let required: Vec = signature .args .iter()