@@ -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 , TermName }
88import Constants .Constant
99
@@ -186,6 +186,36 @@ class TypeUtils:
186186 case self : Types .ThisType => self.cls == cls
187187 case _ => false
188188
189+ /** If `self` is of the form `p.x` where `p` refers to a package
190+ * but `x` is not owned by a package, expand it to
191+ *
192+ * p.package.x
193+ */
194+ def makePackageObjPrefixExplicit (using Context ): Type =
195+ def tryInsert (tpe : NamedType , pkgClass : SymDenotation ): Type = pkgClass match
196+ case pkg : PackageClassDenotation =>
197+ var sym = tpe.symbol
198+ if ! sym.exists && tpe.denot.isOverloaded then
199+ // we know that all alternatives must come from the same package object, since
200+ // otherwise we would get "is already defined" errors. So we can take the first
201+ // symbol we see.
202+ sym = tpe.denot.alternatives.head.symbol
203+ val pobj = pkg.packageObjFor(sym)
204+ if pobj.exists then tpe.derivedSelect(pobj.termRef)
205+ else tpe
206+ case _ =>
207+ tpe
208+ self match
209+ case tpe : NamedType =>
210+ if tpe.symbol.isRoot then
211+ tpe
212+ else
213+ tpe.prefix match
214+ case pre : ThisType if pre.cls.is(Package ) => tryInsert(tpe, pre.cls)
215+ case pre : TermRef if pre.symbol.is(Package ) => tryInsert(tpe, pre.symbol.moduleClass)
216+ case _ => tpe
217+ case tpe => tpe
218+
189219 /** Strip all outer refinements off this type */
190220 def stripRefinement : Type = self match
191221 case self : RefinedOrRecType => self.parent.stripRefinement
0 commit comments