Skip to content

Mark IconData final and @mustBeConst#181345

Open
dcharkes wants to merge 1 commit intomasterfrom
icon-data-class-modifier
Open

Mark IconData final and @mustBeConst#181345
dcharkes wants to merge 1 commit intomasterfrom
icon-data-class-modifier

Conversation

@dcharkes
Copy link
Contributor

@dcharkes dcharkes commented Jan 22, 2026

2026-03-18 update, no push-back on the breaking changes. Migrations done downstream:


Applying breaking changes:

See the issue descriptions for the motivation.

cc @Piinks

It seems we have a non-const use in the Remote Flutter Widgets:

https://github.com/flutter/packages/blob/d0c7d1fad77331724882614345137221c77d5758/packages/rfw/lib/src/flutter/argument_decoders.dart#L801-L811

The package itself makes no mention of it not work with the Flutter Icon Tree Shaker. If it's a know issue that the icon tree shaker is not supported with package:rfw we can add // ignore: non_const_argument_for_const_parameter. (flutter/packages#10858)

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@github-actions github-actions bot added the framework flutter/packages/flutter repository. See also f: labels. label Jan 22, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request makes IconData a final class and adds @mustBeConst to its constructor parameters, which is a great improvement for enforcing immutability and enabling compiler optimizations. My review includes a suggestion to apply the @mustBeConst annotation to all constructor parameters for completeness and consistency.

@dcharkes dcharkes marked this pull request as draft January 22, 2026 21:06
@flutter-dashboard
Copy link

This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a win to me. I want to be sure that Material and Cupertino don't have any breaking usages of IconData though (doesn't look like it to me if I grep through the codebase).

I haven't worked on the rfw codebase, but taking a look it seems like it won't be possible for it to support icon tree shaking. That makes sense for a package that can execute code at runtime that is unknown at compile time, so I expect users of rfw will understand.

@loic-sharma
Copy link
Member

loic-sharma commented Jan 22, 2026

FYI, some folks use the enum Foo implements IconData pattern, which I believe will be broken if we make IconData final. Some examples:

  1. https://github.com/FaFre/WebLibre/blob/82ea06d0ebd9fe72438dcbab63a2234b9314a2dd/app/lib/presentation/icons/tor_icons.dart#L22
  2. https://github.com/PiotrRogulski/advent_of_code/blob/acf963c651a4b83bdd14fc660fa96b35f85d6b9a/lib/design_system/icons.dart#L7
  3. Icon tree shaking ignores IconData implementation getters #126261

This comment explains why folks use this pattern: #126261 (comment)

One of the major if not the biggest value from keeping such values as an enum for me is the values property provided by the language itself. I use values in widgetbook; keeping such list manually is error-prone

However, it seems that this improvement is nice enough to warrant breaking breaking this pattern.

@dcharkes
Copy link
Contributor Author

One of the major if not the biggest value from keeping such values as an enum for me is the values property provided by the language itself. I use values in widgetbook; keeping such list manually is error-prone

@Albert221 You rely on subtyping IconData to get an automated .values. Could you elaborate on how .values is used in the code? My understanding of the tree-shaking mechanism is if .values is used, then all the codePoints are reachable, which means you'll never get any code points tree-shaken from the fonts.

@Albert221
Copy link
Contributor

Albert221 commented Jan 23, 2026

@dcharkes Hey! That is correct, they're not tree-shaken at all if I use .values. But I use it in my Widgetbook instance, where I want my developers and designers to have access to the whole library of available icons. The widgetbook is a separate package, so it doesn't hurt me. In the mobile app itself, I don't use .values at all for icons to have the tree-shaking :)

Setup we use for many projects and that many clients use in their codebases usually is:

  • app_package (depends on app_design_system)
  • app_design_system my custom icon datas
  • design_system_widgetbook (depends on app_design_system) where devs/designers can preview all available components, colors, icons, etc.

@dcharkes
Copy link
Contributor Author

dcharkes commented Jan 23, 2026

Thanks for the reply @Albert221 🙏

Okay, then the tree-shaking is WAI in your use case. I suppose asking you to migrate away from enum ... implements IconData is asking you to break all your users? Would it be possible to migrate to the same structure as the other icon sets, an unrelated class around it with static const members and a code-genned(?) values const.

@Albert221
Copy link
Contributor

Not ideal but possible, yes. If that's for the sake of making Flutter better for everyone, go for it

@dcharkes
Copy link
Contributor Author

Not ideal but possible, yes. If that's for the sake of making Flutter better for everyone, go for it

🙏 👍 !

Okay, when we do this as a breaking change, then older versions of WidgetBook will stop working in a newer Flutter version. Please let me know when you have migrated, and if the migration didn't caused breaking change issues on your side (looking at the code I don't think so, but I might be wrong). I'll wait with landing anything on master for a while (and need to file a breaking change announcement as well) to ensure no one breaks.

@dcharkes dcharkes force-pushed the icon-data-class-modifier branch from 6c1c25c to 63b8163 Compare March 10, 2026 08:43
dcharkes added a commit to flutter/packages that referenced this pull request Mar 11, 2026
We are introducing the `@mustBeConst` annotation on `IconData`
parameters that must be const in order for the icon tree shaker to work:

* flutter/flutter#181344
* flutter/flutter#181345

RFW does not support tree-shaking.

This PR adds the lint ignores with an explicit comment that the lint
ignore is intentional.
@dcharkes dcharkes force-pushed the icon-data-class-modifier branch from 63b8163 to 84a1207 Compare March 12, 2026 15:42
@dcharkes dcharkes added the CICD Run CI/CD label Mar 12, 2026
@dcharkes dcharkes force-pushed the icon-data-class-modifier branch from 84a1207 to 8569697 Compare March 16, 2026 07:31
@dcharkes dcharkes added CICD Run CI/CD and removed CICD Run CI/CD labels Mar 16, 2026
dcharkes added a commit to flutter/tests that referenced this pull request Mar 16, 2026
@dcharkes dcharkes force-pushed the icon-data-class-modifier branch from 8569697 to f9750dd Compare March 16, 2026 18:21
@dcharkes dcharkes added CICD Run CI/CD and removed CICD Run CI/CD labels Mar 16, 2026
dcharkes added a commit to flutter/tests that referenced this pull request Mar 17, 2026
@dcharkes dcharkes force-pushed the icon-data-class-modifier branch from f9750dd to 74fde38 Compare March 18, 2026 19:20
@github-actions github-actions bot removed the CICD Run CI/CD label Mar 18, 2026
@dcharkes dcharkes added the CICD Run CI/CD label Mar 18, 2026
@dcharkes dcharkes marked this pull request as ready for review March 18, 2026 20:34
@dcharkes dcharkes requested a review from Piinks March 18, 2026 20:34
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances IconData by making it a final class and annotating its constructor parameters with @mustBeConst. These changes improve const-correctness, which is beneficial for optimizations like icon tree shaking. The implementation is sound, and I have one suggestion to apply the @mustBeConst annotation more consistently across all relevant parameters.

Comment on lines 54 to 55
this.matchTextDirection = false,
this.fontFamilyFallback,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency and to ensure all properties used by the icon tree shaker are constant, consider also annotating matchTextDirection and fontFamilyFallback with @mustBeConst. All fields of IconData are marked with @_retainForIconTreeShaker, which suggests they are all relevant for tree shaking.

    // ignore: experimental_member_use
    @mustBeConst this.matchTextDirection = false,
    // ignore: experimental_member_use
    @mustBeConst this.fontFamilyFallback,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope they are not read:

      final Object? package = iconDataMap['fontPackage'];
      final Object? fontFamily = iconDataMap['fontFamily'];
      final Object? codePoint = iconDataMap['codePoint'];

packages/flutter_tools/lib/src/build_system/targets/icon_tree_shaker.dart

@Piinks
Copy link
Contributor

Piinks commented Mar 19, 2026

Do we have a migration guide staged in flutter/website for this? There is a template we used for explaining the context of the breakage and how to migrate, which ends up listed here: https://docs.flutter.dev/release/breaking-changes

@dcharkes
Copy link
Contributor Author

Do we have a migration guide staged in flutter/website for this? There is a template we used for explaining the context of the breakage and how to migrate, which ends up listed here: https://docs.flutter.dev/release/breaking-changes

We haven't landed that yet, do we do that first?

@Piinks
Copy link
Contributor

Piinks commented Mar 19, 2026

We haven't landed that yet, do we do that first?

Nah, usually right after this lands. We just make sure they're both ready so we don't forget to add the guide later. :)

Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels.

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

5 participants