Skip to content

Error does not show why rewrite to assignment did not work out #20462

@som-snytt

Description

@som-snytt

Compiler version

3.4.2, 3.5.x

Minimized example

scala> object x { def apply() = 42 ; def update(i: Int) = () }
// defined object x

scala> x()
val res0: Int = 42

scala> x() += 17

scala> x() += "oops"
-- [E008] Not Found Error: ---------------------------------------------------------------------------------------------
1 |x() += "oops"
  |^^^^^^
  |value += is not a member of Int - did you mean Int.!=? or perhaps Int.<=?
1 error found

scala>

Preferred Output

Welcome to Scala 2.13.14 (OpenJDK 64-Bit Server VM, Java 21.0.2).
Type in expressions for evaluation. Or try :help.

scala> object x { def apply() = 42 ; def update(i: Int) = () }
object x

scala> x() += "oops"
           ^
       error: value += is not a member of Int
         Expression does not convert to assignment because:
           type mismatch;
            found   : String
            required: Int
           expansion: x.update(x.apply().+("oops"))

scala>

Expectation

Under -Xsource:3:

$ scala -Xsource:3-cross
Welcome to Scala 2.13.14 -Xsource:3.0.0 (OpenJDK 64-Bit Server VM, Java 21.0.2).
Type in expressions for evaluation. Or try :help.

scala> object x { def apply() = 42 ; def update(i: Int) = () }
object x

scala> x() += "oops"
           ^
       error: value += is not a member of Int
         Expression does not convert to assignment because:
           overloaded method + with alternatives:
             (x: Int)Int <and>
             (x: Char)Int <and>
             (x: Short)Int <and>
             (x: Byte)Int
            cannot be applied to (String)
           expansion: x.update(x.apply().+("oops"))

scala>

Why this Error/Warning was not helpful

The rewrite for assignment operators can fail for a couple of underlying reasons, so it would be more helpful to report those errors.

Suggested improvement

Scala 2 also shows the expansion that failed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:reportingError reporting including formatting, implicit suggestions, etcbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions