Skip to content

Make erasure insert .package in TermRefs to members of package object. #303

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/dotty/tools/dotc/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ object TypeErasure {
def erasedRef(tp: Type)(implicit ctx: Context): Type = tp match {
case tp: TermRef =>
assert(tp.symbol.exists, tp)
TermRef(erasedRef(tp.prefix), tp.symbol.asTerm)
val tp1 = ctx.makePackageObjPrefixExplicit(tp)
if (tp1 ne tp) erasedRef(tp1)
else TermRef(erasedRef(tp.prefix), tp.symbol.asTerm)
case tp: ThisType =>
tp
case tp =>
Expand Down
21 changes: 20 additions & 1 deletion src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dotty.tools.dotc
package core

import Contexts._, Types._, Symbols._, Names._, Flags._, Scopes._
import SymDenotations._
import SymDenotations._, Denotations.Denotation
import config.Printers._
import Decorators._
import StdNames._
Expand Down Expand Up @@ -224,6 +224,25 @@ trait TypeOps { this: Context =>
cls.enter(sym, decls)
}

/** If `tpe` is of the form `p.x` where `p` refers to a package
* but `x` is not owned by a package, expand it to
*
* p.package.x
*/
def makePackageObjPrefixExplicit(tpe: NamedType): Type = {
def tryInsert(pkgClass: SymDenotation): Type = pkgClass match {
case pkgCls: PackageClassDenotation if !(tpe.symbol.maybeOwner is Package) =>
tpe.derivedSelect(pkgCls.packageObj.valRef)
case _ =>
tpe
}
tpe.prefix match {
case pre: ThisType if pre.cls is Package => tryInsert(pre.cls)
case pre: TermRef if pre.symbol is Package => tryInsert(pre.symbol.moduleClass)
case _ => tpe
}
}

/** If we have member definitions
*
* type argSym v= from
Expand Down
17 changes: 1 addition & 16 deletions src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,6 @@ trait TypeAssigner {
* that the package object shows up as the prefix.
*/
def ensureAccessible(tpe: Type, superAccess: Boolean, pos: Position)(implicit ctx: Context): Type = {

def tryInsertPackageObj(tpe: NamedType, d: Denotation): Type = {
def tryInsert(pkgClass: SymDenotation): Type = pkgClass match {
case pkgCls: PackageClassDenotation if !(d.symbol.maybeOwner is Package) =>
tpe.derivedSelect(pkgCls.packageObj.valRef)
case _ =>
tpe
}
tpe.prefix match {
case pre: ThisType if pre.cls is Package => tryInsert(pre.cls)
case pre: TermRef if pre.symbol is Package => tryInsert(pre.symbol.moduleClass)
case _ => tpe
}
}

def test(tpe: Type, firstTry: Boolean): Type = tpe match {
case tpe: NamedType =>
val pre = tpe.prefix
Expand Down Expand Up @@ -159,7 +144,7 @@ trait TypeAssigner {
else if (d.symbol is TypeParamAccessor) // always dereference type param accessors
ensureAccessible(d.info.bounds.hi, superAccess, pos)
else
tryInsertPackageObj(tpe withDenot d, d)
ctx.makePackageObjPrefixExplicit(tpe withDenot d)
case _ =>
tpe
}
Expand Down
1 change: 1 addition & 0 deletions test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class tests extends CompilerTest {
@Test def pos_nullarify = compileFile(posDir, "nullarify", "-Ycheck:nullarify" :: Nil)
@Test def pos_subtyping = compileFile(posDir, "subtyping")
@Test def pos_t2613 = compileFile(posSpecialDir, "t2613")(allowDeepSubtypes)
@Test def pos_packageObj = compileFile(posDir, "i0239")

@Test def pos_all = compileFiles(posDir, failedOther)

Expand Down