diff --git a/compiler/src/dotty/tools/dotc/typer/CheckCaptures.scala b/compiler/src/dotty/tools/dotc/typer/CheckCaptures.scala index 070d46b08ead..d815c09e99c9 100644 --- a/compiler/src/dotty/tools/dotc/typer/CheckCaptures.scala +++ b/compiler/src/dotty/tools/dotc/typer/CheckCaptures.scala @@ -325,7 +325,8 @@ class CheckCaptures extends Recheck: override def recheckRHS(tree: Tree, pt: Type, sym: Symbol)(using Context): Type = val pt1 = pt match case CapturingType(core, refs, _) - if sym.owner.isClass && refs.elems.contains(sym.owner.thisType) => + if sym.owner.isClass && !sym.owner.isExtensibleClass + && refs.elems.contains(sym.owner.thisType) => val paramCaptures = sym.paramSymss.flatten.foldLeft(CaptureSet.empty) { (cs, p) => val pcs = p.info.captureSet diff --git a/tests/neg-custom-args/captures/lazylists2.check b/tests/neg-custom-args/captures/lazylists2.check index 3fe133acd52e..8e09dd26cccf 100644 --- a/tests/neg-custom-args/captures/lazylists2.check +++ b/tests/neg-custom-args/captures/lazylists2.check @@ -6,7 +6,7 @@ longer explanation available when compiling with `-explain` -- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylists2.scala:18:4 ------------------------------------ -18 | class Mapped extends LazyList[B]: // error +18 | final class Mapped extends LazyList[B]: // error | ^ | Found: {f, xs} LazyList[B] | Required: {f} LazyList[B] @@ -18,7 +18,7 @@ longer explanation available when compiling with `-explain` longer explanation available when compiling with `-explain` -- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylists2.scala:27:4 ------------------------------------ -27 | class Mapped extends LazyList[B]: // error +27 | final class Mapped extends LazyList[B]: // error | ^ | Found: {f, xs} LazyList[B] | Required: {xs} LazyList[B] @@ -36,3 +36,10 @@ longer explanation available when compiling with `-explain` | Required: {xs} LazyList[B] longer explanation available when compiling with `-explain` +-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylists2.scala:59:48 ----------------------------------- +59 | def tail: {this} LazyList[B] = xs.tail.map(f) // error + | ^^^^^^^^^^^^^^ + | Found: {f} LazyList[B] + | Required: {Mapped.this} LazyList[B] + +longer explanation available when compiling with `-explain` diff --git a/tests/neg-custom-args/captures/lazylists2.scala b/tests/neg-custom-args/captures/lazylists2.scala index 588be751592a..c31a1ae5d04f 100644 --- a/tests/neg-custom-args/captures/lazylists2.scala +++ b/tests/neg-custom-args/captures/lazylists2.scala @@ -15,7 +15,7 @@ object LazyNil extends LazyList[Nothing]: extension [A](xs: {*} LazyList[A]) def map[B](f: {*} A => B): {f} LazyList[B] = - class Mapped extends LazyList[B]: // error + final class Mapped extends LazyList[B]: // error this: ({xs, f} Mapped) => def isEmpty = false @@ -24,7 +24,7 @@ extension [A](xs: {*} LazyList[A]) new Mapped def map2[B](f: {*} A => B): {xs} LazyList[B] = - class Mapped extends LazyList[B]: // error + final class Mapped extends LazyList[B]: // error this: ({xs, f} Mapped) => def isEmpty = false @@ -33,7 +33,7 @@ extension [A](xs: {*} LazyList[A]) new Mapped def map3[B](f: {*} A => B): {xs} LazyList[B] = - class Mapped extends LazyList[B]: + final class Mapped extends LazyList[B]: this: ({xs} Mapped) => def isEmpty = false @@ -42,7 +42,7 @@ extension [A](xs: {*} LazyList[A]) new Mapped def map4[B](f: {*} A => B): {xs} LazyList[B] = - class Mapped extends LazyList[B]: + final class Mapped extends LazyList[B]: this: ({xs, f} Mapped) => def isEmpty = false @@ -50,3 +50,15 @@ extension [A](xs: {*} LazyList[A]) def tail: {xs, f} LazyList[B] = xs.tail.map(f) // error new Mapped + def map5[B](f: {*} A => B): LazyList[B] = + class Mapped extends LazyList[B]: + this: ({xs, f} Mapped) => + + def isEmpty = false + def head: B = f(xs.head) + def tail: {this} LazyList[B] = xs.tail.map(f) // error + class Mapped2 extends Mapped: + this: Mapped => + new Mapped2 + + diff --git a/tests/pos-custom-args/captures/lazylists.scala b/tests/pos-custom-args/captures/lazylists.scala index 5c8e5e723e2d..17d5f8546edc 100644 --- a/tests/pos-custom-args/captures/lazylists.scala +++ b/tests/pos-custom-args/captures/lazylists.scala @@ -15,7 +15,7 @@ object LazyNil extends LazyList[Nothing]: extension [A](xs: {*} LazyList[A]) def map[B](f: {*} A => B): {xs, f} LazyList[B] = - class Mapped extends LazyList[B]: + final class Mapped extends LazyList[B]: this: ({xs, f} Mapped) => def isEmpty = false diff --git a/tests/pos-custom-args/captures/lazylists1.scala b/tests/pos-custom-args/captures/lazylists1.scala index 0fd1e767f49b..4c8006fb0e29 100644 --- a/tests/pos-custom-args/captures/lazylists1.scala +++ b/tests/pos-custom-args/captures/lazylists1.scala @@ -13,7 +13,7 @@ object LazyNil extends LazyList[Nothing]: def head = ??? def tail = ??? -class LazyCons[+T](val x: T, val xs: {*} () => {*} LazyList[T]) extends LazyList[T]: +final class LazyCons[+T](val x: T, val xs: {*} () => {*} LazyList[T]) extends LazyList[T]: this: ({*} LazyList[T]) => def isEmpty = false