Skip to content

Strong mode analyzer inconsistent about when types must match #30385

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

Closed
Hixie opened this issue Aug 9, 2017 · 6 comments
Closed

Strong mode analyzer inconsistent about when types must match #30385

Hixie opened this issue Aug 9, 2017 · 6 comments
Labels
closed-duplicate Closed in favor of an existing report legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@Hixie
Copy link
Contributor

Hixie commented Aug 9, 2017

Consider this code:

class A { }
class B extends A { }

void test<T>(T v) { }

void main() {
  final A x = new A();
  test<B>(x); // no error

  test<B>(new A()); // strong_mode_invalid_cast_new_expr
}

Why are these different?

@Hixie
Copy link
Contributor Author

Hixie commented Aug 9, 2017

For the record, I would have expected an error in both cases.

@bwilkerson
Copy link
Member

@leafpetersen

@leafpetersen
Copy link
Member

You've asked me about this before. :) Our previous discussion of which led to these two issues being filed:

#29548
#29547

Strong mode makes implicit downcasts an error when it can immediately prove that the cast will fail. It's not smart enough to track the fact that x is definitely an A and not a B in your example, but it does notice when you try to cast a fresh instance of A to a B.

The --no-implicit-casts flag will make both of those an error.

@Hixie
Copy link
Contributor Author

Hixie commented Aug 10, 2017

Why would we not make implicit downcasts an error in general? They're not sound, right? I thought the point was to be sound.

@Hixie
Copy link
Contributor Author

Hixie commented Aug 10, 2017

So as @leafpetersen said, you can turn off implicit casts, that's implicit-casts: false in your analysis options. Unfortunately that isn't ready for prime time yet. Filed #30402 on some issues I found trying to turn that on.

@bwilkerson bwilkerson added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug labels Aug 14, 2017
@jmesserly
Copy link

removing implicit downcasts is now tracked in #31410

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants