Skip to content

Commit

Permalink
Backport "Fix error message on setter with wrong type" to LTS (#21123)
Browse files Browse the repository at this point in the history
Backports #20444 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 9, 2024
2 parents 9bc6103 + 3210e58 commit 6cb634d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
5 changes: 1 addition & 4 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1137,12 +1137,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))
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i20338a.check
Original file line number Diff line number Diff line change
@@ -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`
10 changes: 10 additions & 0 deletions tests/neg/i20338a.scala
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions tests/neg/i20338b.check
Original file line number Diff line number Diff line change
@@ -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`
10 changes: 10 additions & 0 deletions tests/neg/i20338b.scala
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions tests/neg/i20338c.check
Original file line number Diff line number Diff line change
@@ -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`
9 changes: 9 additions & 0 deletions tests/neg/i20338c.scala
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6cb634d

Please sign in to comment.