Skip to content

Inferred type refers to a local TermRef #2239

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

Closed
liufengyun opened this issue Apr 12, 2017 · 2 comments
Closed

Inferred type refers to a local TermRef #2239

liufengyun opened this issue Apr 12, 2017 · 2 comments

Comments

@liufengyun
Copy link
Contributor

The following code compiles fine with Scalac, but fails in Dotty. If the commented version of ~++ is used, everything is fine.

trait Rule[-In, +A] extends (In => A) {
  def flatMap[In2 <: In, B](fa2ruleb: A => Rule[In2, Seq[B]]): Rule[In, Seq[B]] = ???
  def map[B](fa2b: A => B): Rule[In, B] = ???

  def ~++[In2, B >: A](next: => Rule[In2, Seq[B]]) = for (a <- this; b <- next) yield a :: b.toList
  // def ~++[In2, B >: A](next: => Rule[In2, Seq[B]]): Rule[In, Seq[B]] = for (a <- this; b <- next) yield a :: b.toList
}

class SeqRule {
  type S
  type A
  def * : Rule[S, List[A]] = ???
  def +(rule: Rule[S, A]) : Rule[S, Seq[A]] = rule ~++ *
}

The cause is that ~++ gets an "ill" type that refers to the local variable b (see below at bottom). The check is not handled by Typer#escapingRefs. I tried to modify TypeOps#simplify to simplify TypeRef(TermRef(NoPrefix,b),scala$collection$TraversableOnce$$A) to just ParamRef(B), but didn't succeed.

PolyType(
  List(In2, B),

  List(TypeBounds(TypeRef(ThisType(TypeRef(NoPrefix,scala)),Nothing), TypeRef(ThisType(TypeRef(NoPrefix,scala)),Any)), TypeBounds(TypeRef(TermRef(NoPrefix,rule),Rule$$A), TypeRef(ThisType(TypeRef(NoPrefix,scala)),Any))),

  MethodType(
    List(next),
    List(
      ExprType(
        RefinedType(
          RefinedType(
            TypeRef(ThisType(TypeRef(NoPrefix,<empty>)),Rule),
            Rule$$In,
            TypeAlias(ParamRef(In2), -1)
          ),

          Rule$$A,
          TypeAlias(
            RefinedType(
              TypeRef(ThisType(TypeRef(NoPrefix,collection)),Seq),
              scala$collection$Seq$$A,
              TypeAlias(ParamRef(B), 1)
            ),
          1))
      )
    ),

    RefinedType(
      RefinedType(
        TypeRef(ThisType(TypeRef(NoPrefix,<empty>)),Rule),
        Rule$$In,
        TypeAlias(
          TypeRef(TermRef(NoPrefix,rule),Rule$$In),
        -1)
      ),

      Rule$$A,

      TypeAlias(
        RefinedType(
          TypeRef(ThisType(TypeRef(NoPrefix,collection)),Seq),
          scala$collection$Seq$$A,
          TypeAlias(TypeRef(TermRef(NoPrefix,b),scala$collection$TraversableOnce$$A), 1) <=== b !!
        ),
      1)
   )
  )
)

The expanded version of ~++ looks like follows:

    def ~++[In2, B >: A](next: => Rule[In2, Seq[B]]):
      Rule[In, scala.collection.Seq[B]]
     =
      this.flatMap[Nothing^, B^](
        {
          def $anonfun(a: A): Rule[Nothing^, scala.collection.Seq[B^]] =
            next.map[scala.collection.immutable.List[B]^](
              {
                def $anonfun(b: scala.collection.Seq[B]):
                  scala.collection.immutable.List[B]
                 =
                  {
                    val $1$: A = a
                    b.toList.::[B^]($1$)
                  }
                closure($anonfun)
              }
            )
          closure($anonfun)
        }
      )
  }

Note: this test case is a minimized version from the library json4s.

@felixmulder
Copy link
Contributor

What's the status of this? It seems to be blocking json4s for #2182? Or is that no longer relevant to said issue?

liufengyun added a commit to dotty-staging/dotty that referenced this issue Sep 11, 2017
allanrenucci pushed a commit that referenced this issue Sep 12, 2017
@liufengyun
Copy link
Contributor Author

Note this issue is fixed by #3061 automatically, another merit of the native apply refactoring.

liufengyun added a commit to dotty-staging/dotty that referenced this issue Sep 21, 2017
liufengyun added a commit that referenced this issue Sep 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants