From 3f1b86f5fd92da2a3808ab25464b0f838f07335f Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 7 Aug 2021 21:17:53 +0200 Subject: [PATCH 1/2] Enter symbols under their target names after erasure Enter symbols under their target names in their parent scope after erasure. Fixes #13252 --- compiler/src/dotty/tools/dotc/core/Scopes.scala | 12 ++++++------ compiler/src/dotty/tools/dotc/core/TypeErasure.scala | 9 ++++++++- tests/run/i13252.scala | 12 ++++++++++++ tests/run/i13252/Foo_1.scala | 5 +++++ tests/run/i13252/Test_2.scala | 6 ++++++ 5 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 tests/run/i13252.scala create mode 100644 tests/run/i13252/Foo_1.scala create mode 100644 tests/run/i13252/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/core/Scopes.scala b/compiler/src/dotty/tools/dotc/core/Scopes.scala index cb5f5c55e337..b22ce84849c1 100644 --- a/compiler/src/dotty/tools/dotc/core/Scopes.scala +++ b/compiler/src/dotty/tools/dotc/core/Scopes.scala @@ -257,10 +257,6 @@ object Scopes { e } - /** create and enter a scope entry */ - protected def newScopeEntry(sym: Symbol)(using Context): ScopeEntry = - newScopeEntry(sym.name, sym) - private def enterInHash(e: ScopeEntry)(using Context): Unit = { val idx = e.name.hashCode & (hashTable.length - 1) e.tail = hashTable(idx) @@ -273,7 +269,11 @@ object Scopes { if (sym.isType && ctx.phaseId <= typerPhase.id) assert(lookup(sym.name) == NoSymbol, s"duplicate ${sym.debugString}; previous was ${lookup(sym.name).debugString}") // !!! DEBUG - newScopeEntry(sym) + enter(sym.name, sym) + } + + final def enter[T <: Symbol](name: Name, sym: T)(using Context): T = { + newScopeEntry(name, sym) sym } @@ -375,7 +375,7 @@ object Scopes { } if ((e eq null) && (synthesize != null)) { val sym = synthesize(name) - if (sym.exists) newScopeEntry(sym) else e + if (sym.exists) newScopeEntry(sym.name, sym) else e } else e } diff --git a/compiler/src/dotty/tools/dotc/core/TypeErasure.scala b/compiler/src/dotty/tools/dotc/core/TypeErasure.scala index 3a077407d0b5..7e89b4e2c2c9 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeErasure.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeErasure.scala @@ -654,7 +654,14 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst tr1 :: trs1.filterNot(_.isAnyRef) case nil => nil } - val erasedDecls = decls.filteredScope(sym => !sym.isType || sym.isClass) + var erasedDecls = decls.filteredScope(sym => !sym.isType || sym.isClass).openForMutations + for dcl <- erasedDecls.iterator do + if dcl.lastKnownDenotation.unforcedAnnotation(defn.TargetNameAnnot).isDefined + && dcl.targetName != dcl.name + then + if erasedDecls eq decls then erasedDecls = erasedDecls.cloneScope + erasedDecls.unlink(dcl) + erasedDecls.enter(dcl.targetName, dcl) val selfType1 = if cls.is(Module) then cls.sourceModule.termRef else NoType tp.derivedClassInfo(NoPrefix, erasedParents, erasedDecls, selfType1) // can't replace selftype by NoType because this would lose the sourceModule link diff --git a/tests/run/i13252.scala b/tests/run/i13252.scala new file mode 100644 index 000000000000..d12706f62a95 --- /dev/null +++ b/tests/run/i13252.scala @@ -0,0 +1,12 @@ +object Test: + + class Star + + trait Foo: + @annotation.targetName("star") + val * : Star = new Star + + object Bar extends Foo + + def main(sa: Array[String]): Unit = + Bar.* \ No newline at end of file diff --git a/tests/run/i13252/Foo_1.scala b/tests/run/i13252/Foo_1.scala new file mode 100644 index 000000000000..7e7551e14f4c --- /dev/null +++ b/tests/run/i13252/Foo_1.scala @@ -0,0 +1,5 @@ +class Star + +trait Foo: + @annotation.targetName("star") + val * : Star = new Star diff --git a/tests/run/i13252/Test_2.scala b/tests/run/i13252/Test_2.scala new file mode 100644 index 000000000000..b4d382d2cc1f --- /dev/null +++ b/tests/run/i13252/Test_2.scala @@ -0,0 +1,6 @@ +object Test: + + object Bar extends Foo + + def main(sa: Array[String]): Unit = + Bar.* \ No newline at end of file From 7934075b5847de95b698f33efb64da0f4a76b3d1 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 13 Aug 2021 15:13:09 +0200 Subject: [PATCH 2/2] Address review comment --- tests/run/{i13252.scala => i13252a.scala} | 0 tests/run/{i13252 => i13252b}/Foo_1.scala | 0 tests/run/{i13252 => i13252b}/Test_2.scala | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename tests/run/{i13252.scala => i13252a.scala} (100%) rename tests/run/{i13252 => i13252b}/Foo_1.scala (100%) rename tests/run/{i13252 => i13252b}/Test_2.scala (100%) diff --git a/tests/run/i13252.scala b/tests/run/i13252a.scala similarity index 100% rename from tests/run/i13252.scala rename to tests/run/i13252a.scala diff --git a/tests/run/i13252/Foo_1.scala b/tests/run/i13252b/Foo_1.scala similarity index 100% rename from tests/run/i13252/Foo_1.scala rename to tests/run/i13252b/Foo_1.scala diff --git a/tests/run/i13252/Test_2.scala b/tests/run/i13252b/Test_2.scala similarity index 100% rename from tests/run/i13252/Test_2.scala rename to tests/run/i13252b/Test_2.scala