Added 'variable expected' error on assignments

This commit is contained in:
svtk
2011-11-04 15:58:46 +04:00
parent 33fad16fd5
commit 639497d7c3
3 changed files with 47 additions and 3 deletions
@@ -143,17 +143,18 @@ public interface Errors {
PsiElementOnlyDiagnosticFactory3<JetModifierListOwner, CallableMemberDescriptor, CallableMemberDescriptor, DeclarationDescriptor> VIRTUAL_MEMBER_HIDDEN = PsiElementOnlyDiagnosticFactory3.create(ERROR, "''{0}'' hides ''{1}'' in class {2} and needs 'override' modifier", DescriptorRenderer.TEXT);
PsiElementOnlyDiagnosticFactory1<JetSimpleNameExpression, DeclarationDescriptor> UNINITIALIZED_VARIABLE = PsiElementOnlyDiagnosticFactory1.create(ERROR, "Variable ''{0}'' must be initialized", NAME);
PsiElementOnlyDiagnosticFactory2<JetSimpleNameExpression, DeclarationDescriptor, JetProperty[]> VAL_REASSIGNMENT = new PsiElementOnlyDiagnosticFactory2<JetSimpleNameExpression, DeclarationDescriptor, JetProperty[]>(ERROR, "Val can not be reassigned", NAME) {
PsiElementOnlyDiagnosticFactory2<JetExpression, DeclarationDescriptor, JetProperty[]> VAL_REASSIGNMENT = new PsiElementOnlyDiagnosticFactory2<JetExpression, DeclarationDescriptor, JetProperty[]>(ERROR, "Val can not be reassigned", NAME) {
@NotNull
@Override
public DiagnosticWithPsiElement<JetSimpleNameExpression> on(@NotNull JetSimpleNameExpression elementToBlame, @NotNull ASTNode nodeToMark, @NotNull DeclarationDescriptor declarationDescriptor, @NotNull JetProperty[] property) {
DiagnosticWithPsiElement<JetSimpleNameExpression> diagnostic = super.on(elementToBlame, nodeToMark, declarationDescriptor, property);
public DiagnosticWithPsiElement<JetExpression> on(@NotNull JetExpression elementToBlame, @NotNull ASTNode nodeToMark, @NotNull DeclarationDescriptor declarationDescriptor, @NotNull JetProperty[] property) {
DiagnosticWithPsiElement<JetExpression> diagnostic = super.on(elementToBlame, nodeToMark, declarationDescriptor, property);
if (property.length == 1) {
return diagnostic.add(DiagnosticParameters.PROPERTY, property[0]);
}
return diagnostic;
}
};
SimplePsiElementOnlyDiagnosticFactory<JetExpression> VARIABLE_EXPECTED = new SimplePsiElementOnlyDiagnosticFactory<JetExpression>(ERROR, "Variable expected");
PsiElementOnlyDiagnosticFactory1<JetSimpleNameExpression, DeclarationDescriptor> INITIALIZATION_USING_BACKING_FIELD = PsiElementOnlyDiagnosticFactory1.create(ERROR, "Initialization using backing field required", NAME);
@@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.TemporaryBindingTrace;
import org.jetbrains.jet.lang.resolve.TopDownAnalyzer;
import org.jetbrains.jet.lang.resolve.calls.CallMaker;
@@ -150,6 +151,10 @@ public class ExpressionTypingVisitorForStatements extends BasicExpressionTypingV
if (right != null) {
JetType rightType = facade.getType(right, context.replaceExpectedType(leftType).replaceScope(scope));
}
VariableDescriptor variable = BindingContextUtils.extractVariableDescriptorIfAny(context.trace.getBindingContext(), left, true);
if (variable == null && leftType != null) { //if leftType == null, some another error has been generated
context.trace.report(VARIABLE_EXPECTED.on(left != null ? left : expression.getLeft()));
}
if (left instanceof JetSimpleNameExpression) {
JetSimpleNameExpression simpleName = (JetSimpleNameExpression) left;
String referencedName = simpleName.getReferencedName();
@@ -0,0 +1,38 @@
namespace lvalue_assignment
open class B() {
var b: Int = 2
val c: Int = 34
}
class C() : B() {
var x = 4
fun foo(c: C) {
this.x = 34
this.b = 123
super.b = 23
<!VAL_REASSIGNMENT!>this.c<!> = 34
<!VAL_REASSIGNMENT!>super.c<!> = 3535
<!VARIABLE_EXPECTED!>getInt()<!> = 12
}
fun foo(c: C) {
<!VARIABLE_EXPECTED!>this<!> = c //should be an error
}
}
fun getInt() = 0
class D() {
class B() {
fun foo() {
<!VARIABLE_EXPECTED!>this@D<!> = D()
}
}
}
fun cannotBe() {
<!UNRESOLVED_REFERENCE!>z<!> = 30;
<!VARIABLE_EXPECTED!>()<!> = ();
}