question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pattern matching on a local class is unsound, should emit an unchecked warning

See original GitHub issue

Adapted from http://wouter.coekaerts.be/2018/java-type-system-broken which demonstrates the same thing in Java:

object Test {
  var prev: Any = _
  def test[T](x: T): T = {
    class A(val elem: T)
    if (prev == null) {
      val a = new A(x)
      prev = a
      x
    } else {
      prev match {
        case prev: A => // This should warn, but doesn't
          prev.elem
        case _ =>
          x
      }
    }
  }

  def main(args: Array[String]): Unit = {
    test(1)
    val x: String = test("") // ClassCastException: java.lang.Integer cannot be cast to java.lang.String
  }
}

There’s no way to distinguish instances of a local class coming from different method calls, so the type test is never safe and should always require an @unchecked annotation.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dwijnandcommented, Oct 28, 2022
0reactions
SethTisuecommented, Oct 28, 2022

(It would be nice if the error message gave some clue as to the specific nature of the problem. I wouldn’t have guessed this — neither @WojciechMazur nor I guessed it at today at #16259.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unsound implementation of singleton type matching #9359
#8430 - I was hoping that we could just emit a warning along the lines of "singleton type patterns are unsound with the...
Read more >
Warning about an unchecked type argument in this Scala ...
Scala version 2.9.0.1. I don't see how an erased type parameter is needed to perform the match. That first case clause is meant...
Read more >
Recommended Scalac Flags for 2.12 - tpolecat
scalacOptions ++= Seq( "-deprecation", // Emit warning and location for ... "-Xlint:unsound-match", // Pattern match may not be typesafe.
Read more >
Chapter 8. Classes - Oracle Help Center
Class declarations define new reference types and describe how they are implemented (§8.1). A top level class is a class that is not...
Read more >
A tour of the Dart language
This page shows you how to use each major Dart feature, from variables and operators to classes and libraries, with the assumption that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found