dart-lang / dart-lang/language
Is is permissible for an implementation to have a single Error class?
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
The spec describes many `Error`s. Is it permissible for an implementation to have one class that implements all of the described `Error` classes? This would make it impossible to dispatch on the subtypes of `Error` but would still provide a guarantee that an certain `Error` is thrown.
When compiling to JavaScript, it happens, or can arranged, that the generated JavaScript code would crash at the same place under the same conditions.
Typically there is a property access on a value that has a JavaScript `null` or `undefined` value, or has a missing property that is called. Both result in a JavaScript `TypeError`, which is opaque. This can be very efficient in terms of the generated code size (zero code to do the check!) the but it is generally not possible to map the JavaScript `TypeError` to a specific Dart `Error`. The following are examples of situations where a property access could be used to signal an error:
- `NoSuchMethodError` for a `null` receiver (`dynamic d = null;... d.foo`).
- `NoSuchMethodError` for dynamic calls: generated `a.foo$0()` where `a` might not have a `foo` method.
- `CastError` for a null-check operator: `a!.foo()` could be folded into call which will crash anyway, compiling to `a.foo$0()`.
- `LateInitializationError` - if a `final` `late` field without initializer has a non-nullable type, a `null` initial value could be used as a sentinel. The read guard could be implemented as a property access of the (un)initialized value. Again, we can compile `this.field.foo()` to `this.field.foo$()`, relying on the pre-assigned value being `null`.
Using a JavaScript crash to implement a Dart crash is very efficient as it often requires no explicit check in the generated code. However, it does make it difficult to know exactly what the problem was. In fact a single JavaScript location could be *all* of the above in some sequence.
It is permissible for the implementation to have a single `Error` class like the following?
```dart
class _JavaScriptTypeError extends Error
implements
TypeError,
CastError,
NoSuchMethodError,
LateInitializationError,
... {
...
}
```
Contributor guide
Research direction
Start by reviewing the specification requirements for the listed Error types and the JavaScript compilation examples in this issue. Compare whether one implementation class preserves the specified subtype and throwing guarantees; done when the specification question has an explicit resolution.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, javascript
- Domain
- compilers
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100