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

existential type in patmat causes cast that breaks @tailrec

    Details

      Description

      This code works for Scala 2.10.0-M2, causes warning for 2.10.0-M3, and does not work for 2.10.0-M4, 2.10.0-M5, 2.10.0-M6, and 2.10.0-M7

        private val ParentPackagePattern = """^(?:(.*)\.|)\w+$"""r
      
        @annotation.tailrec
        final def searchClass(packageName: String,
                              className: String): Class[_] = {
          packageName match {
            case null | "" =>
              Class.forName(className)
            case ParentPackagePattern(parentPackageName) =>
              try {
                return Class.forName(packageName + "." + className)
              } catch {
                case e: ClassNotFoundException =>
              }
              searchClass(parentPackageName, className)
            case _ =>
              throw new IllegalArgumentException("Bad package name: " + packageName)
          }
        }
      

        Activity

        Hide
        Vlad Ureche added a comment - - edited

        What is the error message? Can it be related to SI-4649?

        Show
        Vlad Ureche added a comment - - edited What is the error message? Can it be related to SI-4649 ?
        Hide
        Vlad Ureche added a comment -

        Adriaan, since this is a regression, it should be critical, right?

        Show
        Vlad Ureche added a comment - Adriaan, since this is a regression, it should be critical, right?
        Hide
        Adriaan Moors added a comment -
        object Test {
          // the existential causes a cast and the cast makes searchClass not be in tail position
          // can we get rid of the useless cast?
          @annotation.tailrec
          final def searchClass: Class[_] = {
            "packageName" match {
              case _ =>
                searchClass
            }
          }
        }
        
        Show
        Adriaan Moors added a comment - object Test { // the existential causes a cast and the cast makes searchClass not be in tail position // can we get rid of the useless cast? @annotation.tailrec final def searchClass: Class[_] = { "packageName" match { case _ => searchClass } } }
        Show
        Adriaan Moors added a comment - https://github.com/scala/scala/pull/1064
        Hide
        杨博 added a comment - - edited

        The origin test case still cannot be compiled for Scala 2.10.0-M7

        Show
        杨博 added a comment - - edited The origin test case still cannot be compiled for Scala 2.10.0-M7
        Hide
        Adriaan Moors added a comment -

        there were two problems with the original test: one was fixed (the existential type that made a tail position into a non-tail position), the other is not a regression: the try/catch with the return was never supported by tailcalls. Here's a simpler (yet isomorphic) test case that does not work on 2.9 nor 2.10:

         @annotation.tailrec
         final def searchClass: String = {
           "x" match {
             case null =>
               try {
                 ""
               } catch {
                 case e: ClassNotFoundException =>
                  searchClass
               }
           }
         }
        
        Show
        Adriaan Moors added a comment - there were two problems with the original test: one was fixed (the existential type that made a tail position into a non-tail position), the other is not a regression: the try/catch with the return was never supported by tailcalls. Here's a simpler (yet isomorphic) test case that does not work on 2.9 nor 2.10: @annotation.tailrec final def searchClass: String = { "x" match { case null => try { "" } catch { case e: ClassNotFoundException => searchClass } } }
        Hide
        Adriaan Moors added a comment -

        I'm not sure we can support tail calls in try/catches or mixed with return statements. Please open a separate ticket for that if the improvement is important to you. Thank you!

        Show
        Adriaan Moors added a comment - I'm not sure we can support tail calls in try/catches or mixed with return statements. Please open a separate ticket for that if the improvement is important to you. Thank you!
        Hide
        杨博 added a comment -

        The try/catch issue is also a regression. I open another ticket here: https://issues.scala-lang.org/browse/SI-6479

        Show
        杨博 added a comment - The try/catch issue is also a regression. I open another ticket here: https://issues.scala-lang.org/browse/SI-6479

          People

          • Assignee:
            Adriaan Moors
            Reporter:
            杨博
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development