Skip to content

Backport "Fix error message on setter with wrong type" to LTS #21123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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