From db661a120d3e0d84c20f57f97764135814173089 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Tue, 25 Jul 2023 18:04:09 -0700 Subject: [PATCH] Prepare flutter engine for enabling private final field promotion. (flutter/engine#43959) Some parts of the flutter engine are built using the latest version of the Dart SDK, ignoring SDK constraints in `pubspec.yaml` files. Therefore, before the Dart SDK can switch on the "private field promotion" feature (https://github.com/dart-lang/language/issues/2020), these parts of the flutter engine need to be modified so that they won't have any "unnecessary `!`" warnings after field private field promotion is enabled. This PR makes the necessary changes. This PR doesn't introduce any functional change. In principle it might improve performance slightly (by avoiding redundant memory accesses), but in practice the difference is unlikely to rise out of the measurement noise. Issue fixed by this PR: https://github.com/flutter/flutter/issues/131198 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- engine/src/flutter/lib/ui/painting.dart | 10 ++-- .../flutter/lib/ui/platform_dispatcher.dart | 5 +- engine/src/flutter/lib/ui/text.dart | 49 +++++++++++-------- .../platform_views/message_handler.dart | 4 +- .../lib/src/engine/semantics/semantics.dart | 10 ++-- 5 files changed, 46 insertions(+), 32 deletions(-) diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 0a6ccf3331..b0f5a60a7c 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -3589,10 +3589,11 @@ class ColorFilter implements ImageFilter { } return _ColorFilter.mode(this); case _kTypeMatrix: - if (_matrix == null) { + final List? matrix = _matrix; + if (matrix == null) { return null; } - assert(_matrix!.length == 20, 'Color Matrix must have 20 entries.'); + assert(matrix.length == 20, 'Color Matrix must have 20 entries.'); return _ColorFilter.matrix(this); case _kTypeLinearToSrgbGamma: return _ColorFilter.linearToSrgbGamma(this); @@ -3616,7 +3617,10 @@ class ColorFilter implements ImageFilter { } @override - int get hashCode => Object.hash(_color, _blendMode, _matrix == null ? null : Object.hashAll(_matrix!), _type); + int get hashCode { + final List? matrix = _matrix; + return Object.hash(_color, _blendMode, matrix == null ? null : Object.hashAll(matrix), _type); + } @override String get _shortDescription { diff --git a/engine/src/flutter/lib/ui/platform_dispatcher.dart b/engine/src/flutter/lib/ui/platform_dispatcher.dart index d529fac09e..ebcc86456d 100644 --- a/engine/src/flutter/lib/ui/platform_dispatcher.dart +++ b/engine/src/flutter/lib/ui/platform_dispatcher.dart @@ -2315,8 +2315,9 @@ class Locale { if (scriptCode != null && scriptCode!.isNotEmpty) { out.write('$separator$scriptCode'); } - if (_countryCode != null && _countryCode!.isNotEmpty) { - out.write('$separator$countryCode'); + final String? countryCode = _countryCode; + if (countryCode != null && countryCode.isNotEmpty) { + out.write('$separator${this.countryCode}'); } return out.toString(); } diff --git a/engine/src/flutter/lib/ui/text.dart b/engine/src/flutter/lib/ui/text.dart index 95095a4d88..a77396ca83 100644 --- a/engine/src/flutter/lib/ui/text.dart +++ b/engine/src/flutter/lib/ui/text.dart @@ -1550,26 +1550,32 @@ class TextStyle { } @override - int get hashCode => Object.hash( - Object.hashAll(_encoded), - _leadingDistribution, - _fontFamily, - _fontFamilyFallback, - _fontSize, - _letterSpacing, - _wordSpacing, - _height, - _locale, - _background, - _foreground, - _shadows == null ? null : Object.hashAll(_shadows!), - _decorationThickness, - _fontFeatures == null ? null : Object.hashAll(_fontFeatures!), - _fontVariations == null ? null : Object.hashAll(_fontVariations!), - ); + int get hashCode { + final List? shadows = _shadows; + final List? fontFeatures = _fontFeatures; + final List? fontVariations = _fontVariations; + return Object.hash( + Object.hashAll(_encoded), + _leadingDistribution, + _fontFamily, + _fontFamilyFallback, + _fontSize, + _letterSpacing, + _wordSpacing, + _height, + _locale, + _background, + _foreground, + shadows == null ? null : Object.hashAll(shadows), + _decorationThickness, + fontFeatures == null ? null : Object.hashAll(fontFeatures), + fontVariations == null ? null : Object.hashAll(fontVariations), + ); + } @override String toString() { + final List? fontFamilyFallback = _fontFamilyFallback; return 'TextStyle(' 'color: ${ _encoded[0] & 0x00002 == 0x00002 ? Color(_encoded[1]) : "unspecified"}, ' 'decoration: ${ _encoded[0] & 0x00004 == 0x00004 ? TextDecoration._(_encoded[2]) : "unspecified"}, ' @@ -1583,8 +1589,8 @@ class TextStyle { 'fontFamily: ${ _encoded[0] & 0x00200 == 0x00200 && _fontFamily != '' ? _fontFamily : "unspecified"}, ' 'fontFamilyFallback: ${ _encoded[0] & 0x00200 == 0x00200 - && _fontFamilyFallback != null - && _fontFamilyFallback!.isNotEmpty ? _fontFamilyFallback : "unspecified"}, ' + && fontFamilyFallback != null + && fontFamilyFallback.isNotEmpty ? fontFamilyFallback : "unspecified"}, ' 'fontSize: ${ _encoded[0] & 0x00400 == 0x00400 ? _fontSize : "unspecified"}, ' 'letterSpacing: ${ _encoded[0] & 0x00800 == 0x00800 ? "${_letterSpacing}x" : "unspecified"}, ' 'wordSpacing: ${ _encoded[0] & 0x01000 == 0x01000 ? "${_wordSpacing}x" : "unspecified"}, ' @@ -3185,8 +3191,9 @@ base class _NativeParagraphBuilder extends NativeFieldWrapperClass1 implements P void pushStyle(TextStyle style) { final List fullFontFamilies = []; fullFontFamilies.add(style._fontFamily); - if (style._fontFamilyFallback != null) { - fullFontFamilies.addAll(style._fontFamilyFallback!); + final List? fontFamilyFallback = style._fontFamilyFallback; + if (fontFamilyFallback != null) { + fullFontFamilies.addAll(fontFamilyFallback); } final Int32List encoded = style._encoded; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/platform_views/message_handler.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/platform_views/message_handler.dart index b6a5f4533c..c55380ec6a 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/platform_views/message_handler.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/platform_views/message_handler.dart @@ -98,9 +98,7 @@ class PlatformViewMessageHandler { // For now, we don't need anything fancier. If needed, this can be converted // to a PlatformViewStrategy class for each web-renderer backend? - if (_contentHandler != null) { - _contentHandler!(content); - } + _contentHandler?.call(content); callback(_codec.encodeSuccessEnvelope(null)); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart index 8f19b2ef10..e30f5f6e11 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart @@ -2122,14 +2122,15 @@ class EngineSemanticsOwner { // First, update each object's information about itself. This information is // later used to fix the parent-child and sibling relationships between // objects. - for (final SemanticsNodeUpdate nodeUpdate in update._nodeUpdates!) { + final List nodeUpdates = update._nodeUpdates!; + for (final SemanticsNodeUpdate nodeUpdate in nodeUpdates) { final SemanticsObject object = getOrCreateObject(nodeUpdate.id); object.updateSelf(nodeUpdate); } // Second, fix the tree structure. This is moved out into its own loop, // because each object's own information must be updated first. - for (final SemanticsNodeUpdate nodeUpdate in update._nodeUpdates!) { + for (final SemanticsNodeUpdate nodeUpdate in nodeUpdates) { final SemanticsObject object = _semanticsTree[nodeUpdate.id]!; object.updateChildren(); object._dirtyFields = 0; @@ -2193,11 +2194,14 @@ class EngineSemanticsOwner { }); // Validate that all updates were applied - for (final SemanticsNodeUpdate update in update._nodeUpdates!) { + for (final SemanticsNodeUpdate update in nodeUpdates) { // Node was added to the tree. assert(_semanticsTree.containsKey(update.id)); } + // Verify that `update._nodeUpdates` has not changed. + assert(identical(update._nodeUpdates, nodeUpdates)); + return true; }()); }