Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1529)

Unified Diff: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java

Issue 16113003: Report SWC. and CTEC.CONST_FIELD_INITIALIZER_NOT_ASSIGNABLE (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add comment Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
diff --git a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
index 8eb7e81903e2be84c6a49af40b8325d5c210ce45..7c10c90d1be34edc2f41b138cff8c0be0d5986f9 100644
--- a/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
+++ b/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/verifier/ErrorVerifier.java
@@ -447,6 +447,7 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
public Void visitConstructorFieldInitializer(ConstructorFieldInitializer node) {
isInConstructorInitializer = true;
try {
+ checkForFieldInitializerNotAssignable(node);
return super.visitConstructorFieldInitializer(node);
} finally {
isInConstructorInitializer = false;
@@ -1942,6 +1943,59 @@ public class ErrorVerifier extends RecursiveASTVisitor<Void> {
}
/**
+ * This verifies that the passed constructor field initializer has compatible field and
+ * initializer expression types.
+ *
+ * @param node the constructor field initializer to test
+ * @return {@code true} if and only if an error code is generated on the passed node
+ * @see CompileTimeErrorCode#CONST_FIELD_INITIALIZER_NOT_ASSIGNABLE
+ * @see StaticWarningCode#FIELD_INITIALIZER_NOT_ASSIGNABLE
+ */
+ private boolean checkForFieldInitializerNotAssignable(ConstructorFieldInitializer node) {
+ // prepare field element
+ Element fieldNameElement = node.getFieldName().getElement();
+ if (!(fieldNameElement instanceof FieldElement)) {
+ return false;
+ }
+ FieldElement fieldElement = (FieldElement) fieldNameElement;
+ // prepare field type
+ Type fieldType = fieldElement.getType();
+ // prepare expression type
+ Expression expression = node.getExpression();
+ if (expression == null) {
+ return false;
+ }
+ // test the static type of the expression
+ Type staticType = getStaticType(expression);
+ if (staticType == null) {
+ return false;
+ }
+ if (staticType.isAssignableTo(fieldType)) {
+ return false;
+ }
+ // test the propagated type of the expression
+ Type propagatedType = getPropagatedType(expression);
+ if (propagatedType != null && propagatedType.isAssignableTo(fieldType)) {
+ return false;
+ }
+ // report problem
+ if (isEnclosingConstructorConst) {
+ errorReporter.reportError(
+ CompileTimeErrorCode.CONST_FIELD_INITIALIZER_NOT_ASSIGNABLE,
+ expression,
+ (propagatedType == null ? staticType : propagatedType).getDisplayName(),
+ fieldType.getDisplayName());
+ } else {
+ errorReporter.reportError(
+ StaticWarningCode.FIELD_INITIALIZER_NOT_ASSIGNABLE,
+ expression,
+ (propagatedType == null ? staticType : propagatedType).getDisplayName(),
+ fieldType.getDisplayName());
+ }
+ return true;
+ }
+
+ /**
* This verifies that the passed field formal parameter is in a constructor declaration.
*
* @param node the field formal parameter to test

Powered by Google App Engine
This is Rietveld 408576698