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

The "implicit-casts: false" behaviour changed between dart 2.1 and 2.2 #36233

Closed
enyo opened this issue Mar 15, 2019 · 7 comments
Closed

The "implicit-casts: false" behaviour changed between dart 2.1 and 2.2 #36233

enyo opened this issue Mar 15, 2019 · 7 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@enyo
Copy link

enyo commented Mar 15, 2019

We switched to Dart 2 as soon as it was available, and have used this analysis_options.yaml since then:

analyzer:
  strong-mode:
    implicit-casts: false
    implicit-dynamic: true

We got a lot of warnings because of implicit-casts: true and fixed them by changing the offending lines to this:

// before:
final div = document.querySelector('.foo');
// after
DivElement div = document.querySelector('.foo');

callFunctionThatExpectsDivElement(div);

and because it was an assignment, it solved the implicit-casts issue.

Now suddenly with the switch to Dart 2.2, this assignment downcast is not working anymore. There was nothing in the Changelog that explained why it isn't working anymore.

Am I right to assume, that now, the only option I have, is to use as to forcefully cast in cases like this?
Is there another option, where I can have the previous behaviour, that allows me to only downcast, and warn if the types are incompatible?

@vsmenon vsmenon added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Mar 17, 2019
@vsmenon
Copy link
Member

vsmenon commented Mar 17, 2019

fyi - @stereotype441 @leafpetersen @srawlins - was there an intended change around assignment casts?

@leafpetersen
Copy link
Member

There was a regression in the interpretation of the implicit-casts flags in dev releases between 2.1 and 2.2 (I don't think it went out in a stable release, but could be wrong). If you want to allow assignment/declaration casts ( as in your example code above), use the following:

analyzer:
  strong-mode:
    implicit-casts: false
    declaration-casts: true
    implicit-dynamic: true

@leafpetersen
Copy link
Member

To be clear, the behavior that you are seeing now is correct: using only the implicit-casts: false setting will disable all implicit casts. If you want to opt into allowing declaration casts (as in your example), you need to do so explicitly as described in my previous comment.

@enyo
Copy link
Author

enyo commented Mar 19, 2019

Thanks @leafpetersen. declaration-casts is deprecated if I'm not mistaken. When will it be removed?

@leafpetersen
Copy link
Member

It looks like it may already have gone away in 2.2. @stereotype441 can confirm.

There was very little uptake on it, so I'm not surprised. The language is moving in the direction of removing implicit downcasts in future versions, since we get a great deal a lot of negative feedback about them.

@enyo
Copy link
Author

enyo commented Mar 20, 2019

Thanks @leafpetersen! Just to make sure: is there an alternative downcast solution? Or do I need o use the cast keyword which casts literally anything to anything without any checks?

@leafpetersen
Copy link
Member

You can use generic methods to get a hand-rolled version:

// Note, order of type variables important
T cast<T extends S, S>(S from) => from as T;

void test() {
  num y = 3;
  int x = cast(y); // Inferred
  String z = cast(y); // Error
}

This isn't all that nice to use though.

With the extension method proposal, you might be able to get nicer syntax (e.g. make cast an extension method so it can be used inline).

Feel free to leave feedback on the main implicit cast issue here or on the sub-issue for discussion of a cast operator here. We haven't had a lot of external feedback one way or the other, and there has been some internal discussion of your use case, so some concrete input would be useful.

@enyo enyo closed this as completed Mar 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

3 participants