Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Unified Diff: pkg/analyzer/lib/src/dart/element/element.dart

Issue 3008453002: Build / resynthesize final fields as ConstFieldElementImpl only if the enclosing class has a consta… (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/builder.dart ('k') | pkg/analyzer/lib/src/generated/resolver.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/dart/element/element.dart
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index 362a70720d3982d4657067236374145ae9ad46ef..448dce5b15972e36d2b52dfb98494ba862e94d05 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -417,6 +417,11 @@ class ClassElementImpl extends AbstractClassElementImpl
*/
final kernel.Class _kernel;
+ /**
+ * If this class is resynthesized, whether it has a constant constructor.
+ */
+ bool _hasConstConstructorCached;
+
/**
* The actual supertype extracted from desugared [_kernel].
*/
@@ -1018,8 +1023,22 @@ class ClassElementImpl extends AbstractClassElementImpl
return null;
}
- bool get _hasConstConstructorKernel =>
- _kernel != null && _kernel.constructors.any((c) => c.isConst);
+ /**
+ * Return whether the class is resynthesized and has a constant constructor.
+ */
+ bool get _hasConstConstructor {
+ if (_hasConstConstructorCached == null) {
+ _hasConstConstructorCached = false;
+ if (_kernel != null) {
+ _hasConstConstructorCached = _kernel.constructors.any((c) => c.isConst);
+ }
+ if (_unlinkedClass != null) {
+ _hasConstConstructorCached = _unlinkedClass.executables.any(
+ (c) => c.kind == UnlinkedExecutableKind.constructor && c.isConst);
+ }
+ }
+ return _hasConstConstructorCached;
+ }
@override
void appendTo(StringBuffer buffer) {
@@ -4552,7 +4571,9 @@ class FieldElementImpl extends PropertyInducingElementImpl
factory FieldElementImpl.forKernelFactory(
ClassElementImpl enclosingClass, kernel.Field kernel) {
if (kernel.isConst ||
- kernel.isFinal && enclosingClass._hasConstConstructorKernel) {
+ kernel.isFinal &&
+ !kernel.isStatic &&
+ enclosingClass._hasConstConstructor) {
return new ConstFieldElementImpl.forKernel(enclosingClass, kernel);
} else {
return new FieldElementImpl.forKernel(enclosingClass, kernel);
@@ -4578,7 +4599,9 @@ class FieldElementImpl extends PropertyInducingElementImpl
UnlinkedVariable unlinkedVariable, ClassElementImpl enclosingClass) {
if (unlinkedVariable.initializer?.bodyExpr != null &&
(unlinkedVariable.isConst ||
- unlinkedVariable.isFinal && !unlinkedVariable.isStatic)) {
+ unlinkedVariable.isFinal &&
+ !unlinkedVariable.isStatic &&
+ enclosingClass._hasConstConstructor)) {
return new ConstFieldElementImpl.forSerialized(
unlinkedVariable, enclosingClass);
} else {
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/builder.dart ('k') | pkg/analyzer/lib/src/generated/resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698