diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index ae50d626cb1f..28bd4b6c1b6f 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1334,12 +1334,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer val setter = toSetter(lhsCore) if setter.isEmpty then reassignmentToVal - else tryEither { + else val assign = untpd.Apply(setter, tree.rhs :: Nil) typed(assign, IgnoredProto(pt)) - } { - (_, _) => reassignmentToVal - } case _ => lhsCore.tpe match { case ref: TermRef => val lhsVal = lhsCore.denot.suchThat(!_.is(Method)) diff --git a/tests/neg/i20338a.check b/tests/neg/i20338a.check new file mode 100644 index 000000000000..a329492bd990 --- /dev/null +++ b/tests/neg/i20338a.check @@ -0,0 +1,7 @@ +-- [E007] Type Mismatch Error: tests/neg/i20338a.scala:10:15 ----------------------------------------------------------- +10 | test.field = "hello" // error + | ^^^^^^^ + | Found: ("hello" : String) + | Required: Int + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i20338a.scala b/tests/neg/i20338a.scala new file mode 100644 index 000000000000..b91982297d78 --- /dev/null +++ b/tests/neg/i20338a.scala @@ -0,0 +1,10 @@ +object types: + opaque type Struct = Int + val test: Struct = 25 + extension (s: Struct) + def field: Int = s + def field_=(other: Int) = () + +@main def hello = + import types.* + test.field = "hello" // error \ No newline at end of file diff --git a/tests/neg/i20338b.check b/tests/neg/i20338b.check new file mode 100644 index 000000000000..382d68a0911c --- /dev/null +++ b/tests/neg/i20338b.check @@ -0,0 +1,7 @@ +-- [E007] Type Mismatch Error: tests/neg/i20338b.scala:10:8 ------------------------------------------------------------ +10 | f.x = 42 // error + | ^^ + | Found: (42 : Int) + | Required: String + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i20338b.scala b/tests/neg/i20338b.scala new file mode 100644 index 000000000000..b8a3463862e0 --- /dev/null +++ b/tests/neg/i20338b.scala @@ -0,0 +1,10 @@ +class Foo(_x: Int) + +extension (s: Foo) + def x_=(x: String): Unit = () + def x: Int = ??? + +@main +def Test = + val f = Foo(42) + f.x = 42 // error diff --git a/tests/neg/i20338c.check b/tests/neg/i20338c.check new file mode 100644 index 000000000000..1d19ec0b3042 --- /dev/null +++ b/tests/neg/i20338c.check @@ -0,0 +1,6 @@ +-- [E052] Type Error: tests/neg/i20338c.scala:9:6 ---------------------------------------------------------------------- +9 | f.x = 42 // error + | ^^^^^^^^ + | Reassignment to val x + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i20338c.scala b/tests/neg/i20338c.scala new file mode 100644 index 000000000000..cfdf38e73b11 --- /dev/null +++ b/tests/neg/i20338c.scala @@ -0,0 +1,9 @@ +class Foo(val x: Int) + +extension (s: Foo) + def x: Int = 43 + +@main +def Test = + val f = Foo(42) + f.x = 42 // error \ No newline at end of file