From e28caa5b47179aa0a7e600a1fcecf864175d2cbe Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Fri, 24 Nov 2017 13:51:11 +0200 Subject: [PATCH] Fixed a bug related to as-pattern in list comprehension. refs #7642 Change-Id: I7f9b3b45d545e2fe793fcaa93b9d392972f0e53b --- .../elaboration/expressions/EListComprehension.java | 10 ++++++++-- .../visitors/CollectFreeVariablesVisitor.java | 7 +++++++ .../scl/compiler/tests/ModuleRegressionTests.java | 1 + .../scl/compiler/tests/scl/ListComprehensionBug1.scl | 8 ++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/ListComprehensionBug1.scl diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EListComprehension.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EListComprehension.java index 78f7ce115..10045b34f 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EListComprehension.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EListComprehension.java @@ -48,13 +48,19 @@ public class EListComprehension extends SimplifiableExpression { @Override public Expression simplify(SimplificationContext context) { + //System.out.println("--------"); context.pushLocation(location); try { - CompiledQualifier cq = qualifier.compile(context); + CompiledQualifier cq = qualifier.compile(context); + /* + System.out.println("cq.pattern = " + cq.pattern); + System.out.println("cq.value = " + cq.value); + System.out.println("head = " + head); + */ Expression exp = context.mapList( context.lambda(cq.pattern, head), cq.value); - //System.out.println("simplified: " + exp); + //System.out.println("simplified = " + exp); /*try { exp.validateType(context.getEnvironment()); } catch (TypeValidationException e) { diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/visitors/CollectFreeVariablesVisitor.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/visitors/CollectFreeVariablesVisitor.java index 04f6791ac..0a380130f 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/visitors/CollectFreeVariablesVisitor.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/visitors/CollectFreeVariablesVisitor.java @@ -178,6 +178,13 @@ public class CollectFreeVariablesVisitor extends StandardExpressionVisitor { freeVariables.remove(variable); } + @Override + public void visit(EAsPattern expression) { + if(expression.var != null) + freeVariables.add(expression.var); + expression.pattern.accept(this); + } + public THashSet getFreeVariables() { return freeVariables; } diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java index 00af273c7..9e5d79216 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/ModuleRegressionTests.java @@ -136,6 +136,7 @@ public class ModuleRegressionTests extends TestBase { @Test public void LambdaMatch() { test(); } @Test public void Layout1() { test(); } @Test public void List() { test(); } + @Test public void ListComprehensionBug1() { test(); } @Test public void ListError1() { test(); } @Test public void ListError2() { test(); } @Test public void ListFunctions() { test(); } diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/ListComprehensionBug1.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/ListComprehensionBug1.scl new file mode 100644 index 000000000..86e9774c6 --- /dev/null +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/ListComprehensionBug1.scl @@ -0,0 +1,8 @@ +import "Prelude" + +f :: [(String,String)] -> [(String,String,String)] +f l = [(show m,x,y) | m@(x,y) <-l ] + +main = f [] +-- +[] -- 2.47.1