Skip to content

Nested defs with context bounds are desugared to nested defs with implicit parameters that have the same name #877

Closed
@smarter

Description

@smarter
class First[A]
class Second[A]

class Foo {
  def foo[A: First] = {
    def bar[B: Second] = {
      val fst: First[A] = implicitly[First[A]]
      val snd: Second[B] = implicitly[Second[B]]
    }
  }
}

is desugared to:

package <empty> {
  class First[A]() extends Object() { 
    type First$$A
    private[this] type A = First$$A
  }
  class Second[A]() extends Object() { 
    type Second$$A
    private[this] type A = Second$$A
  }
  class Foo() extends Object() { 
    def foo[A](implicit evidence$0: First[A]): Unit = {
      def bar[B](implicit evidence$0: Second[B]): Unit = {
        val fst: First[A] = implicitly[First[A]](evidence$0)
        val snd: Second[B] = implicitly[Second[B]](evidence$0)
        ()
      }
      ()
    }
  }
}

Which fails to compile because evidence$0 in bar shadows evidence$0 in foo, scalac avoids this by using different names for the desugared implicit parameters(in fact, it seems to never reuse the same name in a given compilation unit, even in different classes):

    def foo[A](implicit evidence$1: First[A]): Unit = {
      def bar[B](implicit evidence$2: Second[B]): Unit = {
        val fst: First[A] = scala.this.Predef.implicitly[First[A]](evidence$1);
        val snd: Second[B] = scala.this.Predef.implicitly[Second[B]](evidence$2);
        ()
      };
      ()
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions