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

Inappropriate type is inferred for an extracted value

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: Scala 2.11.0-M7
    • Fix Version/s: Scala 2.11.0-M8
    • Component/s: Pattern Matcher
    • Labels:
    • Environment:

      Tested on 2.11 master

      Description

      object Test extends App {
        case class Number(i: Int)
      
        object UnliftNumber {
          def unapply(t: Any): Option[Number] = t match {
            case i: Int => Some(Number(i))
            case _ => None
          }
        }
      
        def eval(expr: Any): Option[Number] = expr match {
          case UnliftNumber(n) => Some(n)
          case _ => None
        }
      
        println(eval(1))
      }
      
      Test.scala:15: error: type mismatch;
       found   : Int
       required: Test.Number
          case UnliftNumber(n) => Some(n)
                                       ^
      one error found
      

      If we the return type of eval is ommited the code compiles and runs just fine:

      $ quickc -d sandbox Test.scala
      
      $ quick -cp sandbox Test
      Some(Number(1))
      

        Activity

        Hide
        Denys Shabalin added a comment -

        Might be related to SI-7850

        Show
        Denys Shabalin added a comment - Might be related to SI-7850
        Hide
        Jason Zaugg added a comment -
        Show
        Jason Zaugg added a comment - Yep, regressed in https://github.com/scala/scala/pull/2848
        Hide
        Jason Zaugg added a comment -

        Diffing the -Ytyper-debug before/after the regression: https://gist.github.com/retronym/7876473

        Show
        Jason Zaugg added a comment - Diffing the -Ytyper-debug before/after the regression: https://gist.github.com/retronym/7876473
        Hide
        Jason Zaugg added a comment -

        Changing `Number` to a non-case class works:

        object Test extends App {
          class Number(i: Int)
         
          object UnliftNumber {
            def unapply(t: Any): Option[Number] = t match {
              case i: Int => Some(new Number(i))
              case _ => None
            }
          }
         
          def eval(expr: Any): Option[Number] = expr match {
            case UnliftNumber(n) => Some(n)
            case _ => None
          }
         
          println(eval(1))
        }
        

        This code seems culpable:

            /** If this is a case class, the case field accessors (which may be an empty list.)
             *  Otherwise, if there are any product selectors, that list.
             *  Otherwise, a list containing only the type itself.
             */
            def typesOfSelectorsOrSelf(tp: Type): List[Type] = (
              if (tp.typeSymbol.isCase)
                typesOfCaseAccessors(tp)
              else typesOfSelectors(tp) match {
                case Nil => tp :: Nil
                case tps => tps
              }
            )
        
        Show
        Jason Zaugg added a comment - Changing `Number` to a non-case class works: object Test extends App { class Number(i: Int) object UnliftNumber { def unapply(t: Any): Option[Number] = t match { case i: Int => Some(new Number(i)) case _ => None } } def eval(expr: Any): Option[Number] = expr match { case UnliftNumber(n) => Some(n) case _ => None } println(eval(1)) } This code seems culpable: /** If this is a case class, the case field accessors (which may be an empty list.) * Otherwise, if there are any product selectors, that list. * Otherwise, a list containing only the type itself. */ def typesOfSelectorsOrSelf(tp: Type): List[Type] = ( if (tp.typeSymbol.isCase) typesOfCaseAccessors(tp) else typesOfSelectors(tp) match { case Nil => tp :: Nil case tps => tps } )
        Show
        Jason Zaugg added a comment - WIP: https://github.com/retronym/scala/compare/ticket;8045?expand=1
        Show
        Paul Phillips added a comment - https://github.com/scala/scala/pull/3275

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Denys Shabalin
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development