Skip to content
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

[Breaking Change Request] Deprecate CastError, make everything a TypeError. #40763

Closed
lrhn opened this issue Feb 25, 2020 · 8 comments
Closed
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. breaking-change-request This tracks requests for feedback on breaking changes library-core

Comments

@lrhn
Copy link
Member

lrhn commented Feb 25, 2020

Intended Change

Make every platform operation currently throwing a CastError instead throw an object implementing both TypeError and CastError. (Effectively make all implementations of CastError also implement TypeError).

Deprecate CastError and recommend that everybody uses TypeError instead.

Implementations are allowed to use a class implementing CastError when they throw a TypeError, so they can decide to only have one class.

Eventually remove CastError from the platform libraries (possibly along with other errors classes that can no longer happen, primarily AbstractClassInstantiationError and FallthroughError).

Rationale

Currently the Dart runtimes throw TypeError in some situations and CastError in other situations.

In Dart 1, the distinction was clear: TypeError was an AssertionError and was only thrown in checked mode when a type assertion failed, and CastError was thrown when an as operation failed.

In Dart 2. that distinction was no longer meaningful. Because of sound typing, a type assertion can no longer fail. If there is a risk that it can fail at run-time, the compiler inserts an implicit cast. It is as if your int x = dynamicValue; was converted to int x = dynamicValue as int;.
Except that the explicit cast throws CastError and the implicit cast throws TypeError. Maybe.

The language specification no longer mentions CastError, simply stating that a failed as cast is a dynamic type error, which means that it should be throwing a TypeError.

When compiling to JavaScript, it's often necessary to convert native exceptions to Dart errors, and having to figure out whether to use a TypeError or a CastError is an extra unnecessary overhead.

All in all, the CastError does not carry its own weight.

See also dart-lang/language#787.

Expected impact

No initial impact. Requires some later migration from catching CastError to catching TypeError before the CastError can be removed.

Catching errors:
By making all errors that are actually thrown implement both CastError and TypeError, any code which currently catches CastError or TypeError will still catch the same errors. The only difference would be in a situation where a try/catch statement attempts to distinguish between the two error types by catching both, with TypeError first, and it will now trigger the former catch clause instead of the latter.

Throwing errors:
No known code outside of the Dart SDK platform libraries are implementing or extending CastError (or TypeError). No known code outside of the Dart SDK throws a new CastError. (There is code throwing a TypeError).

Migration:
Code currently catching CastError should instead catch TypeError.
This is probably primarily testing doing things like expect(..., throwsA(isCastError)).

The CastError is marked as deprecated, so some clients might want to migrate quickly to avoid warnings. Deprecation is not a breaking change.

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core breaking-change-request This tracks requests for feedback on breaking changes labels Feb 25, 2020
@franklinyow
Copy link
Contributor

cc @Hixie @matanlurey @dgrove @vsmenon for review and approval.

@vsmenon
Copy link
Member

vsmenon commented Feb 25, 2020

lgtm for dart

@matanlurey
Copy link
Contributor

LGTM. Can we get rid of AssertionError, also (at least as public type)? :P

@lrhn
Copy link
Member Author

lrhn commented Feb 25, 2020

AssertionError is the kind of error thrown by the assert statement and is specified in the language specification.

Now, AbstractClassInstantiationError and FallthroughError are no longer useful. I'm happy to deprecate them. NullThrownError and CyclicInitializationError will stop being useful when we reach full Null Safety.

@franklinyow
Copy link
Contributor

Ping @Hixie

@Hixie
Copy link
Contributor

Hixie commented Feb 28, 2020

LGTM if this is to enable a Web optimization.

If it wasn't for the optimization, I'd probably suggest keeping TypeError for implicit cast errors and CastError for explicit cast errors, because to the developer they are still distinct, even if the compiler/language doesn't really care about the distinction any more.

That said, this seems very minor and I don't feel strongly at all.

@franklinyow
Copy link
Contributor

Approved

@franklinyow
Copy link
Contributor

Landed. Close as discussed in SCRUM

dart-bot pushed a commit that referenced this issue Mar 18, 2020
Boolean conversion now throws a TypeError, not CastError. See:
* #40317
* #40763

Presubmit: https://critique.corp.google.com/#review/301195917

Change-Id: I76e8aa4a849eb519e47c80f1b4873032a05ad636
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/139402
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Mark Zhou <markzipan@google.com>
dart-bot pushed a commit that referenced this issue Mar 25, 2020
CastError is deprecated.  See
 #40763 for details.

Change-Id: If00963e68987a259396c4b5a0cd6d703bc7ac76c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140885
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. breaking-change-request This tracks requests for feedback on breaking changes library-core
Projects
None yet
Development

No branches or pull requests

5 participants