diff --git a/compiler/src/dotty/tools/dotc/core/ContextOps.scala b/compiler/src/dotty/tools/dotc/core/ContextOps.scala index 55fb31fd1916..427cfa47c7c1 100644 --- a/compiler/src/dotty/tools/dotc/core/ContextOps.scala +++ b/compiler/src/dotty/tools/dotc/core/ContextOps.scala @@ -41,7 +41,10 @@ object ContextOps: else pre.findMember(name, pre, required, excluded) } else // we are in the outermost context belonging to a class; self is invisible here. See inClassContext. - ctx.owner.findMember(name, ctx.owner.thisType, required, excluded) + if ctx.isJava then + javaFindMember(name, ctx.owner.thisType, lookInCompanion = true,required, excluded) + else + ctx.owner.findMember(name, ctx.owner.thisType, required, excluded) else ctx.scope.denotsNamed(name).filterWithFlags(required, excluded).toDenot(NoPrefix) } @@ -55,11 +58,20 @@ object ContextOps: final def javaFindMember(name: Name, pre: Type, lookInCompanion: Boolean, required: FlagSet = EmptyFlags, excluded: FlagSet = EmptyFlags): Denotation = assert(ctx.isJava) inContext(ctx) { - + import dotty.tools.dotc.core.NameOps.* val preSym = pre.typeSymbol - // 1. Try to search in current type and parents. - val directSearch = pre.findMember(name, pre, required, excluded) + val directSearch = + def asModule = + if name.isTypeName && name.endsWith(StdNames.str.MODULE_SUFFIX) then + pre.findMember(name.stripModuleClassSuffix.moduleClassName, pre, required, excluded) match + case NoDenotation => NoDenotation + case symDenot: SymDenotation => + symDenot.companionModule.denot + else NoDenotation + pre.findMember(name, pre, required, excluded) match + case NoDenotation => asModule + case denot => denot // 2. Try to search in companion class if current is an object. def searchCompanionClass = if lookInCompanion && preSym.is(Flags.Module) then diff --git a/compiler/test/dotc/pos-test-pickling.blacklist b/compiler/test/dotc/pos-test-pickling.blacklist index b0da78f0a1eb..32f8cdef1386 100644 --- a/compiler/test/dotc/pos-test-pickling.blacklist +++ b/compiler/test/dotc/pos-test-pickling.blacklist @@ -29,6 +29,7 @@ i16649-irrefutable.scala strict-pattern-bindings-3.0-migration.scala i17186b.scala i11982a.scala +i17255 # Tree is huge and blows stack for printing Text i7034.scala diff --git a/compiler/test/dotc/run-test-pickling.blacklist b/compiler/test/dotc/run-test-pickling.blacklist index 9f19b439135c..954a64db1b66 100644 --- a/compiler/test/dotc/run-test-pickling.blacklist +++ b/compiler/test/dotc/run-test-pickling.blacklist @@ -44,3 +44,5 @@ t6138 t6138-2 i12656.scala trait-static-forwarder +i17255 + diff --git a/tests/pos/i17255/Bar.java b/tests/pos/i17255/Bar.java new file mode 100644 index 000000000000..35269af2179f --- /dev/null +++ b/tests/pos/i17255/Bar.java @@ -0,0 +1,6 @@ +package example; + + +public class Bar { + private static final example.Foo$ MOD = example.Foo$.MODULE$; +} \ No newline at end of file diff --git a/tests/pos/i17255/Baz.java b/tests/pos/i17255/Baz.java new file mode 100644 index 000000000000..6dbb1078949d --- /dev/null +++ b/tests/pos/i17255/Baz.java @@ -0,0 +1,7 @@ +package example; + +import example.Foo$; + +public class Baz { + private static final Foo$ MOD = Foo$.MODULE$; +} \ No newline at end of file diff --git a/tests/pos/i17255/Foo.scala b/tests/pos/i17255/Foo.scala new file mode 100644 index 000000000000..9608b43b430f --- /dev/null +++ b/tests/pos/i17255/Foo.scala @@ -0,0 +1,5 @@ +package example + +case class Foo(i: Int) + +object Foo \ No newline at end of file diff --git a/tests/run/i17255/J.java b/tests/run/i17255/J.java new file mode 100644 index 000000000000..3c6d64d75ab9 --- /dev/null +++ b/tests/run/i17255/J.java @@ -0,0 +1,16 @@ +package p; + +public class J { + public static J j = new J(); + public static p.J f() { + return p.J.j; + } + public static Module$ module2() { + return p.Module$.MODULE$; + } + public static p.Module$ module() { + return p.Module$.MODULE$; + } + + public String toString() { return "J"; } +} \ No newline at end of file diff --git a/tests/run/i17255/Module.scala b/tests/run/i17255/Module.scala new file mode 100644 index 000000000000..e681264093da --- /dev/null +++ b/tests/run/i17255/Module.scala @@ -0,0 +1,13 @@ +// scalajs: --skip + +package p { + object Module { + override def toString = "Module" + } +} + +object Test extends App { + assert(p.J.f().toString == "J") + assert(p.J.module().toString == "Module") + assert(p.J.module2().toString == "Module") +} \ No newline at end of file