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

A singleton type is not a subtype of its refined type #10980

Closed
LPTK opened this issue Jan 3, 2021 · 3 comments · Fixed by #16135
Closed

A singleton type is not a subtype of its refined type #10980

LPTK opened this issue Jan 3, 2021 · 3 comments · Fixed by #16135
Assignees
Milestone

Comments

@LPTK
Copy link
Contributor

LPTK commented Jan 3, 2021

Minimized code

class A:
  val x: AnyRef

@main def m: Unit =
  val a = new A
  a.x: a.x.type // ok
  a: A { val x: a.x.type } // nope

https://scastie.scala-lang.org/0PCX1TY0TxCO1Voo5NohLQ

Output

Found:    (a : A)
Required: A{x: (a.x : AnyRef)}

Expectation

Should type check. This is crucially needed for more advanced dependent-typing use cases.

@LPTK LPTK added the itype:bug label Jan 3, 2021
@odersky
Copy link
Contributor

odersky commented Jan 3, 2021

@LPTK Do you think you can give this a go?

@LPTK
Copy link
Contributor Author

LPTK commented Jan 4, 2021

Ok I'll try to have a look when I have some time (in the coming weeks). Also, I seem to remember that this used to work in a previous version of Dotty.

@LPTK
Copy link
Contributor Author

LPTK commented Mar 28, 2021

Ok, so I had a look and apparently I misremembered – this never worked in Dotty, even as far back as the first RC versions.

I believe the problem is that TypeComparer accesses the underlying type of the singleton type

https://github.com/lampepfl/dotty/blob/eb7b6326bc560830cff998c70592f0da6026b01f/compiler/src/dotty/tools/dotc/core/TypeComparer.scala#L787

and that returns the denotation's info field.

What could be done would be to return a refinement/intersection of this type info with the precise types of the singleton's members. In our example, this would be A { val x: a.x.type }.

However, this goes well beyond the type of changes I'm comfortable doing in Dotty, and I predict that a naive approach would break lots of things and potentially degrade performance, so it'd have to be done very carefully and with better knowledge of Dotty's inner workings than I have.

@mbovel mbovel self-assigned this Jan 25, 2022
mbovel added a commit to dotty-staging/dotty that referenced this issue Oct 3, 2022
mbovel added a commit to dotty-staging/dotty that referenced this issue Oct 3, 2022
@mbovel mbovel linked a pull request Oct 3, 2022 that will close this issue
mpollmeier pushed a commit to mpollmeier/dotty that referenced this issue Oct 16, 2022
@Kordyjan Kordyjan added this to the 3.2.2 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants