val / var are now forbidden on a loop multi declaration parameter #KT-6274 Fixed

VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER error added
KtValVarKeywordOwner interface added for PSIs that have getValOrVarKeyword method
This commit is contained in:
Anton Sukhonosenko
2015-10-26 01:47:16 +03:00
committed by Mikhail Glukhikh
parent 54cb575819
commit 141ffa27dc
17 changed files with 119 additions and 20 deletions
@@ -667,6 +667,7 @@ public interface Errors {
DiagnosticFactory3<KtExpression, DeclarationDescriptor, Visibility, DeclarationDescriptor> INVISIBLE_SETTER = DiagnosticFactory3.create(ERROR);
DiagnosticFactory1<PsiElement, KtKeywordToken> VAL_OR_VAR_ON_LOOP_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KtKeywordToken> VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KtKeywordToken> VAL_OR_VAR_ON_FUN_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KtKeywordToken> VAL_OR_VAR_ON_CATCH_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KtKeywordToken> VAL_OR_VAR_ON_SECONDARY_CONSTRUCTOR_PARAMETER = DiagnosticFactory1.create(ERROR);
@@ -264,6 +264,7 @@ public class DefaultErrorMessages {
MAP.put(VARIABLE_EXPECTED, "Variable expected");
MAP.put(VAL_OR_VAR_ON_LOOP_PARAMETER, "''{0}'' on loop parameter is not allowed", TO_STRING);
MAP.put(VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER, "''{0}'' on loop multi 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(VAL_OR_VAR_ON_SECONDARY_CONSTRUCTOR_PARAMETER, "''{0}'' on secondary constructor parameter is not allowed", TO_STRING);
@@ -29,7 +29,7 @@ import java.util.List;
import static org.jetbrains.kotlin.lexer.KtTokens.*;
public class KtMultiDeclaration extends KtDeclarationImpl {
public class KtMultiDeclaration extends KtDeclarationImpl implements KtValVarKeywordOwner {
public KtMultiDeclaration(@NotNull ASTNode node) {
super(node);
}
@@ -31,7 +31,7 @@ import org.jetbrains.kotlin.psi.typeRefHelpers.TypeRefHelpersKt;
import java.util.Collections;
import java.util.List;
public class KtParameter extends KtNamedDeclarationStub<KotlinParameterStub> implements KtCallableDeclaration {
public class KtParameter extends KtNamedDeclarationStub<KotlinParameterStub> implements KtCallableDeclaration, KtValVarKeywordOwner {
public KtParameter(@NotNull ASTNode node) {
super(node);
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.psi;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
public interface KtValVarKeywordOwner extends PsiElement {
@Nullable
PsiElement getValOrVarKeyword();
}
@@ -16,12 +16,6 @@
package org.jetbrains.kotlin.psi;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
public interface KtVariableDeclaration extends KtCallableDeclaration, KtWithExpressionInitializer {
public interface KtVariableDeclaration extends KtCallableDeclaration, KtWithExpressionInitializer, KtValVarKeywordOwner {
boolean isVar();
@Nullable
PsiElement getValOrVarKeyword();
}
@@ -163,7 +163,7 @@ public class ModifiersChecker {
}
public void checkParameterHasNoValOrVar(
@NotNull KtParameter parameter,
@NotNull KtValVarKeywordOwner parameter,
@NotNull DiagnosticFactory1<PsiElement, KtKeywordToken> diagnosticFactory
) {
PsiElement valOrVar = parameter.getValOrVarKeyword();
@@ -407,6 +407,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
loopScope, multiParameter, iteratorNextAsReceiver, loopRange, context
);
components.modifiersChecker.withTrace(context.trace).checkModifiersForMultiDeclaration(multiParameter);
components.modifiersChecker.withTrace(context.trace).checkParameterHasNoValOrVar(multiParameter, VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER);
components.identifierChecker.checkDeclaration(multiParameter, context.trace);
}
}
@@ -1,3 +1,18 @@
class Pair {
operator fun component1(): Int = null!!
operator fun component2(): Int = null!!
}
class Coll {
operator fun iterator(): It = It()
}
class It {
operator fun next() = Pair()
operator fun hasNext() = false
}
fun f() {
for (<!VAL_OR_VAR_ON_LOOP_PARAMETER!>val<!> i in 1..4) {
@@ -6,4 +21,12 @@ fun f() {
for (<!VAL_OR_VAR_ON_LOOP_PARAMETER!>var<!> i in 1..4) {
}
for (<!VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER!>val<!> (i,j) in Coll()) {
}
for (<!VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER!>var<!> (i,j) in Coll()) {
}
}
@@ -1,3 +1,29 @@
package
public fun f(): kotlin.Unit
public final class Coll {
public constructor Coll()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun iterator(): It
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class It {
public constructor It()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun hasNext(): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun next(): Pair
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Pair {
public constructor Pair()
public final operator fun component1(): kotlin.Int
public final operator fun component2(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -8,7 +8,7 @@ class A {
}
fun foo(list: List<A>) {
for (var (c1, c2, c3) in list) {
for (<!VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER!>var<!> (c1, c2, c3) in list) {
<!UNUSED_VALUE!>c1 =<!> 1
c3 + 1
}