Skip to content

Commit 001f64b

Browse files
author
Brian Slesinsky
committed
print nicer errors for a const dependency graph failure
BUG= [email protected], [email protected] Review URL: https://codereview.chromium.org/1618103002 .
1 parent 1ecd06d commit 001f64b

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

pkg/analyzer/test/generated/constant_test.dart

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,50 @@ main() {
4343
*/
4444
class ConstantEvaluationValidator_ForTest
4545
implements ConstantEvaluationValidator {
46+
final InternalAnalysisContext context;
4647
ConstantValueComputer computer;
47-
4848
ConstantEvaluationTarget _nodeBeingEvaluated;
4949

50+
ConstantEvaluationValidator_ForTest(this.context);
51+
5052
@override
5153
void beforeComputeValue(ConstantEvaluationTarget constant) {
5254
_nodeBeingEvaluated = constant;
5355
}
5456

5557
@override
56-
void beforeGetConstantInitializers(ConstructorElement constructor) {
57-
// Make sure we properly recorded the dependency.
58-
expect(
59-
computer.referenceGraph.containsPath(_nodeBeingEvaluated, constructor),
60-
isTrue);
61-
}
58+
void beforeGetConstantInitializers(ConstructorElement constructor) =>
59+
_checkPathTo(constructor);
6260

6361
@override
64-
void beforeGetEvaluationResult(ConstantEvaluationTarget constant) {
65-
// Make sure we properly recorded the dependency.
66-
expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, constant),
67-
isTrue);
68-
}
62+
void beforeGetEvaluationResult(ConstantEvaluationTarget constant) =>
63+
_checkPathTo(constant);
6964

7065
@override
71-
void beforeGetFieldEvaluationResult(FieldElementImpl field) {
72-
// Make sure we properly recorded the dependency.
73-
expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, field),
74-
isTrue);
75-
}
66+
void beforeGetFieldEvaluationResult(FieldElementImpl field) =>
67+
_checkPathTo(field);
7668

7769
@override
78-
void beforeGetParameterDefault(ParameterElement parameter) {
79-
// Make sure we properly recorded the dependency.
80-
expect(computer.referenceGraph.containsPath(_nodeBeingEvaluated, parameter),
81-
isTrue);
70+
void beforeGetParameterDefault(ParameterElement parameter) =>
71+
_checkPathTo(parameter);
72+
73+
void _checkPathTo(ConstantEvaluationTarget target) {
74+
if (computer.referenceGraph.containsPath(_nodeBeingEvaluated, target)) {
75+
return; // pass
76+
}
77+
// print a nice error message on failure
78+
StringBuffer out = new StringBuffer();
79+
out.writeln("missing path in constant dependency graph");
80+
out.writeln("from $_nodeBeingEvaluated to $target");
81+
for (var s in context.analysisCache.sources) {
82+
String text = context.getContents(s).data;
83+
if (text != "") {
84+
out.writeln('''
85+
=== ${s.shortName}
86+
$text''');
87+
}
88+
}
89+
fail(out.toString());
8290
}
8391
}
8492

@@ -851,17 +859,17 @@ class C {
851859
TestLogger logger = new TestLogger();
852860
AnalysisEngine.instance.logger = logger;
853861
try {
854-
Source librarySource = addSource(r'''
862+
Source source = addSource(r'''
855863
const int a = c;
856864
const int b = a;
857865
const int c = b;''');
858-
LibraryElement libraryElement = resolve2(librarySource);
866+
LibraryElement libraryElement = resolve2(source);
859867
CompilationUnit unit =
860-
analysisContext.resolveCompilationUnit(librarySource, libraryElement);
861-
analysisContext.computeErrors(librarySource);
868+
analysisContext.resolveCompilationUnit(source, libraryElement);
869+
analysisContext.computeErrors(source);
862870
expect(unit, isNotNull);
863871
ConstantValueComputer computer = _makeConstantValueComputer();
864-
computer.add(unit, librarySource, librarySource);
872+
computer.add(unit, source, source);
865873
computer.computeValues();
866874
NodeList<CompilationUnitMember> members = unit.declarations;
867875
expect(members, hasLength(3));
@@ -874,15 +882,15 @@ class C {
874882
}
875883

876884
void test_computeValues_dependentVariables() {
877-
Source librarySource = addSource(r'''
885+
Source source = addSource(r'''
878886
const int b = a;
879887
const int a = 0;''');
880-
LibraryElement libraryElement = resolve2(librarySource);
888+
LibraryElement libraryElement = resolve2(source);
881889
CompilationUnit unit =
882-
analysisContext.resolveCompilationUnit(librarySource, libraryElement);
890+
analysisContext.resolveCompilationUnit(source, libraryElement);
883891
expect(unit, isNotNull);
884892
ConstantValueComputer computer = _makeConstantValueComputer();
885-
computer.add(unit, librarySource, librarySource);
893+
computer.add(unit, source, source);
886894
computer.computeValues();
887895
NodeList<CompilationUnitMember> members = unit.declarations;
888896
expect(members, hasLength(2));
@@ -933,30 +941,30 @@ const int d = c;''');
933941
}
934942

935943
void test_computeValues_singleVariable() {
936-
Source librarySource = addSource("const int a = 0;");
937-
LibraryElement libraryElement = resolve2(librarySource);
944+
Source source = addSource("const int a = 0;");
945+
LibraryElement libraryElement = resolve2(source);
938946
CompilationUnit unit =
939-
analysisContext.resolveCompilationUnit(librarySource, libraryElement);
947+
analysisContext.resolveCompilationUnit(source, libraryElement);
940948
expect(unit, isNotNull);
941949
ConstantValueComputer computer = _makeConstantValueComputer();
942-
computer.add(unit, librarySource, librarySource);
950+
computer.add(unit, source, source);
943951
computer.computeValues();
944952
NodeList<CompilationUnitMember> members = unit.declarations;
945953
expect(members, hasLength(1));
946954
_validate(true, (members[0] as TopLevelVariableDeclaration).variables);
947955
}
948956

949957
void test_computeValues_value_depends_on_enum() {
950-
Source librarySource = addSource('''
958+
Source source = addSource('''
951959
enum E { id0, id1 }
952960
const E e = E.id0;
953961
''');
954-
LibraryElement libraryElement = resolve2(librarySource);
962+
LibraryElement libraryElement = resolve2(source);
955963
CompilationUnit unit =
956-
analysisContext.resolveCompilationUnit(librarySource, libraryElement);
964+
analysisContext.resolveCompilationUnit(source, libraryElement);
957965
expect(unit, isNotNull);
958966
ConstantValueComputer computer = _makeConstantValueComputer();
959-
computer.add(unit, librarySource, librarySource);
967+
computer.add(unit, source, source);
960968
computer.computeValues();
961969
TopLevelVariableDeclaration declaration = unit.declarations
962970
.firstWhere((member) => member is TopLevelVariableDeclaration);
@@ -1963,7 +1971,7 @@ class A {
19631971

19641972
ConstantValueComputer _makeConstantValueComputer() {
19651973
ConstantEvaluationValidator_ForTest validator =
1966-
new ConstantEvaluationValidator_ForTest();
1974+
new ConstantEvaluationValidator_ForTest(analysisContext2);
19671975
validator.computer = new ConstantValueComputer(
19681976
analysisContext2,
19691977
analysisContext2.typeProvider,

0 commit comments

Comments
 (0)