Skip to content

Commit f99f268

Browse files
authored
Fix error message on setter with wrong type (#20444)
Fixes #20338 Poke @mbovel @nicolasstucki @hamzaremmal @AnotherMedo
2 parents cdb640a + 17d5c3d commit f99f268

File tree

7 files changed

+50
-4
lines changed

7 files changed

+50
-4
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,12 +1334,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
13341334

13351335
val setter = toSetter(lhsCore)
13361336
if setter.isEmpty then reassignmentToVal
1337-
else tryEither {
1337+
else
13381338
val assign = untpd.Apply(setter, tree.rhs :: Nil)
13391339
typed(assign, IgnoredProto(pt))
1340-
} {
1341-
(_, _) => reassignmentToVal
1342-
}
13431340
case _ => lhsCore.tpe match {
13441341
case ref: TermRef =>
13451342
val lhsVal = lhsCore.denot.suchThat(!_.is(Method))

tests/neg/i20338a.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i20338a.scala:10:15 -----------------------------------------------------------
2+
10 | test.field = "hello" // error
3+
| ^^^^^^^
4+
| Found: ("hello" : String)
5+
| Required: Int
6+
|
7+
| longer explanation available when compiling with `-explain`

tests/neg/i20338a.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object types:
2+
opaque type Struct = Int
3+
val test: Struct = 25
4+
extension (s: Struct)
5+
def field: Int = s
6+
def field_=(other: Int) = ()
7+
8+
@main def hello =
9+
import types.*
10+
test.field = "hello" // error

tests/neg/i20338b.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i20338b.scala:10:8 ------------------------------------------------------------
2+
10 | f.x = 42 // error
3+
| ^^
4+
| Found: (42 : Int)
5+
| Required: String
6+
|
7+
| longer explanation available when compiling with `-explain`

tests/neg/i20338b.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Foo(_x: Int)
2+
3+
extension (s: Foo)
4+
def x_=(x: String): Unit = ()
5+
def x: Int = ???
6+
7+
@main
8+
def Test =
9+
val f = Foo(42)
10+
f.x = 42 // error

tests/neg/i20338c.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E052] Type Error: tests/neg/i20338c.scala:9:6 ----------------------------------------------------------------------
2+
9 | f.x = 42 // error
3+
| ^^^^^^^^
4+
| Reassignment to val x
5+
|
6+
| longer explanation available when compiling with `-explain`

tests/neg/i20338c.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Foo(val x: Int)
2+
3+
extension (s: Foo)
4+
def x: Int = 43
5+
6+
@main
7+
def Test =
8+
val f = Foo(42)
9+
f.x = 42 // error

0 commit comments

Comments
 (0)