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

Type avoidance does not take type parameter bounds in account and can produce invalid types #6205

Open
smarter opened this issue Apr 1, 2019 · 2 comments
Labels
area:typer backlog No work planned on this by the core team for the time being. itype:enhancement

Comments

@smarter
Copy link
Member

smarter commented Apr 1, 2019

Given:

class Contra[-T >: Null]

object Test {
  def foo = {
    class A

    new Contra[A]
  }
}

The inferred type of foo is Contra[Nothing], but this isn't a legal type (and indeed writing it down would produce a Type argument Nothing does not conform to lower bound Null error).
It seems that ApproximatingTypeMap#derivedAppliedType is missing logic to replace Ranges by types which are in bounds. Moreover, -Ycheck should be enhanced to detect this kind of bound violations.

odersky added a commit to dotty-staging/dotty that referenced this issue Mar 8, 2020
@odersky
Copy link
Contributor

odersky commented Mar 8, 2020

This is now detected with #8462. We now get:

4 |  def foo =
  |         ^
  |Type argument Nothing does not conform to lower bound Null in inferred type Contra[Nothing]

It would be even better if we could avoid bad instantiations, so I leave this issue open. But it is not that easy to do so. In the particular example, we need to avoid class A, which leads to a range of Nothing .. A, which is immediately simplified to Nothing, since we are in a contravariant context. So then the type map would have to change inferred arguments bounds one level up when it gets to the applied type Contra[Nothing]. Furthermore, it would have to find a "solution" even in cases where we have F-bounds, which is impossible in general, so there would still have to be a path where we issue an error.

If the problem happens only rarely, I think it's acceptable to just tell the user that the instantiation is ill-formed and they should add an explicit type instead.

@smarter
Copy link
Member Author

smarter commented Mar 8, 2020

Furthermore, it would have to find a "solution" even in cases where we have F-bounds, which is impossible in general, so there would still have to be a path where we issue an error.

I think you can always use wildcard arguments as a fallback solution.

@odersky odersky added the backlog No work planned on this by the core team for the time being. label Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typer backlog No work planned on this by the core team for the time being. itype:enhancement
Projects
None yet
Development

No branches or pull requests

2 participants