Skip to content

Fix #9928: Don't treat import contexts as properly nested #9950

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

Merged
merged 5 commits into from
Oct 8, 2020

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Oct 6, 2020

This avoids surprises since context stacks sometimes have an import context in more
deeply nested position than the enclosing definition scope. It also corresponds to
the rule for name resolution, where we treat an inner import and an outer definition
of the same name as ambiguous.

This avoids surprises since context stacks sometimes have an import context in more
deeply nested position than the enclosing definition scope. It also corresponds to
the rule for name resolution, where we treat an inner import and an outer definition
of the same name as ambiguous.
I got an error:
```
error while loading Enum$,
class file /Users/odersky/workspace/dotty/library/target/scala-0.27/classes/scala/Enum.class is broken, reading aborted with class dotty.tools.tasty.UnpickleException
TASTy signature has wrong version.
 expected: 24.0
 found   : 23.0
```
even though there was no enum in my program. It turned out that the error was generated when ImportSuggestions tried to access an outdated
class file on the class path. This shoul dnot cause an error; instead the class file should be just ignored.
An import now has the same precedence as a current import and definition only if
both contexts have the same owner. Imports nested in inner owners do shadow definitions
and imports in outer ones.
@@ -304,7 +307,8 @@ object Implicits:
*/
override val level: Int =
if outerImplicits == null then 1
else if migrateTo3(using irefCtx)
else if isImport && (irefCtx.owner eq outerImplicits.irefCtx.owner)
Copy link
Contributor

Choose a reason for hiding this comment

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

Bikeshed: If desired, readability could be slightly improved by refactoring this boolean expression to:

      inline def isSameOwner = irefCtx.owner eq outerImplicits.irefCtx.owner
      inline def isSameScope = irefCtx.scope eq outerImplicits.irefCtx.scope
      inline def isLazyImplicit = refs.head.implicitName.is(LazyImplicitName)

      if outerImplicits == null then 1
      else if migrateTo3(using irefCtx)
              || isSameOwner && (isImport || isSameScope && !isLazyImplicit)
      then outerImplicits.level
      else outerImplicits.level + 1

This way imports also might look a little bit less special than now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

true. And I don't think we need the inline; the JVM is good at inlining these anyway.

@odersky odersky merged commit 64315b7 into scala:master Oct 8, 2020
@odersky odersky deleted the fix-#9928 branch October 8, 2020 21:23
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
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.

3 participants