Closed
Description
When running the following code with -Xprint:getters
:
class A {
extension Ext on (a: Int) {
def foo: Int = 1
}
}
The compiler crashes:
Exception in thread "main" java.util.NoSuchElementException: head of empty list
at scala.collection.immutable.Nil$.head(List.scala:629)
at dotty.tools.dotc.printing.RefinedPrinter.defDefToText$$anonfun$3$$anonfun$2(RefinedPrinter.scala:784)
at dotty.tools.dotc.printing.RefinedPrinter.withEnclosingDef(RefinedPrinter.scala:46)
The problem is that we check whether a def is an extension method using:
val isExtension = tree.hasType && tree.symbol.is(Extension)
but objects in classes are implemented using lazy vals, and after the getters phase, lazy val in classes become def, so the object Ext
which has the Extension
flag gets misinterpreted as an extension method.
SymUtils#isCollectiveExtensionClass has the same problem (but is only called during Typer currently): https://github.com/lampepfl/dotty/blob/11760ea2b23ad7eb5b5749f489ee5395980c0daf/compiler/src/dotty/tools/dotc/transform/SymUtils.scala#L224-L225