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

Unreachable case in TypeTest #18052

Open
bblfish opened this issue Jun 24, 2023 · 4 comments
Open

Unreachable case in TypeTest #18052

bblfish opened this issue Jun 24, 2023 · 4 comments

Comments

@bblfish
Copy link

bblfish commented Jun 24, 2023

Compiler version

Following the fix to issue 16408 I tried to use
Scala 3.3.2-RC1-bin-20230623-171849f-NIGHTLY
in the scala3 version of banana-rdf.

Minimized code

I adapted the longer code from 16408 in the unreachableCase branch RDF.scala and was able to duplicate the warning.
The difference as seen in this commit is just the addition of

  • a BNode type
  • a type Subject[R <: RDF] = URI[R] | BNode[R]
  • a given subjToURITT: TypeTest[RDF.Subject[R], RDF.URI[R]]

(the implementations are in terms of traits rather than a class, but that makes no difference anymore).

import scala.util.Try
import scala.reflect.TypeTest

trait RDF:
  rdf =>

  type R = rdf.type
  type Node <: Matchable
  type URI <: Node 
  type BNode <: Node 

  given rops: ROps[R]
end RDF

object RDF:
  type Node[R <: RDF] = R match
    case GetNode[n] => Matchable //n & rNode[R]

  type URI[R <: RDF] <: Node[R] = R match
    case GetURI[u] => u & Node[R] 

  type BNode[R <: RDF] <: Node[R] = R match
    case GetBNode[b] => b & Node[R] 

  type Subject[R <: RDF] = URI[R] | BNode[R]  

  private type GetNode[N] = RDF { type Node = N }
  private type GetURI[U] = RDF { type URI = U }
  private type GetBNode[B] = RDF { type BNode = B }
end RDF

trait ROps[R <: RDF]:
  def mkUri(str: String): Try[RDF.URI[R]]
  def auth(uri: RDF.URI[R]): Try[String]
  given subjToURITT: TypeTest[RDF.Subject[R], RDF.URI[R]]

object TraitTypes:
  trait Node:
    def value: String

  trait Uri extends Node
  trait BNode extends Node
  
  def mkUri(u: String): Uri =
    new Uri { def value = u }

object TraitRDF extends RDF:
  import TraitTypes as tz

  override opaque type Node <: Matchable = tz.Node
  override opaque type URI <: Node = tz.Uri

  given rops: ROps[R] with
    override def mkUri(str: String): Try[RDF.URI[R]] = Try(tz.mkUri(str))
    override def auth(uri: RDF.URI[R]): Try[String] =
      Try(java.net.URI.create(uri.value).getAuthority())

    given subjToURITT: TypeTest[RDF.Subject[R], RDF.URI[R]] with
       override def unapply(s: RDF.Subject[R]): Option[s.type & URI] =
         s match
           case x: (s.type & RDF.URI[R]) => Some(x)
           case _                        => None
  
end TraitRDF

class Test[R <: RDF](using rops: ROps[R]):
  import rops.given
  lazy val uriT: Try[RDF.URI[R]] = rops.mkUri("https://bblfish.net/#i")
  lazy val x: String = "uri authority=" + uriT.map(u => rops.auth(u))

@main def run =
  val test = Test[TraitRDF.type]
  println(test.x)

Output

$ scala-cli compile -j 17 -S 3.nightly scala
Compiling project (Scala 3.3.2-RC1-bin-20230623-171849f-NIGHTLY, JVM)
[warn] ./scala/RDF.scala:62:17
[warn] Unreachable case except for null (if this is intentional, consider writing case null => instead).
[warn]            case _                        => None
[warn]                 ^
Compiled project (Scala 3.3.2-RC1-bin-20230623-171849f-NIGHTLY, JVM)

Expectation

There should be no warning on unreachable case there as clearly the BNode case is possible.

@bblfish bblfish added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 24, 2023
@odersky odersky added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 24, 2023
@bblfish
Copy link
Author

bblfish commented Jul 15, 2023

Interestingly the above code worked in release 3.0

$ scala-cli compile -j 17 -S 3.0 scala
Downloading Scala 3.0.2 compiler
Compiling project (Scala 3.0.2, JVM)
Compiled project (Scala 3.0.2, JVM)

It worked in release 3.1

$ scala-cli compile -j 17 -S 3.1 scala
Downloading Scala 3.1.3 compiler
Compiling project (Scala 3.1.3, JVM)
Compiled project (Scala 3.1.3, JVM)

and in 3.2

scala-cli compile -j 17 -S 3.2 scala
Compiling project (Scala 3.2.2, JVM)
Compiled project (Scala 3.2.2, JVM)

and in

scala-cli compile -j 17 -S 3.3 scala
Compiling project (Scala 3.3.0, JVM)
Compiled project (Scala 3.3.0, JVM)

but not in the nightly

scala-cli compile -j 17 -S 3.nightly scala

Downloading Scala 3.3.2-RC1-bin-20230714-04eae14-NIGHTLY compiler
Compiling project (Scala 3.3.2-RC1-bin-20230714-04eae14-NIGHTLY, JVM)
[warn] ./scala/RDF.scala:63:17
[warn] Unreachable case except for null (if this is intentional, consider writing case null => instead).
[warn]            case _                        => None
[warn]                 ^
Compiled project (Scala 3.3.2-RC1-bin-20230714-04eae14-NIGHTLY, JVM)

@bblfish
Copy link
Author

bblfish commented Jul 22, 2023

Oops. Warning! It looks like this bug has introduced itself into 3.3.1 Release Candidate 4!

$ scala-cli compile -j 17 -S "3.3.1-RC4" scala

Compiling project (Scala 3.3.1-RC4, JVM)
[warn] ./scala/RDF.scala:63:17
[warn] Unreachable case except for null (if this is intentional, consider writing case null => instead).
[warn]            case _                        => None
[warn]                 ^
Compiled project (Scala 3.3.1-RC4, JVM)

@bblfish
Copy link
Author

bblfish commented Aug 27, 2023

Peraps the issue is fixed?

I don't get an error anymore with either,

$ scala-cli compile -j 17 -S 3.3.1-RC6 scala
$ scala-cli compile -j 17 -S 3.nightly scala

@dwijnand
Copy link
Member

Possibly with #18303.

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

3 participants