Type checking for default values

This commit is contained in:
Andrey Breslav
2011-09-29 21:34:39 +04:00
parent 5c8b060a49
commit f36438dee2
10 changed files with 51 additions and 9 deletions
@@ -85,7 +85,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
public void addConstructor(@NotNull ConstructorDescriptor constructorDescriptor) {
assert constructorDescriptor.getContainingDeclaration() == this;
// assert constructorDescriptor.getTypeParameters().size() == getTypeConstructor().getParameters().size();
// assert constructorDescriptor.getTypeParameters().size() == getTypeConstructor().getValueParameters().size();
constructors.add(constructorDescriptor);
if (defaultType != null) {
// constructorDescriptor.getTypeParameters().addAll(typeParameters);
@@ -32,8 +32,9 @@ public class JetConstructor extends JetDeclaration implements JetDeclarationWith
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
}
@Override
@NotNull
public List<JetParameter> getParameters() {
public List<JetParameter> getValueParameters() {
JetParameterList list = getParameterList();
return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
}
@@ -1,12 +1,15 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* @author abreslav
*/
public interface JetDeclarationWithBody {
public interface JetDeclarationWithBody extends PsiElement {
@Nullable
JetExpression getBodyExpression();
@@ -20,5 +23,8 @@ public interface JetDeclarationWithBody {
@NotNull
JetElement asElement();
@NotNull
List<JetParameter> getValueParameters();
}
@@ -2,9 +2,9 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lexer.JetTokens;
import java.util.List;
/**
* @author max
@@ -29,6 +29,12 @@ public class JetFunctionLiteralExpression extends JetExpression implements JetDe
return (JetFunctionLiteral) findChildByType(JetNodeTypes.FUNCTION_LITERAL);
}
@NotNull
@Override
public List<JetParameter> getValueParameters() {
return getFunctionLiteral().getValueParameters();
}
@Override
public JetBlockExpression getBodyExpression() {
return getFunctionLiteral().getBodyExpression();
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lexer.JetTokens;
import java.util.Collections;
import java.util.List;
/**
@@ -44,6 +45,16 @@ public class JetPropertyAccessor extends JetDeclaration implements JetFunctionOr
return parameters.get(0);
}
@NotNull
@Override
public List<JetParameter> getValueParameters() {
JetParameter parameter = getParameter();
if (parameter == null) {
return Collections.emptyList();
}
return Collections.singletonList(parameter);
}
@Nullable
@Override
public JetExpression getBodyExpression() {
@@ -549,8 +549,8 @@ public class BodyResolver {
@NotNull JetDeclarationWithBody function,
@NotNull FunctionDescriptor functionDescriptor,
@NotNull JetScope declaringScope) {
JetExpression bodyExpression = function.getBodyExpression();
JetExpression bodyExpression = function.getBodyExpression();
if (bodyExpression != null) {
JetFlowInformationProvider flowInformationProvider = context.getClassDescriptorResolver().computeFlowData(function.asElement(), bodyExpression);
JetTypeInferrer.Services typeInferrer = context.getSemanticServices().getTypeInferrerServices(trace, flowInformationProvider);
@@ -558,6 +558,19 @@ public class BodyResolver {
typeInferrer.checkFunctionReturnType(declaringScope, function, functionDescriptor);
}
JetTypeInferrer.Services typeInferrer = context.getSemanticServices().getTypeInferrerServices(trace, JetFlowInformationProvider.THROW_EXCEPTION);
List<JetParameter> valueParameters = function.getValueParameters();
for (int i = 0; i < valueParameters.size(); i++) {
ValueParameterDescriptor valueParameterDescriptor = functionDescriptor.getValueParameters().get(i);
if (valueParameterDescriptor.hasDefaultValue()) {
JetParameter jetParameter = valueParameters.get(i);
JetExpression defaultValue = jetParameter.getDefaultValue();
if (defaultValue != null) {
typeInferrer.getType(declaringScope, defaultValue, valueParameterDescriptor.getOutType());
}
}
}
assert functionDescriptor.getReturnType() != null;
}
}
@@ -692,7 +692,7 @@ public class ClassDescriptorResolver {
@NotNull
public ConstructorDescriptorImpl resolveSecondaryConstructorDescriptor(@NotNull JetScope scope, @NotNull ClassDescriptor classDescriptor, @NotNull JetConstructor constructor) {
return createConstructorDescriptor(scope, classDescriptor, false, constructor.getModifierList(), constructor, classDescriptor.getTypeConstructor().getParameters(), constructor.getParameters());
return createConstructorDescriptor(scope, classDescriptor, false, constructor.getModifierList(), constructor, classDescriptor.getTypeConstructor().getParameters(), constructor.getValueParameters());
}
@NotNull
@@ -17,8 +17,8 @@ public class CallMaker {
private static class ExpressionValueArgument implements ValueArgument {
private final JetExpression expression;
private final PsiElement reportErrorsOn;
private ExpressionValueArgument(@NotNull JetExpression expression) {
this(expression, expression);
}
@@ -0,0 +1,5 @@
val x = ""
fun bar(x : Int = <!TYPE_MISMATCH!>""<!>, y : Int = <!TYPE_MISMATCH!>x<!>, z : String = <!UNRESOLVED_REFERENCE!>y<!>) {
}
@@ -1,7 +1,7 @@
fun foo(a : Int = 1, b : String = "abc") {
}
fun bar(x : Int = "", y : Int = 1, z : String) {
fun bar(x : Int = 1, y : Int = 1, z : String) {
}
fun test() {