Skip to content

Commit db6fa85

Browse files
authored
Merge pull request #8784 from retronym/topic/forward-port-ca7dc4d
[forward port] Use Java semantics for imports in .java files
2 parents ee8c1ef + e5d2fb7 commit db6fa85

File tree

10 files changed

+51
-6
lines changed

10 files changed

+51
-6
lines changed

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,8 @@ trait Contexts { self: Analyzer =>
11121112
/** @return None if a cycle is detected, or Some(infos) containing the in-scope implicits at this context */
11131113
private def implicits: Option[List[ImplicitInfo]] = {
11141114
val firstImport = this.firstImport
1115-
if (owner != outer.owner && owner.isClass && !owner.isPackageClass) {
1115+
if (unit.isJava) SomeOfNil
1116+
else if (owner != outer.owner && owner.isClass && !owner.isPackageClass) {
11161117
if (!owner.isInitialized) None
11171118
else savingEnclClass(this) {
11181119
// !!! In the body of `class C(implicit a: A) { }`, `implicitss` returns `List(List(a), List(a), List(<predef..)))`
@@ -1775,9 +1776,13 @@ trait Contexts { self: Analyzer =>
17751776
@inline def current = selectors.head
17761777
@inline def maybeNonLocalMember(nom: Name): Symbol =
17771778
if (qual.tpe.isError) NoSymbol
1778-
else qual.tpe.nonLocalMember(nom).orElse {
1779-
if (pos.source.isJava) qual.tpe.companion nonLocalMember nom else NoSymbol
1779+
else if (pos.source.isJava) {
1780+
val (_, sym) = NoContext.javaFindMember(qual.tpe, nom, _ => true)
1781+
// We don't need to propagate the new prefix back out to the result of `Context.lookupSymbol`
1782+
// because typechecking .java sources doesn't need it.
1783+
sym
17801784
}
1785+
else qual.tpe.nonLocalMember(nom)
17811786
while ((selectors ne Nil) && result == NoSymbol) {
17821787
if (current.introduces(name))
17831788
result = maybeNonLocalMember(current.name asTypeOf name)

src/compiler/scala/tools/nsc/typechecker/Namers.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,14 @@ trait Namers extends MethodSynthesis {
569569
}
570570
def checkSelector(s: ImportSelector) = {
571571
val ImportSelector(from, fromPos, to, _) = s
572-
def isValid(original: Name, base: Type) =
573-
(base nonLocalMember original.toTermName) != NoSymbol ||
574-
(base nonLocalMember original.toTypeName) != NoSymbol
572+
def isValid(original: Name, base: Type) = {
573+
def lookup(name: Name) =
574+
if (context.unit.isJava)
575+
NoContext.javaFindMember(base, name, _ => true)._2
576+
else
577+
base.nonLocalMember(name)
578+
lookup(original.toTermName) != NoSymbol || lookup(original.toTypeName) != NoSymbol
579+
}
575580

576581
if (!s.isWildcard && base != ErrorType) {
577582
val okay = isValid(from, base) || context.unit.isJava && ( // Java code...
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
java-import-non-existing-selector/BadClient.java:3: error: cannot find symbol
2+
import static p1.Test.DoesNotExist;
3+
^
4+
symbol: static DoesNotExist
5+
location: class
6+
1 error
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Ypickle-java
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package p1;
2+
3+
import static p1.Test.DoesNotExist;
4+
5+
class BadClient {
6+
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package p1;
2+
3+
public class Test extends Base {}
4+
class Base {
5+
static class I {}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Ypickle-java
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package p1;
2+
3+
import static p1.Test.I;
4+
5+
class Client {
6+
void foo(I i) {}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package p1;
2+
3+
public class Test extends Base {}
4+
class Base {
5+
static class I {}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Test

0 commit comments

Comments
 (0)