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

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

Issue 11275093: Issue 5987. Report error if static const references instance const. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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: 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 4c15ab1c402591d20af8fe88deffbee105a19a22..35bc6619e6a6bf849d044f24cde331870a9baf0c 100644
--- a/compiler/java/com/google/dart/compiler/resolver/CompileTimeConstantAnalyzer.java
+++ b/compiler/java/com/google/dart/compiler/resolver/CompileTimeConstantAnalyzer.java
@@ -68,7 +68,10 @@ import java.util.Set;
public class CompileTimeConstantAnalyzer {
private class ExpressionVisitor extends ASTVisitor<Void> {
- private ExpressionVisitor() {
+ private final boolean shouldBeStatic;
+
+ private ExpressionVisitor(boolean shouldBeStatic) {
+ this.shouldBeStatic = shouldBeStatic;
}
private boolean checkBoolean(DartNode x, Type type) {
@@ -367,6 +370,8 @@ public class CompileTimeConstantAnalyzer {
}
Element element = x.getElement();
+ boolean elementIsStatic = element != null
+ && (element.getModifiers().isStatic() || Elements.isTopLevel(element));
switch (ElementKind.of(element)) {
case CLASS:
case PARAMETER:
@@ -376,6 +381,11 @@ public class CompileTimeConstantAnalyzer {
case FIELD:
FieldElement fieldElement = (FieldElement) element;
+ if (shouldBeStatic && !elementIsStatic) {
+ context.onError(new DartCompilationError(x, ResolverErrorCode.NOT_A_STATIC_FIELD,
+ fieldElement.getName()));
+ }
+
// Check for circular references.
if (element != null && visitedElements.contains(element)) {
context.onError(new DartCompilationError(x, ResolverErrorCode.CIRCULAR_REFERENCE));
@@ -417,7 +427,7 @@ public class CompileTimeConstantAnalyzer {
return null;
case METHOD:
- if (!element.getModifiers().isStatic() && !Elements.isTopLevel(element)) {
+ if (!elementIsStatic) {
expectedConstant(x);
}
return null;
@@ -628,7 +638,7 @@ public class CompileTimeConstantAnalyzer {
public Void visitField(DartField node) {
if (node.getParent() != null) {
if (node.getModifiers().isConstant()) {
- Type type = checkConstantExpression(node.getValue());
+ Type type = checkConstantExpression(node.getValue(), node.getModifiers().isStatic());
if (node.getElement().getType().equals(dynamicType)) {
node.getElement().setConstantType(type);
}
@@ -787,8 +797,12 @@ public class CompileTimeConstantAnalyzer {
}
private Type checkConstantExpression(DartExpression expression) {
+ return checkConstantExpression(expression, false);
+ }
+
+ private Type checkConstantExpression(DartExpression expression, boolean shouldBeStatic) {
if (expression != null) {
- ExpressionVisitor visitor = new ExpressionVisitor();
+ ExpressionVisitor visitor = new ExpressionVisitor(shouldBeStatic);
expression.accept(visitor);
return visitor.getMostSpecificType(expression);
}

Powered by Google App Engine
This is Rietveld 408576698