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

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

Issue 2754423002: Fail inference when an instance field is referenced. (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
Index: pkg/analyzer/lib/src/task/dart.dart
diff --git a/pkg/analyzer/lib/src/task/dart.dart b/pkg/analyzer/lib/src/task/dart.dart
index 2a7ca939ddf6115b8c4544196a0960d2ff649843..7bd1dafb634dd2edd98f1acee07e84ac20e0a612 100644
--- a/pkg/analyzer/lib/src/task/dart.dart
+++ b/pkg/analyzer/lib/src/task/dart.dart
@@ -387,6 +387,17 @@ final ResultDescriptor<IgnoreInfo> IGNORE_INFO =
new ResultDescriptor<IgnoreInfo>('IGNORE_INFO', null);
/**
+ * This result is `true` if the variable references an instance field or
+ * an instance getter.
+ *
+ * The result is only available for [VariableElement]s, and only when strong
+ * mode is enabled.
+ */
+final ResultDescriptor<bool> INFERABLE_VARIABLE_REFERENCES_INSTANCE_FIELD =
+ new ResultDescriptor<bool>(
+ 'INFERABLE_VARIABLE_REFERENCES_INSTANCE_FIELD', false);
+
+/**
* A list of the [VariableElement]s whose type should be inferred that another
* inferable static variable (the target) depends on.
*
@@ -2138,8 +2149,10 @@ class ComputeInferableStaticVariableDependenciesTask
static final TaskDescriptor DESCRIPTOR = new TaskDescriptor(
'ComputeInferableStaticVariableDependenciesTask',
createTask,
- buildInputs,
- <ResultDescriptor>[INFERABLE_STATIC_VARIABLE_DEPENDENCIES]);
+ buildInputs, <ResultDescriptor>[
+ INFERABLE_STATIC_VARIABLE_DEPENDENCIES,
+ INFERABLE_VARIABLE_REFERENCES_INSTANCE_FIELD
+ ]);
ComputeInferableStaticVariableDependenciesTask(
InternalAnalysisContext context, VariableElement variable)
@@ -2160,10 +2173,13 @@ class ComputeInferableStaticVariableDependenciesTask
VariableDeclaration declaration = getDeclaration(unit);
VariableGatherer gatherer = new VariableGatherer(_isInferableStatic);
declaration.initializer.accept(gatherer);
+// print('[INFERABLE_STATIC_VARIABLE_DEPENDENCIES][$declaration] ${gatherer.hasInstanceFieldOrGetterReference}');
//
// Record outputs.
//
outputs[INFERABLE_STATIC_VARIABLE_DEPENDENCIES] = gatherer.results.toList();
+ outputs[INFERABLE_VARIABLE_REFERENCES_INSTANCE_FIELD] =
+ gatherer.hasInstanceFieldOrGetterReference;
}
/**
@@ -3678,6 +3694,12 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {
static const String DEPENDENCIES_INPUT = 'DEPENDENCIES_INPUT';
/**
+ * The [INFERABLE_VARIABLE_REFERENCES_INSTANCE_FIELD] input.
+ */
+ static const String REFERENCES_INSTANCE_FIELD_INPUT =
+ 'REFERENCES_INSTANCE_FIELD_INPUT';
+
+ /**
* The name of the [TYPE_PROVIDER] input.
*/
static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT';
@@ -3716,6 +3738,8 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {
//
VariableElementImpl variable = target;
+ bool referencesInstanceField =
+ getRequiredInput(REFERENCES_INSTANCE_FIELD_INPUT);
CompilationUnit unit = getRequiredInput(UNIT_INPUT);
TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT);
@@ -3724,12 +3748,12 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {
List<AnalysisError> errors = AnalysisError.NO_ERRORS;
if (dependencyCycle == null && variable.hasImplicitType) {
VariableDeclaration declaration = getDeclaration(unit);
+ Expression initializer = declaration.initializer;
//
// Re-resolve the variable's initializer so that the inferred types
// of other variables will be propagated.
//
RecordingErrorListener errorListener = new RecordingErrorListener();
- Expression initializer = declaration.initializer;
ResolutionContext resolutionContext =
ResolutionContextBuilder.contextFor(initializer);
ResolverVisitor visitor = new ResolverVisitor(
@@ -3742,13 +3766,20 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {
visitor.initForIncrementalResolution();
initializer.accept(visitor);
+ DartType newType;
+ if (hasInstanceGetterReference(initializer)) {
+ // Instance getters cannot be used for top-level type inference.
+ newType = typeProvider.dynamicType;
+ } else {
+ newType = initializer.staticType;
+ if (newType == null || newType.isBottom || newType.isDartCoreNull) {
+ newType = typeProvider.dynamicType;
+ }
+ }
+
//
// Record the type of the variable.
//
- DartType newType = initializer.staticType;
- if (newType == null || newType.isBottom || newType.isDartCoreNull) {
- newType = typeProvider.dynamicType;
- }
setFieldType(variable, newType);
errors = getUniqueErrors(errorListener.errors);
} else {
@@ -3779,6 +3810,8 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {
DEPENDENCIES_INPUT: INFERABLE_STATIC_VARIABLE_DEPENDENCIES
.of(variable)
.toListOf(INFERRED_STATIC_VARIABLE),
+ REFERENCES_INSTANCE_FIELD_INPUT:
+ INFERABLE_VARIABLE_REFERENCES_INSTANCE_FIELD.of(variable),
TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request),
UNIT_INPUT: RESOLVED_UNIT7.of(unit),
// In strong mode, add additional dependencies to enforce inference

Powered by Google App Engine
This is Rietveld 408576698