Skip to content

Commit

Permalink
Use default self type more often (#21373)
Browse files Browse the repository at this point in the history
We now also use cap as the default for the self type's capture set if a
base class has an explicit self type, but that type's capture set is
universal. This requires fewer self type annotations.
  • Loading branch information
bracevac authored Aug 15, 2024
2 parents 18af52a + 97bbc24 commit bcf5d6a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/cc/CaptureOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,11 @@ extension (cls: ClassSymbol)
// and err on the side of impure.
&& selfType.exists && selfType.captureSet.isAlwaysEmpty

def baseClassHasExplicitSelfType(using Context): Boolean =
def baseClassHasExplicitNonUniversalSelfType(using Context): Boolean =
cls.baseClasses.exists: bc =>
bc.is(CaptureChecked) && bc.givenSelfType.exists
bc.is(CaptureChecked)
&& bc.givenSelfType.exists
&& !bc.givenSelfType.captureSet.isUniversal

def matchesExplicitRefsInBaseClass(refs: CaptureSet)(using Context): Boolean =
cls.baseClasses.tail.exists: bc =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/cc/Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
else if cls.isPureClass then
// is cls is known to be pure, nothing needs to be added to self type
selfInfo
else if !cls.isEffectivelySealed && !cls.baseClassHasExplicitSelfType then
else if !cls.isEffectivelySealed && !cls.baseClassHasExplicitNonUniversalSelfType then
// assume {cap} for completely unconstrained self types of publicly extensible classes
CapturingType(cinfo.selfType, CaptureSet.universal)
else
Expand Down
5 changes: 1 addition & 4 deletions scala2-library-cc/src/scala/collection/IndexedSeqView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ package collection
import scala.annotation.nowarn
import language.experimental.captureChecking

trait IndexedSeqViewOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C] {
self: IndexedSeqViewOps[A, CC, C]^ =>
}
trait IndexedSeqViewOps[+A, +CC[_], +C] extends Any with SeqViewOps[A, CC, C]

/** View defined in terms of indexing a range */
trait IndexedSeqView[+A] extends IndexedSeqViewOps[A, View, View[A]] with SeqView[A] {
self: IndexedSeqView[A]^ =>

override def view: IndexedSeqView[A]^{this} = this

Expand Down
2 changes: 0 additions & 2 deletions scala2-library-cc/src/scala/collection/SeqView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import scala.annotation.unchecked.uncheckedCaptures
* mapping a SeqView with an impure function gives an impure view).
*/
trait SeqViewOps[+A, +CC[_], +C] extends Any with IterableOps[A, CC, C] {
self: SeqViewOps[A, CC, C]^ =>

def length: Int
def apply(x: Int): A
Expand Down Expand Up @@ -75,7 +74,6 @@ trait SeqViewOps[+A, +CC[_], +C] extends Any with IterableOps[A, CC, C] {
}

trait SeqView[+A] extends SeqViewOps[A, View, View[A]] with View[A] {
self: SeqView[A]^ =>

override def view: SeqView[A]^{this} = this

Expand Down

0 comments on commit bcf5d6a

Please sign in to comment.