-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Analyzer/DDC: Allows downcasts of const? #33304
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
Interestingly, this does fails nicely at compile-time in Dart2JS with
|
Code: https://github.com/matanlurey/dart.scratch/tree/master/dart2_const_downcast You can reproduce: # DDC
$ pub run build_runner test
# Dart2JS
$ pub run build_runner test -r |
DDC does runtime constant evaluation, so it's not too surprising that it wouldn't issue an error until then. The analyzer does do const evaluation, and it is likely not doing so with Dart 2 semantics (including downcasts). |
@bwilkerson - can you confirm if the analyzer is ignoring downcasts on consts per @leafpetersen 's comment? |
I just looked into this, and it seems that the analyzer's constant evaluation engine was never updated for strong mode semantics. I wouldn't say that it ignores downcasts per se--it implements the Dart 1.0 rules for checked mode (when you ask it to), and those are similar to Dart 2.0 downcasts in many cases. But they aren't similar enough to catch the error in Matan's example. Digging deeper into Matan's example, there are actually three bugs preventing an error from being reported:
I'll start to work on updating the constant evaluation engine to properly implement Dart 2.0 semantics, and once it does, I'll make sure that constant evaluation errors are reported as errors in Dart 2.0 mode. |
Wow! Thanks for looking into this @stereotype441! |
Looks like #21119 is another contributing cause. |
Partial fix for the first issue is here: https://dart-review.googlesource.com/#/c/sdk/+/60203. Note that the summary format still needs to be extended to allow constants referenced via summaries to have proper list and map types (see #33441). |
This addresses one of the root causes of #33304. Note that when a constant list or map is recorded in a summary, we don't encode enough information in the summary to resynthesize its inferred type correctly, so this only fixes cases where the constant is used in the same build unit as its declaration. Change-Id: Id0034f481cb82f18c77bbe2ee8ebec7e8b244caa Reviewed-on: https://dart-review.googlesource.com/60203 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Paul Berry <[email protected]>
I am moving this out of the dart stable milestone. I think we simply don't have resources to handle this in time. If anyone disagrees....we'll have to scour for helping hands I think. |
@MichaelRFairhurst - to make sure I understand correctly, the fix for this issue will involve moving a guaranteed runtime error to a static error - it isn't possible to have code which compiles and runs successfully today (by avoiding this code path) which would fail to compile and run after the fix? If so I think it's fine to move this to Dart2.1. If instead there can exist some code which will stop working because of the fix I think we should consider it for Dart2Stable. |
Is this planned to be fixed for 2.1? @MichaelRFairhurst |
I'm not sure this is worth doing before the CFE/analyzer integration. It looks like a good chunk of work to move a compile time error to an analysis time error, which seems like in general a lower value proposition. I'm also not particularly good at working in this system, which is quite advanced...and those who are are working on the CFE integration. If you have any concerns with that, if this is particularly important to you, please do voice your concerns here! Also +@devoncarew @leafpetersen for consideration. |
I guess it's just a particularly confusing usability issue - specifically in annotations (Angular) that are used a lot, because downcasts from |
@matanlurey 's original example now causes analyzer to print:
@MichaelRFairhurst was this fixed with const-update-2018? |
@srawlins Sexy! |
Related to #33303, I wouldn't have ever hit this error case if the IDE/analyzer would have stopped me (really, an internal customer I'm proxying for) write this code and check it in.
... produces no errors, warnings, hints, or lints, even in strong mode. This is invalid, though:
... because
Module
statically requiresList<Provider>
. I sort of understand why, at runtime, this is allowed (because of downcasts - and @leafpetersen has seen my many many bugs on this), but I'm not sure why it is allowed statically in a const context (the CFE/VM seems to reject it).Additionally, DDC builds the code ... it only later fails at runtime:
The text was updated successfully, but these errors were encountered: