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

Unified Diff: compiler/java/com/google/dart/compiler/resolver/CompileTimeConstantAnalyzer.java

Issue 10830145: Issue 3847. Allow non-constant instance variable initializers (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use different ErrorCode for using instance fields in instance field initializers Created 8 years, 5 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
« no previous file with comments | « no previous file | compiler/java/com/google/dart/compiler/resolver/ResolverErrorCode.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiler/java/com/google/dart/compiler/resolver/CompileTimeConstantAnalyzer.java
diff --git a/compiler/java/com/google/dart/compiler/resolver/CompileTimeConstantAnalyzer.java b/compiler/java/com/google/dart/compiler/resolver/CompileTimeConstantAnalyzer.java
index f2a4730fab6ad35db753b20b0fb28e8f2d3b82e7..82a1017b89851c73d7ccaa5718a55e68c5bbdf78 100644
--- a/compiler/java/com/google/dart/compiler/resolver/CompileTimeConstantAnalyzer.java
+++ b/compiler/java/com/google/dart/compiler/resolver/CompileTimeConstantAnalyzer.java
@@ -622,14 +622,20 @@ public class CompileTimeConstantAnalyzer {
if (node.getParent() != null) {
DartNode pp = node.getParent().getParent();
boolean isFinalTopLevelField = node.getModifiers().isFinal() && pp instanceof DartUnit;
- boolean isInstanceField = pp instanceof DartClass;
- if (isFinalTopLevelField || isInstanceField) {
+ boolean isClassField = pp instanceof DartClass;
+ boolean isStatic = node.getModifiers().isStatic();
+ boolean isConst = node.getModifiers().isConstant();
+ if (isFinalTopLevelField || (isClassField && isStatic) || isConst) {
Type type = checkConstantExpression(node.getValue());
if (node.getElement().getType().equals(dynamicType)) {
node.getElement().setConstantType(type);
}
return null;
}
+ if (isClassField && !isStatic) {
+ DartExpression value = node.getValue();
+ checkInstanceFieldInitializer(value);
+ }
}
return super.visitField(node);
}
@@ -776,7 +782,32 @@ public class CompileTimeConstantAnalyzer {
return null;
}
- public void exec (DartUnit unit) {
+ public void exec(DartUnit unit) {
unit.accept(new FindCompileTimeConstantExpressionsVisitor());
}
+
+ private void checkInstanceFieldInitializer(DartExpression value) {
+ if (value != null) {
+ value.accept(new ASTVisitor<Void>() {
+ @Override
+ public Void visitThisExpression(DartThisExpression node) {
+ context.onError(new DartCompilationError(node,
+ ResolverErrorCode.CANNOT_USE_THIS_IN_INSTANCE_FIELD_INITIALIZER));
+ return null;
+ }
+ @Override
+ public Void visitIdentifier(DartIdentifier node) {
+ NodeElement element = node.getElement();
+ if (ElementKind.of(element) == ElementKind.FIELD) {
+ FieldElement fieldElement = (FieldElement) element;
+ if (!fieldElement.isStatic()) {
+ context.onError(new DartCompilationError(node,
+ ResolverErrorCode.CANNOT_USE_INSTANCE_FIELD_IN_INSTANCE_FIELD_INITIALIZER));
+ }
+ }
+ return null;
+ }
+ });
+ }
+ }
}
« no previous file with comments | « no previous file | compiler/java/com/google/dart/compiler/resolver/ResolverErrorCode.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698