Report error on local variables having no initializer and no type annotation
This commit is contained in:
@@ -99,6 +99,7 @@ public interface Errors {
|
||||
|
||||
SimpleDiagnosticFactory<JetExpression> MANY_FUNCTION_LITERAL_ARGUMENTS = SimpleDiagnosticFactory.create(ERROR);
|
||||
SimpleDiagnosticFactory<JetProperty> PROPERTY_WITH_NO_TYPE_NO_INITIALIZER = SimpleDiagnosticFactory.create(ERROR, NAMED_ELEMENT);
|
||||
SimpleDiagnosticFactory<JetVariableDeclaration> VARIABLE_WITH_NO_TYPE_NO_INITIALIZER = SimpleDiagnosticFactory.create(ERROR, NAME_IDENTIFIER);
|
||||
SimpleDiagnosticFactory<JetMultiDeclaration> INITIALIZER_REQUIRED_FOR_MULTIDECLARATION = SimpleDiagnosticFactory.create(ERROR, DEFAULT);
|
||||
DiagnosticFactory1<JetExpression, Name> COMPONENT_FUNCTION_MISSING = DiagnosticFactory1.create(ERROR, DEFAULT);
|
||||
DiagnosticFactory2<JetExpression, Name, Collection<? extends ResolvedCall<? extends CallableDescriptor>>> COMPONENT_FUNCTION_AMBIGUITY = DiagnosticFactory2.create(ERROR, DEFAULT);
|
||||
|
||||
+1
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun test() {
|
||||
val <!VARIABLE_WITH_NO_TYPE_NO_INITIALIZER, UNUSED_VARIABLE!>a<!>
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user