Skip to content

Commit

Permalink
Merge pull request #8784 from retronym/topic/forward-port-ca7dc4d
Browse files Browse the repository at this point in the history
[forward port] Use Java semantics for imports in .java files
  • Loading branch information
lrytz committed Mar 4, 2020
2 parents ee8c1ef + e5d2fb7 commit db6fa85
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,8 @@ trait Contexts { self: Analyzer =>
/** @return None if a cycle is detected, or Some(infos) containing the in-scope implicits at this context */
private def implicits: Option[List[ImplicitInfo]] = {
val firstImport = this.firstImport
if (owner != outer.owner && owner.isClass && !owner.isPackageClass) {
if (unit.isJava) SomeOfNil
else if (owner != outer.owner && owner.isClass && !owner.isPackageClass) {
if (!owner.isInitialized) None
else savingEnclClass(this) {
// !!! In the body of `class C(implicit a: A) { }`, `implicitss` returns `List(List(a), List(a), List(<predef..)))`
Expand Down Expand Up @@ -1775,9 +1776,13 @@ trait Contexts { self: Analyzer =>
@inline def current = selectors.head
@inline def maybeNonLocalMember(nom: Name): Symbol =
if (qual.tpe.isError) NoSymbol
else qual.tpe.nonLocalMember(nom).orElse {
if (pos.source.isJava) qual.tpe.companion nonLocalMember nom else NoSymbol
else if (pos.source.isJava) {
val (_, sym) = NoContext.javaFindMember(qual.tpe, nom, _ => true)
// We don't need to propagate the new prefix back out to the result of `Context.lookupSymbol`
// because typechecking .java sources doesn't need it.
sym
}
else qual.tpe.nonLocalMember(nom)
while ((selectors ne Nil) && result == NoSymbol) {
if (current.introduces(name))
result = maybeNonLocalMember(current.name asTypeOf name)
Expand Down
11 changes: 8 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/Namers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,14 @@ trait Namers extends MethodSynthesis {
}
def checkSelector(s: ImportSelector) = {
val ImportSelector(from, fromPos, to, _) = s
def isValid(original: Name, base: Type) =
(base nonLocalMember original.toTermName) != NoSymbol ||
(base nonLocalMember original.toTypeName) != NoSymbol
def isValid(original: Name, base: Type) = {
def lookup(name: Name) =
if (context.unit.isJava)
NoContext.javaFindMember(base, name, _ => true)._2
else
base.nonLocalMember(name)
lookup(original.toTermName) != NoSymbol || lookup(original.toTypeName) != NoSymbol
}

if (!s.isWildcard && base != ErrorType) {
val okay = isValid(from, base) || context.unit.isJava && ( // Java code...
Expand Down
6 changes: 6 additions & 0 deletions test/files/neg/java-import-non-existing-selector.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
java-import-non-existing-selector/BadClient.java:3: error: cannot find symbol
import static p1.Test.DoesNotExist;
^
symbol: static DoesNotExist
location: class
1 error
1 change: 1 addition & 0 deletions test/files/neg/java-import-non-existing-selector.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Ypickle-java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package p1;

import static p1.Test.DoesNotExist;

class BadClient {

}
6 changes: 6 additions & 0 deletions test/files/neg/java-import-non-existing-selector/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package p1;

public class Test extends Base {}
class Base {
static class I {}
}
1 change: 1 addition & 0 deletions test/files/pos/java-import-static-from-subclass.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Ypickle-java
7 changes: 7 additions & 0 deletions test/files/pos/java-import-static-from-subclass/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package p1;

import static p1.Test.I;

class Client {
void foo(I i) {}
}
6 changes: 6 additions & 0 deletions test/files/pos/java-import-static-from-subclass/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package p1;

public class Test extends Base {}
class Base {
static class I {}
}
1 change: 1 addition & 0 deletions test/files/pos/java-import-static-from-subclass/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Test

0 comments on commit db6fa85

Please sign in to comment.