Skip to content

Commit

Permalink
Verify constants using Analyzer.
Browse files Browse the repository at this point in the history
This fixes 44 failing language_2 tests.

Change-Id: I9248a940c56764d030e5cb1c8d558a3d41b88899
Reviewed-on: https://dart-review.googlesource.com/68340
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and commit-bot@chromium.org committed Aug 3, 2018
1 parent b83072c commit 8692936
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 520 deletions.
70 changes: 31 additions & 39 deletions pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class LibraryAnalyzer {
_computePendingMissingRequiredParameters(file, unit);
});

units.values.forEach(_findConstants);
_computeConstants();

PerformanceStatistics.errors.makeCurrentWhile(() {
Expand Down Expand Up @@ -229,8 +230,14 @@ class LibraryAnalyzer {
});
});

units.values.forEach(_findConstants);
_computeConstants();

units.forEach((file, unit) {
ErrorReporter errorReporter = _getErrorReporter(file);
_computeConstantErrors(errorReporter, unit);
});

if (_analysisOptions.hint) {
PerformanceStatistics.hints.makeCurrentWhile(() {
units.forEach((file, unit) {
Expand Down Expand Up @@ -275,6 +282,13 @@ class LibraryAnalyzer {
});
}

void _computeConstantErrors(
ErrorReporter errorReporter, CompilationUnit unit) {
ConstantVerifier constantVerifier = new ConstantVerifier(
errorReporter, _libraryElement, _typeProvider, _declaredVariables);
unit.accept(constantVerifier);
}

/**
* Compute [_constants] in all units.
*/
Expand Down Expand Up @@ -428,9 +442,7 @@ class LibraryAnalyzer {
//
// Use the ConstantVerifier to compute errors.
//
ConstantVerifier constantVerifier = new ConstantVerifier(
errorReporter, _libraryElement, _typeProvider, _declaredVariables);
unit.accept(constantVerifier);
_computeConstantErrors(errorReporter, unit);

//
// Use the ErrorVerifier to compute errors.
Expand Down Expand Up @@ -470,6 +482,17 @@ class LibraryAnalyzer {
return errors.where((AnalysisError e) => !isIgnored(e)).toList();
}

/// Find constants to compute.
void _findConstants(CompilationUnit unit) {
ConstantFinder constantFinder = new ConstantFinder();
unit.accept(constantFinder);
_constants.addAll(constantFinder.constantsToCompute);

var dependenciesFinder = new ConstantExpressionsDependenciesFinder();
unit.accept(dependenciesFinder);
_constants.addAll(dependenciesFinder.dependencies);
}

RecordingErrorListener _getErrorListener(FileState file) =>
_errorListeners.putIfAbsent(file, () => new RecordingErrorListener());

Expand Down Expand Up @@ -709,24 +732,6 @@ class LibraryAnalyzer {

unit.accept(new ResolverVisitor(
_libraryElement, source, _typeProvider, errorListener));

//
// Find constants to compute.
//
{
ConstantFinder constantFinder = new ConstantFinder();
unit.accept(constantFinder);
_constants.addAll(constantFinder.constantsToCompute);
}

//
// Find constant dependencies to compute.
//
{
var finder = new ConstantExpressionsDependenciesFinder();
unit.accept(finder);
_constants.addAll(finder.dependencies);
}
}

void _resolveFile2(FileState file, CompilationUnitImpl unit,
Expand Down Expand Up @@ -847,30 +852,17 @@ class LibraryAnalyzer {
variableList.type?.accept(applier);
for (var variable in variables.reversed) {
variable.initializer?.accept(applier);
var element = variable.declaredElement;
if (element is ConstVariableElement) {
(element as ConstVariableElement).constantInitializer =
variable.initializer;
}
}
applier.applyToAnnotations(declaration);
} else {
throw new StateError('(${declaration.runtimeType}) $declaration');
}
}

//
// Find constants to compute.
//
{
ConstantFinder constantFinder = new ConstantFinder();
unit.accept(constantFinder);
_constants.addAll(constantFinder.constantsToCompute);
}

//
// Find constant dependencies to compute.
//
{
var finder = new ConstantExpressionsDependenciesFinder();
unit.accept(finder);
_constants.addAll(finder.dependencies);
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion pkg/analyzer/lib/src/fasta/resolution_applier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,14 @@ class ResolutionApplier extends GeneralizingAstVisitor {
node.name.staticElement = element;
element.type = type;
}

node.initializer?.accept(this);

if (element is ConstVariableElement) {
(element as ConstVariableElement).constantInitializer =
node.initializer;
}
}
node.initializer?.accept(this);
}

@override
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/kernel/resynthesize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ class _ExprBuilder {
var arguments = _toArguments(expr.arguments);
var creation = AstTestFactory.instanceCreationExpression(
keyword, constructorName, arguments);
creation.staticElement = element;
creation.staticElement = ConstructorMember.from(element, type);
creation.staticType = type;
return creation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ class CheckedModeCompileTimeErrorCodeTest_Kernel
@override
bool get usingFastaParser => true;

@failingTest
@override
test_assertion_throws() async {
// Not yet generating errors in kernel mode.
await super.test_assertion_throws();
}

@failingTest
@override
test_fieldFormalParameterAssignableToField_fieldType_unresolved_null() async {
Expand All @@ -39,29 +32,6 @@ class CheckedModeCompileTimeErrorCodeTest_Kernel
.test_fieldFormalParameterAssignableToField_fieldType_unresolved_null();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0;
// 1 errors of type StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, found 0
await super.test_fieldFormalParameterNotAssignableToField();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_extends() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0
await super.test_fieldFormalParameterNotAssignableToField_extends();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_fieldType() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0;
// 1 errors of type StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, found 0
await super.test_fieldFormalParameterNotAssignableToField_fieldType();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_fieldType_unresolved() async {
Expand All @@ -71,71 +41,13 @@ class CheckedModeCompileTimeErrorCodeTest_Kernel
.test_fieldFormalParameterNotAssignableToField_fieldType_unresolved();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_implements() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0
await super.test_fieldFormalParameterNotAssignableToField_implements();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_list() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0
await super.test_fieldFormalParameterNotAssignableToField_list();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_map_keyMismatch() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0
await super.test_fieldFormalParameterNotAssignableToField_map_keyMismatch();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_map_valueMismatch() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0
await super
.test_fieldFormalParameterNotAssignableToField_map_valueMismatch();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_optional() async {
// UnimplementedError: kernel: (Let) let final dynamic #t1 = "foo" in let ...
await super.test_fieldFormalParameterNotAssignableToField_optional();
}

@failingTest
@override
test_fieldFormalParameterNotAssignableToField_typedef() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0;
// 1 errors of type StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, found 0
await super.test_fieldFormalParameterNotAssignableToField_typedef();
}

@failingTest
@override
test_fieldInitializerNotAssignable() async {
// UnimplementedError: kernel: (Let) let final dynamic #t2 = "" in let ...
await super.test_fieldInitializerNotAssignable();
}

@failingTest
@override
test_fieldTypeMismatch() async {
// UnimplementedError: kernel: (AsExpression) x as{TypeError} dart.core::int
await super.test_fieldTypeMismatch();
}

@failingTest
@override
test_fieldTypeMismatch_generic() async {
// UnimplementedError: kernel: (Let) let final dynamic #t3 = #lib1::y in let ...
await super.test_fieldTypeMismatch_generic();
}

@failingTest
@override
test_fieldTypeMismatch_unresolved() async {
Expand All @@ -158,12 +70,6 @@ class CheckedModeCompileTimeErrorCodeTest_Kernel
await super.test_listElementTypeNotAssignable();
}

@failingTest
@FastaProblem('https://github.com/dart-lang/sdk/issues/31936')
@override
test_listLiteral_inferredElementType() async =>
super.test_listLiteral_inferredElementType();

@failingTest
@override
test_mapKeyTypeNotAssignable() async {
Expand All @@ -172,18 +78,6 @@ class CheckedModeCompileTimeErrorCodeTest_Kernel
await super.test_mapKeyTypeNotAssignable();
}

@failingTest
@FastaProblem('https://github.com/dart-lang/sdk/issues/31936')
@override
test_mapLiteral_inferredKeyType() async =>
super.test_mapLiteral_inferredKeyType();

@failingTest
@FastaProblem('https://github.com/dart-lang/sdk/issues/31936')
@override
test_mapLiteral_inferredValueType() async =>
super.test_mapLiteral_inferredValueType();

@failingTest
@override
test_mapValueTypeNotAssignable() async {
Expand All @@ -199,22 +93,6 @@ class CheckedModeCompileTimeErrorCodeTest_Kernel
await super.test_parameterAssignable_undefined_null();
}

@failingTest
@override
test_parameterNotAssignable() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0;
// 1 errors of type StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, found 0
await super.test_parameterNotAssignable();
}

@failingTest
@override
test_parameterNotAssignable_typeSubstitution() async {
// Expected 1 errors of type CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_PARAM_TYPE_MISMATCH, found 0;
// 1 errors of type StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE, found 0
await super.test_parameterNotAssignable_typeSubstitution();
}

@failingTest
@override
test_parameterNotAssignable_undefined() async {
Expand All @@ -223,34 +101,13 @@ class CheckedModeCompileTimeErrorCodeTest_Kernel
await super.test_parameterNotAssignable_undefined();
}

@failingTest
@override
test_redirectingConstructor_paramTypeMismatch() async {
// Bad state: Expected element reference for analyzer offset 33; got one for kernel offset 36
await super.test_redirectingConstructor_paramTypeMismatch();
}

@failingTest
@override
test_superConstructor_paramTypeMismatch() async {
// UnimplementedError: kernel: (AsExpression) d as{TypeError} dart.core::double
await super.test_superConstructor_paramTypeMismatch();
}

@failingTest
@override
test_topLevelVarAssignable_undefined_null() async {
// Expected 1 errors of type StaticWarningCode.UNDEFINED_CLASS, found 0
await super.test_topLevelVarAssignable_undefined_null();
}

@failingTest
@override
test_topLevelVarNotAssignable() async {
// UnimplementedError: kernel: (Let) let final dynamic #t5 = "foo" in let ...
await super.test_topLevelVarNotAssignable();
}

@failingTest
@override
test_topLevelVarNotAssignable_undefined() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,19 @@ const int y = 1;
var v = const C<String>();
''');
await computeAnalysisResult(source);
assertErrors(source, [
CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH,
StaticTypeWarningCode.INVALID_ASSIGNMENT
]);
assertErrors(
source,
useCFE
? [
CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE,
StaticTypeWarningCode.INVALID_ASSIGNMENT
]
: [
CheckedModeCompileTimeErrorCode
.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH,
StaticTypeWarningCode.INVALID_ASSIGNMENT
],
);
verify([source]);
}

Expand Down Expand Up @@ -451,7 +460,15 @@ const int y = 1;
var v = const C<int>();
''');
await computeAnalysisResult(source);
assertErrors(source, [StaticTypeWarningCode.INVALID_ASSIGNMENT]);
assertErrors(
source,
useCFE
? [
CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE,
StaticTypeWarningCode.INVALID_ASSIGNMENT
]
: [StaticTypeWarningCode.INVALID_ASSIGNMENT],
);
verify([source]);
}

Expand Down
Loading

0 comments on commit 8692936

Please sign in to comment.