Bug with scoped import?


#1

The following code cannot be compiled with 2.12.8:

package foo

class Bar(y: Int) {
  import Bar.x

  def this() = this(x)
}

object Bar {
  def x = 42
}

It works fine if the import statement is moved outside class Bar. Is this an intended behavior, based on some peculiarities of import I’m not aware of, or a bug?


#2

Must be some ancient scoping bug. But I can’t immediately find a relevant bug report.
Even dotty has this issue.


#3

#4

If you’re (relatively) sure this is not a known bug, I suggest you report it.


#5

This behavior is correct. From https://www.scala-lang.org/files/archive/spec/2.11/05-classes-and-objects.html#constructor-definitions:

The signature and the self constructor invocation of a constructor definition are type-checked and evaluated in the scope which is in effect at the point of the enclosing class definition, augmented by any type parameters of the enclosing class and by any early definitions of the enclosing template.


#6

It makes sense. I suspected something like that, but wasn’t sure it would apply to imports as well. Thanks.