KT-353 Generic type argument inference sometimes doesn't work

KT-459 Type argument inference fails when class names are fully qualified
This commit is contained in:
Andrey Breslav
2011-11-09 22:15:37 +03:00
parent 8ab1f1b420
commit c6f6f182b6
3 changed files with 47 additions and 8 deletions
@@ -57,7 +57,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
}
}
else {
return getSelectorReturnType(NO_RECEIVER, null, expression, context); // TODO : Extensions to this
return DataFlowUtils.checkType(getSelectorReturnType(NO_RECEIVER, null, expression, context), expression, context); // TODO : Extensions to this
}
return null;
}
@@ -419,14 +419,13 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
}
@Override
public JetType visitQualifiedExpression(JetQualifiedExpression expression, ExpressionTypingContext contextWithExpectedType) {
ExpressionTypingContext context = contextWithExpectedType.replaceExpectedType(NO_EXPECTED_TYPE);
public JetType visitQualifiedExpression(JetQualifiedExpression expression, ExpressionTypingContext context) {
// TODO : functions as values
JetExpression selectorExpression = expression.getSelectorExpression();
JetExpression receiverExpression = expression.getReceiverExpression();
ExpressionTypingContext contextWithNoExpectedType = context.replaceExpectedType(NO_EXPECTED_TYPE);
JetType receiverType = facade.getType(receiverExpression,
context
.replaceExpectedType(NO_EXPECTED_TYPE)
contextWithNoExpectedType
.replaceExpectedReturnType(NO_EXPECTED_TYPE)
.replaceNamespacesAllowed(true));
if (selectorExpression == null) return null;
@@ -462,7 +461,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
if (result != null) {
context.trace.record(BindingContext.EXPRESSION_TYPE, selectorExpression, result);
}
return DataFlowUtils.checkType(result, expression, contextWithExpectedType);
return DataFlowUtils.checkType(result, expression, context);
}
private void propagateConstantValues(JetQualifiedExpression expression, ExpressionTypingContext context, JetSimpleNameExpression selectorExpression) {
@@ -508,14 +507,14 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
VariableDescriptor variableDescriptor = context.replaceBindingTrace(temporaryTrace).resolveSimpleProperty(receiver, callOperationNode, nameExpression);
if (variableDescriptor != null) {
temporaryTrace.commit();
return DataFlowUtils.checkType(variableDescriptor.getOutType(), nameExpression, context);
return variableDescriptor.getOutType();
}
ExpressionTypingContext newContext = receiver.exists() ? context.replaceScope(receiver.getType().getMemberScope()) : context;
JetType jetType = lookupNamespaceOrClassObject(nameExpression, nameExpression.getReferencedName(), newContext);
if (jetType == null) {
context.trace.report(UNRESOLVED_REFERENCE.on(nameExpression));
}
return DataFlowUtils.checkType(jetType, nameExpression, context);
return jetType;
}
else if (selectorExpression instanceof JetQualifiedExpression) {
JetQualifiedExpression qualifiedExpression = (JetQualifiedExpression) selectorExpression;
@@ -0,0 +1,8 @@
// KT-459 Type argument inference fails when class names are fully qualified
fun test() {
val attributes : java.util.HashMap<String, String> = java.util.HashMap() // failure!
attributes["href"] = "1" // inference fails, but it shouldn't
}
fun <K, V> java.util.Map<K, V>.set(key : K, value : V) {}//= this.put(key, value)
@@ -0,0 +1,32 @@
// KT-353 Generic type argument inference sometimes doesn't work
trait A {
fun <T> gen() : T
}
fun foo(a: A) {
val g : fun() : Unit = {
a.gen() //it works: Unit is derived
}
val u: Unit = a.gen() //type mismatch, but Unit can be derived
if (true) {
a.gen() //it works: Unit is derived
}
val b : fun() : Unit = {
if (true) {
a.gen() //type mismatch, but Unit can be derived
}
else {
()
}
}
val f : fun() : Int = { () : Int =>
a.gen() //type mismatch, but Int can be derived
}
a.gen() //it works: Unit is derived
}