From 306c27a9bb6a0a848f3609b3588292048369be66 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Fri, 17 Aug 2012 20:54:07 +0400 Subject: [PATCH] Report error on local variables having no initializer and no type annotation --- .../src/org/jetbrains/jet/lang/diagnostics/Errors.java | 1 + .../lang/diagnostics/rendering/DefaultErrorMessages.java | 1 + .../org/jetbrains/jet/lang/resolve/DescriptorResolver.java | 7 +++++-- .../LocalVariableWithNoTypeInformation.kt | 3 +++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 5 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/declarationChecks/LocalVariableWithNoTypeInformation.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index fcbe9c2506e..2492d55659d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -99,6 +99,7 @@ public interface Errors { SimpleDiagnosticFactory MANY_FUNCTION_LITERAL_ARGUMENTS = SimpleDiagnosticFactory.create(ERROR); SimpleDiagnosticFactory PROPERTY_WITH_NO_TYPE_NO_INITIALIZER = SimpleDiagnosticFactory.create(ERROR, NAMED_ELEMENT); + SimpleDiagnosticFactory VARIABLE_WITH_NO_TYPE_NO_INITIALIZER = SimpleDiagnosticFactory.create(ERROR, NAME_IDENTIFIER); SimpleDiagnosticFactory INITIALIZER_REQUIRED_FOR_MULTIDECLARATION = SimpleDiagnosticFactory.create(ERROR, DEFAULT); DiagnosticFactory1 COMPONENT_FUNCTION_MISSING = DiagnosticFactory1.create(ERROR, DEFAULT); DiagnosticFactory2>> COMPONENT_FUNCTION_AMBIGUITY = DiagnosticFactory2.create(ERROR, DEFAULT); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java index 939b3ae9c38..0342399931e 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java @@ -112,6 +112,7 @@ public class DefaultErrorMessages { MAP.put(MANY_FUNCTION_LITERAL_ARGUMENTS, "Only one function literal is allowed outside a parenthesized argument list"); MAP.put(PROPERTY_WITH_NO_TYPE_NO_INITIALIZER, "This property must either have a type annotation or be initialized"); + MAP.put(VARIABLE_WITH_NO_TYPE_NO_INITIALIZER, "This variable must either have a type annotation or be initialized"); MAP.put(INITIALIZER_REQUIRED_FOR_MULTIDECLARATION, "Initializer required for multi-declaration"); MAP.put(COMPONENT_FUNCTION_MISSING, "Multi-declaration initializer must have a ''{0}()'' function", TO_STRING); MAP.put(COMPONENT_FUNCTION_AMBIGUITY, "Method ''{0}'' is ambiguous for this expression: {1}", TO_STRING, AMBIGUOUS_CALLS); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java index d1762a02762..441d4d1c395 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java @@ -819,7 +819,7 @@ public class DescriptorResolver { @NotNull final JetScope scope, @NotNull final JetVariableDeclaration variable, @NotNull final DataFlowInfo dataFlowInfo, - boolean allowDeferred, + boolean notLocal, final BindingTrace trace ) { JetTypeReference propertyTypeRef = variable.getTypeRef(); @@ -827,6 +827,9 @@ public class DescriptorResolver { if (propertyTypeRef == null) { final JetExpression initializer = variable.getInitializer(); if (initializer == null) { + if (!notLocal) { + trace.report(VARIABLE_WITH_NO_TYPE_NO_INITIALIZER.on(variable)); + } return ErrorUtils.createErrorType("No type, no body"); } else { @@ -836,7 +839,7 @@ public class DescriptorResolver { return expressionTypingServices.safeGetType(scope, initializer, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo, trace); } }; - if (allowDeferred) { + if (notLocal) { return DeferredType.create(trace, lazyValue); } else { diff --git a/compiler/testData/diagnostics/tests/declarationChecks/LocalVariableWithNoTypeInformation.kt b/compiler/testData/diagnostics/tests/declarationChecks/LocalVariableWithNoTypeInformation.kt new file mode 100644 index 00000000000..592a462d586 --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/LocalVariableWithNoTypeInformation.kt @@ -0,0 +1,3 @@ +fun test() { + val a +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 21f7be5884b..ec5b8be83ea 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -1088,6 +1088,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/declarationChecks/kt559.kt"); } + @TestMetadata("LocalVariableWithNoTypeInformation.kt") + public void testLocalVariableWithNoTypeInformation() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/LocalVariableWithNoTypeInformation.kt"); + } + @TestMetadata("MultiDeclarationErrors.kt") public void testMultiDeclarationErrors() throws Exception { doTest("compiler/testData/diagnostics/tests/declarationChecks/MultiDeclarationErrors.kt");