@@ -3,7 +3,7 @@ package dotc
33package core
44
55import TypeErasure .ErasedValueType
6- import Types .* , Contexts .* , Symbols .* , Flags .* , Decorators .*
6+ import Types .* , Contexts .* , Symbols .* , Flags .* , Decorators .* , SymDenotations . *
77import Names .Name
88import StdNames .nme
99
@@ -147,6 +147,37 @@ class TypeUtils:
147147 val cls = self.underlyingClassRef(refinementOK = false ).typeSymbol
148148 cls.isTransparentClass && (! traitOnly || cls.is(Trait ))
149149
150+
151+ /** If `self` is of the form `p.x` where `p` refers to a package
152+ * but `x` is not owned by a package, expand it to
153+ *
154+ * p.package.x
155+ */
156+ def makePackageObjPrefixExplicit (using Context ): Type =
157+ def tryInsert (tpe : NamedType , pkgClass : SymDenotation ): Type = pkgClass match
158+ case pkg : PackageClassDenotation =>
159+ var sym = tpe.symbol
160+ if ! sym.exists && tpe.denot.isOverloaded then
161+ // we know that all alternatives must come from the same package object, since
162+ // otherwise we would get "is already defined" errors. So we can take the first
163+ // symbol we see.
164+ sym = tpe.denot.alternatives.head.symbol
165+ val pobj = pkg.packageObjFor(sym)
166+ if pobj.exists then tpe.derivedSelect(pobj.termRef)
167+ else tpe
168+ case _ =>
169+ tpe
170+ self match
171+ case tpe : NamedType =>
172+ if tpe.symbol.isRoot then
173+ tpe
174+ else
175+ tpe.prefix match
176+ case pre : ThisType if pre.cls.is(Package ) => tryInsert(tpe, pre.cls)
177+ case pre : TermRef if pre.symbol.is(Package ) => tryInsert(tpe, pre.symbol.moduleClass)
178+ case _ => tpe
179+ case tpe => tpe
180+
150181 /** The constructors of this type that are applicable to `argTypes`, without needing
151182 * an implicit conversion. Curried constructors are always excluded.
152183 * @param adaptVarargs if true, allow a constructor with just a varargs argument to
0 commit comments