KT-3341 Prohibit val/var keywords for catch parameters

#KT-3341
This commit is contained in:
Evgeny Gerashchenko
2013-02-12 01:14:19 +04:00
parent b1cb8edd18
commit 18d6e86e60
6 changed files with 26 additions and 1 deletions
@@ -475,6 +475,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, JetKeywordToken> VAL_OR_VAR_ON_LOOP_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, JetKeywordToken> VAL_OR_VAR_ON_FUN_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, JetKeywordToken> VAL_OR_VAR_ON_CATCH_PARAMETER = DiagnosticFactory1.create(ERROR);
// Backing fields
@@ -181,6 +181,7 @@ public class DefaultErrorMessages {
MAP.put(VAL_OR_VAR_ON_LOOP_PARAMETER, "''{0}'' on loop parameter is not allowed", TO_STRING);
MAP.put(VAL_OR_VAR_ON_FUN_PARAMETER, "''{0}'' on function parameter is not allowed", TO_STRING);
MAP.put(VAL_OR_VAR_ON_CATCH_PARAMETER, "''{0}'' on catch parameter is not allowed", TO_STRING);
MAP.put(INITIALIZATION_USING_BACKING_FIELD_CUSTOM_SETTER,
"This property has a custom setter, so initialization using backing field required", NAME);
@@ -712,7 +712,7 @@ public class DescriptorResolver {
annotationResolver.getResolvedAnnotations(parameter.getModifierList(), trace),
JetPsiUtil.safeName(parameter.getName()),
type,
parameter.isMutable());
false);
trace.record(BindingContext.VALUE_PARAMETER, parameter, variableDescriptor);
return variableDescriptor;
}
@@ -432,6 +432,11 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
JetParameter catchParameter = catchClause.getCatchParameter();
JetExpression catchBody = catchClause.getCatchBody();
if (catchParameter != null) {
ASTNode valOrVarNode = catchParameter.getValOrVarNode();
if (valOrVarNode != null) {
context.trace.report(VAL_OR_VAR_ON_CATCH_PARAMETER.on(valOrVarNode.getPsi(), ((JetKeywordToken) valOrVarNode.getElementType())));
}
VariableDescriptor variableDescriptor = context.expressionTypingServices.getDescriptorResolver().resolveLocalVariableDescriptor(
context.scope.getContainingDeclaration(), context.scope, catchParameter, context.trace);
JetType throwableType = KotlinBuiltIns.getInstance().getThrowable().getDefaultType();
@@ -0,0 +1,13 @@
fun f() {
try {
} catch (<!VAL_OR_VAR_ON_CATCH_PARAMETER!>val<!> e: Exception) {
}
try {
} catch (<!VAL_OR_VAR_ON_CATCH_PARAMETER!>var<!> e: Exception) {
}
try {
} catch (e: Exception) {
}
}
@@ -1045,6 +1045,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
doTest("compiler/testData/diagnostics/tests/controlStructures/tryReturnType.kt");
}
@TestMetadata("valVarCatchParameter.kt")
public void testValVarCatchParameter() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlStructures/valVarCatchParameter.kt");
}
@TestMetadata("valVarLoopParameter.kt")
public void testValVarLoopParameter() throws Exception {
doTest("compiler/testData/diagnostics/tests/controlStructures/valVarLoopParameter.kt");