Skip to content

Commit 668ceb1

Browse files
committed
Don't assume this is visible in constructors
A constructor should never capture `this`, the object it constructs.
1 parent d71358f commit 668ceb1

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,13 @@ class CheckCaptures extends Recheck, SymTransformer:
466466
// if `sym` is not defined inside the owner of the environment.
467467
inline def isVisibleFromEnv(sym: Symbol, env: Env) =
468468
sym.exists && {
469+
val effectiveOwner =
470+
if env.owner.isConstructor then env.owner.owner
471+
else env.owner
469472
if env.kind == EnvKind.NestedInOwner then
470-
!sym.isProperlyContainedIn(env.owner)
473+
!sym.isProperlyContainedIn(effectiveOwner)
471474
else
472-
!sym.isContainedIn(env.owner)
475+
!sym.isContainedIn(effectiveOwner)
473476
}
474477

475478
/** Avoid locally defined capability by charging the underlying type

0 commit comments

Comments
 (0)