-
Notifications
You must be signed in to change notification settings - Fork 30
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
Consider adding invalid_runtime_check_with_js_interop_types
to the recommended
rule set
#188
Comments
It looks like that commit is in 3.5-dev; we're about to publish 3.4 as stable. So this lint won't be available in a stable release for a release cycle (~3 months). We also just published a new major version of this package, so we won't want to do a stable release for a while (~6 months). FYI in terms of the timing of this.
Just for thinking about this, will all users want this lint on? Are the issues it catches always errors? Does it have false positives? For background about how we think of |
The timing is fine with me. We can recommend users enable this lint themselves in the interim.
I suppose if you don't care about compiling to Wasm, this might be noisier than you might need. Still, even in that case, I'd expect you'd want this on because it avoids confusing checks/undefined runtime specifics.
No, not necessarily. I give one example above but generally speaking, there are plenty of examples where the code is consistent between all compilers but this lint catches because it relies on runtime specifics which we don't guarantee. Even ignoring the contrived cases (users doing an This is partly why I think |
Can you elaborate on this? Are we planning any opt-out mechanism outside of My initial reaction is this seems like something that should be in the core set, but I don't really understand the cases where someone would intentionally violate this. |
All in all the rules seem reasonable, but possibly incomplete. If related means actual subtype or supertype relationship, then I think most reasonable use-cases will be possible. Comments on the description (taken from the original commit):
What about function types or record types containing JS interop types?
Is this
Should it be a lint?
What does "unrelated" mean? (Up-cast or down-cast only?)
What does "incompatible" mean? Same as unrelated?
|
Sure. Here's a quick example: void f<T extends JSObject>(T t) {
t as JSArray; // LINT
} We don't know if
I don't have any plans to add a flag if that's what you're alluding to.
I think it's going to be rare for users to write code that will come across false positives.
Yep, that's what "related" means here. I don't love the terminology but I was trying to avoid being too verbose. Perhaps there's a better word that can be used here?
These indeed should be lints, but we don't have code that handles
It's not a requirement for JS interop extension types to contain that annotation as we determine based on their representation type if they're an interop type (which here,
It is. :) See the next CL in the relation for where it's added and this message is modified: https://dart-review.googlesource.com/c/sdk/+/364167.
Yes, but I was aiming to differentiate a little by using |
Is there a reason to disallow side-casts casts specifically on JS-types, but allow up/down-casts? (That's casts to other JS-types, casting a JS-type to a non-(extension-on-)JS-type should probably always be a warning.) Knowing that all JS-types form a tree would be a reason, it would imply that a side-cast is always going to throw. If that is not the case, a side-cast isn't more dangerous than a down-cast. Either can be wrong, and either can be right, and the failure mode isn't significantly different. If the reasoning is that "side-casts are more likely to be wrong" and "wrong casts are more damaging for JS-types", then maybe it does mean that warning about side-casts for JS types is, all-in-all, more warranted than for non-JS-types. I can buy that reasoning too, but we might want to say explicitly that that is the reasoning. (I could get behind a "no-side-cast" lint in general, but otherwise I'd want some argument for treating JS-types differently from non-JS-types.) |
Sidecasts always succeed when running with dart2wasm (JS types are just different extension types on the same underlying class), but fail using the JS compilers, so we statically know that it will always do the "wrong" thing in one compiler. With downcasts, we don't statically know that, and we don't want to get in the way of a legitimate downcast e.g. The effect is ultimately the same between downcasting to the wrong type and sidecasting to another JS type: you'll see an error when using the JS compilers and but not when using dart2wasm. Casting to unrelated Dart types i.e. not
I suspect you're referring to the above discussion about |
We should put this in the queue for Right when Dart 3.5 releases, correct? |
From an off-line discussion:
resolution: this is ok to add to |
It will almost CERTAINLY cause CI failures for folks. I'd love it out soon...but also in a major version |
Well...considering that it'll only affect folks using the new JS-interop bits, I'd love to validate it first. It's certainly good to get it out sooner rather than later. |
Do you have links or examples where you've seen this? |
I know that |
Related: dart-lang/sdk#56396 |
An update: lets add to a |
Describe the rule you'd like to see added and to what rule set
invalid_runtime_check_with_js_interop_types
is a new rule added to address runtime type inconsistencies for JS interop types available throughdart:js_interop
. It will trigger when users use anis
oras
expression involving a JS interop type that may result in either confusing or inconsistent results.https://dart.dev/tools/linter-rules/invalid_runtime_check_with_js_interop_types (will be in Dart 3.5+)
We may want to add this to the
recommended
rule set.Additional context
This is predominantly intended to address the inconsistencies between dart2wasm and the JS compilers. When switching between compilers, most of the cases this lint rule catches either are likely to end in an error in the case of
as
expressions or a different boolean (which is worse!) in the case ofis
expressions.This rule also only affects expressions where a JS interop type is used, so it should not affect other code. We could also consider adding it to the
core
set based on the guidelines, but I can imagine cases where it won't result in an error or inconsistency e.g.JSString s = ...; s is Future;
. It's undesirable, but not necessarily worrisome.The text was updated successfully, but these errors were encountered: