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 #21888

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dwijnand
Copy link
Member

@dwijnand dwijnand commented Nov 4, 2024

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
(Replaces #21871)

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 requested a review from odersky November 4, 2024 15:40
@dwijnand dwijnand marked this pull request as ready for review November 12, 2024 09:22
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.

The boxing is a heavy price to pay here. I am worried that we make findRefRecur slower for a niche case which is what #18529 is. It's trivially fixed by moving the named import behind the two wildcard imports. And the error message is informative enough to suggest something like that.

What we could do: If we get an ambiguity, try a targeted action to see whether we get a
named import in the same import sequence that would resolve the problem. That can be done without changing the signature of findRefRecur.

@@ -238,38 +238,40 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
!owner.isEmptyPackage || ctx.owner.enclosingPackageClass.isEmptyPackage
}

import BindingPrec.*
type Result = (Type, BindingPrec)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should use a boxed type here. findRefRecur is very hot, we should avoid allocating where we can.

@odersky odersky assigned dwijnand and unassigned odersky Nov 17, 2024
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
2 participants