Skip to content

Commit

Permalink
Make eraseInfo work for classes with EmptyScopes (#19550)
Browse files Browse the repository at this point in the history
Fixes #19530
  • Loading branch information
noti0na1 authored Jan 30, 2024
2 parents 321a614 + 7a6c230 commit f89f429
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
27 changes: 18 additions & 9 deletions compiler/src/dotty/tools/dotc/core/Scopes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,28 @@ object Scopes {
}

/** The scope that keeps only those symbols from this scope that match the
* given predicates. If all symbols match, returns the scope itself, otherwise
* a copy with the matching symbols.
* given predicates, renamed with the given rename function.
* If renaming is not needed for a symbol, the rename function should return `null`.
* If all symbols match and none are renamed, returns the scope itself, otherwise
* a copy with the matching and renamed symbols.
*/
final def filteredScope(p: Symbol => Boolean)(using Context): Scope = {
final def filteredScope(
keep: Symbol => Boolean,
rename: Symbol => Name | Null = _ => null)(using Context): Scope =
var result: MutableScope | Null = null
for (sym <- iterator)
if (!p(sym)) {
if (result == null) result = cloneScope
for sym <- iterator do
def drop() =
if result == null then result = cloneScope
result.nn.unlink(sym)
}
if keep(sym) then
val newName = rename(sym)
if newName != null then
drop()
result.nn.enter(newName, sym)
else
drop()
// TODO: improve flow typing to handle this case
if (result == null) this else result.uncheckedNN
}
if result == null then this else result.uncheckedNN

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

Expand Down
19 changes: 10 additions & 9 deletions compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,14 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
tr1 :: trs1.filterNot(_.isAnyRef)
case nil => nil
}
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 erasedDecls = decls.filteredScope(
keep = sym => !sym.isType || sym.isClass,
rename = sym =>
if sym.lastKnownDenotation.unforcedAnnotation(defn.TargetNameAnnot).isDefined
&& sym.targetName != sym.name
then sym.targetName
else null
)
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
Expand Down Expand Up @@ -814,7 +814,8 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
eraseResult(tp1.resultType) match
case rt: MethodType => rt
case rt => MethodType(Nil, Nil, rt)
case tp1 => this(tp1)
case tp1 =>
this(tp1)

private def eraseDerivedValueClass(tp: Type)(using Context): Type = {
val cls = tp.classSymbol.asClass
Expand Down
3 changes: 3 additions & 0 deletions tests/pos/i19530.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object A {
def x = classOf[scala.Singleton]
}

0 comments on commit f89f429

Please sign in to comment.