-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Explain why a LateInitializationError
was thrown
#44361
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
Comments
Looks like the web compilers associated a different error message with the different error cases. @alexmarkov does the VM implement this itself, or does it just pick up the front end lowering of late? Is there anything stopping the VM from calling a different constructor in the two different cases? |
This is implemented in the VM (CFE lowering is not used for late fields/variables in the VM). We can throw different exception types or we can adjust the exception messages - please suggest what exactly we should do. Also, it would be nice to unify that across all Dart implementations (VM, dart2js, DDC). |
Yes, I think consistency would be good. I believe the web compilers both use the implementation in lib/internal/errors.dart (is that right @nshahan @sigmundch ). So if we're happy with just making the error message consistent, then we maybe we could either consolidate those implementations or make them consistent? cc @mit-mit @lrhn @munificent @natebosch @jakemac53 for thoughts on whether custom error messages are good enough or whether there's a justification for doing the additional work to specify and implement separate error classes. |
FWIW @rakudrama and I have discussed opting out of the late lowering in some cases where we could do something more efficient in JavaScript.
This is true for DDC. |
For dart2js we will move away from using the default code patterns and Error classes. The code size tax of the current implementation is too high (see #43361), so we want to be able to generate JavaScript where it is not possible to tell what kind of LateError is generated, or what the late variable was called, or even if the problem is a LateError or some other kind of Error. For production code we don't want to leak the names, and forcing a null dereference (NoSuchMethodError) instead of a LateError is an order of magnitude more compact. We will try to generate accurate, actionable messages where possible, but need the freedom to do something else to make the |
Ok, so it sounds like there are no issues with making DDC and the VM consistent, and dart2js in production at least will diverge significantly no matter what. Does that seem reasonable to all? Are we happy with just making sure that there are good error messages associated with the exception (as currently implemented by DDC)? |
Do we want to underspecify the error? (The |
Per discussion in the language meeting today, we would like to remove LateInitializationError from the public API, and loosen the specification to simply specify that some error is thrown. The VM and DDC may choose (hopefully will choose) to implement this consistently, e.g by using the current mechanism. |
As of the fix above, the new output is:
|
When the VM detects an
LateInitializationError
with null safety, it throws a generic error like:This error does not detail which of the two cases of LateInitializationError happened: did we read something that wasn't initialized, or did we write to an already initialized final late variable.
It would be nice to either add this detail in the error output, or potentially even split
LateInitializationError
into two errors (likeLateUninitializedError
andLateFinalReInitializationError
).Further it could be made a bit clearer which variable triggered the exception.
The text was updated successfully, but these errors were encountered: