-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Unified errors with implicit/explicit down-casts #34097
Comments
@leafpetersen and @lrhn, do you have strong preferences for maintaining this distinction, is it important for developers to be able to catch a |
Adding some background info: The spec currently requires every failing |
@mkustermann suggested that the type used when a On the positive side, code catching either one of them would would continue to work when such an error this thrown. On the other hand, code written to catch one of them and ignore/rethrow the other one would be broken. I don't know if that occurs so frequently that it will break anything in practice. |
See also #33950, where I asked about the difference (and hoped that we can remove one or the other in Dart 3). |
cc @vsmenon I believe that in DDC we are considering having a mode where failure of implicit checks becomes a hard failure rather than an exception, or perhaps just an exception that you need to explicitly opt into catching. This would be to help users avoid silently swallowing type errors with overly broad exception handlers (particularly since some internal users turn off type checks in dart2js). So DDC at least may want to be able to distinguish between them. This is probably not definitive either way on whether the production platforms choose to unify them: DDC could still have an opt in mode to behave differently on type errors as a debugging aid. I do have the sense that in so far as users are catching things which are in the |
Does this mean that different backends can decide themselves how to handle explicit/implicit downcast errors?
Do you mean distinguish between cast errors and other exceptions or between implicit cast errors and explicit cast errors? Personally I think it is just a matter of style whether to rely on implicit downcasts or writing them explicitly in code. Therefore IMHO we should behave the same way. For example in the protobuf package we have this code T _$get<T>(int index, T defaultValue) {
var value = _values[index];
if (value != null) return value as T;
if (defaultValue != null) return defaultValue;
return _getDefault(_nonExtensionInfoByIndex(index)) as T;
} Now this will behave differently whether
I agree. One option would be to add a hint to dartanalyzer which recommends people to only |
Actually, I'd prefer a debugging mode where all Re the web, the main point is that Dart2JS provides aggressive options that do not preserve behavior in the presence of a runtime Per @leafpetersen's point, it's true today that Dart2JS treats |
Thanks to all for adding their view on the topic! After talking also to @lrhn about this, it appears there are not very strong opinions on what the VM will do here, so we might want to find a way to unify these two mechanisms into one, possibly by throwing an object which implements both interfaces. |
Per discussion, I'm fine with unifying these errors. |
We've unified the implementation of implicit/explicit |
With NNBD implicit casts are disallowed, so we need to replace them with explicit casts when migrating code. The difference in exceptions between explicit and implicit casts makes such changes breaking. For example, sdk/sdk/lib/collection/hash_set.dart Lines 96 to 102 in fd992e4
was migrated to sdk/sdk_nnbd/lib/collection/hash_set.dart Lines 94 to 100 in fd992e4
This effectively changed the exception which is thrown if elements are not assignable to /cc @lrhn @leafpetersen |
This makes CastError implement TypeError, and changes all test expectations to look for TypeError. A followup CL will deprecate CastError. Bug: dart-lang/language#787 Bug: #34097 Change-Id: I7102c6260901317572d2df08c4be9c4c48197688 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138670 Reviewed-by: Bob Nystrom <rnystrom@google.com> Reviewed-by: Sigmund Cherem <sigmund@google.com>
This issue has been marked as being required for unforking the SDK, what work remains to be done here? |
@a-siva I marked this issue as
Do you know if those tests are still failing? It is possible that this has been fixed already (cc @leafpetersen ). |
This issue seems to have been fixed in the co19 tests see |
Tl;DR I believe this is now a non-issue. All SDK implementation objects implementing TypeError or CastError were changed to implement both in https://dart-review.googlesource.com/c/sdk/+/138670 , and this has rolled out as a breaking change. This means that any test which expects a I filed this issue to track the additional work required to get the VM to stop throwing CastErrorImpl and instead always throw TypeErrorImpl. |
The dart-lang/co19#527 fixes have rolled into the SDK a long time ago (188c8fb). |
This example:
prints
Notice that
_TypeError
with implicit down casts and_CastError
on explicit down castsObject._as
_TypeError
s withtry { ... } on AssertionError (e, s) { ...}
(I guess we should not make_TypeError
implementAssertionError
)The VM currently has two (very) different implementations of explicit and implicit down casts. It would be really nice if we could always throw _TypeError for implicit as well as explicit downcasts.
The text was updated successfully, but these errors were encountered: