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

Merged
merged 2 commits into from
Dec 17, 2024

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)

@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.

compiler/src/dotty/tools/dotc/typer/Typer.scala Outdated Show resolved Hide resolved
@odersky odersky assigned dwijnand and unassigned odersky Nov 17, 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.
@dwijnand dwijnand assigned odersky and unassigned dwijnand Dec 2, 2024
@dwijnand dwijnand requested a review from odersky December 2, 2024 21: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.

That looks good now.

@odersky odersky merged commit e52aea4 into scala:main Dec 17, 2024
29 checks passed
@odersky odersky deleted the java-imports-diff branch December 17, 2024 22:07
@WojciechMazur WojciechMazur added this to the 3.6.4 milestone Jan 16, 2025
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