Skip to content

Commit

Permalink
Merge pull request #10333 from som-snytt/issue/12740-serializable-pac…
Browse files Browse the repository at this point in the history
…kage-object

Force root packages on lookup
  • Loading branch information
lrytz committed Jan 15, 2024
2 parents aa99433 + 5438660 commit 22958c5
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/compiler/scala/tools/nsc/typechecker/Analyzer.scala
Expand Up @@ -80,6 +80,7 @@ trait Analyzer extends AnyRef
def apply(unit: CompilationUnit): Unit = {
openPackageObjectsTraverser(unit.body)
deferredOpen.foreach(openPackageModule(_))
deferredOpen.clear()
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/scala/tools/nsc/typechecker/Contexts.scala
Expand Up @@ -1940,7 +1940,15 @@ trait Contexts { self: Analyzer =>
// because typechecking .java sources doesn't need it.
sym
}
else qual.tpe.nonLocalMember(nom)
else {
val tp = qual.tpe
val sym = tp.typeSymbol
// opening package objects is delayed (scala/scala#9661), but that can lead to missing symbols for
// package object types that are forced early through Definitions; see scala/bug#12740 / scala/scala#10333
if (phase.id < currentRun.typerPhase.id && sym.hasPackageFlag && analyzer.packageObjects.deferredOpen.remove(sym))
openPackageModule(sym)
tp.nonLocalMember(nom)
}
while ((selectors ne Nil) && result == NoSymbol) {
if (current.introduces(name))
result = maybeNonLocalMember(current.name asTypeOf name)
Expand Down
5 changes: 2 additions & 3 deletions src/reflect/scala/reflect/internal/Definitions.scala
Expand Up @@ -1661,14 +1661,13 @@ trait Definitions extends api.StandardDefinitions {
}

// documented in JavaUniverse.init
def init(): Unit = {
if (isInitialized) return
def init(): Unit = if (!isInitialized) {
ObjectClass.initialize
ScalaPackageClass.initialize
symbolsNotPresentInBytecode
NoSymbol
isInitialized = true
} //init
}

class UniverseDependentTypes(universe: Tree) {
lazy val nameType = universeMemberType(tpnme.Name)
Expand Down
6 changes: 2 additions & 4 deletions src/reflect/scala/reflect/internal/SymbolTable.scala
Expand Up @@ -389,15 +389,13 @@ abstract class SymbolTable extends macros.Universe

/** if there's a `package` member object in `pkgClass`, enter its members into it. */
def openPackageModule(pkgClass: Symbol, force: Boolean = false): Unit = {

val pkgModule = pkgClass.packageObject
val pkgModule = pkgClass.packageObject
def fromSource = pkgModule.rawInfo match {
case ltp: SymLoader => ltp.fromSource
case _ => false
}
if (pkgModule.isModule && !fromSource) {
if (pkgModule.isModule && !fromSource)
openPackageModule(pkgModule, pkgClass)
}
}

object perRunCaches {
Expand Down
2 changes: 1 addition & 1 deletion src/reflect/scala/reflect/runtime/SymbolLoaders.scala
Expand Up @@ -75,7 +75,7 @@ private[reflect] trait SymbolLoaders { self: SymbolTable =>
override def complete(sym: Symbol): Unit = {
assert(sym.isPackageClass, "Must be package")
// Time travel to a phase before refchecks avoids an initialization issue. `openPackageModule`
// creates a module symbol and invokes invokes `companionModule` while the `infos` field is
// creates a module symbol and invokes `companionModule` while the `infos` field is
// still null. This calls `isModuleNotMethod`, which forces the `info` if run after refchecks.
slowButSafeEnteringPhaseNotLaterThan(picklerPhase) {
sym setInfo new ClassInfoType(List(), new PackageScope(sym), sym)
Expand Down
17 changes: 17 additions & 0 deletions test/files/pos/t12740.scala
@@ -0,0 +1,17 @@
class C(x: NoSuchElementException)

// available in java.lang
package object K extends Cloneable

package object XX extends Serializable
package object XY extends NoSuchElementException

object XZ extends NoSuchElementException

package object Y {
type NSE = java.util.NoSuchElementException
}
package Z {
import Y._
class C extends NSE
}
4 changes: 4 additions & 0 deletions test/files/pos/t12740b/Y_1.scala
@@ -0,0 +1,4 @@

package object Y {
type NSE = java.util.NoSuchElementException
}
6 changes: 6 additions & 0 deletions test/files/pos/t12740b/Z_2.scala
@@ -0,0 +1,6 @@
//> using options -Yimports:java.lang,scala,scala.Predef,Y

package Z {
//import Y._
class C extends NSE
}

0 comments on commit 22958c5

Please sign in to comment.