KT-1572 Frontend doesn't mark all vars included in closure as refs.
This commit is contained in:
+3
-2
@@ -68,7 +68,9 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
public JetType visitSimpleNameExpression(JetSimpleNameExpression expression, ExpressionTypingContext context) {
|
public JetType visitSimpleNameExpression(JetSimpleNameExpression expression, ExpressionTypingContext context) {
|
||||||
// TODO : other members
|
// TODO : other members
|
||||||
// TODO : type substitutions???
|
// 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) {
|
private JetType lookupNamespaceOrClassObject(JetSimpleNameExpression expression, String referencedName, ExpressionTypingContext context) {
|
||||||
@@ -715,7 +717,6 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
context.trace.record(BindingContext.VARIABLE_REASSIGNMENT, expression);
|
context.trace.record(BindingContext.VARIABLE_REASSIGNMENT, expression);
|
||||||
ExpressionTypingUtils.checkWrappingInRef(baseExpression, context);
|
|
||||||
|
|
||||||
checkLValue(context.trace, baseExpression);
|
checkLValue(context.trace, baseExpression);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-5
@@ -129,11 +129,9 @@ public class ExpressionTypingUtils {
|
|||||||
).contains(expression.getNode().getElementType());
|
).contains(expression.getNode().getElementType());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void checkWrappingInRef(JetExpression expression, ExpressionTypingContext context) {
|
public static void checkWrappingInRef(JetSimpleNameExpression expression, ExpressionTypingContext context) {
|
||||||
if (!(expression instanceof JetSimpleNameExpression)) return;
|
VariableDescriptor variable = BindingContextUtils.extractVariableDescriptorIfAny(context.trace.getBindingContext(), expression, true);
|
||||||
JetSimpleNameExpression simpleName = (JetSimpleNameExpression) expression;
|
if (variable != null && variable.isVar()) {
|
||||||
VariableDescriptor variable = BindingContextUtils.extractVariableDescriptorIfAny(context.trace.getBindingContext(), simpleName, true);
|
|
||||||
if (variable != null) {
|
|
||||||
DeclarationDescriptor containingDeclaration = variable.getContainingDeclaration();
|
DeclarationDescriptor containingDeclaration = variable.getContainingDeclaration();
|
||||||
if (context.scope.getContainingDeclaration() != containingDeclaration && containingDeclaration instanceof CallableDescriptor) {
|
if (context.scope.getContainingDeclaration() != containingDeclaration && containingDeclaration instanceof CallableDescriptor) {
|
||||||
context.trace.record(MUST_BE_WRAPPED_IN_A_REF, variable);
|
context.trace.record(MUST_BE_WRAPPED_IN_A_REF, variable);
|
||||||
|
|||||||
-4
@@ -216,7 +216,6 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
|
|||||||
} else {
|
} else {
|
||||||
binaryOperationTrace.commit();
|
binaryOperationTrace.commit();
|
||||||
context.trace.record(VARIABLE_REASSIGNMENT, expression);
|
context.trace.record(VARIABLE_REASSIGNMENT, expression);
|
||||||
ExpressionTypingUtils.checkWrappingInRef(expression.getLeft(), context);
|
|
||||||
if (left instanceof JetArrayAccessExpression) {
|
if (left instanceof JetArrayAccessExpression) {
|
||||||
ExpressionTypingContext contextForResolve = context.replaceScope(scope).replaceBindingTrace(TemporaryBindingTrace.create(contextWithExpectedType.trace));
|
ExpressionTypingContext contextForResolve = context.replaceScope(scope).replaceBindingTrace(TemporaryBindingTrace.create(contextWithExpectedType.trace));
|
||||||
basic.resolveArrayAccessSetMethod((JetArrayAccessExpression) left, expression.getRight(), contextForResolve, context.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
|
if (leftType != null) { //if leftType == null, some another error has been generated
|
||||||
basic.checkLValue(context.trace, expression.getLeft());
|
basic.checkLValue(context.trace, expression.getLeft());
|
||||||
}
|
}
|
||||||
if (left instanceof JetSimpleNameExpression) {
|
|
||||||
ExpressionTypingUtils.checkWrappingInRef(left, context);
|
|
||||||
}
|
|
||||||
return DataFlowUtils.checkStatementType(expression, contextWithExpectedType);
|
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 <!UNUSED_VARIABLE!>a<!> = object {
|
||||||
val x = b
|
val x = b
|
||||||
{
|
{
|
||||||
<!VAL_REASSIGNMENT!>b<!> = 4
|
<!VAL_REASSIGNMENT!>b<!> = <!UNUSED_VALUE!>4<!>
|
||||||
<!UNRESOLVED_REFERENCE!>$b<!> = 3
|
<!UNRESOLVED_REFERENCE!>$b<!> = 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,4 +28,8 @@ public class SafeRefTest extends CodegenTestCase {
|
|||||||
public void test232 () throws Exception {
|
public void test232 () throws Exception {
|
||||||
blackBoxFile("regressions/kt232.jet");
|
blackBoxFile("regressions/kt232.jet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void test1572 () throws Exception {
|
||||||
|
blackBoxFile("regressions/kt1572.jet");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -235,8 +235,8 @@ fun mergeAutocasts(a: Any?) {
|
|||||||
|
|
||||||
//mutability
|
//mutability
|
||||||
fun f(): String {
|
fun f(): String {
|
||||||
var a: Any = 11
|
var <info>a</info>: Any = 11
|
||||||
if (a is String) {
|
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>
|
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")
|
<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> }
|
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> }
|
||||||
|
|||||||
Reference in New Issue
Block a user