KT-1572 Frontend doesn't mark all vars included in closure as refs.

This commit is contained in:
Svetlana Isakova
2012-03-14 15:07:23 +04:00
parent af9f00c177
commit 9dc9e9984b
8 changed files with 37 additions and 15 deletions
@@ -68,7 +68,9 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
public JetType visitSimpleNameExpression(JetSimpleNameExpression expression, ExpressionTypingContext context) {
// TODO : other members
// TODO : type substitutions???
return DataFlowUtils.checkType(getSelectorReturnType(NO_RECEIVER, null, expression, context), expression, context); // TODO : Extensions to this
JetType type = DataFlowUtils.checkType(getSelectorReturnType(NO_RECEIVER, null, expression, context), expression, context);
ExpressionTypingUtils.checkWrappingInRef(expression, context);
return type; // TODO : Extensions to this
}
private JetType lookupNamespaceOrClassObject(JetSimpleNameExpression expression, String referencedName, ExpressionTypingContext context) {
@@ -715,7 +717,6 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
}
else {
context.trace.record(BindingContext.VARIABLE_REASSIGNMENT, expression);
ExpressionTypingUtils.checkWrappingInRef(baseExpression, context);
checkLValue(context.trace, baseExpression);
}
@@ -129,11 +129,9 @@ public class ExpressionTypingUtils {
).contains(expression.getNode().getElementType());
}
public static void checkWrappingInRef(JetExpression expression, ExpressionTypingContext context) {
if (!(expression instanceof JetSimpleNameExpression)) return;
JetSimpleNameExpression simpleName = (JetSimpleNameExpression) expression;
VariableDescriptor variable = BindingContextUtils.extractVariableDescriptorIfAny(context.trace.getBindingContext(), simpleName, true);
if (variable != null) {
public static void checkWrappingInRef(JetSimpleNameExpression expression, ExpressionTypingContext context) {
VariableDescriptor variable = BindingContextUtils.extractVariableDescriptorIfAny(context.trace.getBindingContext(), expression, true);
if (variable != null && variable.isVar()) {
DeclarationDescriptor containingDeclaration = variable.getContainingDeclaration();
if (context.scope.getContainingDeclaration() != containingDeclaration && containingDeclaration instanceof CallableDescriptor) {
context.trace.record(MUST_BE_WRAPPED_IN_A_REF, variable);
@@ -216,7 +216,6 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
} else {
binaryOperationTrace.commit();
context.trace.record(VARIABLE_REASSIGNMENT, expression);
ExpressionTypingUtils.checkWrappingInRef(expression.getLeft(), context);
if (left instanceof JetArrayAccessExpression) {
ExpressionTypingContext contextForResolve = context.replaceScope(scope).replaceBindingTrace(TemporaryBindingTrace.create(contextWithExpectedType.trace));
basic.resolveArrayAccessSetMethod((JetArrayAccessExpression) left, expression.getRight(), contextForResolve, context.trace);
@@ -244,9 +243,6 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
if (leftType != null) { //if leftType == null, some another error has been generated
basic.checkLValue(context.trace, expression.getLeft());
}
if (left instanceof JetSimpleNameExpression) {
ExpressionTypingUtils.checkWrappingInRef(left, context);
}
return DataFlowUtils.checkStatementType(expression, contextWithExpectedType);
}
@@ -0,0 +1,23 @@
//KT-1572 Frontend doesn't mark all vars included in closure as refs.
class A(val t : Int) {}
fun testKt1572() : Boolean {
var a = A(0)
var b = A(3)
val changer = {a = b}
b = A(10) // this change has no effect on changer
changer()
return (a.t == 10)
}
fun testPrimitives() : Boolean {
var a = 0
var b = 3
val changer = {a = b}
b = 10
changer()
return (a == 10)
}
fun box() = if (testKt1572() && testPrimitives()) "OK" else "fail"
@@ -15,7 +15,7 @@ open class AllEvenNum() {
}
}
(i++)
(i = i + 1)
}
}
@@ -328,7 +328,7 @@ fun func() {
val <!UNUSED_VARIABLE!>a<!> = object {
val x = b
{
<!VAL_REASSIGNMENT!>b<!> = 4
<!VAL_REASSIGNMENT!>b<!> = <!UNUSED_VALUE!>4<!>
<!UNRESOLVED_REFERENCE!>$b<!> = 3
}
}
@@ -28,4 +28,8 @@ public class SafeRefTest extends CodegenTestCase {
public void test232 () throws Exception {
blackBoxFile("regressions/kt232.jet");
}
public void test1572 () throws Exception {
blackBoxFile("regressions/kt1572.jet");
}
}
+2 -2
View File
@@ -235,8 +235,8 @@ fun mergeAutocasts(a: Any?) {
//mutability
fun f(): String {
var a: Any = 11
if (a is String) {
var <info>a</info>: Any = 11
if (<info>a</info> is String) {
val <warning>i</warning>: String = <error descr="[AUTOCAST_IMPOSSIBLE] Automatic cast to String is impossible, because a could have changed since the is-check">a</error>
<error descr="[AUTOCAST_IMPOSSIBLE] Automatic cast to String is impossible, because a could have changed since the is-check">a</error>.compareTo("f")
val <warning>f</warning>: Function0<String> = { <error descr="[AUTOCAST_IMPOSSIBLE] Automatic cast to String is impossible, because a could have changed since the is-check">a</error> }