From 0876baf8c0a1f607a930b7f9340a76284891f10e Mon Sep 17 00:00:00 2001 From: odersky Date: Fri, 19 Apr 2024 18:55:57 +0200 Subject: [PATCH 1/2] Fix conversion of this.fld capture refs under separate compilation --- compiler/src/dotty/tools/dotc/core/TypeUtils.scala | 9 +++++++++ compiler/src/dotty/tools/dotc/core/Types.scala | 2 +- tests/pos/captureRef-separate-compilation/Future_1.scala | 5 +++++ tests/pos/captureRef-separate-compilation/test_2.scala | 5 +++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/pos/captureRef-separate-compilation/Future_1.scala create mode 100644 tests/pos/captureRef-separate-compilation/test_2.scala diff --git a/compiler/src/dotty/tools/dotc/core/TypeUtils.scala b/compiler/src/dotty/tools/dotc/core/TypeUtils.scala index 4f0162a0fa96..a3d6ab065a77 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeUtils.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeUtils.scala @@ -135,5 +135,14 @@ class TypeUtils { case _ => val cls = self.underlyingClassRef(refinementOK = false).typeSymbol cls.isTransparentClass && (!traitOnly || cls.is(Trait)) + + /** Is this type the ThisType of class `cls?`. Note we can't use `self eq cls.thisType` for this, + * since ThisTypes take TermRef parameters and semantically equal TermRefs could have different + * forms (for instance one could use as a prefix the ThisType of an enclosing static module or package, + * and the other could select it from something further out) + */ + def isThisTypeOf(cls: Symbol)(using Context) = self match + case self: Types.ThisType => self.cls == cls + case _ => false } } diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 747c3a0fd3dd..870e985fd48e 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -2991,7 +2991,7 @@ object Types extends TypeUtils { */ override def isTrackableRef(using Context) = ((prefix eq NoPrefix) - || symbol.is(ParamAccessor) && (prefix eq symbol.owner.thisType) + || symbol.is(ParamAccessor) && prefix.isThisTypeOf(symbol.owner) || isRootCapability ) && !symbol.isOneOf(UnstableValueFlags) diff --git a/tests/pos/captureRef-separate-compilation/Future_1.scala b/tests/pos/captureRef-separate-compilation/Future_1.scala new file mode 100644 index 000000000000..c01aeadd9360 --- /dev/null +++ b/tests/pos/captureRef-separate-compilation/Future_1.scala @@ -0,0 +1,5 @@ +class Future[T] +object Future: + class Collector[T](fs: (Future[T]^)*) + class MutableCollector[T](val futures: (Future[T]^)*) extends Collector[T](futures*): + def add(future: Future[T]^{futures*}) = ??? diff --git a/tests/pos/captureRef-separate-compilation/test_2.scala b/tests/pos/captureRef-separate-compilation/test_2.scala new file mode 100644 index 000000000000..35714090a9ac --- /dev/null +++ b/tests/pos/captureRef-separate-compilation/test_2.scala @@ -0,0 +1,5 @@ +def test = + val collector: Future.MutableCollector[Int] = Future.MutableCollector() + collector.add(???) + + From 736c6f4cc945dc3418cf68c74fbd680ca060e69b Mon Sep 17 00:00:00 2001 From: odersky Date: Fri, 19 Apr 2024 18:57:29 +0200 Subject: [PATCH 2/2] Move test to proper directory --- .../captures}/captureRef-separate-compilation/Future_1.scala | 0 .../captures}/captureRef-separate-compilation/test_2.scala | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/{pos => pos-custom-args/captures}/captureRef-separate-compilation/Future_1.scala (100%) rename tests/{pos => pos-custom-args/captures}/captureRef-separate-compilation/test_2.scala (100%) diff --git a/tests/pos/captureRef-separate-compilation/Future_1.scala b/tests/pos-custom-args/captures/captureRef-separate-compilation/Future_1.scala similarity index 100% rename from tests/pos/captureRef-separate-compilation/Future_1.scala rename to tests/pos-custom-args/captures/captureRef-separate-compilation/Future_1.scala diff --git a/tests/pos/captureRef-separate-compilation/test_2.scala b/tests/pos-custom-args/captures/captureRef-separate-compilation/test_2.scala similarity index 100% rename from tests/pos/captureRef-separate-compilation/test_2.scala rename to tests/pos-custom-args/captures/captureRef-separate-compilation/test_2.scala