Skip to content

Top level imports of universal methods induce ambiguity #14847

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

Open
som-snytt opened this issue Apr 4, 2022 · 0 comments
Open

Top level imports of universal methods induce ambiguity #14847

som-snytt opened this issue Apr 4, 2022 · 0 comments

Comments

@som-snytt
Copy link
Contributor

Compiler version

3.1.1

Minimized code

  snips cat gottenclass.scala

object X
object Y

import X.*
import Y.*

def f = getClass  // ambiguous

class C {
  import X.*
  import Y.*
  def g = getClass  // OK, "normal" definition
}

@main def test() =
  println(f)
  println(C().g)

Output

  snips scalac gottenclass.scala
-- [E049] Reference Error: gottenclass.scala:8:8 -----------------------------------------------------------------------
8 |def f = getClass
  |        ^^^^^^^^
  |        Reference to getClass is ambiguous,
  |        it is both imported by import X._
  |        and imported subsequently by import Y._

longer explanation available when compiling with `-explain`
1 error found

Expectation

The Scala 2 fix was that "root imports" do not import universal methods. That was to avoid a bug where secondary constructors used getClass and got Predef.getClass.

In this case, getClass would be expected to pick up the class of the enclosing package object, as happens with this.getClass.

Without the ambiguity and without this, getClass is resolved by some arbitrary wildcard import, which is obviously unintended. (That is, more unintended than importing from Predef.)

Real world example at https://github.com/lampepfl/dotty/blob/3.1.2/compiler/test/dotty/tools/utils.scala#L16 is one wildcard import away from ambiguity.

More importantly, that example shows a usage of getClass where class loading might be important (that is, getResource might be using an unexpected class path).

Related to #7713 except here the competing import is not a root import.

@som-snytt som-snytt added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 4, 2022
@anatoliykmetyuk anatoliykmetyuk added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 8, 2022
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