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

Equality in type refinement fails in some contexts #8599

Open
scabug opened this issue May 17, 2014 · 3 comments
Open

Equality in type refinement fails in some contexts #8599

scabug opened this issue May 17, 2014 · 3 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented May 17, 2014

The following code uses type refinement to prove the equality of two types. The equality sought is essentially the same in each case, but in one it succeeds and in one it fails:

object RefinementTest {
    sealed trait Grandparent[A]
    sealed trait Parent[A] extends Grandparent[A]
    case class Child[C](c: (C, C)) extends Parent[(C, C)]

    def extractParent[A](parent: Parent[A]): A = parent match {
      case child: Child[c] => child.c
    }

    // Fails
    def extractGrandparent1[A](grandparent: Grandparent[A]): A = grandparent match {
      case parent: Parent[a] =>
        parent match {
          //     Error: type mismatch;
          //     found   : (c, c)
          //     required: a
          case child: Child[c] => child.c: a
        }
    }

    // Works
    def extractGrandparent2[A](grandparent: Grandparent[A]): A = grandparent match {
      case parent: Parent[a] => extractParent(parent)
    }
  }
@scabug
Copy link
Author

scabug commented May 17, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8599?orig=1
Reporter: Owen Healy (ellbur)
Affected Versions: 2.11.0

@scabug
Copy link
Author

scabug commented May 17, 2014

@retronym said:
The relevant players in this bug / limitation are shown in the following log output:

% qbin/scalac -Ylog:typer -Xdev -explaintypes sandbox/test.scala
[log typer] updated bounds: type A from  to:  >: (c, c) <: (c, c)
[log typer] instantiateTypeVar with nonEmpty saved type bounds {
  enclosing  Context(test.scala) {    owner       = method extractParent
      saved  List((type A,))
     tparam  AbstractTypeSymbol type c
}
[log typer] redundant bounds: discarding TypeBounds(Nothing, Any) for type c, no improvement on TypeBounds(Nothing, Any)
[log typer] Discarding inferred TypeBounds(lo=(c, c), hi=(c, c)) because type A does not appear in: (c, c)
[log typer] Adding cast to pattern because (c, c) does not conform to expected type A
[log typer] updated bounds: type a from  to:  >: A <: A
[log typer] inconsistent bounds: discarding TypeBounds(Any, A with (c, c))
[log typer] redundant bounds: discarding TypeBounds(Nothing, Any) for type c, no improvement on TypeBounds(Nothing, Any)
sandbox/test.scala:17: error: type mismatch;
 found   : (c, c)
 required: a
        case child: Child[c] => child.c: a
                                      ^
(c, c) <: a?
false
[log typer] Discarding inferred TypeBounds(lo=A, hi=A) because type a does not appear in: A
[log typer] updated bounds: type a from  to:  >: A <: A
warning: !!! Preserving inference: a=A in a (based on TypeBounds(lo=A, hi=A)) before restoring type a to saved <empty bounds>: A
one warning found
one error found

@scabug
Copy link
Author

scabug commented May 17, 2014

@retronym said:
scala/scala#3759

@scabug scabug added the typer label Apr 7, 2017
@scabug scabug added this to the Backlog milestone Apr 7, 2017
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

2 participants