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

Resolve name when named imp is behind wild imps #21871

Closed
wants to merge 1 commit into from

Conversation

dwijnand
Copy link
Member

When a named import (such as import bug.util.List) is defined before
two clashing wildcard imports (import bug.util.*; import java.util.*)
the name "List" should resolve to it, rather than a resolution error
being emitted.

This was due to the fact that findRefRecur didn't return the
precedence at which it found that import, checkImportAlternatives used
the prevPrec to checkNewOrShadowed. Now we check against the entire
foundResult, allowing an early named import to be picked over later
wildcard imports.

Fixes #18529

When a named import (such as `import bug.util.List`) is defined before
two clashing wildcard imports (`import bug.util.*; import java.util.*`)
the name "List" should resolve to it, rather than a resolution error
being emitted.

This was due to the fact that `findRefRecur` didn't return the
precedence at which it found that import, `checkImportAlternatives` used
the `prevPrec` to `checkNewOrShadowed`.  Now we check against the entire
`foundResult`, allowing an early named import to be picked over later
wildcard imports.
@dwijnand dwijnand marked this pull request as ready for review October 31, 2024 21:21
@dwijnand dwijnand requested a review from odersky October 31, 2024 21:21
@som-snytt
Copy link
Contributor

I was dubious at first because the motivation is "how Java imports work".

My previous questions about name resolution in Dotty came down to understanding contexts not scopes. However, the spec only talks about scopes.

Scala 2 has the same current behavior. As a lover of wildcards, why doesn't that come up more often?

I must further spool up findRef; in particular, I don't understand what the previous recurAndCheckNewOrShadowed addresses, as the test was strange.

So with a grain of salt, this modified example from the spec, with an ambiguating import, and a second import to disambiguate, is still ambiguous:

package p {                   // `X' bound by package clause
  import p.X  // attempt to disambiguate
  object Y {
    println(s"L4: $X")          // `X' refers to `p.X' here
    locally {
      import q._                // `X' bound by wildcard import
      import p._                // ambiguating
      println(s"L7: $X")        // was: `X' refers to `q.X' here
    }
  }
}

where

package p {
  object X { val x = 1; val y = 2 }
}

package q {
  object X { val x = true; val y = false }
}

yielding

➜  i18529 ~/projects/dotty/bin/scalacQ -d ~/sandbox -cp ~/sandbox spec-mini.scala
-- [E049] Reference Error: spec-mini.scala:9:21 --------------------------------
9 |      println(s"L7: $X")        // `X' refers to `q.X' here
  |                     ^
  |                     Reference to X is ambiguous.
  |                     It is both imported by name by import p.X
  |                     and imported subsequently by import q._

That is unchanged from 3.5.2 (actually), but Scala 2 complains about the competing wildcards:

spec-mini.scala:9: error: reference to X is ambiguous;
it is imported twice in the same scope by
import p._
and import q._
      println(s"L7: $X")        // `X' refers to `q.X' here
                     ^

This variant of the test works as expected, and 3.5.2 complains about the wildcards:

package bug.code

import bug.util.List

class Lister {
  import bug.util.*
  import java.util.*
  import scala.collection.immutable.*
  def f(xs: List[Int]): Unit = ()
}

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we piush this to staging please? That makes it easier for me to run my own diff tool.

It's going to be difficult to review, since there are a lot of changes in code that I know to be very subtle.

@dwijnand dwijnand closed this Nov 4, 2024
@dwijnand dwijnand deleted the java-imports-diff branch November 4, 2024 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java type name resolution with wildcard imports is incorrect
3 participants