Skip to content

Commit ae94fb6

Browse files
Backport "Make eraseInfo work for classes with EmptyScopes" to LTS (#20868)
Backports #19550 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 89ec44c + 2c8480d commit ae94fb6

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

compiler/src/dotty/tools/dotc/core/Scopes.scala

+18-9
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,28 @@ object Scopes {
158158
}
159159

160160
/** The scope that keeps only those symbols from this scope that match the
161-
* given predicates. If all symbols match, returns the scope itself, otherwise
162-
* a copy with the matching symbols.
161+
* given predicates, renamed with the given rename function.
162+
* If renaming is not needed for a symbol, the rename function should return `null`.
163+
* If all symbols match and none are renamed, returns the scope itself, otherwise
164+
* a copy with the matching and renamed symbols.
163165
*/
164-
final def filteredScope(p: Symbol => Boolean)(using Context): Scope = {
166+
final def filteredScope(
167+
keep: Symbol => Boolean,
168+
rename: Symbol => Name | Null = _ => null)(using Context): Scope =
165169
var result: MutableScope | Null = null
166-
for (sym <- iterator)
167-
if (!p(sym)) {
168-
if (result == null) result = cloneScope
170+
for sym <- iterator do
171+
def drop() =
172+
if result == null then result = cloneScope
169173
result.nn.unlink(sym)
170-
}
174+
if keep(sym) then
175+
val newName = rename(sym)
176+
if newName != null then
177+
drop()
178+
result.nn.enter(newName, sym)
179+
else
180+
drop()
171181
// TODO: improve flow typing to handle this case
172-
if (result == null) this else result.uncheckedNN
173-
}
182+
if result == null then this else result.uncheckedNN
174183

175184
def implicitDecls(using Context): List[TermRef] = Nil
176185

compiler/src/dotty/tools/dotc/core/TypeErasure.scala

+10-9
Original file line numberDiff line numberDiff line change
@@ -725,14 +725,14 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
725725
tr1 :: trs1.filterNot(_.isAnyRef)
726726
case nil => nil
727727
}
728-
var erasedDecls = decls.filteredScope(sym => !sym.isType || sym.isClass).openForMutations
729-
for dcl <- erasedDecls.iterator do
730-
if dcl.lastKnownDenotation.unforcedAnnotation(defn.TargetNameAnnot).isDefined
731-
&& dcl.targetName != dcl.name
732-
then
733-
if erasedDecls eq decls then erasedDecls = erasedDecls.cloneScope
734-
erasedDecls.unlink(dcl)
735-
erasedDecls.enter(dcl.targetName, dcl)
728+
val erasedDecls = decls.filteredScope(
729+
keep = sym => !sym.isType || sym.isClass,
730+
rename = sym =>
731+
if sym.lastKnownDenotation.unforcedAnnotation(defn.TargetNameAnnot).isDefined
732+
&& sym.targetName != sym.name
733+
then sym.targetName
734+
else null
735+
)
736736
val selfType1 = if cls.is(Module) then cls.sourceModule.termRef else NoType
737737
tp.derivedClassInfo(NoPrefix, erasedParents, erasedDecls, selfType1)
738738
// can't replace selftype by NoType because this would lose the sourceModule link
@@ -814,7 +814,8 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
814814
eraseResult(tp1.resultType) match
815815
case rt: MethodType => rt
816816
case rt => MethodType(Nil, Nil, rt)
817-
case tp1 => this(tp1)
817+
case tp1 =>
818+
this(tp1)
818819

819820
private def eraseDerivedValueClass(tp: Type)(using Context): Type = {
820821
val cls = tp.classSymbol.asClass

tests/pos/i19530.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object A {
2+
def x = classOf[scala.Singleton]
3+
}

0 commit comments

Comments
 (0)