New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New lint: Avoid unstable final fields #3578
Conversation
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 looks good. Just a couple of nits and forward pointers to changes that will land soon.
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.
Got it, thanks!
aba56ab
to
7f97666
Compare
de211ad
to
1cb730f
Compare
Now that Dart 3 has landed, I'd love to hear your thoughts on continuing down this road, @eernstg . The description above says this is not intended to be landed at this point. I think I'd be very interested in hearing whether stable getters (dart-lang/language#1518) is still on the table for any upcoming release. It is probably my favorite possibly upcoming feature :) That being said, this rule would still provide great value even if we never land that feature, yes? Thanks! |
Thanks for the kind words! The 'final getters'/'stable getters' feature does come up in discussions in the language team repeatedly, and I'm not alone in pushing it. But most of the language team is worried about the potential breakage (if we change the meaning of the existing modifier Nevertheless, I think it would be great if we could land the lint: It flags situations where a final instance variable is overridden by a getter which is not known to return the same object on repeated invocations, and that seems to be a somewhat delicate situation anyway. It would hardly hurt anyone if the lint exists, as long as it isn't part of a set like 'recommended' or 'core'. One thing needs to be settled, however: There must be a way to indicate that a given |
Yeah I think this is a good path forward (to enable the language feature, or as its own end).
I think we'd be very open to adding an annotation to |
It shouldn't be a problem to add an annotation to mark an unstable getter. Just to double check though, are we sure we want a lint with an annotation to opt out rather than no lint and an annotation to opt in (that is, assume every getter is unstable unless it's marked as being Assuming we want the lint, we'll probably need a fix for cases where the lint breaks the code. Is there any remediation other than marking all overriding getters as being unstable? If we have the notion of a stable getter, is there anything we can/should do at the invocation sites? I could, for example, imagine suggesting that invocations within a loop could be hoisted (as long as the receiver doesn't change), or suggesting that a single invocation would be more efficient when the same getter is invoked multiple times. These are, of course, things that it would be nice for the compiler to optimize automatically, but if this isn't a language feature the compiler probably can't depend on the semantics. |
That is true in the general case. The stable getters / final getters proposal allows declarations like However, it is already possible to do it with an abstract getter: Replace
These things are part of the motivation for having stable/final getters in the first place. Another benefit would be that every stable expression (like But those benefits are specific to the language mechanism. As long as we only have a lint we can only used it to give developers a heads-up if there is an apparently-immutable property which isn't actually immutable (that is: a |
…in several cases
14eab32
to
f2a1621
Compare
Since the last comment on this PR we've moved the linter sources into the |
Sure! I'll do that tomorrow. |
@bwilkerson, I've created https://dart-review.googlesource.com/c/sdk/+/326342. |
Closing: This effort is now handled via https://dart-review.googlesource.com/c/sdk/+/326342. |
This CL transfers the changes made by dart-lang/linter#3578 to Gerrit. Change-Id: Ib1eae56d2112e25bcf9916abf27b05bd3de15735 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/326342 Commit-Queue: Erik Ernst <eernst@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This PR implements the lint
avoid_unstable_final_fields
, cf. issue #3440.The basic idea is that a final field declaration is taken to indicate that the given property is immutable, and hence every override which is not guaranteed to return the same value each time it is invoked will be linted.
The PR is intended to serve as a point of reference, facilitating discussion and feedback on the lint and the underlying ruleset, as well as on the implementation.
[Edit: Removed remarks about landing this PR.]