[const_finder] Ignore constructor invocations from generated tear-off declarations (flutter/engine#38131)

* fix

* ignore tear off declarations

* add TODO comment

* clean up

* fix analysis
This commit is contained in:
Christopher Fujino
2022-12-09 15:06:08 -08:00
committed by GitHub
parent e057132b24
commit f92da9d18f
6 changed files with 108 additions and 16 deletions

View File

@@ -32,6 +32,33 @@ class _ConstVisitor extends RecursiveVisitor<void> {
bool inIgnoredClass = false;
/// Whether or not we are currently within the declaration of the target class.
///
/// We use this to determine when to skip tracking non-constant
/// [ConstructorInvocation]s. This is because, in web builds, a static
/// method is always created called _#new#tearOff() which returns the result
/// of a non-constant invocation of the unnamed constructor.
///
/// For the following Dart class "FooBar":
///
/// class FooBar {
/// const FooBar();
/// }
///
/// The following kernel structure is generated:
///
/// class FooBar extends core::Object /*hasConstConstructor*/ {
/// const constructor •() → min::FooBar
/// : super core::Object::•()
/// ;
/// static method _#new#tearOff() → min::FooBar
/// return new min::FooBar::•(); /* this is a non-const constructor invocation */
/// method noOp() → void {}
/// }
bool inTargetClass = false;
bool inTargetTearOff = false;
/// The name of the name of the class of the annotation marking classes
/// whose constant references should be ignored.
final String? annotationClassName;
@@ -58,6 +85,18 @@ class _ConstVisitor extends RecursiveVisitor<void> {
// Avoid visiting the same constant more than once.
final Set<Constant> _cache = LinkedHashSet<Constant>.identity();
@override
void visitProcedure(Procedure node) {
final bool isTearOff = node.isStatic &&
node.kind == ProcedureKind.Method &&
node.name.text == '_#new#tearOff';
if (inTargetClass && isTearOff) {
inTargetTearOff = true;
}
super.visitProcedure(node);
inTargetTearOff = false;
}
@override
void defaultConstant(Constant node) {
if (_cache.add(node)) {
@@ -73,7 +112,7 @@ class _ConstVisitor extends RecursiveVisitor<void> {
@override
void visitConstructorInvocation(ConstructorInvocation node) {
final Class parentClass = node.target.parent! as Class;
if (_matches(parentClass)) {
if (!inTargetTearOff && _matches(parentClass)) {
final Location location = node.location!;
nonConstantLocations.add(<String, dynamic>{
'file': location.file.toString(),
@@ -86,9 +125,11 @@ class _ConstVisitor extends RecursiveVisitor<void> {
@override
void visitClass(Class node) {
inTargetClass = _matches(node);
// check if this is a class that we should ignore
inIgnoredClass = _classShouldBeIgnored(node);
super.visitClass(node);
inTargetClass = false;
inIgnoredClass = false;
}

View File

@@ -6,7 +6,7 @@ name: const_finder
publish_to: none
environment:
sdk: ">=2.17.0 <3.0.0"
sdk: ">=2.17.0 <4.0.0"
# Do not add any dependencies that require more than what is provided in
# //third_party/dart/pkg or //third_party/dart/third_party/pkg.

View File

@@ -31,10 +31,6 @@ void expectInstances(dynamic value, dynamic expected, Compiler compiler) {
final Equality<Object?> equality;
if (compiler == Compiler.dart2js) {
// Ignore comparing nonConstantLocations in web dills because it will have
// extra fields.
(value as Map<String, Object?>).remove('nonConstantLocations');
(expected as Map<String, Object?>).remove('nonConstantLocations');
equality = const Dart2JSDeepCollectionEquality();
} else {
equality = const DeepCollectionEquality();
@@ -69,9 +65,7 @@ void _checkConsts(String dillPath, Compiler compiler) {
classLibraryUri: 'package:const_finder_fixtures/target.dart',
className: 'Target',
);
expectInstances(
finder.findInstances(),
<String, dynamic>{
final Map<String, Object?> expectation = <String, dynamic>{
'constantInstances': <Map<String, dynamic>>[
<String, dynamic>{'stringValue': '100', 'intValue': 100, 'targetValue': null},
<String, dynamic>{'stringValue': '102', 'intValue': 102, 'targetValue': null},
@@ -95,7 +89,27 @@ void _checkConsts(String dillPath, Compiler compiler) {
<String, dynamic>{'stringValue': 'package', 'intValue':-1, 'targetValue': null},
],
'nonConstantLocations': <dynamic>[],
},
};
if (compiler == Compiler.aot) {
expectation['nonConstantLocations'] = <Object?>[];
} else {
final String fixturesUrl = Platform.isWindows
? '/$fixtures'.replaceAll(Platform.pathSeparator, '/')
: fixtures;
// Without true tree-shaking, there is a non-const reference in a
// never-invoked function that will be present in the dill.
expectation['nonConstantLocations'] = <Object?>[
<String, dynamic>{
'file': 'file://$fixturesUrl/pkg/package.dart',
'line': 14,
'column': 25,
},
];
}
expectInstances(
finder.findInstances(),
expectation,
compiler,
);
@@ -125,8 +139,9 @@ void _checkAnnotation(String dillPath, Compiler compiler) {
annotationClassName: 'StaticIconProvider',
annotationClassLibraryUri: 'package:const_finder_fixtures/static_icon_provider.dart',
);
final Map<String, dynamic> instances = finder.findInstances();
expectInstances(
finder.findInstances(),
instances,
<String, dynamic>{
'constantInstances': <Map<String, Object?>>[
<String, Object?>{
@@ -140,6 +155,8 @@ void _checkAnnotation(String dillPath, Compiler compiler) {
'targetValue': null,
},
],
// TODO(fujino): This should have non-constant locations from the use of
// a tear-off, see https://github.com/flutter/flutter/issues/116797
'nonConstantLocations': <Object?>[],
},
compiler,
@@ -212,6 +229,9 @@ void _checkNonConstsWeb(String dillPath, Compiler compiler) {
className: 'Target',
);
final String fixturesUrl = Platform.isWindows
? '/$fixtures'.replaceAll(Platform.pathSeparator, '/')
: fixtures;
expectInstances(
finder.findInstances(),
<String, dynamic>{
@@ -225,7 +245,33 @@ void _checkNonConstsWeb(String dillPath, Compiler compiler) {
<String, dynamic>{'stringValue': '7', 'intValue': 7, 'targetValue': null},
<String, dynamic>{'stringValue': 'package', 'intValue': -1, 'targetValue': null},
],
'nonConstantLocations': <dynamic>[]
'nonConstantLocations': <dynamic>[
<String, dynamic>{
'file': 'file://$fixturesUrl/lib/consts_and_non.dart',
'line': 14,
'column': 26,
},
<String, dynamic>{
'file': 'file://$fixturesUrl/lib/consts_and_non.dart',
'line': 16,
'column': 26,
},
<String, dynamic>{
'file': 'file://$fixturesUrl/lib/consts_and_non.dart',
'line': 16,
'column': 41,
},
<String, dynamic>{
'file': 'file://$fixturesUrl/lib/consts_and_non.dart',
'line': 17,
'column': 26,
},
<String, dynamic>{
'file': 'file://$fixturesUrl/pkg/package.dart',
'line': 14,
'column': 25,
}
],
},
compiler,
);

View File

@@ -4,12 +4,12 @@
{
"name": "const_finder_fixtures",
"rootUri": "../lib/",
"languageVersion": "2.12"
"languageVersion": "2.17"
},
{
"name": "const_finder_fixtures_package",
"rootUri": "../pkg/",
"languageVersion": "2.12"
"languageVersion": "2.17"
}
]
}

View File

@@ -7,6 +7,12 @@ import 'target.dart';
void main() {
Targets.used1.hit();
Targets.used2.hit();
final Target nonConstUsed3 = helper(Target.new);
nonConstUsed3.hit();
}
Target helper(Target Function(String, int, Target?) tearOff) {
return tearOff('from tear-off', 3, null);
}
@staticIconProvider

View File

@@ -15,8 +15,7 @@ class Target {
}
class ExtendsTarget extends Target {
const ExtendsTarget(String stringValue, int intValue, Target? targetValue)
: super(stringValue, intValue, targetValue);
const ExtendsTarget(super.stringValue, super.intValue, super.targetValue);
}
class ImplementsTarget implements Target {