Skip to content

Issue with bounded recursive match types #18546

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
SimonGuilloud opened this issue Sep 12, 2023 · 8 comments
Closed

Issue with bounded recursive match types #18546

SimonGuilloud opened this issue Sep 12, 2023 · 8 comments

Comments

@SimonGuilloud
Copy link

SimonGuilloud commented Sep 12, 2023

Compiler version

3.3.1

Minimized example

trait A[N]

type B[N] <: A[N] = N match {
  case 0 => A[0]
  case _ => A[N]
}

Output

Found:    A[(0 : Int)]
Required: A[N]

Expectation

It seems like this should compile. I'm not sure if this is a bug or intended behaviour, but I found no reference to this situation. A real life scenario would have for example a trait A0 extending A[0].

@SimonGuilloud SimonGuilloud added the stat:needs triage Every issue needs to have an "area" and "itype" label label Sep 12, 2023
@sjrd
Copy link
Member

sjrd commented Sep 12, 2023

There is no GADT reasoning inside match types. So no, this does not compile.

@SimonGuilloud
Copy link
Author

OK! But what is the GADT (or even ADT) in this example? It rather seems to me the issue is that there is no reasoning about the value of N in individual branch.
Is there a way to force this to compile, (something like asInstanceOf for types)?

@dwijnand
Copy link
Member

GADT reasoning is the name for any subtyping constraints that are learnt from a match case (more rarely also referred to as "subtype reconstruction"). Here, it's recording that N <: 0, which we don't do at the moment. Doing so is an enhancement/request.

You can force this to compile by intersecting the type you wanted to return with the bound you have. So, in the minimisation, A[0] & A[N].

@dwijnand dwijnand added itype:enhancement area:match-types area:gadt and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 13, 2023
@SimonGuilloud
Copy link
Author

SimonGuilloud commented Sep 13, 2023

GADT reasoning is the name for any subtyping constraints that are learnt from a match case (more rarely also referred to as "subtype reconstruction"). Here, it's recording that N <: 0, which we don't do at the moment. Doing so is an enhancement/request.

You can force this to compile by intersecting the type you wanted to return with the bound you have. So, in the minimisation, A[0] & A[N].

Great, thanks for the explanation!

@Linyxus
Copy link
Contributor

Linyxus commented Apr 5, 2024

I guess we could close this as not planned?

@smarter
Copy link
Member

smarter commented Apr 6, 2024

There is no GADT reasoning inside match types.

There is some common logic since #14563 which recently lead to #20032

@Linyxus
Copy link
Contributor

Linyxus commented Jul 3, 2024

It seems that this minimisation should not compile. In the match case the constraint we get is N <: 0 but to type check the body the required constraint is 0 <: N, which we cannot prove anyway.

WDYT? @dwijnand

@dwijnand
Copy link
Member

dwijnand commented Jul 3, 2024

Hmm, perhaps. On the other hand 0 is a singleton (as well as Int being final), which I mention because those are criteria in refinementIsInvariant. But ultimately I think you're right, if we call B[0 & Other] it's just a bounds violation to return A[0].

@dwijnand dwijnand closed this as not planned Won't fix, can't repro, duplicate, stale Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants