Skip to content

Commit e0bf175

Browse files
committed
Merge pull request #303 from dotty-staging/fix-packageObjects-members-erasure
Make erasure insert `.package` in TermRefs to members of package object.
2 parents a68980c + 0a64618 commit e0bf175

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

src/dotty/tools/dotc/TypeErasure.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ object TypeErasure {
108108
def erasedRef(tp: Type)(implicit ctx: Context): Type = tp match {
109109
case tp: TermRef =>
110110
assert(tp.symbol.exists, tp)
111-
TermRef(erasedRef(tp.prefix), tp.symbol.asTerm)
111+
val tp1 = ctx.makePackageObjPrefixExplicit(tp)
112+
if (tp1 ne tp) erasedRef(tp1)
113+
else TermRef(erasedRef(tp.prefix), tp.symbol.asTerm)
112114
case tp: ThisType =>
113115
tp
114116
case tp =>

src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.dotc
22
package core
33

44
import Contexts._, Types._, Symbols._, Names._, Flags._, Scopes._
5-
import SymDenotations._
5+
import SymDenotations._, Denotations.Denotation
66
import config.Printers._
77
import Decorators._
88
import StdNames._
@@ -224,6 +224,25 @@ trait TypeOps { this: Context =>
224224
cls.enter(sym, decls)
225225
}
226226

227+
/** If `tpe` is of the form `p.x` where `p` refers to a package
228+
* but `x` is not owned by a package, expand it to
229+
*
230+
* p.package.x
231+
*/
232+
def makePackageObjPrefixExplicit(tpe: NamedType): Type = {
233+
def tryInsert(pkgClass: SymDenotation): Type = pkgClass match {
234+
case pkgCls: PackageClassDenotation if !(tpe.symbol.maybeOwner is Package) =>
235+
tpe.derivedSelect(pkgCls.packageObj.valRef)
236+
case _ =>
237+
tpe
238+
}
239+
tpe.prefix match {
240+
case pre: ThisType if pre.cls is Package => tryInsert(pre.cls)
241+
case pre: TermRef if pre.symbol is Package => tryInsert(pre.symbol.moduleClass)
242+
case _ => tpe
243+
}
244+
}
245+
227246
/** If we have member definitions
228247
*
229248
* type argSym v= from

src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,6 @@ trait TypeAssigner {
112112
* that the package object shows up as the prefix.
113113
*/
114114
def ensureAccessible(tpe: Type, superAccess: Boolean, pos: Position)(implicit ctx: Context): Type = {
115-
116-
def tryInsertPackageObj(tpe: NamedType, d: Denotation): Type = {
117-
def tryInsert(pkgClass: SymDenotation): Type = pkgClass match {
118-
case pkgCls: PackageClassDenotation if !(d.symbol.maybeOwner is Package) =>
119-
tpe.derivedSelect(pkgCls.packageObj.valRef)
120-
case _ =>
121-
tpe
122-
}
123-
tpe.prefix match {
124-
case pre: ThisType if pre.cls is Package => tryInsert(pre.cls)
125-
case pre: TermRef if pre.symbol is Package => tryInsert(pre.symbol.moduleClass)
126-
case _ => tpe
127-
}
128-
}
129-
130115
def test(tpe: Type, firstTry: Boolean): Type = tpe match {
131116
case tpe: NamedType =>
132117
val pre = tpe.prefix
@@ -159,7 +144,7 @@ trait TypeAssigner {
159144
else if (d.symbol is TypeParamAccessor) // always dereference type param accessors
160145
ensureAccessible(d.info.bounds.hi, superAccess, pos)
161146
else
162-
tryInsertPackageObj(tpe withDenot d, d)
147+
ctx.makePackageObjPrefixExplicit(tpe withDenot d)
163148
case _ =>
164149
tpe
165150
}

test/dotc/tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class tests extends CompilerTest {
6868
@Test def pos_nullarify = compileFile(posDir, "nullarify", "-Ycheck:nullarify" :: Nil)
6969
@Test def pos_subtyping = compileFile(posDir, "subtyping")
7070
@Test def pos_t2613 = compileFile(posSpecialDir, "t2613")(allowDeepSubtypes)
71+
@Test def pos_packageObj = compileFile(posDir, "i0239")
7172

7273
@Test def pos_all = compileFiles(posDir, failedOther)
7374

0 commit comments

Comments
 (0)