From e15473d2c939bca28bd1f567945b6a2794c63278 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Tue, 27 Jul 2021 14:39:53 +0800 Subject: [PATCH] fixed pattern matching --- nac3core/src/typecheck/function_check.rs | 18 +++++++++++------- nac3core/src/typecheck/type_inferencer/test.rs | 7 +++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/nac3core/src/typecheck/function_check.rs b/nac3core/src/typecheck/function_check.rs index e0db2afd..29868941 100644 --- a/nac3core/src/typecheck/function_check.rs +++ b/nac3core/src/typecheck/function_check.rs @@ -8,19 +8,23 @@ impl<'a> Inferencer<'a> { &mut self, pattern: &Expr>, defined_identifiers: &mut Vec, - ) { + ) -> Result<(), String> { match &pattern.node { ExprKind::Name { id, .. } => { if !defined_identifiers.contains(id) { defined_identifiers.push(id.clone()); } + Ok(()) } ExprKind::Tuple { elts, .. } => { for elt in elts.iter() { - self.check_pattern(elt, defined_identifiers); + self.check_pattern(elt, defined_identifiers)?; } + Ok(()) + } + _ => { + self.check_expr(pattern, defined_identifiers) } - _ => unimplemented!(), } } @@ -106,7 +110,7 @@ impl<'a> Inferencer<'a> { } = &generators[0]; self.check_expr(iter, defined_identifiers)?; let mut defined_identifiers = defined_identifiers.to_vec(); - self.check_pattern(target, &mut defined_identifiers); + self.check_pattern(target, &mut defined_identifiers)?; for term in once(elt.as_ref()).chain(ifs.iter()) { self.check_expr(term, &defined_identifiers)?; } @@ -151,7 +155,7 @@ impl<'a> Inferencer<'a> { self.check_stmt(stmt, defined_identifiers)?; } let mut defined_identifiers = defined_identifiers.clone(); - self.check_pattern(target, &mut defined_identifiers); + self.check_pattern(target, &mut defined_identifiers)?; for stmt in body.iter() { self.check_stmt(stmt, &mut defined_identifiers)?; } @@ -185,14 +189,14 @@ impl<'a> Inferencer<'a> { StmtKind::Assign { targets, value, .. } => { self.check_expr(value, defined_identifiers)?; for target in targets { - self.check_pattern(target, defined_identifiers); + self.check_pattern(target, defined_identifiers)?; } Ok(false) } StmtKind::AnnAssign { target, value, .. } => { if let Some(value) = value { self.check_expr(value, defined_identifiers)?; - self.check_pattern(target, defined_identifiers); + self.check_pattern(target, defined_identifiers)?; } Ok(false) } diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index 598aedf9..bae64f28 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -222,6 +222,13 @@ impl TestEnvironment { [("a", "virtual[Bar]"), ("b", "int32")].iter().cloned().collect(), &[("Bar", "Bar"), ("Bar2", "Bar")] ; "virtual test")] +#[test_case(indoc! {" + a = [virtual(Bar(), Bar), virtual(Bar2())] + b = [x.b() for x in a] + "}, + [("a", "list[virtual[Bar]]"), ("b", "list[int32]")].iter().cloned().collect(), + &[("Bar", "Bar"), ("Bar2", "Bar")] + ; "virtual list test")] fn test_basic(source: &str, mapping: HashMap<&str, &str>, virtuals: &[(&str, &str)]) { println!("source:\n{}", source); let mut env = TestEnvironment::new();