Skip to content

Commit

Permalink
Issue 27053. Record errors in InferStaticVariableTypeTask and include…
Browse files Browse the repository at this point in the history
… them into unit errors.

R=brianwilkerson@google.com, jmesserly@google.com
BUG=

Review URL: https://codereview.chromium.org/2239613002 .
  • Loading branch information
scheglov committed Aug 11, 2016
1 parent 25bff74 commit 296d81f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
6 changes: 6 additions & 0 deletions pkg/analyzer/doc/tasks.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@
InferInstanceMembersInUnitTask -> CREATED_RESOLVED_UNIT11
InferInstanceMembersInUnitTask -> RESOLVED_UNIT11
InferStaticVariableTypeTask -> INFERRED_STATIC_VARIABLE
InferStaticVariableTypeTask -> STATIC_VARIABLE_RESOLUTION_ERRORS
InferStaticVariableTypesInUnitTask -> CREATED_RESOLVED_UNIT9
InferStaticVariableTypesInUnitTask -> RESOLVED_UNIT9
InferStaticVariableTypesInUnitTask -> STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT
LIBRARY_CYCLE [shape=box]
LIBRARY_CYCLE_DEPENDENCIES -> InferInstanceMembersInUnitTask
LIBRARY_CYCLE_DEPENDENCIES -> InferStaticVariableTypeTask
Expand Down Expand Up @@ -327,6 +329,10 @@
SCAN_ERRORS [shape=box]
SOURCE_KIND -> BuildDirectiveElementsTask
SOURCE_KIND [shape=box]
STATIC_VARIABLE_RESOLUTION_ERRORS -> InferStaticVariableTypesInUnitTask
STATIC_VARIABLE_RESOLUTION_ERRORS [shape=box]
STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT -> LibraryUnitErrorsTask
STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT [shape=box]
STRONG_MODE_ERRORS -> LibraryUnitErrorsTask
STRONG_MODE_ERRORS [shape=box]
ScanDartTask -> IGNORE_INFO
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/generated/incremental_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ class IncrementalResolver {
_shiftErrors_NEW(RESOLVE_TYPE_NAMES_ERRORS);
_shiftErrors_NEW(RESOLVE_TYPE_BOUNDS_ERRORS);
_shiftErrors_NEW(RESOLVE_UNIT_ERRORS);
_shiftErrors_NEW(STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT);
_shiftErrors_NEW(STRONG_MODE_ERRORS);
_shiftErrors_NEW(VARIABLE_REFERENCE_ERRORS);
_shiftErrors_NEW(VERIFY_ERRORS);
Expand Down
66 changes: 56 additions & 10 deletions pkg/analyzer/lib/src/task/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,28 @@ final ListResultDescriptor<AnalysisError> SCAN_ERRORS =
new ListResultDescriptor<AnalysisError>(
'SCAN_ERRORS', AnalysisError.NO_ERRORS);

/**
* The errors produced while resolving a static [VariableElement] initializer.
*
* The result is only available for [VariableElement]s, and only when strong
* mode is enabled.
*/
final ListResultDescriptor<AnalysisError> STATIC_VARIABLE_RESOLUTION_ERRORS =
new ListResultDescriptor<AnalysisError>(
'STATIC_VARIABLE_RESOLUTION_ERRORS', AnalysisError.NO_ERRORS);

/**
* A list of the [AnalysisError]s reported while resolving static
* [INFERABLE_STATIC_VARIABLES_IN_UNIT] defined in a unit.
*
* The result is only available for [LibrarySpecificUnit]s, and only when strong
* mode is enabled.
*/
final ListResultDescriptor<AnalysisError>
STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT =
new ListResultDescriptor<AnalysisError>(
'STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT', null);

/**
* The additional strong mode errors produced while verifying a
* compilation unit.
Expand Down Expand Up @@ -3627,19 +3649,22 @@ class InferStaticVariableTypesInUnitTask extends SourceBasedAnalysisTask {
static const String UNIT_INPUT = 'UNIT_INPUT';

/**
* The name of the input whose value is a list of the inferable static
* variables whose types have been computed.
* The name of the [STATIC_VARIABLE_RESOLUTION_ERRORS] for all static
* variables in the compilation unit.
*/
static const String INFERRED_VARIABLES_INPUT = 'INFERRED_VARIABLES_INPUT';
static const String ERRORS_LIST_INPUT = 'INFERRED_VARIABLES_INPUT';

/**
* The task descriptor describing this kind of task.
*/
static final TaskDescriptor DESCRIPTOR = new TaskDescriptor(
'InferStaticVariableTypesInUnitTask',
createTask,
buildInputs,
<ResultDescriptor>[CREATED_RESOLVED_UNIT9, RESOLVED_UNIT9]);
buildInputs, <ResultDescriptor>[
CREATED_RESOLVED_UNIT9,
RESOLVED_UNIT9,
STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT
]);

/**
* Initialize a newly created task to build a library element for the given
Expand All @@ -3658,13 +3683,16 @@ class InferStaticVariableTypesInUnitTask extends SourceBasedAnalysisTask {
// Prepare inputs.
//
CompilationUnit unit = getRequiredInput(UNIT_INPUT);
List<List<AnalysisError>> errorLists = getRequiredInput(ERRORS_LIST_INPUT);
//
// Record outputs. There is no additional work to be done at this time
// because the work has implicitly been done by virtue of the task model
// preparing all of the inputs.
//
outputs[RESOLVED_UNIT9] = unit;
outputs[CREATED_RESOLVED_UNIT9] = true;
outputs[STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT] =
AnalysisError.mergeLists(errorLists);
}

/**
Expand All @@ -3675,9 +3703,12 @@ class InferStaticVariableTypesInUnitTask extends SourceBasedAnalysisTask {
static Map<String, TaskInput> buildInputs(AnalysisTarget target) {
LibrarySpecificUnit unit = target;
return <String, TaskInput>{
INFERRED_VARIABLES_INPUT: INFERABLE_STATIC_VARIABLES_IN_UNIT
'inferredTypes': INFERABLE_STATIC_VARIABLES_IN_UNIT
.of(unit)
.toListOf(INFERRED_STATIC_VARIABLE),
ERRORS_LIST_INPUT: INFERABLE_STATIC_VARIABLES_IN_UNIT
.of(unit)
.toListOf(STATIC_VARIABLE_RESOLUTION_ERRORS),
UNIT_INPUT: RESOLVED_UNIT8.of(unit)
};
}
Expand Down Expand Up @@ -3716,8 +3747,10 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {
static final TaskDescriptor DESCRIPTOR = new TaskDescriptor(
'InferStaticVariableTypeTask',
createTask,
buildInputs,
<ResultDescriptor>[INFERRED_STATIC_VARIABLE]);
buildInputs, <ResultDescriptor>[
INFERRED_STATIC_VARIABLE,
STATIC_VARIABLE_RESOLUTION_ERRORS
]);

InferStaticVariableTypeTask(
InternalAnalysisContext context, VariableElement variable)
Expand Down Expand Up @@ -3745,17 +3778,19 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {

// If we're not in a dependency cycle, and we have no type annotation,
// re-resolve the right hand side and do inference.
List<AnalysisError> errors = AnalysisError.NO_ERRORS;
if (dependencyCycle == null && variable.hasImplicitType) {
VariableDeclaration declaration = getDeclaration(unit);
//
// 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, AnalysisErrorListener.NULL_LISTENER);
ResolverVisitor visitor = new ResolverVisitor(variable.library,
variable.source, typeProvider, AnalysisErrorListener.NULL_LISTENER,
ResolverVisitor visitor = new ResolverVisitor(
variable.library, variable.source, typeProvider, errorListener,
nameScope: resolutionContext.scope);
if (resolutionContext.enclosingClassDeclaration != null) {
visitor.prepareToResolveMembersInClass(
Expand All @@ -3772,6 +3807,7 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {
newType = typeProvider.dynamicType;
}
setFieldType(variable, newType);
errors = getUniqueErrors(errorListener.errors);
} else {
// TODO(brianwilkerson) For now we simply don't infer any type for
// variables or fields involved in a cycle. We could try to be smarter
Expand All @@ -3784,6 +3820,7 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask {
// Record outputs.
//
outputs[INFERRED_STATIC_VARIABLE] = variable;
outputs[STATIC_VARIABLE_RESOLUTION_ERRORS] = errors;
}

/**
Expand Down Expand Up @@ -3897,6 +3934,12 @@ class LibraryUnitErrorsTask extends SourceBasedAnalysisTask {
*/
static const String LINTS_INPUT = 'LINTS';

/**
* The name of the [STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT] input.
*/
static const String STATIC_VARIABLE_RESOLUTION_ERRORS_INPUT =
'STATIC_VARIABLE_RESOLUTION_ERRORS_INPUT';

/**
* The name of the [STRONG_MODE_ERRORS] input.
*/
Expand Down Expand Up @@ -3958,6 +4001,7 @@ class LibraryUnitErrorsTask extends SourceBasedAnalysisTask {
errorLists.add(getRequiredInput(RESOLVE_TYPE_NAMES_ERRORS_INPUT));
errorLists.add(getRequiredInput(RESOLVE_TYPE_NAMES_ERRORS2_INPUT));
errorLists.add(getRequiredInput(RESOLVE_UNIT_ERRORS_INPUT));
errorLists.add(getRequiredInput(STATIC_VARIABLE_RESOLUTION_ERRORS_INPUT));
errorLists.add(getRequiredInput(STRONG_MODE_ERRORS_INPUT));
errorLists.add(getRequiredInput(VARIABLE_REFERENCE_ERRORS_INPUT));
errorLists.add(getRequiredInput(VERIFY_ERRORS_INPUT));
Expand All @@ -3980,6 +4024,8 @@ class LibraryUnitErrorsTask extends SourceBasedAnalysisTask {
RESOLVE_TYPE_NAMES_ERRORS_INPUT: RESOLVE_TYPE_NAMES_ERRORS.of(unit),
RESOLVE_TYPE_NAMES_ERRORS2_INPUT: RESOLVE_TYPE_BOUNDS_ERRORS.of(unit),
RESOLVE_UNIT_ERRORS_INPUT: RESOLVE_UNIT_ERRORS.of(unit),
STATIC_VARIABLE_RESOLUTION_ERRORS_INPUT:
STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT.of(unit),
STRONG_MODE_ERRORS_INPUT: STRONG_MODE_ERRORS.of(unit),
VARIABLE_REFERENCE_ERRORS_INPUT: VARIABLE_REFERENCE_ERRORS.of(unit),
VERIFY_ERRORS_INPUT: VERIFY_ERRORS.of(unit)
Expand Down
20 changes: 20 additions & 0 deletions pkg/analyzer/test/src/task/dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2999,6 +2999,26 @@ var Y = () {
InterfaceType intType = context.typeProvider.intType;
expect(expression.staticType, intType);
}

test_staticModeHints_forStaticVariableInference() {
context.analysisOptions =
new AnalysisOptionsImpl.from(context.analysisOptions)
..strongModeHints = true;
Source source = newSource(
'/test.dart',
r'''
var V = [42];
''');
LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
computeResult(target, RESOLVED_UNIT9);
expect(outputs[RESOLVED_UNIT9], isNotNull);
expect(outputs[CREATED_RESOLVED_UNIT9], isTrue);
// An INFERRED_TYPE_LITERAL error should be generated.
List<AnalysisError> errors = outputs[
STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT] as List<AnalysisError>;
expect(errors, hasLength(1));
expect(errors[0].errorCode, StrongModeCode.INFERRED_TYPE_LITERAL);
}
}

@reflectiveTest
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/tool/task_dependency_graph/tasks.dot
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ digraph G {
InferInstanceMembersInUnitTask -> CREATED_RESOLVED_UNIT11
InferInstanceMembersInUnitTask -> RESOLVED_UNIT11
InferStaticVariableTypeTask -> INFERRED_STATIC_VARIABLE
InferStaticVariableTypeTask -> STATIC_VARIABLE_RESOLUTION_ERRORS
InferStaticVariableTypesInUnitTask -> CREATED_RESOLVED_UNIT9
InferStaticVariableTypesInUnitTask -> RESOLVED_UNIT9
InferStaticVariableTypesInUnitTask -> STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT
LIBRARY_CYCLE [shape=box]
LIBRARY_CYCLE_DEPENDENCIES -> InferInstanceMembersInUnitTask
LIBRARY_CYCLE_DEPENDENCIES -> InferStaticVariableTypeTask
Expand Down Expand Up @@ -322,6 +324,10 @@ digraph G {
SCAN_ERRORS [shape=box]
SOURCE_KIND -> BuildDirectiveElementsTask
SOURCE_KIND [shape=box]
STATIC_VARIABLE_RESOLUTION_ERRORS -> InferStaticVariableTypesInUnitTask
STATIC_VARIABLE_RESOLUTION_ERRORS [shape=box]
STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT -> LibraryUnitErrorsTask
STATIC_VARIABLE_RESOLUTION_ERRORS_IN_UNIT [shape=box]
STRONG_MODE_ERRORS -> LibraryUnitErrorsTask
STRONG_MODE_ERRORS [shape=box]
ScanDartTask -> IGNORE_INFO
Expand Down

0 comments on commit 296d81f

Please sign in to comment.