diff --git a/compiler/src/dotty/tools/dotc/typer/CheckCaptures.scala b/compiler/src/dotty/tools/dotc/typer/CheckCaptures.scala index 1415016fea26..69f184bd79ef 100644 --- a/compiler/src/dotty/tools/dotc/typer/CheckCaptures.scala +++ b/compiler/src/dotty/tools/dotc/typer/CheckCaptures.scala @@ -263,7 +263,12 @@ class CheckCaptures extends Recheck: case ref: TermRef => ref.symbol.enclosure != ownEnclosure case _ => true } - checkSubset(targetSet, curEnv.captured, pos) + def includeIn(env: Env) = + capt.println(i"Include call capture $targetSet in ${env.owner}") + checkSubset(targetSet, env.captured, pos) + includeIn(curEnv) + if curEnv.owner.isTerm && curEnv.outer.owner.isClass then + includeIn(curEnv.outer) def includeBoxedCaptures(tp: Type, pos: SrcPos)(using Context): Unit = if curEnv.isOpen then diff --git a/tests/neg-custom-args/captures/nestedclass.check b/tests/neg-custom-args/captures/nestedclass.check new file mode 100644 index 000000000000..d3912d417a4c --- /dev/null +++ b/tests/neg-custom-args/captures/nestedclass.check @@ -0,0 +1,7 @@ +-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/nestedclass.scala:15:15 ---------------------------------- +15 | val xsc: C = xs // error + | ^^ + | Found: (xs : {cap1} C) + | Required: C + +longer explanation available when compiling with `-explain` diff --git a/tests/neg-custom-args/captures/nestedclass.scala b/tests/neg-custom-args/captures/nestedclass.scala new file mode 100644 index 000000000000..38adf7998868 --- /dev/null +++ b/tests/neg-custom-args/captures/nestedclass.scala @@ -0,0 +1,15 @@ +class CC +type Cap = {*} CC + +abstract class C: + def head: String + +def test(cap1: Cap, cap2: Cap) = + def f(x: String): String = if cap1 == cap1 then "" else "a" + def g(x: String): String = if cap2 == cap2 then "" else "a" + + val xs = + class Cimpl extends C: + def head = f("") + new Cimpl + val xsc: C = xs // error