Bug with scoped import?

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?

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

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

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.

1 Like

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