From c6f6f182b68cd16d7237004fddbc88628ec00d13 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Wed, 9 Nov 2011 22:15:37 +0300 Subject: [PATCH] KT-353 Generic type argument inference sometimes doesn't work KT-459 Type argument inference fails when class names are fully qualified --- .../BasicExpressionTypingVisitor.java | 15 ++++----- .../full/regression/kt459.jet | 8 +++++ .../quick/regressions/kt353.jet | 32 +++++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/checkerWithErrorTypes/full/regression/kt459.jet create mode 100644 compiler/testData/checkerWithErrorTypes/quick/regressions/kt353.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index 1939b0192d0..4e19232a122 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -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; diff --git a/compiler/testData/checkerWithErrorTypes/full/regression/kt459.jet b/compiler/testData/checkerWithErrorTypes/full/regression/kt459.jet new file mode 100644 index 00000000000..e2dd54ef0da --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/full/regression/kt459.jet @@ -0,0 +1,8 @@ +// KT-459 Type argument inference fails when class names are fully qualified + +fun test() { + val attributes : java.util.HashMap = java.util.HashMap() // failure! + attributes["href"] = "1" // inference fails, but it shouldn't +} + +fun java.util.Map.set(key : K, value : V) {}//= this.put(key, value) \ No newline at end of file diff --git a/compiler/testData/checkerWithErrorTypes/quick/regressions/kt353.jet b/compiler/testData/checkerWithErrorTypes/quick/regressions/kt353.jet new file mode 100644 index 00000000000..5a7c9569795 --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/quick/regressions/kt353.jet @@ -0,0 +1,32 @@ +// KT-353 Generic type argument inference sometimes doesn't work + +trait A { + fun 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 +}