diff options
5 files changed, 36 insertions, 0 deletions
diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 2cefc52a0..706e8b5bf 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -605,6 +605,25 @@ class Namer { typer: Typer => case EmptyTree => } } + + // If a top-level object has no companion class in the current run, we + // enter a dummy companion class symbol (`denot.isAbsent` returns true) in + // scope. This ensures that we never use a companion from a previous run + // or from the classpath. See tests/pos/false-companion for an + // example where this matters. + if (ctx.owner.is(PackageClass)) { + for (cdef @ TypeDef(moduleName, _) <- moduleDef.values) { + val moduleSym = ctx.denotNamed(moduleName.encode).symbol + if (moduleSym.isDefinedInCurrentRun) { + val className = moduleName.stripModuleClassSuffix.toTypeName + val classSym = ctx.denotNamed(className.encode).symbol + if (!classSym.isDefinedInCurrentRun) { + val absentClassSymbol = ctx.newClassSymbol(ctx.owner, className, EmptyFlags, _ => NoType) + enterSymbol(absentClassSymbol) + } + } + } + } } stats.foreach(expand) diff --git a/tests/pending/pos/false-companion/00_outerinnerTest_2.scala b/tests/pending/pos/false-companion/00_outerinnerTest_2.scala new file mode 100644 index 000000000..8f5802343 --- /dev/null +++ b/tests/pending/pos/false-companion/00_outerinnerTest_2.scala @@ -0,0 +1,5 @@ +package outer +package inner +object Test { + val x: Foo = new Foo +} diff --git a/tests/pending/pos/false-companion/01_outerinnerFoo_2.scala b/tests/pending/pos/false-companion/01_outerinnerFoo_2.scala new file mode 100644 index 000000000..111f7fcd5 --- /dev/null +++ b/tests/pending/pos/false-companion/01_outerinnerFoo_2.scala @@ -0,0 +1,5 @@ +package outer +package inner +object Foo { + // val a: Int = 1 +} diff --git a/tests/pending/pos/false-companion/outerFoo_1.scala b/tests/pending/pos/false-companion/outerFoo_1.scala new file mode 100644 index 000000000..8c2ef109c --- /dev/null +++ b/tests/pending/pos/false-companion/outerFoo_1.scala @@ -0,0 +1,2 @@ +package outer +class Foo diff --git a/tests/pending/pos/false-companion/outerinnerFoo_1.scala b/tests/pending/pos/false-companion/outerinnerFoo_1.scala new file mode 100644 index 000000000..29f3df724 --- /dev/null +++ b/tests/pending/pos/false-companion/outerinnerFoo_1.scala @@ -0,0 +1,5 @@ +package outer +package inner +object Foo { + val a: Int = 1 +} |