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

Covariant overrides with value class parameters produce broken forwarders

    Details

      Description

      Overriding a method C => A with a method C => B, where C is some value class and A and B are regular classes with B <: A, generates a broken forwarder. The forwarder attempts to box to the value class erasure's box and then cast to the value class box, producing a ClassCastException at runtime. Naturally, this affects overrides returning this.type as well. Tested with nightly Scala-2.11.0-SNAPSHOT.

      object ValueClassForwarderVariance extends App {
        class Box(val value: Int) extends AnyVal
        
        trait Foo {
          def append(box: Box): Foo
        }
        
        class Bar extends Foo {
          override def append(box: Box): Bar = this // produces bad forwarder
        }
        
        // throws ClassCastException: java.lang.Integer cannot be cast to _$Box
        ((new Bar): Foo).append(new Box(0))
      }
      
      // boxes to value class erasure's box,
      // casts to value class box (fail),
      // then unboxes and forwards.
      public ValueClassForwarderVariance$Foo append(int);
      Code:
         0: aload_0       
         1: iload_1       
         2: invokestatic  #20 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
         5: checkcast     #22 // class ValueClassForwarderVariance$Box
         8: invokevirtual #26 // Method ValueClassForwarderVariance$Box.value:()I
        11: invokevirtual #28 // Method append:(I)LValueClassForwarderVariance$Bar;
        14: areturn            
      

        Activity

        Hide
        Chris Sachs added a comment -

        Simplified test case.

        Show
        Chris Sachs added a comment - Simplified test case.
        Hide
        Adriaan Moors added a comment -

        Paul, I know you love variance and value classes. Please re-assign to Grzegorz or some other person who loves them more.

        Show
        Adriaan Moors added a comment - Paul, I know you love variance and value classes. Please re-assign to Grzegorz or some other person who loves them more.
        Hide
        Paul Phillips added a comment -

        Terminology note to submitter: that's a bridge method, not a forwarder. (It is true that it forwards, so it's not like it's a crazy choice of word, but it's confusing because we have other things called forwarders.)

        Show
        Paul Phillips added a comment - Terminology note to submitter: that's a bridge method, not a forwarder. (It is true that it forwards, so it's not like it's a crazy choice of word, but it's confusing because we have other things called forwarders.)
        Hide
        Chris Sachs added a comment -

        Good to know definitively about the bridge method terminology, I'd wondered. I was about to report that value class methods also forget their type parameter bounds, but I see you already found and fixed that one Thanks.

        Show
        Chris Sachs added a comment - Good to know definitively about the bridge method terminology, I'd wondered. I was about to report that value class methods also forget their type parameter bounds, but I see you already found and fixed that one Thanks.
        Hide
        Martin Odersky added a comment -

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

        (Sigh of relief: this turned out to be a silly omission; I feared a deeper design flaw).

        Show
        Martin Odersky added a comment - https://github.com/scala/scala/pull/1539 (Sigh of relief: this turned out to be a silly omission; I feared a deeper design flaw).
        Hide
        Adriaan Moors added a comment -
        Show
        Adriaan Moors added a comment - it's actually https://github.com/scala/scala/pull/1540

          People

          • Assignee:
            Martin Odersky
            Reporter:
            Chris Sachs
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development