Scala Programming Language
  1. Scala Programming Language
  2. SI-6084

Type alias in package object causes type checker assertion failure

    Details

      Description

      The program below causes a crash, apparently due to the type alias in the package object. The commented out version compiles without a crash. See the attachment for the stack trace.

      Code similar to this has been present in our Kiama library for quite a while and has compiled fine with the 2.9 series compilers.

      // This version crashes the compiler:
      
      package object foo {
      
          type ==>[T,U] = PartialFunction[T,U]
      
      }
      
      package foo {
      
          abstract class Foo[T,U] (val f : T ==> U) extends (T ==> U) {
      
              def bar (r : T ==> U) : T ==> U = 
                  r match {
                      case d : Foo[_, _] => d.f
                      case _             => r
                  }
      
          }
      
      }
      
      // This version compiles with no crashes:
      
      // package foo {
      
      //     abstract class Foo[T,U] (val f : PartialFunction[T,U]) extends (PartialFunction[T,U]) {
      
      //         def bar (r : PartialFunction[T,U]) : PartialFunction[T,U] = 
      //             r match {
      //                 case d : Foo[_, _] => d.f
      //                 case _             => r
      //             }
      
      //     }
      
      // }
      

        Activity

        Hide
        Paul Phillips added a comment -

        Someone elaborated:

        
        package object foo { type X[T, U] = (T => U) }
        
        package foo {
          abstract class Foo[T, U](val d: T => U) extends (T => U) {
            def f1(r: X[T, U])           = r match { case x: Foo[_,_] => x.d }  // inferred ok
            def f2(r: X[T, U]): (T => U) = r match { case x: Foo[_,_] => x.d }  // dealiased ok
            def f3(r: X[T, U]): X[T, U]  = r match { case x: Foo[_,_] => x.d }  // alias not ok
        
            // x.d : foo.this.package.type.X[?scala.reflect.internal.Types$NoPrefix$?.T, ?scala.reflect.internal.Types$NoPrefix$?.U] ~>scala.this.Function1[?scala.reflect.internal.Types$NoPrefix$?.T, ?scala.reflect.internal.Types$NoPrefix$?.U]
            //  at scala.Predef$.assert(Predef.scala:170)
            //  at scala.tools.nsc.Global.assert(Global.scala:235)
            //  at scala.tools.nsc.ast.TreeGen.mkCast(TreeGen.scala:252)
            //  at scala.tools.nsc.typechecker.Typers$Typer.typedCase(Typers.scala:2263)
          }
        }
        
        Show
        Paul Phillips added a comment - Someone elaborated: package object foo { type X[T, U] = (T => U) } package foo { abstract class Foo[T, U](val d: T => U) extends (T => U) { def f1(r: X[T, U]) = r match { case x: Foo[_,_] => x.d } // inferred ok def f2(r: X[T, U]): (T => U) = r match { case x: Foo[_,_] => x.d } // dealiased ok def f3(r: X[T, U]): X[T, U] = r match { case x: Foo[_,_] => x.d } // alias not ok // x.d : foo.this.package.type.X[?scala.reflect.internal.Types$NoPrefix$?.T, ?scala.reflect.internal.Types$NoPrefix$?.U] ~>scala.this.Function1[?scala.reflect.internal.Types$NoPrefix$?.T, ?scala.reflect.internal.Types$NoPrefix$?.U] // at scala.Predef$.assert(Predef.scala:170) // at scala.tools.nsc.Global.assert(Global.scala:235) // at scala.tools.nsc.ast.TreeGen.mkCast(TreeGen.scala:252) // at scala.tools.nsc.typechecker.Typers$Typer.typedCase(Typers.scala:2263) } }
        Hide
        Tony Sloane added a comment -

        This regression is still present in 2.10.0-M6.

        Show
        Tony Sloane added a comment - This regression is still present in 2.10.0-M6.
        Hide
        Jason Zaugg added a comment -

        Paul, the umbrella mkCast you added in 214c145 didn't quite fit this spot: https://github.com/scala/scala/commit/214c145#L11L1916

        Show
        Jason Zaugg added a comment - Paul, the umbrella mkCast you added in 214c145 didn't quite fit this spot: https://github.com/scala/scala/commit/214c145#L11L1916
        Hide
        Jason Zaugg added a comment -

        Marking as critical to get it into contention for 2.10: it's a regression and the fix looks trivial.

        Show
        Jason Zaugg added a comment - Marking as critical to get it into contention for 2.10: it's a regression and the fix looks trivial.
        Hide
        Paul Phillips added a comment -

        That's the right spot, although it seems like my real problem was believing the comment ("no need for pt.normalize here")

        Show
        Paul Phillips added a comment - That's the right spot, although it seems like my real problem was believing the comment ("no need for pt.normalize here")
        Show
        Paul Phillips added a comment - https://github.com/scala/scala/pull/1023
        Hide
        Tony Sloane added a comment -

        To close the loop here, I can confirm that this problem does not occur in Kiama when building with 2.10.0-M7.

        Show
        Tony Sloane added a comment - To close the loop here, I can confirm that this problem does not occur in Kiama when building with 2.10.0-M7.

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Tony Sloane
          • Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development