From 7bc63e289213385fe529a68e5bf2c1dd5d6907be Mon Sep 17 00:00:00 2001 From: ychenfo Date: Sun, 12 Dec 2021 05:39:48 +0800 Subject: [PATCH 1/2] nac3core: fix for loop type inference --- nac3core/src/typecheck/type_inferencer/mod.rs | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/nac3core/src/typecheck/type_inferencer/mod.rs b/nac3core/src/typecheck/type_inferencer/mod.rs index 4ab40e6..c18bed8 100644 --- a/nac3core/src/typecheck/type_inferencer/mod.rs +++ b/nac3core/src/typecheck/type_inferencer/mod.rs @@ -122,9 +122,36 @@ impl<'a> fold::Fold<()> for Inferencer<'a> { }, } } - ast::StmtKind::For { ref target, .. } => { - self.infer_pattern(target)?; - fold::fold_stmt(self, node)? + ast::StmtKind::For { target, iter, body, orelse, config_comment, type_comment } => { + self.infer_pattern(&target)?; + let target = self.fold_expr(*target)?; + let iter = self.fold_expr(*iter)?; + if self.unifier.unioned(iter.custom.unwrap(), self.primitives.range) { + self.unify(self.primitives.int32, target.custom.unwrap(), &target.location)?; + } else { + let list = self.unifier.add_ty(TypeEnum::TList { ty: target.custom.unwrap() }); + self.unify(list, iter.custom.unwrap(), &iter.location)?; + } + let body = body + .into_iter() + .map(|b| self.fold_stmt(b)) + .collect::, _>>()?; + let orelse = orelse + .into_iter() + .map(|o| self.fold_stmt(o)) + .collect::, _>>()?; + Located { + location: node.location, + node: ast::StmtKind::For { + target: Box::new(target), + iter: Box::new(iter), + body, + orelse, + config_comment, + type_comment, + }, + custom: None + } } ast::StmtKind::Assign { ref mut targets, ref config_comment, .. } => { for target in targets.iter_mut() { @@ -201,14 +228,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> { _ => fold::fold_stmt(self, node)?, }; match &stmt.node { - ast::StmtKind::For { target, iter, .. } => { - if self.unifier.unioned(iter.custom.unwrap(), self.primitives.range) { - self.unify(self.primitives.int32, target.custom.unwrap(), &target.location)?; - } else { - let list = self.unifier.add_ty(TypeEnum::TList { ty: target.custom.unwrap() }); - self.unify(list, iter.custom.unwrap(), &iter.location)?; - } - } + ast::StmtKind::For { .. } => {} ast::StmtKind::If { test, .. } | ast::StmtKind::While { test, .. } => { self.unify(test.custom.unwrap(), self.primitives.bool, &test.location)?; } -- 2.44.1 From 4a63ba42a2caa5436c78a0e18c8d65029e309386 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Sun, 12 Dec 2021 05:40:22 +0800 Subject: [PATCH 2/2] nac3core: add missing expr concrete type check --- nac3core/src/typecheck/function_check.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nac3core/src/typecheck/function_check.rs b/nac3core/src/typecheck/function_check.rs index 65c810e..b65f499 100644 --- a/nac3core/src/typecheck/function_check.rs +++ b/nac3core/src/typecheck/function_check.rs @@ -24,11 +24,13 @@ impl<'a> Inferencer<'a> { if !defined_identifiers.contains(id) { defined_identifiers.insert(*id); } + self.check_expr(pattern, defined_identifiers)?; self.should_have_value(pattern)?; Ok(()) } ExprKind::Tuple { elts, .. } => { for elt in elts.iter() { + self.check_expr(pattern, defined_identifiers)?; self.check_pattern(elt, defined_identifiers)?; self.should_have_value(elt)?; } -- 2.44.1