From d09b3768f601b5ee977c2e80c23ef96e9879f8f1 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Tue, 11 Jun 2019 16:57:07 +0300 Subject: [PATCH] Fix endless loop in type inference for over-applied functions gitlab #303 Change-Id: I5b7c4bc9dafc2cabcc5b152525824d7bc3432345 --- .../compiler/elaboration/expressions/EApply.java | 6 ++++++ .../scl/compiler/tests/ModuleRegressionTests.java | 1 + .../scl/compiler/tests/scl/TypeInferenceBug4.scl | 14 ++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TypeInferenceBug4.scl diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApply.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApply.java index 7f50d730b..06ff05c18 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApply.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApply.java @@ -249,6 +249,12 @@ public class EApply extends Expression { current.setType(mfun.returnType); if(marity < current.parameters.length) { + if (marity == 0) { + // Cannot eat away any more parameters + context.getErrorLog().log(location, "Application of non-function"); + return current; + } + Expression[] missingParameters = Arrays.copyOfRange(current.parameters, marity, current.parameters.length); functionType = mfun.returnType; current.parameters = Arrays.copyOf(current.parameters, marity); 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 6a416687d..f577ad37d 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 @@ -291,6 +291,7 @@ public class ModuleRegressionTests extends TestBase { @Test public void TypeClassBug1() { test(); } @Test(timeout=1000L) public void TypeInferenceBug2() { test(); } @Test public void TypeInferenceBug3() { test(); } + @Test(timeout=100L) public void TypeInferenceBug4() { test(); } @Test public void TypeOf1() { test(); } @Test public void TypingBug1() { test(); } @Test public void TypingError1() { test(); } diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TypeInferenceBug4.scl b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TypeInferenceBug4.scl new file mode 100644 index 000000000..9331fa30d --- /dev/null +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/scl/TypeInferenceBug4.scl @@ -0,0 +1,14 @@ +import "Prelude" + +// See gitlab #303 + +test :: () -> Integer +test _ = let + f = id :: Integer -> Integer + in min f 1 f 2 + +main = test () +-- +8:8-8:11: Constraint Integer)> is not given and cannot be derived. +8:8-8:19: Application of non-function +8:16-8:17: Expected got Integer>. -- 2.43.2