Skip to content

Commit

Permalink
Merge pull request #10630 from lrytz/t10350-backport
Browse files Browse the repository at this point in the history
[backport] Align package detection in Java with Java
  • Loading branch information
lrytz committed Dec 13, 2023
2 parents e576dde + caa3973 commit 0a974ce
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Expand Up @@ -5269,16 +5269,21 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper

setError(tree)
}
// ignore current variable scope in patterns to enforce linearity
// ignore current variable scope in patterns to enforce linearity
val startContext = if (mode.typingPatternOrTypePat) context.outer else context

def asTypeName = if (mode.inAll(MonoQualifierModes) && unit.isJava && name.isTermName) {
startContext.lookupSymbol(name.toTypeName, qualifies).symbol
} else NoSymbol

val nameLookup = tree.symbol match {
case NoSymbol => startContext.lookupSymbol(name, qualifies)
case sym => LookupSucceeded(EmptyTree, sym)
// in Java, only pick a package if it is rooted
def termQualifies(sym: Symbol) = qualifies(sym) && (
!startContext.unit.isJava || !sym.hasPackageFlag
|| sym.owner.isEffectiveRoot || sym.owner.isRootPackage || sym.isRootPackage
)
val nameLookup = tree.symbol match {
case NoSymbol => startContext.lookupSymbol(name, termQualifies)
case sym => LookupSucceeded(EmptyTree, sym)
}
import InferErrorGen._
nameLookup match {
Expand All @@ -5300,7 +5305,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
// actual call to the stubbed classOf method is generated, returning null.
typedClassOf(tree, TypeTree(pt.typeArgs.head).setPos(tree.pos.focus))
}
else {
else {
val pre1 = if (sym.isTopLevel) sym.owner.thisType else if (qual == EmptyTree) NoPrefix else qual.tpe
val tree1 = if (qual == EmptyTree) tree else {
val pos = tree.pos
Expand Down
6 changes: 6 additions & 0 deletions test/files/pos/t10350/Bar.scala
@@ -0,0 +1,6 @@

package bar

object Bar {
def xxx(s: String): foo.Foo = foo.Foo.create(s)
}
5 changes: 5 additions & 0 deletions test/files/pos/t10350/Baz.java
@@ -0,0 +1,5 @@

package foo.java;

interface Baz {
}
8 changes: 8 additions & 0 deletions test/files/pos/t10350/Foo.java
@@ -0,0 +1,8 @@

package foo;

public interface Foo {
static Foo create(java.lang.String v) {
return null;
}
}
8 changes: 8 additions & 0 deletions test/files/pos/t10350b/Bot.java
@@ -0,0 +1,8 @@

package p.p.q;

public class Bot {
public p.Top topper() {
return new p.Top();
}
}
5 changes: 5 additions & 0 deletions test/files/pos/t10350b/Top.java
@@ -0,0 +1,5 @@

package p;

public class Top {
}
6 changes: 6 additions & 0 deletions test/files/pos/t10350b/s.scala
@@ -0,0 +1,6 @@

package s

object Test extends App {
println(new p.p.q.Bot().topper)
}

0 comments on commit 0a974ce

Please sign in to comment.