Skip to content

Commit

Permalink
Make eraseInfo work for classes with empty scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Jan 28, 2024
1 parent 1716bcd commit c5d3a10
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Scopes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ object Scopes {

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

def openForMutations: MutableScope = unsupported("openForMutations")
def openForMutations: MutableScope = unsupported(s"openForMutations $this")

final def toText(printer: Printer): Text = printer.toText(this)

Expand Down
22 changes: 13 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,17 @@ 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)
var erasedDecls = decls.filteredScope(sym => !sym.isType || sym.isClass)
if !erasedDecls.isEmpty then
var eds = erasedDecls.openForMutations
for dcl <- eds.iterator do
if dcl.lastKnownDenotation.unforcedAnnotation(defn.TargetNameAnnot).isDefined
&& dcl.targetName != dcl.name
then
if eds eq decls then eds = eds.cloneScope
eds.unlink(dcl)
eds.enter(dcl.targetName, dcl)
erasedDecls = eds
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 +817,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
2 changes: 1 addition & 1 deletion tests/neg/i19506.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//> using options "-source:3.4-migration",
//> using options -source 3.4-migration

trait Reader[T]
def read[T: Reader](s: String, trace: Boolean = false): T = ???
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 c5d3a10

Please sign in to comment.