Scala Programming Language
  1. Scala Programming Language
  2. SI-5663

Equals sensibility warnings imprecise for case classes

    Details

      Description

      This is related to SI-5648, warnings on comparisons.

      There is one edge-casey bug: (new Kase() == kaseAlias) causes the "fresh object" warning. It should be either no warning or "unrelated types" warning, depending on the types.

      The similar comparison (finalKase == kaseAlias) will emit "always yield false" (if there is no subtype relationship) instead of the weaker "unrelated types" warnings. (Can be true if the types are siblings extending the case class.)

      The useful improvement would be to handle comparisons between case classes again, since if the case classes are different they can't satisfy the synthetic equals. (Some(1) == Thing(1))

      A couple of related improvements would be to tighten up "fresh object" warnings, of the form (new Some(1) == None).

      scala> :paste
      // Entering paste mode (ctrl-D to finish)
      
      // alias
      trait Thingy
      
      // sibling classes that extend a case class
      case class Thing(i: Int)
      class ThingOne(x:Int) extends Thing(x)
      class ThingTwo(y:Int) extends Thing(y) with Thingy
      final class ThingThree(z:Int) extends Thing(z)
      
      val t1 = new ThingOne(11)
      val t2: Thingy = new ThingTwo(11)
      val t4 = new ThingThree(11)
      
      // Exiting paste mode, now interpreting.
      
      defined trait Thingy
      defined class Thing
      defined class ThingOne
      defined class ThingTwo
      defined class ThingThree
      t1: ThingOne = Thing(11)
      t2: Thingy = Thing(11)
      t4: ThingThree = Thing(11)
      
      scala> (new ThingTwo(11) == t2)
      <console>:13: warning: comparing a fresh object using `==' will always yield false
                    (new ThingTwo(11) == t2)
                                      ^
      res0: Boolean = true
      
      scala> (t4 == t2)  // unrelated types
      <console>:15: warning: comparing values of types ThingThree and Thingy using `==' will always yield false
                    (t4 == t2)
                        ^
      res1: Boolean = true
      
      scala> (Some(1) == Thing(1))  // could warn
      res2: Boolean = false
      
      

      I'll prepare a pull if I can type faster than Paul.

        Activity

        Hide
        A. P. Marki added a comment -

        https://github.com/scala/scala/pull/392/

        It's not possible to run partest without -deprecation.
        Since detecting the warnings requires a neg test with
        -Xfatal-warnings, and deprecation terminates the compile.
        During a deprecation cycle, it seems necessary to test
        deprecated features (e.g., warnings).

        (The deprecated feature here was trait extends Kase,
        where the case class is parenless.)

        I tried supplying a .javaopts file to turn off the default
        -deprecation, without success. On a related note, I would
        expect to be able to "scalac --deprecation=false".

        Show
        A. P. Marki added a comment - https://github.com/scala/scala/pull/392/ It's not possible to run partest without -deprecation. Since detecting the warnings requires a neg test with -Xfatal-warnings, and deprecation terminates the compile. During a deprecation cycle, it seems necessary to test deprecated features (e.g., warnings). (The deprecated feature here was trait extends Kase, where the case class is parenless.) I tried supplying a .javaopts file to turn off the default -deprecation, without success. On a related note, I would expect to be able to "scalac --deprecation=false".
        Hide
        A. P. Marki added a comment -

        commit 72d86cbe8cb4792a2eae190aee3677854bffb89f

        Show
        A. P. Marki added a comment - commit 72d86cbe8cb4792a2eae190aee3677854bffb89f

          People

          • Assignee:
            A. P. Marki
            Reporter:
            A. P. Marki
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development