Skip to content

Commit e4fb6b5

Browse files
committed
Java package must be rooted
1 parent dffb407 commit e4fb6b5

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ trait TypeAssigner {
125125

126126
/** The type of the selection `tree`, where `qual1` is the typed qualifier part. */
127127
def selectionType(tree: untpd.RefTree, qual1: Tree)(using Context): Type =
128-
val qualType0 = qual1.tpe.widenIfUnstable
129128
val qualType =
129+
val qualType0 = qual1.tpe.widenIfUnstable
130130
if !qualType0.hasSimpleKind && tree.name != nme.CONSTRUCTOR then
131131
// constructors are selected on type constructor, type arguments are passed afterwards
132132
errorType(em"$qualType0 takes type parameters", qual1.srcPos)
@@ -199,7 +199,7 @@ trait TypeAssigner {
199199

200200
/** Type assignment method. Each method takes as parameters
201201
* - an untpd.Tree to which it assigns a type,
202-
* - typed child trees it needs to access to cpmpute that type,
202+
* - typed child trees it needs to access to compute that type,
203203
* - any further information it needs to access to compute that type.
204204
*/
205205
def assignType(tree: untpd.Ident, tp: Type)(using Context): Ident =

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,24 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
993993
typedSelectWithAdapt(tree, pt, qual).withSpan(tree.span).computeNullable()
994994

995995
def javaSelection(qual: Tree)(using Context) =
996-
val tree1 = assignType(cpy.Select(tree)(qual, tree.name), qual)
997-
tree1.tpe match
998-
case moduleRef: TypeRef if moduleRef.symbol.is(ModuleClass, butNot = JavaDefined) =>
999-
// handle unmangling of module names (Foo$ -> Foo[ModuleClass])
1000-
cpy.Select(tree)(qual, tree.name.unmangleClassName).withType(moduleRef)
1001-
case _ =>
1002-
tree1
996+
qual match
997+
case id @ Ident(name) if id.symbol.is(Package) && !id.symbol.owner.isRoot =>
998+
def nextPackage(last: Symbol)(using Context): Type =
999+
val startAt = ctx.outersIterator.dropWhile(_.owner != last.owner).drop(1).next()
1000+
val next = findRef(name, WildcardType, required = Package, EmptyFlags, qual.srcPos)(using startAt)
1001+
if next.exists && !next.typeSymbol.owner.isRoot then nextPackage(next.typeSymbol)
1002+
else next
1003+
val next = nextPackage(id.symbol)
1004+
val qual1 = if next.exists then assignType(cpy.Ident(id)(tree.name), next) else qual
1005+
assignType(cpy.Select(tree)(qual1, tree.name), qual1)
1006+
case _ =>
1007+
val tree1 = assignType(cpy.Select(tree)(qual, tree.name), qual)
1008+
tree1.tpe match
1009+
case moduleRef: TypeRef if moduleRef.symbol.is(ModuleClass, butNot = JavaDefined) =>
1010+
// handle unmangling of module names (Foo$ -> Foo[ModuleClass])
1011+
cpy.Select(tree)(qual, tree.name.unmangleClassName).withType(moduleRef)
1012+
case _ =>
1013+
tree1
10031014

10041015
def tryJavaSelectOnType(using Context): Tree = tree.qualifier match {
10051016
case sel @ Select(qual, name) =>

0 commit comments

Comments
 (0)