Skip to content

Commit 6eaf815

Browse files
oderskyDarkDimius
authored andcommitted
Refactored common behavior from TypeAssigner and TypeErasure
1 parent 48966b4 commit 6eaf815

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/dotty/tools/dotc/TypeErasure.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +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-
if(tp.prefix.widenDealias.classSymbol.is(Flags.Package) && !tp.termSymbol.owner.is(Flags.Package))
112-
// we are accessing a definition inside a package object
113-
TermRef(erasedRef(tp.prefix).member(nme.PACKAGE).asSymDenotation.termRef, tp.symbol.asTerm)
114-
else
115-
TermRef(erasedRef(tp.prefix), tp.symbol.asTerm)
111+
val tp1 = ctx.makePackageObjPrefixExplicit(tp, tp.symbol)
112+
if (tp1 ne tp) erasedRef(tp1)
113+
else TermRef(erasedRef(tp.prefix), tp.symbol.asTerm)
116114
case tp: ThisType =>
117115
tp
118116
case tp =>

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

Lines changed: 15 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,20 @@ trait TypeOps { this: Context =>
224224
cls.enter(sym, decls)
225225
}
226226

227+
def makePackageObjPrefixExplicit(tpe: NamedType, d: Denotation): Type = {
228+
def tryInsert(pkgClass: SymDenotation): Type = pkgClass match {
229+
case pkgCls: PackageClassDenotation if !(d.symbol.maybeOwner is Package) =>
230+
tpe.derivedSelect(pkgCls.packageObj.valRef)
231+
case _ =>
232+
tpe
233+
}
234+
tpe.prefix match {
235+
case pre: ThisType if pre.cls is Package => tryInsert(pre.cls)
236+
case pre: TermRef if pre.symbol is Package => tryInsert(pre.symbol.moduleClass)
237+
case _ => tpe
238+
}
239+
}
240+
227241
/** If we have member definitions
228242
*
229243
* 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, d)
163148
case _ =>
164149
tpe
165150
}

0 commit comments

Comments
 (0)