Skip to content

Commit 8b38acb

Browse files
committed
Insert .package for package object references
Package object members are seen as members of the enclosing package during typer. The normalization inserts the missing .package reference to such members. It is necessary to satisfy a new postcondition of FirstTransform: In a selection `x.m`, the type of `x` must derive from the owner of `m`.
1 parent 979fa47 commit 8b38acb

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/dotty/tools/dotc/transform/FirstTransform.scala

+18-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import Types._
1010
import Constants.Constant
1111
import Contexts.Context
1212
import Symbols._
13+
import SymDenotations._
14+
import Decorators._
1315
import scala.collection.mutable
1416
import DenotTransformers._
1517
import typer.Checking
@@ -21,6 +23,7 @@ import NameOps._
2123
* - ensures there are companion objects for all classes except module classes
2224
* - eliminates some kinds of trees: Imports, NamedArgs, all TypTrees other than TypeTree
2325
* - converts Select/Ident/SelectFromTypeTree nodes that refer to types to TypeTrees.
26+
* - inserts `.package` for selections of package object members
2427
* - checks the bounds of AppliedTypeTrees
2528
* - stubs out native methods
2629
*/
@@ -29,6 +32,12 @@ class FirstTransform extends MiniPhaseTransform with IdentityDenotTransformer {
2932

3033
override def phaseName = "companions"
3134

35+
override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = tree match {
36+
case Select(qual, _) if tree.symbol.exists =>
37+
assert(qual.tpe derivesFrom tree.symbol.owner, i"non member selection of ${tree.symbol.showLocated} from ${qual.tpe}")
38+
case _ =>
39+
}
40+
3241
/** Reorder statements so that module classes always come after their companion classes, add missing companion classes */
3342
private def reorderAndComplete(stats: List[Tree])(implicit ctx: Context): List[Tree] = {
3443
val moduleClassDefs, singleClassDefs = mutable.Map[Name, Tree]()
@@ -96,7 +105,15 @@ class FirstTransform extends MiniPhaseTransform with IdentityDenotTransformer {
96105
}
97106

98107
override def transformSelect(tree: Select)(implicit ctx: Context, info: TransformerInfo) =
99-
normalizeType(tree)
108+
normalizeType {
109+
val qual = tree.qualifier
110+
qual.symbol.moduleClass.denot match {
111+
case pkg: PackageClassDenotation if tree.symbol.maybeOwner.isPackageObject =>
112+
cpy.Select(tree)(qual select pkg.packageObj.symbol, tree.name)
113+
case _ =>
114+
tree
115+
}
116+
}
100117

101118
override def transformSelectFromTypeTree(tree: SelectFromTypeTree)(implicit ctx: Context, info: TransformerInfo) =
102119
normalizeType(tree)

0 commit comments

Comments
 (0)