Skip to content

Commit f9d1ab8

Browse files
committed
Make eraseInfo work for classes with EmptyScopes
Fixes #19506 [Cherry-picked 8d9af0c][modified]
1 parent a69ac87 commit f9d1ab8

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

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

+17-9
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,27 @@ 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 all symbols match and none are renamed, returns the scope itself, otherwise
163+
* a copy with the matching and renamed symbols.
163164
*/
164-
final def filteredScope(p: Symbol => Boolean)(using Context): Scope = {
165+
final def filteredScope(
166+
keep: Symbol => Boolean,
167+
rename: (Symbol, Name) => Name = (_, name) => name)(using Context): Scope =
165168
var result: MutableScope | Null = null
166-
for (sym <- iterator)
167-
if (!p(sym)) {
168-
if (result == null) result = cloneScope
169+
for sym <- iterator do
170+
def drop() =
171+
if result == null then result = cloneScope
169172
result.nn.unlink(sym)
170-
}
173+
if keep(sym) then
174+
val newName = rename(sym, sym.name)
175+
if newName ne sym.name then
176+
drop()
177+
result.nn.enter(newName, sym)
178+
else
179+
drop()
171180
// TODO: improve flow typing to handle this case
172-
if (result == null) this else result.uncheckedNN
173-
}
181+
if result == null then this else result.uncheckedNN
174182

175183
def implicitDecls(using Context): List[TermRef] = Nil
176184

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -725,14 +725,13 @@ 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, name) =>
731+
if sym.lastKnownDenotation.unforcedAnnotation(defn.TargetNameAnnot).isDefined
732+
then sym.targetName
733+
else name
734+
)
736735
val selfType1 = if cls.is(Module) then cls.sourceModule.termRef else NoType
737736
tp.derivedClassInfo(NoPrefix, erasedParents, erasedDecls, selfType1)
738737
// can't replace selftype by NoType because this would lose the sourceModule link
@@ -814,7 +813,8 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
814813
eraseResult(tp1.resultType) match
815814
case rt: MethodType => rt
816815
case rt => MethodType(Nil, Nil, rt)
817-
case tp1 => this(tp1)
816+
case tp1 =>
817+
this(tp1)
818818

819819
private def eraseDerivedValueClass(tp: Type)(using Context): Type = {
820820
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)