Skip to content
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

import shadows enclosing packaging #11296

Closed
som-snytt opened this issue Feb 3, 2021 · 3 comments
Closed

import shadows enclosing packaging #11296

som-snytt opened this issue Feb 3, 2021 · 3 comments

Comments

@som-snytt
Copy link
Contributor

Compiler version

Scala compiler version 3.0.0-M3 -- Copyright 2002-2020, LAMP/EPFL

Minimized code

// test.scala
package p {
  class C
}

package q {
  package p {
    class K {
      import _root_.p
      def f() = new p.C
    }
  }
}

Output

Hey, it compiles!

Expectation

An import cannot shadow an enclosing definition.

s/package p/object p results in expected ambiguous reference.

Arguably a package is not a definition, the symbol is introduced by a packaging.

Here, q.p is made available via packagings in the current compilation unit; for definitions, that binding is higher precedence than an import.

Possibly, this behavior is expected or understood, and the spec should say explicitly that bindings of names of packagings are always of least precedence.

@odersky
Copy link
Contributor

odersky commented Feb 3, 2021

The behavior is as expected. Package definitions are not definitions.

I think we will need a separate repo for spec bugs and suggestions

@som-snytt
Copy link
Contributor Author

som-snytt commented Feb 3, 2021

This ticket duplicates #9889 which has a slightly more compelling test case, where multiple package util compete.

The ergonomics is to avoid ambiguity with enclosing packages:

package mutable {
  import collection.mutable
  def f() = mutable.ListBuffer.empty[Int]   // obviously OK?
}

That example was obviously OK to me until a second ago, when I felt déjà vu. When bugs in imports were fixed on Scala 2, some code with illegible expectations broke. Like the old joke about variance, "Add import util._ until it compiles."

(Edit: the clarifying comment with a similar example was added to the Scala 2 spec several years ago. Unfortunately, the note includes the misnomer, "package definition", instead of "packaging".)

This ticket and the linked ticket can be closed or moved to spec-land or the RAQ for rarely asked questions.

@som-snytt
Copy link
Contributor Author

Closing in favor of an explanatory FAQ or RAQ about what is a packaging?

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