Skip to content

Commit fa58bd7

Browse files
authored
Revert "Const finder fixes (flutter#15880)"
This reverts commit a431488.
1 parent 3f14964 commit fa58bd7

File tree

8 files changed

+57
-117
lines changed

8 files changed

+57
-117
lines changed

tools/const_finder/bin/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ void main(List<String> args) {
4343
valueHelp: 'path/to/main.dill',
4444
help: 'The path to a kernel file to parse, which was created from the '
4545
'main-package-uri library.')
46+
..addOption('main-library-uri',
47+
help: 'The package: URI to treat as the main entrypoint library '
48+
'(the same package used to create the kernel-file).',
49+
valueHelp: 'package:hello_world/main.dart')
4650
..addOption('class-library-uri',
4751
help: 'The package: URI of the class to find.',
4852
valueHelp: 'package:flutter/src/widgets/icon_data.dart')
@@ -69,6 +73,7 @@ void main(List<String> args) {
6973

7074
final ConstFinder finder = ConstFinder(
7175
kernelFilePath: getArg<String>('kernel-file'),
76+
targetLibraryUri: getArg<String>('main-library-uri'),
7277
classLibraryUri: getArg<String>('class-library-uri'),
7378
className: getArg<String>('class-name'),
7479
);

tools/const_finder/lib/const_finder.dart

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@ import 'package:meta/meta.dart';
88
class _ConstVisitor extends RecursiveVisitor<void> {
99
_ConstVisitor(
1010
this.kernelFilePath,
11+
this.targetLibraryUri,
1112
this.classLibraryUri,
1213
this.className,
1314
) : assert(kernelFilePath != null),
15+
assert(targetLibraryUri != null),
1416
assert(classLibraryUri != null),
1517
assert(className != null),
16-
_visitedInstnaces = <String>{},
1718
constantInstances = <Map<String, dynamic>>[],
1819
nonConstantLocations = <Map<String, dynamic>>[];
1920

2021
/// The path to the file to open.
2122
final String kernelFilePath;
2223

24+
/// The library URI for the main entrypoint of the target library.
25+
final String targetLibraryUri;
26+
2327
/// The library URI for the class to find.
2428
final String classLibraryUri;
2529

2630
/// The name of the class to find.
2731
final String className;
2832

29-
final Set<String> _visitedInstnaces;
3033
final List<Map<String, dynamic>> constantInstances;
3134
final List<Map<String, dynamic>> nonConstantLocations;
3235

@@ -40,7 +43,6 @@ class _ConstVisitor extends RecursiveVisitor<void> {
4043
final Class parentClass = node.target.parent as Class;
4144
if (!_matches(parentClass)) {
4245
super.visitConstructorInvocation(node);
43-
return;
4446
}
4547
nonConstantLocations.add(<String, dynamic>{
4648
'file': node.location.file.toString(),
@@ -51,22 +53,15 @@ class _ConstVisitor extends RecursiveVisitor<void> {
5153

5254
@override
5355
void visitInstanceConstantReference(InstanceConstant node) {
54-
node.fieldValues.values.whereType<InstanceConstant>().forEach(visitInstanceConstantReference);
5556
if (!_matches(node.classNode)) {
56-
super.visitInstanceConstantReference(node);
5757
return;
5858
}
5959
final Map<String, dynamic> instance = <String, dynamic>{};
6060
for (MapEntry<Reference, Constant> kvp in node.fieldValues.entries) {
61-
if (kvp.value is! PrimitiveConstant<dynamic>) {
62-
continue;
63-
}
6461
final PrimitiveConstant<dynamic> value = kvp.value as PrimitiveConstant<dynamic>;
6562
instance[kvp.key.canonicalName.name] = value.value;
6663
}
67-
if (_visitedInstnaces.add(instance.toString())) {
68-
constantInstances.add(instance);
69-
}
64+
constantInstances.add(instance);
7065
}
7166
}
7267

@@ -76,27 +71,53 @@ class ConstFinder {
7671
/// be null.
7772
///
7873
/// The `kernelFilePath` is the path to a dill (kernel) file to process.
74+
///
75+
/// The `targetLibraryUri` is the `package:` URI of the main entrypoint to
76+
/// search from.
77+
///
78+
///
79+
///
7980
ConstFinder({
8081
@required String kernelFilePath,
82+
@required String targetLibraryUri,
8183
@required String classLibraryUri,
8284
@required String className,
8385
}) : _visitor = _ConstVisitor(
8486
kernelFilePath,
87+
targetLibraryUri,
8588
classLibraryUri,
8689
className,
8790
);
8891

8992
final _ConstVisitor _visitor;
9093

94+
Library _getRoot() {
95+
final Component binary = loadComponentFromBinary(_visitor.kernelFilePath);
96+
return binary.libraries.firstWhere(
97+
(Library library) => library.canonicalName.name == _visitor.targetLibraryUri,
98+
orElse: () => throw LibraryNotFoundException._(_visitor.targetLibraryUri),
99+
);
100+
}
101+
91102
/// Finds all instances
92103
Map<String, dynamic> findInstances() {
93-
_visitor._visitedInstnaces.clear();
94-
for (Library library in loadComponentFromBinary(_visitor.kernelFilePath).libraries) {
95-
library.visitChildren(_visitor);
96-
}
104+
final Library root = _getRoot();
105+
root.visitChildren(_visitor);
97106
return <String, dynamic>{
98107
'constantInstances': _visitor.constantInstances,
99108
'nonConstantLocations': _visitor.nonConstantLocations,
100109
};
101110
}
102111
}
112+
113+
/// Exception thrown by [ConstFinder.findInstances] when the target library
114+
/// is not found.
115+
class LibraryNotFoundException implements Exception {
116+
const LibraryNotFoundException._(this.targetLibraryUri);
117+
118+
/// The library target URI that could not be found.
119+
final String targetLibraryUri;
120+
121+
@override
122+
String toString() => 'Could not find target library for "$targetLibraryUri".';
123+
}

tools/const_finder/test/const_finder_test.dart

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void _checkConsts() {
3434
print('Checking for expected constants.');
3535
final ConstFinder finder = ConstFinder(
3636
kernelFilePath: constsDill,
37+
targetLibraryUri: 'package:const_finder_fixtures/consts.dart',
3738
classLibraryUri: 'package:const_finder_fixtures/target.dart',
3839
className: 'Target',
3940
);
@@ -42,15 +43,8 @@ void _checkConsts() {
4243
jsonEncode(finder.findInstances()),
4344
jsonEncode(<String, dynamic>{
4445
'constantInstances': <Map<String, dynamic>>[
45-
<String, dynamic>{'stringValue': '1', 'intValue': 1, 'targetValue': null},
46-
<String, dynamic>{'stringValue': '4', 'intValue': 4, 'targetValue': null},
47-
<String, dynamic>{'stringValue': '2', 'intValue': 2},
48-
<String, dynamic>{'stringValue': '6', 'intValue': 6, 'targetValue': null},
49-
<String, dynamic>{'stringValue': '8', 'intValue': 8, 'targetValue': null},
50-
<String, dynamic>{'stringValue': '10', 'intValue': 10, 'targetValue': null},
51-
<String, dynamic>{'stringValue': '9', 'intValue': 9},
52-
<String, dynamic>{'stringValue': '7', 'intValue': 7, 'targetValue': null},
53-
<String, dynamic>{'stringValue': 'package', 'intValue':-1, 'targetValue': null},
46+
<String, dynamic>{'stringValue': '1', 'intValue': 1},
47+
<String, dynamic>{'stringValue': '2', 'intValue': 2}
5448
],
5549
'nonConstantLocations': <dynamic>[],
5650
}),
@@ -61,6 +55,7 @@ void _checkNonConsts() {
6155
print('Checking for non-constant instances.');
6256
final ConstFinder finder = ConstFinder(
6357
kernelFilePath: constsAndNonDill,
58+
targetLibraryUri: 'package:const_finder_fixtures/consts_and_non.dart',
6459
classLibraryUri: 'package:const_finder_fixtures/target.dart',
6560
className: 'Target',
6661
);
@@ -69,34 +64,19 @@ void _checkNonConsts() {
6964
jsonEncode(finder.findInstances()),
7065
jsonEncode(<String, dynamic>{
7166
'constantInstances': <dynamic>[
72-
<String, dynamic>{'stringValue': '1', 'intValue': 1, 'targetValue': null},
73-
<String, dynamic>{'stringValue': '6', 'intValue': 6, 'targetValue': null},
74-
<String, dynamic>{'stringValue': '8', 'intValue': 8, 'targetValue': null},
75-
<String, dynamic>{'stringValue': '10', 'intValue': 10, 'targetValue': null},
76-
<String, dynamic>{'stringValue': '9', 'intValue': 9},
77-
<String, dynamic>{'stringValue': '7', 'intValue': 7, 'targetValue': null},
67+
<String, dynamic>{'stringValue': '1', 'intValue': 1}
7868
],
7969
'nonConstantLocations': <dynamic>[
8070
<String, dynamic>{
8171
'file': 'file://$fixtures/lib/consts_and_non.dart',
82-
'line': 14,
83-
'column': 26,
84-
},
85-
<String, dynamic>{
86-
'file': 'file://$fixtures/lib/consts_and_non.dart',
87-
'line': 17,
88-
'column': 26,
72+
'line': 12,
73+
'column': 26
8974
},
9075
<String, dynamic>{
9176
'file': 'file://$fixtures/lib/consts_and_non.dart',
92-
'line': 19,
93-
'column': 26,
77+
'line': 14,
78+
'column': 26
9479
},
95-
<String, dynamic>{
96-
'file': 'file://$fixtures/pkg/package.dart',
97-
'line': 10,
98-
'column': 25,
99-
}
10080
]
10181
}),
10282
);
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# Generated by pub on 2020-01-15 10:08:29.776333.
22
const_finder_fixtures:lib/
3-
const_finder_fixtures_package:pkg/

tools/const_finder/test/fixtures/lib/consts.dart

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,14 @@
44

55
import 'dart:core';
66

7-
import 'package:const_finder_fixtures_package/package.dart';
8-
97
import 'target.dart';
108

119
void main() {
12-
const Target target1 = Target('1', 1, null);
13-
const Target target2 = Target('2', 2, Target('4', 4, null));
10+
const Target target1 = Target('1', 1);
11+
const Target target2 = Target('2', 2);
1412
// ignore: unused_local_variable
15-
const Target target3 = Target('3', 3, Target('5', 5, null)); // should be tree shaken out.
13+
const Target target3 = Target('3', 3); // should be tree shaken out.
1614
target1.hit();
1715
target2.hit();
18-
19-
blah(const Target('6', 6, null));
20-
21-
const IgnoreMe ignoreMe = IgnoreMe(Target('7', 7, null)); // IgnoreMe is ignored but 7 is not.
22-
// ignore: prefer_const_constructors
23-
final IgnoreMe ignoreMe2 = IgnoreMe(const Target('8', 8, null));
24-
// ignore: prefer_const_constructors
25-
final IgnoreMe ignoreMe3 = IgnoreMe(const Target('9', 9, Target('10', 10, null)));
26-
print(ignoreMe);
27-
print(ignoreMe2);
28-
print(ignoreMe3);
29-
30-
createTargetInPackage();
3116
}
3217

33-
class IgnoreMe {
34-
const IgnoreMe([this.target]);
35-
36-
final Target target;
37-
}
38-
39-
void blah(Target target) {
40-
print(target);
41-
}

tools/const_finder/test/fixtures/lib/consts_and_non.dart

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,13 @@
55
// ignore_for_file: prefer_const_constructors
66
import 'dart:core';
77

8-
import 'package:const_finder_fixtures_package/package.dart';
9-
108
import 'target.dart';
119

1210
void main() {
13-
const Target target1 = Target('1', 1, null);
14-
final Target target2 = Target('2', 2, const Target('4', 4, null));
15-
16-
// ignore: unused_local_variable
17-
final Target target3 = Target('3', 3, Target('5', 5, null)); // should be tree shaken out.
11+
const Target target1 = Target('1', 1);
12+
final Target target2 = Target('2', 2);
1813
// ignore: unused_local_variable
19-
final Target target6 = Target('6', 6, null); // should be tree shaken out.
14+
final Target target3 = Target('3', 3); // should be tree shaken out.
2015
target1.hit();
2116
target2.hit();
22-
23-
blah(const Target('6', 6, null));
24-
25-
const IgnoreMe ignoreMe = IgnoreMe(Target('7', 7, null)); // IgnoreMe is ignored but 7 is not.
26-
// ignore: prefer_const_constructors
27-
final IgnoreMe ignoreMe2 = IgnoreMe(const Target('8', 8, null));
28-
// ignore: prefer_const_constructors
29-
final IgnoreMe ignoreMe3 = IgnoreMe(const Target('9', 9, Target('10', 10, null)));
30-
print(ignoreMe);
31-
print(ignoreMe2);
32-
print(ignoreMe3);
33-
34-
createNonConstTargetInPackage();
35-
}
36-
37-
class IgnoreMe {
38-
const IgnoreMe([this.target]);
39-
40-
final Target target;
41-
}
42-
43-
void blah(Target target) {
44-
print(target);
4517
}

tools/const_finder/test/fixtures/lib/target.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
// found in the LICENSE file.
44

55
class Target {
6-
const Target(this.stringValue, this.intValue, this.targetValue);
6+
const Target(this.stringValue, this.intValue);
77

88
final String stringValue;
99
final int intValue;
10-
final Target targetValue;
1110

1211
void hit() {
1312
print('$stringValue $intValue');

tools/const_finder/test/fixtures/pkg/package.dart

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)