From a08789a7ef5eec67c28551611792c1dc8f573b8d Mon Sep 17 00:00:00 2001 From: odersky Date: Sun, 4 Sep 2022 13:33:53 +0200 Subject: [PATCH] Harden canAssign Fixes #15897 --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 +- tests/neg/i15897.scala | 8 ++++++++ tests/pos/i15897.scala | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i15897.scala create mode 100644 tests/pos/i15897.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index d802ef0df973..628dcc5a9d92 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -989,7 +989,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer def canAssign(sym: Symbol) = sym.is(Mutable, butNot = Accessor) || - ctx.owner.isPrimaryConstructor && !sym.is(Method) && sym.owner == ctx.owner.owner || + ctx.owner.isPrimaryConstructor && !sym.is(Method) && sym.maybeOwner == ctx.owner.owner || // allow assignments from the primary constructor to class fields ctx.owner.name.is(TraitSetterName) || ctx.owner.isStaticConstructor diff --git a/tests/neg/i15897.scala b/tests/neg/i15897.scala new file mode 100644 index 000000000000..d9dbe94c4eef --- /dev/null +++ b/tests/neg/i15897.scala @@ -0,0 +1,8 @@ +object O { + class AC(code: => Unit) + + val action = new AC({mode = ???}) {} // error + + def mode: AnyRef = ??? + def mode=(em: AnyRef): Unit = {} // error // error // error +} \ No newline at end of file diff --git a/tests/pos/i15897.scala b/tests/pos/i15897.scala new file mode 100644 index 000000000000..b039401a6519 --- /dev/null +++ b/tests/pos/i15897.scala @@ -0,0 +1,8 @@ +object O { + class AC(code: => Unit) + + val action = new AC({mode = ???}) {} + + def mode: AnyRef = ??? + def mode_=(em: AnyRef): Unit = {} +} \ No newline at end of file