From b0553ff651611e17dc0905498357a8056825a3fd Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Sat, 9 Jun 2012 23:25:41 +0400 Subject: [PATCH] properly handle empty lines in REPL --- .../types/expressions/ExpressionTypingServices.java | 12 ++++++++---- compiler/testData/codegen/script/empty.ktscript | 2 ++ compiler/testData/repl/empty.repl | 8 ++++++++ .../org/jetbrains/jet/codegen/ScriptGenTest.java | 4 ++++ .../org/jetbrains/jet/repl/ReplInterpreterTest.java | 5 +++++ .../org/jetbrains/jet/repl/ReplSessionTestFile.java | 8 ++++++-- 6 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/codegen/script/empty.ktscript create mode 100644 compiler/testData/repl/empty.repl diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java index 2c1c53a319f..da74017946c 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/ExpressionTypingServices.java @@ -177,9 +177,6 @@ public class ExpressionTypingServices { @Nullable public JetType getBlockReturnedType(@NotNull JetScope outerScope, @NotNull JetBlockExpression expression, @NotNull CoercionStrategy coercionStrategyForLastExpression, ExpressionTypingContext context, BindingTrace trace) { List block = expression.getStatements(); - if (block.isEmpty()) { - return DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, context); - } DeclarationDescriptor containingDescriptor = outerScope.getContainingDeclaration(); if (containingDescriptor instanceof ScriptDescriptor) { @@ -192,7 +189,14 @@ public class ExpressionTypingServices { WritableScope scope = new WritableScopeImpl( outerScope, containingDescriptor, new TraceBasedRedeclarationHandler(context.trace), "getBlockReturnedType"); scope.changeLockLevel(WritableScope.LockLevel.BOTH); - JetType r = getBlockReturnedTypeWithWritableScope(scope, block, coercionStrategyForLastExpression, context, trace); + + JetType r; + if (block.isEmpty()) { + r = DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, context); + } + else { + r = getBlockReturnedTypeWithWritableScope(scope, block, coercionStrategyForLastExpression, context, trace); + } scope.changeLockLevel(WritableScope.LockLevel.READING); if (containingDescriptor instanceof ScriptDescriptor) { diff --git a/compiler/testData/codegen/script/empty.ktscript b/compiler/testData/codegen/script/empty.ktscript new file mode 100644 index 00000000000..1378a17d91c --- /dev/null +++ b/compiler/testData/codegen/script/empty.ktscript @@ -0,0 +1,2 @@ + +// expected: rv: null diff --git a/compiler/testData/repl/empty.repl b/compiler/testData/repl/empty.repl new file mode 100644 index 00000000000..cb1f405f180 --- /dev/null +++ b/compiler/testData/repl/empty.repl @@ -0,0 +1,8 @@ +>>> +null +>>> 101 +101 +>>> +null +>>> 102 +102 diff --git a/compiler/tests/org/jetbrains/jet/codegen/ScriptGenTest.java b/compiler/tests/org/jetbrains/jet/codegen/ScriptGenTest.java index 70a2b8f5954..40aad5cfa94 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ScriptGenTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ScriptGenTest.java @@ -82,4 +82,8 @@ public class ScriptGenTest extends CodegenTestCase { blackBoxFile("script/parameterClosure.ktscript"); } + public void testEmpty() { + blackBoxFile("script/empty.ktscript"); + } + } diff --git a/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTest.java b/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTest.java index 7f602efd14d..f6db5a44202 100644 --- a/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTest.java +++ b/compiler/tests/org/jetbrains/jet/repl/ReplInterpreterTest.java @@ -92,5 +92,10 @@ public class ReplInterpreterTest { testFile("twoClosures.repl"); } + @Test + public void empty() { + testFile("empty.repl"); + } + } diff --git a/compiler/tests/org/jetbrains/jet/repl/ReplSessionTestFile.java b/compiler/tests/org/jetbrains/jet/repl/ReplSessionTestFile.java index 3336e518a94..fa49700a66f 100644 --- a/compiler/tests/org/jetbrains/jet/repl/ReplSessionTestFile.java +++ b/compiler/tests/org/jetbrains/jet/repl/ReplSessionTestFile.java @@ -26,6 +26,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author Stepan Koltsov @@ -66,10 +68,12 @@ public class ReplSessionTestFile { return new ReplSessionTestFile(list); } - if (!odd.startsWith(">>> ")) { + Pattern pattern = Pattern.compile(">>>( |$)(.*)"); + Matcher matcher = pattern.matcher(odd); + if (!matcher.matches()) { throw new IllegalStateException("odd lines must start with >>>"); } - String code = odd.substring(4); + String code = matcher.group(2); String even = reader.readLine(); if (even == null) {