From 5f6fc55680cf8405959931daf44b803fa51bcedb Mon Sep 17 00:00:00 2001 From: Fengyun Liu Date: Mon, 26 Jun 2023 23:43:56 +0200 Subject: [PATCH 1/2] Fix #17997: Handle intersection type as this type of super type --- .../tools/dotc/transform/init/Semantic.scala | 15 +++++++++++++- tests/init/pos/i17997.scala | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/init/pos/i17997.scala diff --git a/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala b/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala index 261577cb718f..67fbd39e76a9 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, _) => + // This 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 From 7d31ef2c2fd2e26de9d71155e4dc52355591ad17 Mon Sep 17 00:00:00 2001 From: Fengyun Liu Date: Tue, 27 Jun 2023 20:42:13 +0200 Subject: [PATCH 2/2] Update code comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondřej Lhoták --- compiler/src/dotty/tools/dotc/transform/init/Semantic.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala b/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala index 67fbd39e76a9..b75a688d6e6c 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Semantic.scala @@ -1235,7 +1235,7 @@ object Semantic: cases(thisTp, thisV, klass) case AndType(thisTp: ThisType, _) => - // This type annotation will generate an intersection type for `this`. + // Self-type annotation will generate an intersection type for `this`. // See examples/i17997.scala cases(thisTp, thisV, klass)