Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 40a4279

Browse files
authored
Make @keepToString annotation work for web and non-web (#51251)
* Update documentation for the annotation * Add `@keepToString` annotation to web's `Locale` class (similar to non-webs `Locale` class) Issue flutter/flutter#52759
1 parent 0246484 commit 40a4279

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

lib/ui/annotations.dart

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

55
part of dart.ui;
66

7-
// TODO(dnfield): Update this if/when we default this to on in the tool,
8-
// see: https://github.com/flutter/flutter/issues/52759
9-
/// Annotation used by Flutter's Dart compiler to indicate that an
10-
/// [Object.toString] override should not be replaced with a supercall.
11-
///
12-
/// Since `dart:ui` and `package:flutter` override `toString` purely for
13-
/// debugging purposes, the frontend compiler is instructed to replace all
14-
/// `toString` bodies with `return super.toString()` during compilation. This
15-
/// significantly reduces release code size, and would make it impossible to
16-
/// implement a meaningful override of `toString` for release mode without
17-
/// disabling the feature and losing the size savings. If a package uses this
18-
/// feature and has some unavoidable need to keep the `toString` implementation
19-
/// for a specific class, applying this annotation will direct the compiler
20-
/// to leave the method body as-is.
7+
/// Annotation to keep [Object.toString] overrides as-is instead of removing
8+
/// them for size optimization purposes.
9+
///
10+
/// For certain uris (currently `dart:ui` and `package:flutter`) the Dart
11+
/// compiler will remove [Object.toString] overrides from classes in
12+
/// profile/release mode to reduce code size.
13+
///
14+
/// Individual classes can opt out of this behavior via the following
15+
/// annotations:
16+
///
17+
/// * `@pragma('flutter:keep-to-string')`
18+
/// * `@pragma('flutter:keep-to-string-in-subtypes')`
19+
///
20+
/// See https://github.com/dart-lang/sdk/blob/main/runtime/docs/pragmas.md
2121
///
2222
/// For example, in the following class the `toString` method will remain as
2323
/// `return _buffer.toString();`, even if the `--delete-tostring-package-uri`

lib/web_ui/lib/annotations.dart

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,39 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// TODO(dnfield): Remove unused_import ignores when https://github.com/dart-lang/sdk/issues/35164 is resolved.
6-
75
part of ui;
86

9-
// TODO(dnfield): Update this if/when we default this to on in the tool,
10-
// see: https://github.com/flutter/flutter/issues/52759
11-
const _KeepToString keepToString = _KeepToString();
12-
13-
class _KeepToString {
14-
const _KeepToString();
15-
}
7+
/// Annotation to keep [Object.toString] overrides as-is instead of removing
8+
/// them for size optimization purposes.
9+
///
10+
/// For certain uris (currently `dart:ui` and `package:flutter`) the Dart
11+
/// compiler may remove [Object.toString] overrides from classes in
12+
/// profile/release mode to reduce code size.
13+
///
14+
/// Individual classes can opt out of this behavior via the following
15+
/// annotations:
16+
///
17+
/// * `@pragma('flutter:keep-to-string')`
18+
/// * `@pragma('flutter:keep-to-string-in-subtypes')`
19+
///
20+
/// See https://github.com/dart-lang/sdk/blob/main/runtime/docs/pragmas.md
21+
///
22+
/// For example, in the following class the `toString` method will remain as
23+
/// `return _buffer.toString();`, even if the `--delete-tostring-package-uri`
24+
/// option would otherwise apply and replace it with `return super.toString()`.
25+
/// (By convention, `dart:ui` is usually imported `as ui`, hence the prefix.)
26+
///
27+
/// ```dart
28+
/// class MyStringBuffer {
29+
/// final StringBuffer _buffer = StringBuffer();
30+
///
31+
/// // ...
32+
///
33+
/// @ui.keepToString
34+
/// @override
35+
/// String toString() {
36+
/// return _buffer.toString();
37+
/// }
38+
/// }
39+
/// ```
40+
const pragma keepToString = pragma('flutter:keep-to-string');

lib/web_ui/lib/platform_dispatcher.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ class Locale {
502502
@override
503503
int get hashCode => Object.hash(languageCode, scriptCode, countryCode);
504504

505+
@keepToString
505506
@override
506507
String toString() => _rawToString('_');
507508

0 commit comments

Comments
 (0)