Skip to content

Commit 004c4a8

Browse files
committed
Java package must be rooted
1 parent 01a51c3 commit 004c4a8

File tree

10 files changed

+74
-9
lines changed

10 files changed

+74
-9
lines changed

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

+2-2
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

+18-7
Original file line numberDiff line numberDiff line change
@@ -1008,13 +1008,24 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
10081008
typedSelectWithAdapt(tree, pt, qual).withSpan(tree.span).computeNullable()
10091009

10101010
def javaSelection(qual: Tree)(using Context) =
1011-
val tree1 = assignType(cpy.Select(tree)(qual, tree.name), qual)
1012-
tree1.tpe match
1013-
case moduleRef: TypeRef if moduleRef.symbol.is(ModuleClass, butNot = JavaDefined) =>
1014-
// handle unmangling of module names (Foo$ -> Foo[ModuleClass])
1015-
cpy.Select(tree)(qual, tree.name.unmangleClassName).withType(moduleRef)
1016-
case _ =>
1017-
tree1
1011+
qual match
1012+
case id @ Ident(name) if id.symbol.is(Package) && !id.symbol.owner.isRoot =>
1013+
def nextPackage(last: Symbol)(using Context): Type =
1014+
val startAt = ctx.outersIterator.dropWhile(_.owner != last.owner).drop(1).next()
1015+
val next = findRef(name, WildcardType, required = Package, EmptyFlags, qual.srcPos)(using startAt)
1016+
if next.exists && !next.typeSymbol.owner.isRoot then nextPackage(next.typeSymbol)
1017+
else next
1018+
val next = nextPackage(id.symbol)
1019+
val qual1 = if next.exists then assignType(cpy.Ident(id)(tree.name), next) else qual
1020+
assignType(cpy.Select(tree)(qual1, tree.name), qual1)
1021+
case _ =>
1022+
val tree1 = assignType(cpy.Select(tree)(qual, tree.name), qual)
1023+
tree1.tpe match
1024+
case moduleRef: TypeRef if moduleRef.symbol.is(ModuleClass, butNot = JavaDefined) =>
1025+
// handle unmangling of module names (Foo$ -> Foo[ModuleClass])
1026+
cpy.Select(tree)(qual, tree.name.unmangleClassName).withType(moduleRef)
1027+
case _ =>
1028+
tree1
10181029

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

tests/pos/t10350/Bar.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
package bar
3+
4+
object Bar {
5+
def xxx(s: String): foo.Foo = foo.Foo.create(s)
6+
}

tests/pos/t10350/Baz.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
package foo.java;
3+
4+
interface Baz {
5+
}

tests/pos/t10350/Foo.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
package foo;
3+
4+
public interface Foo {
5+
static Foo create(java.lang.String v) {
6+
return null;
7+
}
8+
}
9+
10+
/*
11+
5 | static Foo create(java.lang.String v) {
12+
| ^^^^^^^^^
13+
| value lang is not a member of foo.java
14+
*/

tests/pos/t11788/Bar.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Bar extends App {
2+
println(new Foo().test())
3+
}

tests/pos/t11788/Foo.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Foo {
2+
private String java;
3+
4+
public java.lang.Integer test() {
5+
//return Integer.valueOf(42);
6+
throw null;
7+
}
8+
}

tests/pos/t11788b/Bar.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Bar extends App {
2+
println(new Foo().test())
3+
}

tests/pos/t11788b/Foo.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Foo {
2+
private String java;
3+
4+
public java.lang.Integer test() {
5+
//return Integer.valueOf(42);
6+
throw null;
7+
}
8+
}

tests/pos/t11788b/java.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
public class java {
3+
public static class lang {
4+
public static class Integer {
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)