-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[plugin_platform_interface] Switch PlatformInterface._instanceToken to an expando.
#6411
Conversation
This change replaces `PlatformInterface._instanceToken` (an instance field that points from a `PlatformInterface` to its corresponding token) with an expando that maps from `PlatformInterface` to `Object`. There is no change to the public API, and no change to the behavior of users' production code. This change ensures that if a customer tries to implement `PlatformInterface` using `implements` rather than `extends`, the code in `PlatformInterface._verify` won't try to access the private `_instanceToken` field in `PlatformInterface`. This is important because an upcoming change to the dart language is going to cause such accesses to throw exceptions rather than deferring to `noSuchMethod` (see dart-lang/language#2020 for details). Fixes flutter/flutter#109339.
|
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!). 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. |
|
|
||
| final Object? _instanceToken; | ||
| /// Expando mapping instances of PlatformInterface to their associated tokens. | ||
| /// The reason we don't simply use a private field of type `Object?` is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: could you adjust this comment (here and below) to remove the use of "we"? I can't actually tell who "we" is in this context (see go/avoidwe).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
stuartmorgan-g
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just some minor comments on the explanation comment.
| /// Expando mapping instances of PlatformInterface to their associated tokens. | ||
| /// The reason we don't simply use a private field of type `Object?` is | ||
| /// because as part of implementing | ||
| /// https://github.com/dart-lang/language/issues/2020, it will soon become a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment will outlive the work, so referring to "soon" will soon be confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
packages/plugin_platform_interface/lib/plugin_platform_interface.dart
Outdated
Show resolved
Hide resolved
|
@stuartmorgan, given that this change only affects the behavior of some corner cases in tests, I'm unsure whether I need:
What do you think? |
It's changing production code (and will actually change behavior for clients), so needs both.
Since we already have tests that would break later without this change, I think it's covered; I would ping Hixie for an exemption on this. |
Thanks! I've added a CHANGELOG entry and bumped the version; I'll ping Hixie now. |
|
Do we know of anyone who uses |
The point of this logic is to enforce that people don't make implementations of platform interfaces of federated plugins that use
This allows us to throw a controlled error that matches our docs and has clear actionable instructions, instead of throwing something different in different cases for subtle language implementation reasons. I think having code that almost nobody ever needs to interact with be more complex is a reasonable tradeoff here in order to have a stable, actionable error for the many, many potential clients of this logic. |
|
Ah, that was not clear from the original commit message or changelog entry. I definitely support the goal here. I just wish there was a cleaner (read, less Expando-magical way) to do it... |
|
I think this is worth testing, FWIW. Verifying that we give good clear error messages for things like this is valuable. |
Test added! |
stuartmorgan-g
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM; one optional test change.
packages/plugin_platform_interface/test/plugin_platform_interface_test.dart
Show resolved
Hide resolved
…anceToken` to an expando. (flutter/plugins#6411)
… to an expando. (flutter#6411) This change replaces `PlatformInterface._instanceToken` (an instance field that points from a `PlatformInterface` to its corresponding token) with an expando that maps from `PlatformInterface` to `Object`. There is no change to the public API, and no change to the behavior of users' production code. This change ensures that if a customer tries to implement `PlatformInterface` using `implements` rather than `extends`, the code in `PlatformInterface._verify` won't try to access the private `_instanceToken` field in `PlatformInterface`. This is important because an upcoming change to the dart language is going to cause such accesses to throw exceptions rather than deferring to `noSuchMethod` (see dart-lang/language#2020 for details). Fixes flutter/flutter#109339.
… to an expando. (flutter#6411) This change replaces `PlatformInterface._instanceToken` (an instance field that points from a `PlatformInterface` to its corresponding token) with an expando that maps from `PlatformInterface` to `Object`. There is no change to the public API, and no change to the behavior of users' production code. This change ensures that if a customer tries to implement `PlatformInterface` using `implements` rather than `extends`, the code in `PlatformInterface._verify` won't try to access the private `_instanceToken` field in `PlatformInterface`. This is important because an upcoming change to the dart language is going to cause such accesses to throw exceptions rather than deferring to `noSuchMethod` (see dart-lang/language#2020 for details). Fixes flutter/flutter#109339.
This change replaces
PlatformInterface._instanceToken(an instance field that points from aPlatformInterfaceto its corresponding token) with an expando that maps fromPlatformInterfacetoObject. There is no change to the public API, and no change to the behavior of users' production code.This change ensures that if a customer tries to implement
PlatformInterfaceusingimplementsrather thanextends, the code inPlatformInterface._verifywon't try to access the private_instanceTokenfield inPlatformInterface. This is important because an upcoming change to the dart language is going to cause such accesses to throw exceptions rather than deferring tonoSuchMethod(see dart-lang/language#2020 for details).Fixes flutter/flutter#109339.
Pre-launch Checklist
dart format.)[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style.///).If you need help, consider asking for advice on the #hackers-new channel on Discord.