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

Unified Diff: compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java

Issue 10919087: Change type of variable using assignment only if it was not explicitly declared (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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/type/TypeAnalyzer.java
diff --git a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
index 8efe4b6af7478b637c8c18c6435859a27ba2bc05..b1ffc7252561b950efebd63c436eff5fb94708bc 100644
--- a/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
+++ b/compiler/java/com/google/dart/compiler/type/TypeAnalyzer.java
@@ -198,9 +198,24 @@ public class TypeAnalyzer implements DartCompilationPhase {
}
}
void setType(VariableElement element, Type newType) {
- rememberOldType(element, element.getType());
- newTypes.put(element, newType);
- Elements.setType(element, newType);
+ if (canSetType(element)) {
+ rememberOldType(element, element.getType());
+ newTypes.put(element, newType);
+ Elements.setType(element, newType);
+ }
+ }
+ boolean canSetType(VariableElement element) {
+ Type type = element.getType();
+ // no type declared, no assignment yet
+ if (TypeKind.of(type) == TypeKind.DYNAMIC) {
+ return true;
+ }
+ // was assignment, inferred
+ if (type != null && type.isInferred()) {
+ return true;
+ }
+ // was declared with type, keep it
+ return false;
}
Map<VariableElement, Type> getNewTypesAndRestoreOld() {
for (Entry<VariableElement, Type> entry : oldTypes.entrySet()) {
@@ -262,8 +277,8 @@ public class TypeAnalyzer implements DartCompilationPhase {
}
private void onError(SourceInfo errorTarget, ErrorCode errorCode, Object... arguments) {
- Source source = errorTarget.getSource();
if (suppressSdkWarnings && errorCode.getErrorSeverity() == ErrorSeverity.WARNING) {
+ Source source = errorTarget.getSource();
if (source != null && PackageLibraryManager.isDartUri(source.getUri())) {
return;
}

Powered by Google App Engine
This is Rietveld 408576698