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

Unified Diff: pkg/analyzer/lib/src/task/strong_mode.dart

Issue 2741033004: Infer types of instance methods before any other inference. (Closed)
Patch Set: Created 3 years, 9 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 | « pkg/analyzer/lib/src/task/dart.dart ('k') | pkg/analyzer/test/src/task/strong_mode_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/task/strong_mode.dart
diff --git a/pkg/analyzer/lib/src/task/strong_mode.dart b/pkg/analyzer/lib/src/task/strong_mode.dart
index 284a31ad55cc6386e4caa930421d87215029c06f..c1ffe3b5d0986a283266e1badac991ccaa101d20 100644
--- a/pkg/analyzer/lib/src/task/strong_mode.dart
+++ b/pkg/analyzer/lib/src/task/strong_mode.dart
@@ -73,7 +73,7 @@ class InstanceMemberInferrer {
* The classes that have been visited while attempting to infer the types of
* instance members of some base class.
*/
- HashSet<ClassElementImpl> elementsBeingInferred =
+ HashSet<ClassElementImpl> classesBeingInferred =
new HashSet<ClassElementImpl>();
/**
@@ -102,6 +102,17 @@ class InstanceMemberInferrer {
}
/**
+ * Infer types for all of the instance methods in [unit].
+ */
+ void inferInstanceMethods(CompilationUnitElement unit) {
+ for (ClassElement classElement in unit.types) {
+ try {
+ _inferClassInstanceMethods(classElement);
+ } on _CycleException {}
+ }
+ }
+
+ /**
* Return `true` if the list of [elements] contains only methods.
*/
bool _allSameElementKind(
@@ -203,7 +214,7 @@ class InstanceMemberInferrer {
if (classElement.hasBeenInferred) {
return;
}
- if (!elementsBeingInferred.add(classElement)) {
+ if (!classesBeingInferred.add(classElement)) {
// We have found a circularity in the class hierarchy. For now we just
// stop trying to infer any type information for any classes that
// inherit from any class in the cycle. We could potentially limit the
@@ -224,7 +235,6 @@ class InstanceMemberInferrer {
//
classElement.fields.forEach(_inferField);
classElement.accessors.forEach(_inferExecutable);
- classElement.methods.forEach(_inferExecutable);
//
// Infer initializing formal parameter types. This must happen after
// field types are inferred.
@@ -232,7 +242,49 @@ class InstanceMemberInferrer {
classElement.constructors.forEach(_inferConstructorFieldFormals);
classElement.hasBeenInferred = true;
} finally {
- elementsBeingInferred.remove(classElement);
+ classesBeingInferred.remove(classElement);
+ }
+ }
+ }
+
+ /**
+ * Infer types for all of the instance methods in the given [classElement].
+ */
+ void _inferClassInstanceMethods(ClassElement classElement) {
+ if (classElement is ClassElementImpl) {
+ if (classElement.hasInferredInstanceMethods) {
+ return;
+ }
+ if (!classesBeingInferred.add(classElement)) {
+ // We have found a circularity in the class hierarchy. For now we just
+ // stop trying to infer any type information for any classes that
+ // inherit from any class in the cycle. We could potentially limit the
+ // algorithm to only not inferring types in the classes in the cycle,
+ // but it isn't clear that the results would be significantly better.
+ throw new _CycleException();
+ }
+ try {
+ //
+ // Process supertypes first.
+ //
+ void processSupertype(InterfaceType type) {
+ ClassElement element = type?.element;
+ if (element != null) {
+ _inferClassInstanceMethods(element);
+ }
+ }
+
+ processSupertype(classElement.supertype);
+ classElement.mixins.forEach(processSupertype);
+ classElement.interfaces.forEach(processSupertype);
+
+ //
+ // Then infer the types for the instance methods in this class.
+ //
+ classElement.methods.forEach(_inferExecutable);
+ classElement.hasInferredInstanceMethods = true;
+ } finally {
+ classesBeingInferred.remove(classElement);
}
}
}
« no previous file with comments | « pkg/analyzer/lib/src/task/dart.dart ('k') | pkg/analyzer/test/src/task/strong_mode_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698