Skip to content

Commit f89f429

Browse files
authored
Make eraseInfo work for classes with EmptyScopes (#19550)
Fixes #19530
2 parents 321a614 + 7a6c230 commit f89f429

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/Scopes.scala

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

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

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

Diff for: 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

Diff for: 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)