Skip to content

Commit

Permalink
KT-3181 Prohibit val/var keywords for function parameters
Browse files Browse the repository at this point in the history
 #KT-3181
  • Loading branch information
Evgeny Gerashchenko authored and Evgeny Gerashchenko committed Feb 13, 2013
1 parent 808dbd1 commit b1cb8ed
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion compiler/frontend/src/jet/Arrays.jet
@@ -1,6 +1,6 @@
package jet

public fun arrayOfNulls<T>(public val size : Int) : Array<T?>
public fun arrayOfNulls<T>(public size : Int) : Array<T?>

public class Array<reified T>(public val size : Int, init : (Int) -> T) {
public fun get(index : Int) : T
Expand Down
14 changes: 7 additions & 7 deletions compiler/frontend/src/jet/Numbers.jet
Expand Up @@ -87,7 +87,7 @@ public class Double : Number, Comparable<Double> {
public override fun toByte() : Byte

public override fun hashCode() : Int
public override fun equals(val other : Any?) : Boolean
public override fun equals(other : Any?) : Boolean
}

public class Float : Number, Comparable<Float> {
Expand Down Expand Up @@ -161,7 +161,7 @@ public class Float : Number, Comparable<Float> {
public override fun toByte() : Byte

public override fun hashCode() : Int
public override fun equals(val other : Any?) : Boolean
public override fun equals(other : Any?) : Boolean
}

public class Long : Number, Comparable<Long> {
Expand Down Expand Up @@ -243,7 +243,7 @@ public class Long : Number, Comparable<Long> {
public override fun toByte() : Byte

public override fun hashCode() : Int
public override fun equals(val other : Any?) : Boolean
public override fun equals(other : Any?) : Boolean
}

public class Int : Number, Comparable<Int> {
Expand Down Expand Up @@ -325,7 +325,7 @@ public class Int : Number, Comparable<Int> {
public override fun toByte() : Byte

public override fun hashCode() : Int
public override fun equals(val other : Any?) : Boolean
public override fun equals(other : Any?) : Boolean
}

public class Char : Number, Comparable<Char> {
Expand Down Expand Up @@ -393,7 +393,7 @@ public class Char : Number, Comparable<Char> {
public override fun toByte() : Byte

public override fun hashCode() : Int
public override fun equals(val other : Any?) : Boolean
public override fun equals(other : Any?) : Boolean
}

public class Short : Number, Comparable<Short> {
Expand Down Expand Up @@ -467,7 +467,7 @@ public class Short : Number, Comparable<Short> {
public override fun toByte() : Byte

public override fun hashCode() : Int
public override fun equals(val other : Any?) : Boolean
public override fun equals(other : Any?) : Boolean
}

public class Byte : Number, Comparable<Byte> {
Expand Down Expand Up @@ -541,5 +541,5 @@ public class Byte : Number, Comparable<Byte> {
public override fun toByte() : Byte

public override fun hashCode() : Int
public override fun equals(val other : Any?) : Boolean
public override fun equals(other : Any?) : Boolean
}
Expand Up @@ -474,6 +474,7 @@ public interface Errors {
DiagnosticFactory3<JetExpression, DeclarationDescriptor, Visibility, DeclarationDescriptor> INVISIBLE_SETTER = DiagnosticFactory3.create(ERROR);

DiagnosticFactory1<PsiElement, JetKeywordToken> VAL_OR_VAR_ON_LOOP_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, JetKeywordToken> VAL_OR_VAR_ON_FUN_PARAMETER = DiagnosticFactory1.create(ERROR);

// Backing fields

Expand Down
Expand Up @@ -180,6 +180,7 @@ public String render(@NotNull Collection<JetKeywordToken> tokens) {
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_FUN_PARAMETER, "''{0}'' on function 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);
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.lexer.JetKeywordToken;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.util.lazy.RecursionIntolerantLazyValue;
import org.jetbrains.jet.util.lazy.RecursionIntolerantLazyValueWithDefault;
Expand Down Expand Up @@ -440,7 +441,7 @@ private List<ValueParameterDescriptor> resolveValueParameters(
BindingTrace trace
) {
List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
for (int i = 0, valueParametersSize = valueParameters.size(); i < valueParametersSize; i++) {
for (int i = 0; i < valueParameters.size(); i++) {
JetParameter valueParameter = valueParameters.get(i);
JetTypeReference typeReference = valueParameter.getTypeReference();

Expand All @@ -453,6 +454,13 @@ private List<ValueParameterDescriptor> resolveValueParameters(
type = typeResolver.resolveType(parameterScope, typeReference, trace, true);
}

if (!(functionDescriptor instanceof ConstructorDescriptor)) {
ASTNode valOrVarNode = valueParameter.getValOrVarNode();
if (valOrVarNode != null) {
trace.report(VAL_OR_VAR_ON_FUN_PARAMETER.on(valOrVarNode.getPsi(), ((JetKeywordToken) valOrVarNode.getElementType())));
}
}

ValueParameterDescriptor valueParameterDescriptor =
resolveValueParameterDescriptor(parameterScope, functionDescriptor, valueParameter, i, type, trace);
parameterScope.addVariableDescriptor(valueParameterDescriptor);
Expand All @@ -477,7 +485,7 @@ public MutableValueParameterDescriptor resolveValueParameterDescriptor(
index,
annotationResolver.resolveAnnotations(scope, valueParameter.getModifierList(), trace),
JetPsiUtil.safeName(valueParameter.getName()),
valueParameter.isMutable(),
false,
variableType,
valueParameter.getDefaultValue() != null,
varargElementType
Expand Down
@@ -0,0 +1,12 @@
fun f(
<!VAL_OR_VAR_ON_FUN_PARAMETER!>val<!> a: Int,
<!VAL_OR_VAR_ON_FUN_PARAMETER!>var<!> b: Int,
c: Int,
vararg <!VAL_OR_VAR_ON_FUN_PARAMETER!>var<!> d: Int,
vararg <!VAL_OR_VAR_ON_FUN_PARAMETER!>val<!> e: Int,
vararg f: Int
) {


a + b + c + d[0] + e[0] + f[0] // to avoid 'unused parameter'
}
Expand Up @@ -1543,6 +1543,11 @@ public void testRedeclarationsInMultiDecl() throws Exception {
doTest("compiler/testData/diagnostics/tests/declarationChecks/RedeclarationsInMultiDecl.kt");
}

@TestMetadata("valVarFunctionParameter.kt")
public void testValVarFunctionParameter() throws Exception {
doTest("compiler/testData/diagnostics/tests/declarationChecks/valVarFunctionParameter.kt");
}

@TestMetadata("VarianceOnFunctionAndPropertyTypeParameters.kt")
public void testVarianceOnFunctionAndPropertyTypeParameters() throws Exception {
doTest("compiler/testData/diagnostics/tests/declarationChecks/VarianceOnFunctionAndPropertyTypeParameters.kt");
Expand Down

0 comments on commit b1cb8ed

Please sign in to comment.