-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Analyzer: No warning when mixing null-conditional and null-coalescing operators with wrong types #31776
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
What error are you expecting to see here? My understanding of the logic is as follows. The static type of (Although I'm a bit surprised that there isn't a warning that |
I expected it to tell me that the String ‘hest’ wasn’t assignable to a bool, like it does if I only use ??. I probably reduced the example too much. The original code had a field of type String that might be null, and looked more like bool fisk = textField?.contains(...) ?? ‘hest’ (This was part of a larger expression, and I deliberately put in a constant of the wrong type to check if the operators had the precedence I expected, and was surprised I didn’t get an error.) |
But I think the same logic still applies. In this case the static type of the variable initializer is the least upper bound of the static type of |
The fact that you don't get an error on the method call to null is a bug, looks like it's fixed in the new front end. I thought we had an open issue for this, but don't see one. The main issue is basically an instance of this: #25368 . I had hoped we would be able to fix this in the new front end. The intention of this phrasing from the local type inference proposal:
(and similarly for the conditional operator) was intended to imply that (in your example) This issue bites people often enough that it would be really nice to fix it for 2.0, but if that's not feasible maybe we should treat this as a just an open bug? |
@leafpetersen aha, I see two issues with the current front end implementation: (2) Checking whether a downcast is valid is only done where necessary for soundness, so in the example We could fix this in the front end by fixing (1), and then we would be forced to update the logic for But your statement that " |
It's very clear that I need to (somehow!) make time to get back to the local type inference proposal, so I'll try to do that and get more comprehensive answers. Yes, it is the intention that in general sub-expressions be checked for assignability to their context. Some examples:
In general, I think the correctness criterion is that if you infer an expression
So I think that answers your question about the context containing Does that make sense? Issues with it? In the analyzer implementation, we somewhat separate this out: inference figures out the types of things, and then you later look for sub-expressions that require downcasts. This is one of the reasons this was never fixed in the analyzer - there's not really a place to write down the type of the conditional expression (for example) separately from the type of its sub-expressions, so there was no natural way to post-hoc check the type of the sub-expressions for downcasts. This is actually a concern given that the analyzer implementation is planned to stay around for a while - if we do fix this in the new front end, the analyzer will have to figure out a way to make this work. This definitely isn't the highest priority issue, except in so far as we need the analyzer and the common front end to reasonably agree. |
Every |
@leafpetersen Ok, it looks like there are some small differences between what you intended and what I implemented in the new front end. I'm going to let this bug continue to refer to the specific case of |
Yes I think I'm speaking imprecisely above. I think this could probably be worked out, it was just not obvious how to do it given my level of understanding of the analyzer at the time (and also, it didn't slot in entirely naturally with the existing analyzer checking). I think what you are proposing, which I think would work, is to annotate the super expression with a type, and the sub-expression with its own type, and make the error verifier check that given:
etc. One wrinkle though is that eventually you'd like completion to give you good answers when you're typing inside of the conditional expression (or whatever). I imagine you could work it out though. Another possibility would be to add a synthetic node in all of the places where this can come up (there probably aren't too many) and use that to record the typing change if any. |
FWIW, analyzer still reports no diagnostics for this code. |
Could someone explain where this reasoning goes wrong:
(We can move the compile-time error into the individual branches as well, requiring each to be assignable to the context type, but that shouldn't affect the static type of the expression. Neither expression depends on the context type in any way, other than perhaps not satisfying it, but that's still just a compile-time error.) The one thing I can't find specified (in https://github.com/dart-lang/language/tree/master/resources/type-system or https://github.com/dart-lang/language/tree/master/accepted/2.12/nnbd) is step 7, the static type of If it's defined as anything else than (effectively) what I wrote, what is that then? That is, I don't understand where the What I'd expect for type inference on
With that, the type of |
dartanalyzer version 2.0.0-dev.14.0
The following code doesn't generate a warning:
(yes, this is a reduced example.)
The text was updated successfully, but these errors were encountered: