From 6eaf815923e194cd4d0da8faa0f132dbf8de4608 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 17 Dec 2014 17:13:30 +0100 Subject: Refactored common behavior from TypeAssigner and TypeErasure --- src/dotty/tools/dotc/TypeErasure.scala | 8 +++----- src/dotty/tools/dotc/core/TypeOps.scala | 16 +++++++++++++++- src/dotty/tools/dotc/typer/TypeAssigner.scala | 17 +---------------- 3 files changed, 19 insertions(+), 22 deletions(-) (limited to 'src/dotty/tools') diff --git a/src/dotty/tools/dotc/TypeErasure.scala b/src/dotty/tools/dotc/TypeErasure.scala index 13c8b4df7..9f9894c57 100644 --- a/src/dotty/tools/dotc/TypeErasure.scala +++ b/src/dotty/tools/dotc/TypeErasure.scala @@ -108,11 +108,9 @@ object TypeErasure { def erasedRef(tp: Type)(implicit ctx: Context): Type = tp match { case tp: TermRef => assert(tp.symbol.exists, tp) - if(tp.prefix.widenDealias.classSymbol.is(Flags.Package) && !tp.termSymbol.owner.is(Flags.Package)) - // we are accessing a definition inside a package object - TermRef(erasedRef(tp.prefix).member(nme.PACKAGE).asSymDenotation.termRef, tp.symbol.asTerm) - else - TermRef(erasedRef(tp.prefix), tp.symbol.asTerm) + val tp1 = ctx.makePackageObjPrefixExplicit(tp, tp.symbol) + if (tp1 ne tp) erasedRef(tp1) + else TermRef(erasedRef(tp.prefix), tp.symbol.asTerm) case tp: ThisType => tp case tp => diff --git a/src/dotty/tools/dotc/core/TypeOps.scala b/src/dotty/tools/dotc/core/TypeOps.scala index 8190b8cb6..4716d89b7 100644 --- a/src/dotty/tools/dotc/core/TypeOps.scala +++ b/src/dotty/tools/dotc/core/TypeOps.scala @@ -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._ @@ -224,6 +224,20 @@ trait TypeOps { this: Context => cls.enter(sym, decls) } + def makePackageObjPrefixExplicit(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 + } + } + /** If we have member definitions * * type argSym v= from diff --git a/src/dotty/tools/dotc/typer/TypeAssigner.scala b/src/dotty/tools/dotc/typer/TypeAssigner.scala index 97c959a7a..4394b8557 100644 --- a/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -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 @@ -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, d) case _ => tpe } -- cgit v1.2.3