diff --git a/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala b/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala index 261577cb718f..b75a688d6e6c 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala @@ -1229,7 +1229,20 @@ object Semantic: ref match case Select(supert: Super, _) => val SuperType(thisTp, superTp) = supert.tpe: @unchecked - val thisValue2 = extendTrace(ref) { resolveThis(thisTp.classSymbol.asClass, thisV, klass) } + val thisValue2 = extendTrace(ref) { + thisTp match + case thisTp: ThisType => + cases(thisTp, thisV, klass) + + case AndType(thisTp: ThisType, _) => + // Self-type annotation will generate an intersection type for `this`. + // See examples/i17997.scala + cases(thisTp, thisV, klass) + + case _ => + report.warning("[Internal error] Unexpected type " + thisTp.show + ", trace:\n" + Trace.show, ref) + Hot + } withTrace(trace2) { thisValue2.call(ref.symbol, args, thisTp, superTp) } case Select(qual, _) => diff --git a/tests/init/pos/i17997.scala b/tests/init/pos/i17997.scala new file mode 100644 index 000000000000..ba311696e9ef --- /dev/null +++ b/tests/init/pos/i17997.scala @@ -0,0 +1,20 @@ +abstract class FunSuite: + def foo(): Unit = println("FunSuite") + + foo() + +trait MySelfType + +trait MyTrait extends FunSuite { this: MySelfType => +} + +abstract class MyAbstractClass extends FunSuite { this: MySelfType => + + override def foo() = { + println("MyAbstractClass") + super.foo() + } +} + +final class MyFinalClass extends MyAbstractClass with MyTrait with MySelfType: + val n: Int = 100