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

Match Types for Type Projection not working in 3.4.0-RC1 #19445

Closed
chungonn opened this issue Jan 15, 2024 · 4 comments
Closed

Match Types for Type Projection not working in 3.4.0-RC1 #19445

chungonn opened this issue Jan 15, 2024 · 4 comments

Comments

@chungonn
Copy link

The minimized code below compiles in Scala 3.3.1 but is failing in Scala 3.4.0-RC1. Is this a bug or an expected behaviour in Scala 3.4.0? If yes, is there any workaround for this?

trait Foo:
  case class Bar[A](value: A)

object FooBar extends Foo

type UnwrapTypes[Xs] =
  Xs match
    case EmptyTuple => Xs
    case x *: xs => UnwrapTypes[x] *: UnwrapTypes[xs]
    case Foo#Bar[x] => UnwrapTypes[x]
    case String => String
    case Int => Int

val x: Int = ??? : UnwrapTypes[FooBar.Bar[Int]]
val tup: (Int, String)  = ??? : UnwrapTypes[(FooBar.Bar[Int], FooBar.Bar[String])]

Output

[info] compiling 1 Scala source to /home/chungonn/development/experiments/scala3-types-match/target/scala-3.4.0-RC1/classes ...
[error] -- [E007] Type Mismatch Error: /home/chungonn/development/experiments/scala3-types-match/src/main/scala/matchtypes/Main.scala:16:13 
[error] 16 |val x: Int = ??? : UnwrapTypes[FooBar.Bar[Int]]
[error]    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |Found:    matchtypes.UnwrapTypes[matchtypes.FooBar.Bar[Int]]
[error]    |Required: Int
[error]    |
[error]    |Note: a match type could not be fully reduced:
[error]    |
[error]    |  trying to reduce  matchtypes.UnwrapTypes[matchtypes.FooBar.Bar[Int]]
[error]    |  failed since selector matchtypes.FooBar.Bar[Int]
[error]    |  does not match  case matchtypes.Foo#Bar[x] => matchtypes.UnwrapTypes[x]
[error]    |  and cannot be shown to be disjoint from it either.
[error]    |  Therefore, reduction cannot advance to the remaining cases
[error]    |
[error]    |    case String => String
[error]    |    case Int => Int
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] -- [E007] Type Mismatch Error: /home/chungonn/development/experiments/scala3-types-match/src/main/scala/matchtypes/Main.scala:17:26 
[error] 17 |val tup: (Int, String)  = ??? : UnwrapTypes[(FooBar.Bar[Int], FooBar.Bar[String])]
[error]    |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |Found:    (matchtypes.UnwrapTypes[matchtypes.FooBar.Bar[Int]],
[error]    |  matchtypes.UnwrapTypes[matchtypes.FooBar.Bar[String]])
[error]    |Required: (Int, String)
[error]    |
[error]    |Note: a match type could not be fully reduced:
[error]    |
[error]    |  trying to reduce  matchtypes.UnwrapTypes[matchtypes.FooBar.Bar[Int]]
[error]    |  failed since selector matchtypes.FooBar.Bar[Int]
[error]    |  does not match  case matchtypes.Foo#Bar[x] => matchtypes.UnwrapTypes[x]
[error]    |  and cannot be shown to be disjoint from it either.
[error]    |  Therefore, reduction cannot advance to the remaining cases
[error]    |
[error]    |    case String => String
[error]    |    case Int => Int
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
@chungonn chungonn added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 15, 2024
@soronpo
Copy link
Contributor

soronpo commented Jan 15, 2024

@sjrd Maybe you can respond.

@jchyb
Copy link
Contributor

jchyb commented Jan 15, 2024

Minimized (tuples does not seem to cause any problems here):

trait Foo:
  case class Bar[A](value: A)

object FooBar extends Foo

type UnwrapTypes[Xs] =
  Xs match
    case Foo#Bar[x] => UnwrapTypes[x]
    case Int => Int

val x: Int = ??? : UnwrapTypes[FooBar.Bar[Int]]

@jchyb jchyb added area:match-types and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 15, 2024
@chungonn
Copy link
Author

chungonn commented Jan 15, 2024

@jchyb your code does not compile both in my environment (Ubuntu 23.10, Java 17, Scala 3.4.0-RC1) and in scastie

https://scastie.scala-lang.org/IaEVOKQWSHqmoTmZH7U7zg

Here's the compilation error with your code. Also, I reported the problem using type matching the Tuple type :)

[error] -- [E007] Type Mismatch Error: /home/chungonn/development/experiments/scala3-types-match/src/main/scala/matchtypes/Main2.scala:14:13 
[error] 14 |val x: Int = ??? : UnwrapTypes[FooBar.Bar[Int]]
[error]    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |Found:    matchtypes.UnwrapTypes[matchtypes.FooBar.Bar[Int]]
[error]    |Required: Int
[error]    |
[error]    |Note: a match type could not be fully reduced:
[error]    |
[error]    |  trying to reduce  matchtypes.UnwrapTypes[matchtypes.FooBar.Bar[Int]]
[error]    |  failed since selector matchtypes.FooBar.Bar[Int]
[error]    |  does not match  case matchtypes.Foo#Bar[x] => matchtypes.UnwrapTypes[x]
[error]    |  and cannot be shown to be disjoint from it either.
[error]    |  Therefore, reduction cannot advance to the remaining case
[error]    |
[error]    |    case Int => Int
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 0 s, completed Jan 16, 2024, 6:24:27 AM

Here's my original reported code in scastie

https://scastie.scala-lang.org/21Lz8vxoQMOIwwCO4KySQw

Both above snippet code will only compile using 3.3.1

@sjrd
Copy link
Member

sjrd commented Jan 16, 2024

Looks like a bug, indeed.

@sjrd sjrd self-assigned this Jan 16, 2024
odersky added a commit that referenced this issue Jan 23, 2024
Previously, for a `BaseTypeTest`, we explicitly checked the prefix of
the type constructor for `=:=`. This is however too strict, as shown by
#19445. We remove the test entirely, as the correct subprefixing test is
already part of the overall
`scrut <:< instantiatedPat` done at the end.
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

4 participants