Skip to content

Commit 8540fb8

Browse files
committed
Don't generate capture set variables for self types of pure classes
The tricky thing here is how to recognize that a class is pure since that is known only during capture checking and we are at Setup, the phase before. But we can approximate by treating the `Pure` trait as definitely pure.
1 parent 1fb0619 commit 8540fb8

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

compiler/src/dotty/tools/dotc/cc/Setup.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,9 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
522522
tree.symbol match
523523
case cls: ClassSymbol =>
524524
val cinfo @ ClassInfo(prefix, _, ps, decls, selfInfo) = cls.classInfo
525-
if (selfInfo eq NoType) || cls.is(ModuleClass) && !cls.isStatic then
525+
if ((selfInfo eq NoType) || cls.is(ModuleClass) && !cls.isStatic)
526+
&& !cls.isPureClass
527+
then
526528
// add capture set to self type of nested classes if no self type is given explicitly.
527529
val newSelfType = CapturingType(cinfo.selfType, CaptureSet.Var(cls))
528530
val ps1 = inContext(ctx.withOwner(cls)):

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ class Definitions {
14431443
/** Base classes that are assumed to be pure for the purposes of capture checking.
14441444
* Every class inheriting from a pure baseclass is pure.
14451445
*/
1446-
@tu lazy val pureBaseClasses = Set(defn.ThrowableClass)
1446+
@tu lazy val pureBaseClasses = Set(ThrowableClass, PureClass)
14471447

14481448
/** Non-inheritable lasses that are assumed to be pure for the purposes of capture checking,
14491449
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
trait Stepper[+A]:
3+
this: Stepper[A]^ =>
4+
5+
object Stepper:
6+
trait EfficientSplit
7+
8+
sealed trait StepperShape[-T, S <: Stepper[_]^] extends Pure
9+
10+
trait IterableOnce[+A] extends Any:
11+
this: IterableOnce[A]^ =>
12+
def stepper[S <: Stepper[_]^{this}](implicit shape: StepperShape[A, S]): S = ???
13+
14+
sealed abstract class ArraySeq[sealed T] extends IterableOnce[T], Pure:
15+
def array: Array[_]
16+
17+
def sorted[B >: T](implicit ord: Ordering[B]): ArraySeq[T] =
18+
val arr = array.asInstanceOf[Array[T]].sorted(ord.asInstanceOf[Ordering[Any]]).asInstanceOf[Array[T]]
19+
ArraySeq.make(arr).asInstanceOf[ArraySeq[T]]
20+
21+
object ArraySeq:
22+
23+
def make[sealed T](x: Array[T]): ArraySeq[T] = ???
24+
25+
final class ofRef[T <: AnyRef](val array: Array[T]) extends ArraySeq[T], Pure:
26+
override def stepper[S <: Stepper[_]](implicit shape: StepperShape[T, S]): S & Stepper.EfficientSplit = ???
27+

0 commit comments

Comments
 (0)