Skip to content

Commit

Permalink
fix error message on setter with wrong type
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Bovel<[email protected]>
Co-authored-by: Nicolas Stucki<>
Co-authored-by: Hamza Remmal<>
Co-authored-by: Mehdi Alaoui<>

fix check file
  • Loading branch information
iusildra committed May 22, 2024
1 parent 8563571 commit 17d5c3d
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 @@ -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))
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 17d5c3d

Please sign in to comment.