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

emit unchecked warning when outer is not checked in pattern match #6583

Open
scabug opened this issue Oct 29, 2012 · 6 comments
Open

emit unchecked warning when outer is not checked in pattern match #6583

scabug opened this issue Oct 29, 2012 · 6 comments
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Oct 29, 2012

Maybe 3 different bugs in here? You decide.

  1. No unchecked warnings.
  2. See contents of Test3.
  3. ClassCastException for Test1.
trait Outer[T] {
  sealed trait Inner
  case class C1(val x: T) extends Inner
  case class C2(val x: T) extends Inner
}

object Test1 extends Outer[Int] {
  def f(x: Any) = println(x match {
    case C1(x) => true
    case _     => false
  })

  def main(args: Array[String]): Unit = {
    f(Test1.C1(5))
    f(Test2.C1("abc"))
  }
}

object Test2 extends Outer[String] { }

object Test3 {
  type SomeInner = Outer[X]#Inner forSome { type X }

  // Clearly Test1.C1(5) is an instance of SomeInner, as seen here:
  var x: SomeInner = _
  x = Test1.C1(5)

  // And yet,
  //
  // def f0(x: SomeInner) = x match { case Test1.C1(_) => }
  //
  // ./a.scala:35: error: constructor cannot be instantiated to expected type;
  //  found   : Test1.C1
  //  required: Outer[X]#Inner where type X
  //   def f0(x: SomeInner) = x match { case Test1.C1(_) => }
  //
  // Whereas this is no problem:
  def f1(x: Any) = x match { case Test1.C1(_) => }
}

/**

% rcscalac -unchecked ./a.scala
% rcscala Test1
true
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

**/
@scabug
Copy link
Author

scabug commented Oct 29, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6583?orig=1
Reporter: @paulp
Affected Versions: 2.10.0

@scabug
Copy link
Author

scabug commented Oct 30, 2012

@adriaanm said:
I'd say two bugs:

  • we should either emit an unchecked warning or do the outer test (I'm reusing logic from the old pattern matcher to decide that: needsOuterTest in Types)
  • existential types should be approximated (to wildcards) before checking type conformance in adaptConstrPattern

@scabug
Copy link
Author

scabug commented Oct 30, 2012

@adriaanm said:
in 2.10.x we cannot do better than emitting unchecked warnings, and we should decide for 2.11 whether to change the spec or do better outer tests (i'll open a separate ticket for that)

@scabug
Copy link
Author

scabug commented Feb 5, 2013

@adriaanm said:
on the fence about changing warning behaviour in a minor release

@scabug
Copy link
Author

scabug commented Feb 10, 2017

Bernhard Messerer (messi) said:
Has this been forgotten? This is really, really awkward now that no warnings are emitted anymore and just adds to the things one needs to "just know" about Scala, adding to complexity. While the "best" solution would be for these things to "just work", a warning would be better than nothing.

@SethTisue
Copy link
Member

fyi, the largest ticket/discussion in this general area is #4440

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants